This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
name-service-js: add tests #4013
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ cd name-service/js | |
| yarn install --pure-lockfile | ||
| yarn lint | ||
| yarn build | ||
| yarn test | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { | ||
| Connection, | ||
| Keypair, | ||
| LAMPORTS_PER_SOL, | ||
| PublicKey, | ||
| sendAndConfirmTransaction, | ||
| Transaction, | ||
| } from '@solana/web3.js'; | ||
| import chai, { expect } from 'chai'; | ||
| import chaiAsPromised from 'chai-as-promised'; | ||
|
|
||
| import { | ||
| createNameRegistry, | ||
| deleteNameRegistry, | ||
| getHashedName, | ||
| getNameAccountKey, | ||
| NameRegistryState, | ||
| reallocNameAccount, | ||
| transferNameOwnership, | ||
| updateNameRegistryData, | ||
| } from '../../src'; | ||
|
|
||
| chai.use(chaiAsPromised); | ||
| const url = 'http://localhost:8899'; | ||
|
|
||
| describe('Name Service Program', async () => { | ||
| const connection = new Connection(url, 'confirmed'); | ||
| const payer = Keypair.generate(); | ||
| const owner = Keypair.generate(); | ||
| const space = 20; | ||
| let nameKey: PublicKey; | ||
joncinque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let name: string; | ||
| before(async () => { | ||
| const airdropSignature = await connection.requestAirdrop( | ||
| payer.publicKey, | ||
| LAMPORTS_PER_SOL | ||
| ); | ||
| await connection.confirmTransaction({ | ||
| signature: airdropSignature, | ||
| ...(await connection.getLatestBlockhash()), | ||
| }); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| name = Math.random().toString() + '.sol'; | ||
| nameKey = await getNameKey(name); | ||
| const lamports = await connection.getMinimumBalanceForRentExemption( | ||
| space + NameRegistryState.HEADER_LEN | ||
| ); | ||
| const inst = await createNameRegistry( | ||
| connection, | ||
| name, | ||
| space, | ||
| payer.publicKey, | ||
| owner.publicKey, | ||
| lamports | ||
| ); | ||
| const tx = new Transaction().add(inst); | ||
| await sendAndConfirmTransaction(connection, tx, [payer]); | ||
| }); | ||
|
|
||
| it('Create Name Registery', async () => { | ||
| const nameAccount = await NameRegistryState.retrieve(connection, nameKey); | ||
| nameAccount.owner.equals(owner.publicKey); | ||
| expect(nameAccount.data?.length).to.eql(space); | ||
| }); | ||
| it('Update Name Registery', async () => { | ||
atharmohammad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const data = Buffer.from('@Dudl'); | ||
| const inst = await updateNameRegistryData(connection, name, 0, data); | ||
| const tx = new Transaction().add(inst); | ||
| await sendAndConfirmTransaction(connection, tx, [payer, owner]); | ||
| const nameAccount = await NameRegistryState.retrieve(connection, nameKey); | ||
| nameAccount.data?.equals(data); | ||
| }); | ||
| it('Transfer Name Ownership', async () => { | ||
| const newOwner = Keypair.generate(); | ||
| const inst = await transferNameOwnership( | ||
| connection, | ||
| name, | ||
| newOwner.publicKey | ||
| ); | ||
| const tx = new Transaction().add(inst); | ||
| await sendAndConfirmTransaction(connection, tx, [payer, owner]); | ||
| const nameAccount = await NameRegistryState.retrieve(connection, nameKey); | ||
| nameAccount.owner.equals(newOwner.publicKey); | ||
| }); | ||
| it('Realloc Name Account to bigger space', async () => { | ||
| const inst = await reallocNameAccount( | ||
| connection, | ||
| name, | ||
| space + 10, | ||
| payer.publicKey | ||
| ); | ||
| const tx = new Transaction().add(inst); | ||
| await sendAndConfirmTransaction(connection, tx, [payer, owner]); | ||
| const nameAccount = await NameRegistryState.retrieve(connection, nameKey); | ||
| expect(nameAccount.data?.length).to.eql(space + 10); | ||
| }); | ||
| it('Realloc Name Account to smaller space', async () => { | ||
| const inst = await reallocNameAccount( | ||
| connection, | ||
| name, | ||
| space - 10, | ||
| payer.publicKey | ||
| ); | ||
| const tx = new Transaction().add(inst); | ||
| await sendAndConfirmTransaction(connection, tx, [payer, owner]); | ||
| const nameAccount = await NameRegistryState.retrieve(connection, nameKey); | ||
| expect(nameAccount.data?.length).to.eql(space - 10); | ||
| }); | ||
| it('Delete Name Registry', async () => { | ||
| const inst = await deleteNameRegistry(connection, name, payer.publicKey); | ||
| const tx = new Transaction().add(inst); | ||
| await sendAndConfirmTransaction(connection, tx, [payer, owner]); | ||
| const nameAccount = await connection.getAccountInfo(nameKey); | ||
| expect(nameAccount).to.be.null; | ||
| }); | ||
| }); | ||
|
|
||
| const getNameKey = async ( | ||
| name: string, | ||
| nameClass?: PublicKey, | ||
| parentName?: PublicKey | ||
| ) => { | ||
| const hashedName = await getHashedName(name); | ||
| const nameAccountKey = await getNameAccountKey( | ||
| hashedName, | ||
| nameClass, | ||
| parentName | ||
| ); | ||
| return nameAccountKey; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { | ||
| Keypair, | ||
| LAMPORTS_PER_SOL, | ||
| PublicKey, | ||
| SystemProgram, | ||
| } from '@solana/web3.js'; | ||
| import chai, { expect } from 'chai'; | ||
| import chaiAsPromised from 'chai-as-promised'; | ||
|
|
||
| import { | ||
| createInstruction, | ||
| deleteInstruction, | ||
| reallocInstruction, | ||
| transferInstruction, | ||
| updateInstruction, | ||
| } from '../../src'; | ||
| import { Numberu32, Numberu64 } from '../../src/utils'; | ||
|
|
||
| chai.use(chaiAsPromised); | ||
|
|
||
| describe('SplNameService Instructions', () => { | ||
| const nameServiceAddress = new PublicKey( | ||
| 'namesLPneVptA9Z5rqUDD9tMTWEJwofgaYwp8cawRkX' | ||
| ); | ||
| const nameAccountKey = Keypair.generate().publicKey; | ||
| const nameOwnerKey = Keypair.generate().publicKey; | ||
| const payerKey = Keypair.generate().publicKey; | ||
| const nameClassKey = Keypair.generate().publicKey; | ||
| const nameParent = Keypair.generate().publicKey; | ||
| const nameParentOwner = Keypair.generate().publicKey; | ||
| const name = Buffer.from('hello'); | ||
|
|
||
| it('createInstruction without class and parent name key', () => { | ||
| const instruction = createInstruction( | ||
| nameServiceAddress, | ||
| SystemProgram.programId, | ||
| nameAccountKey, | ||
| nameOwnerKey, | ||
| payerKey, | ||
| name, | ||
| new Numberu64(LAMPORTS_PER_SOL), | ||
| new Numberu64(10) | ||
| ); | ||
|
|
||
| expect(instruction.keys).to.have.length(6); | ||
| instruction.keys[0].pubkey.equals(SystemProgram.programId); | ||
| instruction.keys[1].pubkey.equals(payerKey); | ||
| instruction.keys[2].pubkey.equals(nameAccountKey); | ||
| instruction.keys[3].pubkey.equals(nameOwnerKey); | ||
| instruction.keys[4].pubkey.equals(new PublicKey(Buffer.alloc(32))); | ||
| instruction.keys[5].pubkey.equals(new PublicKey(Buffer.alloc(32))); | ||
atharmohammad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| it('createInstruction with class and parent name key', () => { | ||
| const instruction = createInstruction( | ||
| nameServiceAddress, | ||
| SystemProgram.programId, | ||
| nameAccountKey, | ||
| nameOwnerKey, | ||
| payerKey, | ||
| name, | ||
| new Numberu64(LAMPORTS_PER_SOL), | ||
| new Numberu64(10), | ||
| nameClassKey, | ||
| nameParent, | ||
| nameParentOwner | ||
| ); | ||
|
|
||
| expect(instruction.keys).to.have.length(7); | ||
| instruction.keys[0].pubkey.equals(SystemProgram.programId); | ||
| instruction.keys[1].pubkey.equals(payerKey); | ||
| instruction.keys[2].pubkey.equals(nameAccountKey); | ||
| instruction.keys[3].pubkey.equals(nameOwnerKey); | ||
| instruction.keys[4].pubkey.equals(nameClassKey); | ||
| instruction.keys[5].pubkey.equals(nameParent); | ||
| instruction.keys[6].pubkey.equals(nameParentOwner); | ||
| }); | ||
|
|
||
| it('updateInstruction', () => { | ||
| const data = Buffer.from('@Dudl'); | ||
| const instruction = updateInstruction( | ||
| nameServiceAddress, | ||
| nameAccountKey, | ||
| new Numberu32(0), | ||
| data, | ||
| nameOwnerKey, | ||
| undefined | ||
| ); | ||
|
|
||
| expect(instruction.keys).to.have.length(2); | ||
| instruction.keys[0].pubkey.equals(nameAccountKey); | ||
| instruction.keys[1].pubkey.equals(nameOwnerKey); | ||
| }); | ||
|
|
||
| it('transferInstruction', () => { | ||
| const newOwner = Keypair.generate().publicKey; | ||
| const instruction = transferInstruction( | ||
| nameServiceAddress, | ||
| nameAccountKey, | ||
| newOwner, | ||
| nameOwnerKey | ||
| ); | ||
|
|
||
| expect(instruction.keys).to.have.length(2); | ||
| instruction.keys[0].pubkey.equals(nameAccountKey); | ||
| instruction.keys[1].pubkey.equals(nameOwnerKey); | ||
| }); | ||
|
|
||
| it('deleteInstruction', () => { | ||
| const instruction = deleteInstruction( | ||
| nameServiceAddress, | ||
| nameAccountKey, | ||
| payerKey, | ||
| nameOwnerKey | ||
| ); | ||
|
|
||
| expect(instruction.keys).to.have.length(3); | ||
| instruction.keys[0].pubkey.equals(nameAccountKey); | ||
| instruction.keys[1].pubkey.equals(nameOwnerKey); | ||
| instruction.keys[2].pubkey.equals(payerKey); | ||
| }); | ||
|
|
||
| it('reallocInstruction', () => { | ||
| const instruction = reallocInstruction( | ||
| nameServiceAddress, | ||
| SystemProgram.programId, | ||
| payerKey, | ||
| nameAccountKey, | ||
| nameOwnerKey, | ||
| new Numberu32(30) | ||
| ); | ||
|
|
||
| expect(instruction.keys).to.have.length(4); | ||
| instruction.keys[0].pubkey.equals(SystemProgram.programId); | ||
| instruction.keys[1].pubkey.equals(payerKey); | ||
| instruction.keys[2].pubkey.equals(nameAccountKey); | ||
| instruction.keys[3].pubkey.equals(nameOwnerKey); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.