| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- // SPDX-License-Identifier: Apache-2.0
- import { loadContractAndCallConstructor } from "./setup";
- import { Keypair, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
- import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
- import { BN } from '@coral-xyz/anchor';
- describe('Test system instructions', function () {
- this.timeout(500000);
- const system_account = new PublicKey('11111111111111111111111111111111');
- const recent_block_hashes = new PublicKey('SysvarRecentB1ockHashes11111111111111111111');
- const rentAddress = new PublicKey('SysvarRent111111111111111111111111111111111');
- const seed = 'my_seed_is_tea';
- it('create account', async function create_account() {
- const { program, storage, payer } = await loadContractAndCallConstructor('TestingInstruction');
- const to_key_pair = Keypair.generate();
- await program.methods.createAccount(
- payer.publicKey,
- to_key_pair.publicKey,
- new BN(100000000),
- new BN(5),
- TOKEN_PROGRAM_ID)
- .remainingAccounts([
- { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
- { pubkey: payer.publicKey, isSigner: true, isWritable: false },
- { pubkey: to_key_pair.publicKey, isSigner: true, isWritable: true },
- ])
- .signers([payer, to_key_pair]).rpc();
- });
- it('create account with seed', async function create_account_with_seed() {
- const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
- const base_keypair = Keypair.generate();
- const to_key_pair = await PublicKey.createWithSeed(base_keypair.publicKey, seed, TOKEN_PROGRAM_ID);
- await program.methods.createAccountWithSeed(
- payer.publicKey,
- to_key_pair,
- base_keypair.publicKey,
- seed,
- new BN(100000000),
- new BN(5),
- TOKEN_PROGRAM_ID)
- .remainingAccounts([
- { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
- { pubkey: payer.publicKey, isSigner: true, isWritable: false },
- { pubkey: to_key_pair, isSigner: false, isWritable: true },
- { pubkey: base_keypair.publicKey, isSigner: true, isWritable: false },
- ])
- .signers([payer, base_keypair]).rpc();
- });
- it('assign', async function assign() {
- const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
- const to_key_pair = Keypair.generate();
- const assign_account = new PublicKey('AddressLookupTab1e1111111111111111111111111');
- await program.methods.assign(
- to_key_pair.publicKey,
- assign_account)
- .remainingAccounts([
- { pubkey: payer.publicKey, isSigner: false, isWritable: false },
- { pubkey: to_key_pair.publicKey, isSigner: true, isWritable: true },
- ])
- .signers([payer, to_key_pair]).rpc();
- });
- it('assign with seed', async function assign_with_with_seed() {
- const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
- const assign_account = new PublicKey('AddressLookupTab1e1111111111111111111111111');
- const to_key_pair = await PublicKey.createWithSeed(payer.publicKey, seed, assign_account);
- await program.methods.assignWithSeed(
- to_key_pair,
- payer.publicKey,
- seed,
- assign_account)
- .remainingAccounts([
- { pubkey: assign_account, isSigner: false, isWritable: false },
- { pubkey: payer.publicKey, isSigner: false, isWritable: false },
- { pubkey: to_key_pair, isSigner: false, isWritable: true },
- ])
- .signers([payer]).rpc();
- });
- it('transfer', async function transfer() {
- const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
- const dest = new Keypair();
- await program.methods.transfer(
- payer.publicKey,
- dest.publicKey,
- new BN(100000000))
- .remainingAccounts([
- { pubkey: payer.publicKey, isSigner: false, isWritable: true },
- { pubkey: dest.publicKey, isSigner: false, isWritable: true },
- ])
- .signers([payer]).rpc();
- });
- it('transfer with seed', async function transfer_with_seed() {
- const { storage, payer, provider, program } = await loadContractAndCallConstructor('TestingInstruction');
- const dest = new Keypair();
- const assign_account = new PublicKey('AddressLookupTab1e1111111111111111111111111');
- const derived_payer = await PublicKey.createWithSeed(payer.publicKey, seed, assign_account);
- let signature = await provider.connection.requestAirdrop(derived_payer, LAMPORTS_PER_SOL);
- await provider.connection.confirmTransaction(signature, 'confirmed');
- await program.methods.transferWithSeed(
- derived_payer, // from_pubkey
- payer.publicKey, // from_base
- seed, // seed
- assign_account, // from_owner
- dest.publicKey, // to_pubkey
- new BN(100000000))
- .remainingAccounts([
- { pubkey: assign_account, isSigner: false, isWritable: false },
- { pubkey: derived_payer, isSigner: false, isWritable: true },
- { pubkey: dest.publicKey, isSigner: false, isWritable: true },
- { pubkey: payer.publicKey, isSigner: true, isWritable: false },
- ])
- .signers([payer]).rpc();
- });
- it('allocate', async function allocate() {
- const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
- const account = Keypair.generate();
- await program.methods.allocate(
- account.publicKey,
- new BN(2))
- .remainingAccounts([
- { pubkey: account.publicKey, isSigner: true, isWritable: true },
- ])
- .signers([payer, account]).rpc();
- });
- it('allocate with seed', async function allocate_with_seed() {
- const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
- const account = Keypair.generate();
- const owner = new PublicKey('Stake11111111111111111111111111111111111111');
- const derived_key = await PublicKey.createWithSeed(account.publicKey, seed, owner);
- await program.methods.allocateWithSeed(
- derived_key,
- account.publicKey,
- seed,
- new BN(200),
- owner)
- .remainingAccounts([
- { pubkey: owner, isSigner: false, isWritable: false },
- { pubkey: account.publicKey, isSigner: true, isWritable: false },
- { pubkey: derived_key, isSigner: false, isWritable: true },
- ])
- .signers([payer, account]).rpc();
- });
- it('create nonce account with seed', async function create_nonce_account_with_seed() {
- const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
- const base_address = Keypair.generate();
- const derived_account = await PublicKey.createWithSeed(base_address.publicKey, seed, system_account);
- const authority = Keypair.generate();
- await program.methods.createNonceAccountWithSeed(
- payer.publicKey,
- derived_account,
- base_address.publicKey,
- seed,
- authority.publicKey,
- new BN(100000000))
- .remainingAccounts([
- { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
- { pubkey: rentAddress, isSigner: false, isWritable: false },
- { pubkey: payer.publicKey, isSigner: false, isWritable: true },
- { pubkey: derived_account, isSigner: false, isWritable: true },
- { pubkey: base_address.publicKey, isSigner: true, isWritable: true },
- ])
- .signers([payer, base_address]).rpc();
- });
- it('nonce accounts', async function nonce_accounts() {
- const { storage, payer, program } = await loadContractAndCallConstructor('TestingInstruction');
- const nonce = Keypair.generate();
- const authority = Keypair.generate();
- await program.methods.createNonceAccount(
- payer.publicKey,
- nonce.publicKey,
- authority.publicKey,
- new BN(100000000))
- .remainingAccounts([
- { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
- { pubkey: rentAddress, isSigner: false, isWritable: false },
- { pubkey: payer.publicKey, isSigner: false, isWritable: true },
- { pubkey: nonce.publicKey, isSigner: true, isWritable: true },
- ])
- .signers([payer, nonce]).rpc();
- await program.methods.advanceNonceAccount(
- nonce.publicKey,
- authority.publicKey)
- .remainingAccounts([
- { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
- { pubkey: authority.publicKey, isSigner: true, isWritable: false },
- { pubkey: nonce.publicKey, isSigner: false, isWritable: true },
- ])
- .signers([authority]).rpc();
- await program.methods.withdrawNonceAccount(
- nonce.publicKey,
- authority.publicKey,
- payer.publicKey,
- new BN(1000))
- .remainingAccounts([
- { pubkey: recent_block_hashes, isSigner: false, isWritable: false },
- { pubkey: rentAddress, isSigner: false, isWritable: false },
- { pubkey: authority.publicKey, isSigner: true, isWritable: false },
- { pubkey: nonce.publicKey, isSigner: false, isWritable: true },
- { pubkey: payer.publicKey, isSigner: false, isWritable: true },
- ])
- .signers([authority]).rpc();
- const new_authority = Keypair.generate();
- await program.methods.authorizeNonceAccount(
- nonce.publicKey,
- authority.publicKey,
- new_authority.publicKey)
- .remainingAccounts([
- { pubkey: authority.publicKey, isSigner: true, isWritable: false },
- { pubkey: nonce.publicKey, isSigner: false, isWritable: true },
- ])
- .signers([authority]).rpc();
- });
- });
|