123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- import * as anchor from "@project-serum/anchor";
- import { Native } from "@project-serum/anchor";
- import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
- import {
- Keypair,
- LAMPORTS_PER_SOL,
- NONCE_ACCOUNT_LENGTH,
- PublicKey,
- SystemProgram,
- SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
- } from "@solana/web3.js";
- import * as assert from "assert";
- import BN from "bn.js";
- describe("system-coder", () => {
- // Configure the client to use the local cluster.
- const provider = anchor.AnchorProvider.env();
- anchor.setProvider(provider);
- // Client.
- const program = Native.system();
- // Constants.
- const aliceKeypair = Keypair.generate();
- it("Creates an account", async () => {
- // arrange
- const space = 100;
- const lamports =
- await program.provider.connection.getMinimumBalanceForRentExemption(
- space
- );
- const owner = SystemProgram.programId;
- // act
- await program.methods
- .createAccount(new BN(lamports), new BN(space), owner)
- .accounts({
- from: provider.wallet.publicKey,
- to: aliceKeypair.publicKey,
- })
- .signers([aliceKeypair])
- .rpc();
- // assert
- const aliceAccount = await program.provider.connection.getAccountInfo(
- aliceKeypair.publicKey
- );
- assert.notEqual(aliceAccount, null);
- assert.ok(owner.equals(aliceAccount.owner));
- assert.equal(lamports, aliceAccount.lamports);
- });
- it("Assigns an account to a program", async () => {
- // arrange
- const owner = TOKEN_PROGRAM_ID;
- // act
- await program.methods
- .assign(owner)
- .accounts({
- pubkey: aliceKeypair.publicKey,
- })
- .signers([aliceKeypair])
- .rpc();
- // assert
- const aliceAccount = await program.provider.connection.getAccountInfo(
- aliceKeypair.publicKey
- );
- assert.notEqual(aliceAccount, null);
- assert.ok(owner.equals(aliceAccount.owner));
- });
- it("Allocates space to an account", async () => {
- // arrange
- const newKeypair = Keypair.generate();
- const space = 100;
- const lamports =
- await program.provider.connection.getMinimumBalanceForRentExemption(
- space
- );
- // act
- await program.methods
- .allocate(new BN(space))
- .accounts({
- pubkey: newKeypair.publicKey,
- })
- .postInstructions([
- await program.methods
- .transfer(new BN(lamports))
- .accounts({
- from: provider.wallet.publicKey,
- to: newKeypair.publicKey,
- })
- .instruction(),
- ])
- .signers([newKeypair])
- .rpc();
- // assert
- const newAccountAfter = await program.provider.connection.getAccountInfo(
- newKeypair.publicKey
- );
- assert.equal(space, newAccountAfter.data.byteLength);
- });
- it("Creates an account with seed", async () => {
- const space = 100;
- const lamports =
- await program.provider.connection.getMinimumBalanceForRentExemption(
- space
- );
- const owner = SystemProgram.programId;
- const seed = "seeds";
- const bobPublicKey = await PublicKey.createWithSeed(
- aliceKeypair.publicKey,
- seed,
- owner
- );
- // act
- await program.methods
- .createAccountWithSeed(
- aliceKeypair.publicKey,
- seed,
- new BN(lamports),
- new BN(space),
- owner
- )
- .accounts({
- base: aliceKeypair.publicKey,
- from: provider.wallet.publicKey,
- to: bobPublicKey,
- })
- .signers([aliceKeypair])
- .rpc();
- // assert
- const bobAccount = await program.provider.connection.getAccountInfo(
- bobPublicKey
- );
- assert.notEqual(bobAccount, null);
- });
- it("Allocates and assigns an account with seed", async () => {
- const owner = TOKEN_PROGRAM_ID;
- const seed = "seeds2";
- const space = 100;
- const lamports =
- await program.provider.connection.getMinimumBalanceForRentExemption(
- space
- );
- const bobPublicKey = await PublicKey.createWithSeed(
- aliceKeypair.publicKey,
- seed,
- owner
- );
- // act
- await program.methods
- .allocateWithSeed(aliceKeypair.publicKey, seed, new BN(space), owner)
- .accounts({
- base: aliceKeypair.publicKey,
- account: bobPublicKey,
- })
- .postInstructions([
- await program.methods
- .transfer(new BN(lamports))
- .accounts({
- from: provider.wallet.publicKey,
- to: bobPublicKey,
- })
- .instruction(),
- await program.methods
- .assignWithSeed(aliceKeypair.publicKey, seed, owner)
- .accounts({
- base: aliceKeypair.publicKey,
- account: bobPublicKey,
- })
- .instruction(),
- ])
- .signers([aliceKeypair])
- .rpc();
- // assert
- const bobAccount = await program.provider.connection.getAccountInfo(
- bobPublicKey
- );
- assert.notEqual(bobAccount, null);
- assert.ok(owner.equals(bobAccount.owner));
- });
- it("Transfers from account with seed", async () => {
- const lamports = 1 * LAMPORTS_PER_SOL;
- const owner = SystemProgram.programId;
- const seed = "seeds3";
- const bobPublicKey = await PublicKey.createWithSeed(
- aliceKeypair.publicKey,
- seed,
- owner
- );
- const aliceAccountBefore = await program.provider.connection.getAccountInfo(
- aliceKeypair.publicKey
- );
- // act
- await program.methods
- .transfer(new BN(lamports))
- .accounts({
- from: provider.wallet.publicKey,
- to: bobPublicKey,
- })
- .rpc();
- await program.methods
- .transferWithSeed(new BN(lamports), seed, owner)
- .accounts({
- from: bobPublicKey,
- base: aliceKeypair.publicKey,
- to: aliceKeypair.publicKey,
- })
- .signers([aliceKeypair])
- .rpc();
- // assert
- const aliceAccountAfter = await program.provider.connection.getAccountInfo(
- aliceKeypair.publicKey
- );
- assert.equal(
- aliceAccountBefore.lamports + lamports,
- aliceAccountAfter.lamports
- );
- });
- it("Transfers lamports", async () => {
- // arrange
- const receiverKeypair = Keypair.generate();
- const lamports = 0.1 * LAMPORTS_PER_SOL;
- // act
- await program.methods
- .transfer(new BN(lamports))
- .accounts({
- from: provider.wallet.publicKey,
- to: receiverKeypair.publicKey,
- })
- .rpc();
- // assert
- const receiverAccount = await program.provider.connection.getAccountInfo(
- receiverKeypair.publicKey
- );
- assert.notEqual(receiverAccount, null);
- assert.equal(lamports, receiverAccount.lamports);
- });
- it("Initializes nonce account", async () => {
- // arrange
- const nonceKeypair = Keypair.generate();
- const owner = SystemProgram.programId;
- const space = NONCE_ACCOUNT_LENGTH;
- const lamports =
- await provider.connection.getMinimumBalanceForRentExemption(space);
- // act
- await program.methods
- .initializeNonceAccount(provider.wallet.publicKey)
- .accounts({
- nonce: nonceKeypair.publicKey,
- recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
- })
- .preInstructions([
- await program.methods
- .createAccount(new BN(lamports), new BN(space), owner)
- .accounts({
- from: provider.wallet.publicKey,
- to: nonceKeypair.publicKey,
- })
- .instruction(),
- ])
- .signers([nonceKeypair])
- .rpc();
- // assert
- const nonceAccount = await program.account.nonce.fetch(
- nonceKeypair.publicKey
- );
- assert.notEqual(nonceAccount, null);
- assert.ok(nonceAccount.authorizedPubkey.equals(provider.wallet.publicKey));
- });
- it("Advances a nonce account", async () => {
- // arrange
- const nonceKeypair = Keypair.generate();
- const owner = SystemProgram.programId;
- const space = NONCE_ACCOUNT_LENGTH;
- const lamports =
- await provider.connection.getMinimumBalanceForRentExemption(space);
- // act
- await program.methods
- .initializeNonceAccount(provider.wallet.publicKey)
- .accounts({
- nonce: nonceKeypair.publicKey,
- recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
- })
- .preInstructions([
- await program.methods
- .createAccount(new BN(lamports), new BN(space), owner)
- .accounts({
- from: provider.wallet.publicKey,
- to: nonceKeypair.publicKey,
- })
- .instruction(),
- ])
- .signers([nonceKeypair])
- .rpc();
- // These have to be separate to make sure advance is in another slot.
- await program.methods
- .advanceNonceAccount(provider.wallet.publicKey)
- .accounts({
- nonce: nonceKeypair.publicKey,
- recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
- })
- .rpc();
- // assert
- const nonceAccount = await program.account.nonce.fetch(
- nonceKeypair.publicKey
- );
- assert.notEqual(nonceAccount, null);
- });
- it("Authorizes a nonce account", async () => {
- // arrange
- const nonceKeypair = Keypair.generate();
- const owner = SystemProgram.programId;
- const space = NONCE_ACCOUNT_LENGTH;
- const lamports =
- await provider.connection.getMinimumBalanceForRentExemption(space);
- // act
- await program.methods
- .initializeNonceAccount(provider.wallet.publicKey)
- .accounts({
- nonce: nonceKeypair.publicKey,
- recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
- })
- .preInstructions([
- await program.methods
- .createAccount(new BN(lamports), new BN(space), owner)
- .accounts({
- from: provider.wallet.publicKey,
- to: nonceKeypair.publicKey,
- })
- .instruction(),
- ])
- .signers([nonceKeypair])
- .rpc();
- await program.methods
- .authorizeNonceAccount(aliceKeypair.publicKey)
- .accounts({
- nonce: nonceKeypair.publicKey,
- authorized: provider.wallet.publicKey,
- })
- .rpc();
- // assert
- const nonceAccount = await program.account.nonce.fetch(
- nonceKeypair.publicKey
- );
- assert.notEqual(nonceAccount, null);
- assert.ok(nonceAccount.authorizedPubkey.equals(aliceKeypair.publicKey));
- });
- it("Withdraws from nonce account", async () => {
- // arrange
- const nonceKeypair = Keypair.generate();
- const owner = SystemProgram.programId;
- const space = NONCE_ACCOUNT_LENGTH;
- const lamports =
- await provider.connection.getMinimumBalanceForRentExemption(space);
- const amount = 0.1 * LAMPORTS_PER_SOL;
- const aliceBalanceBefore = (
- await program.provider.connection.getAccountInfo(aliceKeypair.publicKey)
- ).lamports;
- // act
- await program.methods
- .initializeNonceAccount(provider.wallet.publicKey)
- .accounts({
- nonce: nonceKeypair.publicKey,
- recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
- })
- .preInstructions([
- await program.methods
- .createAccount(new BN(lamports), new BN(space), owner)
- .accounts({
- from: provider.wallet.publicKey,
- to: nonceKeypair.publicKey,
- })
- .instruction(),
- ])
- .signers([nonceKeypair])
- .rpc();
- await program.methods
- .advanceNonceAccount(provider.wallet.publicKey)
- .accounts({
- nonce: nonceKeypair.publicKey,
- recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
- })
- .postInstructions([
- await program.methods
- .transfer(new BN(amount))
- .accounts({
- from: provider.wallet.publicKey,
- to: nonceKeypair.publicKey,
- })
- .instruction(),
- ])
- .rpc();
- await program.methods
- .authorizeNonceAccount(aliceKeypair.publicKey)
- .accounts({
- nonce: nonceKeypair.publicKey,
- authorized: provider.wallet.publicKey,
- })
- .rpc();
- await program.methods
- .withdrawNonceAccount(new BN(amount))
- .accounts({
- authorized: aliceKeypair.publicKey,
- nonce: nonceKeypair.publicKey,
- recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
- to: aliceKeypair.publicKey,
- })
- .signers([aliceKeypair])
- .rpc();
- // assert
- const aliceBalanceAfter = (
- await program.provider.connection.getAccountInfo(aliceKeypair.publicKey)
- ).lamports;
- assert.equal(aliceBalanceAfter - aliceBalanceBefore, amount);
- });
- });
|