feat: add API for decoding system instructions
This commit is contained in:
committed by
Michael Vines
parent
662ce22cdd
commit
6ed2bad9d0
@ -109,10 +109,10 @@ test('get program accounts', async () => {
|
||||
result: {Ok: null},
|
||||
},
|
||||
]);
|
||||
let transaction = SystemProgram.assign(
|
||||
account0.publicKey,
|
||||
programId.publicKey,
|
||||
);
|
||||
let transaction = SystemProgram.assign({
|
||||
fromPubkey: account0.publicKey,
|
||||
programId: programId.publicKey,
|
||||
});
|
||||
await sendAndConfirmTransaction(connection, transaction, account0);
|
||||
|
||||
mockRpc.push([
|
||||
@ -140,7 +140,11 @@ test('get program accounts', async () => {
|
||||
result: {Ok: null},
|
||||
},
|
||||
]);
|
||||
transaction = SystemProgram.assign(account1.publicKey, programId.publicKey);
|
||||
transaction = SystemProgram.assign({
|
||||
fromPubkey: account1.publicKey,
|
||||
programId: programId.publicKey,
|
||||
});
|
||||
|
||||
await sendAndConfirmTransaction(connection, transaction, account1);
|
||||
|
||||
mockGetRecentBlockhash('recent');
|
||||
@ -651,14 +655,16 @@ test('get confirmed block', async () => {
|
||||
url,
|
||||
{
|
||||
method: 'getConfirmedBlock',
|
||||
params: [10000],
|
||||
params: [Number.MAX_SAFE_INTEGER],
|
||||
},
|
||||
{
|
||||
error: null,
|
||||
result: null,
|
||||
},
|
||||
]);
|
||||
await expect(connection.getConfirmedBlock(10000)).rejects.toThrow();
|
||||
await expect(
|
||||
connection.getConfirmedBlock(Number.MAX_SAFE_INTEGER),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
test('get recent blockhash', async () => {
|
||||
@ -962,11 +968,11 @@ test('transaction', async () => {
|
||||
},
|
||||
]);
|
||||
|
||||
const transaction = SystemProgram.transfer(
|
||||
accountFrom.publicKey,
|
||||
accountTo.publicKey,
|
||||
10,
|
||||
);
|
||||
const transaction = SystemProgram.transfer({
|
||||
fromPubkey: accountFrom.publicKey,
|
||||
toPubkey: accountTo.publicKey,
|
||||
lamports: 10,
|
||||
});
|
||||
const signature = await connection.sendTransaction(transaction, accountFrom);
|
||||
|
||||
mockRpc.push([
|
||||
@ -1088,12 +1094,16 @@ test('multi-instruction transaction', async () => {
|
||||
|
||||
// 1. Move(accountFrom, accountTo)
|
||||
// 2. Move(accountTo, accountFrom)
|
||||
const transaction = SystemProgram.transfer(
|
||||
accountFrom.publicKey,
|
||||
accountTo.publicKey,
|
||||
100,
|
||||
).add(
|
||||
SystemProgram.transfer(accountTo.publicKey, accountFrom.publicKey, 100),
|
||||
const transaction = SystemProgram.transfer({
|
||||
fromPubkey: accountFrom.publicKey,
|
||||
toPubkey: accountTo.publicKey,
|
||||
lamports: 100,
|
||||
}).add(
|
||||
SystemProgram.transfer({
|
||||
fromPubkey: accountTo.publicKey,
|
||||
toPubkey: accountFrom.publicKey,
|
||||
lamports: 100,
|
||||
}),
|
||||
);
|
||||
const signature = await connection.sendTransaction(
|
||||
transaction,
|
||||
@ -1149,11 +1159,11 @@ test('account change notification', async () => {
|
||||
|
||||
await connection.requestAirdrop(owner.publicKey, LAMPORTS_PER_SOL);
|
||||
try {
|
||||
let transaction = SystemProgram.transfer(
|
||||
owner.publicKey,
|
||||
programAccount.publicKey,
|
||||
balanceNeeded,
|
||||
);
|
||||
let transaction = SystemProgram.transfer({
|
||||
fromPubkey: owner.publicKey,
|
||||
toPubkey: programAccount.publicKey,
|
||||
lamports: balanceNeeded,
|
||||
});
|
||||
await sendAndConfirmTransaction(connection, transaction, owner);
|
||||
} catch (err) {
|
||||
await connection.removeAccountChangeListener(subscriptionId);
|
||||
@ -1213,11 +1223,11 @@ test('program account change notification', async () => {
|
||||
|
||||
await connection.requestAirdrop(owner.publicKey, LAMPORTS_PER_SOL);
|
||||
try {
|
||||
let transaction = SystemProgram.transfer(
|
||||
owner.publicKey,
|
||||
programAccount.publicKey,
|
||||
balanceNeeded,
|
||||
);
|
||||
let transaction = SystemProgram.transfer({
|
||||
fromPubkey: owner.publicKey,
|
||||
toPubkey: programAccount.publicKey,
|
||||
lamports: balanceNeeded,
|
||||
});
|
||||
await sendAndConfirmTransaction(connection, transaction, owner);
|
||||
} catch (err) {
|
||||
await connection.removeProgramAccountChangeListener(subscriptionId);
|
||||
|
@ -86,12 +86,12 @@ test('create and query nonce account', async () => {
|
||||
},
|
||||
]);
|
||||
|
||||
const transaction = SystemProgram.createNonceAccount(
|
||||
from.publicKey,
|
||||
nonceAccount.publicKey,
|
||||
from.publicKey,
|
||||
minimumAmount,
|
||||
);
|
||||
const transaction = SystemProgram.createNonceAccount({
|
||||
fromPubkey: from.publicKey,
|
||||
noncePubkey: nonceAccount.publicKey,
|
||||
authorizedPubkey: from.publicKey,
|
||||
lamports: minimumAmount,
|
||||
});
|
||||
await connection.sendTransaction(transaction, from, nonceAccount);
|
||||
|
||||
const expectedData = Buffer.alloc(68);
|
||||
|
@ -12,7 +12,6 @@ import {
|
||||
StakeInstruction,
|
||||
StakeProgram,
|
||||
SystemInstruction,
|
||||
SystemProgram,
|
||||
Transaction,
|
||||
} from '../src';
|
||||
import {mockRpcEnabled} from './__mocks__/node-fetch';
|
||||
@ -34,6 +33,7 @@ test('createAccountWithSeed', () => {
|
||||
const authorizedPubkey = new Account().publicKey;
|
||||
const authorized = new Authorized(authorizedPubkey, authorizedPubkey);
|
||||
const lockup = new Lockup(0, 0, fromPubkey);
|
||||
const lamports = 123;
|
||||
const transaction = StakeProgram.createAccountWithSeed({
|
||||
fromPubkey,
|
||||
stakePubkey: newAccountPubkey,
|
||||
@ -41,19 +41,26 @@ test('createAccountWithSeed', () => {
|
||||
seed,
|
||||
authorized,
|
||||
lockup,
|
||||
lamports: 123,
|
||||
lamports,
|
||||
});
|
||||
|
||||
expect(transaction.instructions).toHaveLength(2);
|
||||
const [systemInstruction, stakeInstruction] = transaction.instructions;
|
||||
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
||||
|
||||
// TODO decode system instruction
|
||||
|
||||
const params = StakeInstruction.decodeInitialize(stakeInstruction);
|
||||
expect(params.stakePubkey).toEqual(newAccountPubkey);
|
||||
expect(params.authorized).toEqual(authorized);
|
||||
expect(params.lockup).toEqual(lockup);
|
||||
const systemParams = {
|
||||
fromPubkey,
|
||||
newAccountPubkey,
|
||||
basePubkey: fromPubkey,
|
||||
seed,
|
||||
lamports,
|
||||
space: StakeProgram.space,
|
||||
programId: StakeProgram.programId,
|
||||
};
|
||||
expect(systemParams).toEqual(
|
||||
SystemInstruction.decodeCreateWithSeed(systemInstruction),
|
||||
);
|
||||
const initParams = {stakePubkey: newAccountPubkey, authorized, lockup};
|
||||
expect(initParams).toEqual(
|
||||
StakeInstruction.decodeInitialize(stakeInstruction),
|
||||
);
|
||||
});
|
||||
|
||||
test('createAccount', () => {
|
||||
@ -62,24 +69,31 @@ test('createAccount', () => {
|
||||
const authorizedPubkey = new Account().publicKey;
|
||||
const authorized = new Authorized(authorizedPubkey, authorizedPubkey);
|
||||
const lockup = new Lockup(0, 0, fromPubkey);
|
||||
const lamports = 123;
|
||||
const transaction = StakeProgram.createAccount({
|
||||
fromPubkey,
|
||||
stakePubkey: newAccountPubkey,
|
||||
authorized,
|
||||
lockup,
|
||||
lamports: 123,
|
||||
lamports,
|
||||
});
|
||||
|
||||
expect(transaction.instructions).toHaveLength(2);
|
||||
const [systemInstruction, stakeInstruction] = transaction.instructions;
|
||||
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
||||
const systemParams = {
|
||||
fromPubkey,
|
||||
newAccountPubkey,
|
||||
lamports,
|
||||
space: StakeProgram.space,
|
||||
programId: StakeProgram.programId,
|
||||
};
|
||||
expect(systemParams).toEqual(
|
||||
SystemInstruction.decodeCreateAccount(systemInstruction),
|
||||
);
|
||||
|
||||
// TODO decode system instruction
|
||||
|
||||
const params = StakeInstruction.decodeInitialize(stakeInstruction);
|
||||
expect(params.stakePubkey).toEqual(newAccountPubkey);
|
||||
expect(params.authorized).toEqual(authorized);
|
||||
expect(params.lockup).toEqual(lockup);
|
||||
const initParams = {stakePubkey: newAccountPubkey, authorized, lockup};
|
||||
expect(initParams).toEqual(
|
||||
StakeInstruction.decodeInitialize(stakeInstruction),
|
||||
);
|
||||
});
|
||||
|
||||
test('delegate', () => {
|
||||
@ -127,7 +141,16 @@ test('split', () => {
|
||||
const transaction = StakeProgram.split(params);
|
||||
expect(transaction.instructions).toHaveLength(2);
|
||||
const [systemInstruction, stakeInstruction] = transaction.instructions;
|
||||
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
||||
const systemParams = {
|
||||
fromPubkey: stakePubkey,
|
||||
newAccountPubkey: splitStakePubkey,
|
||||
lamports: 0,
|
||||
space: StakeProgram.space,
|
||||
programId: StakeProgram.programId,
|
||||
};
|
||||
expect(systemParams).toEqual(
|
||||
SystemInstruction.decodeCreateAccount(systemInstruction),
|
||||
);
|
||||
expect(params).toEqual(StakeInstruction.decodeSplit(stakeInstruction));
|
||||
});
|
||||
|
||||
@ -182,13 +205,10 @@ test('StakeInstructions', () => {
|
||||
);
|
||||
|
||||
expect(createWithSeedTransaction.instructions).toHaveLength(2);
|
||||
const systemInstruction = SystemInstruction.from(
|
||||
const systemInstructionType = SystemInstruction.decodeInstructionType(
|
||||
createWithSeedTransaction.instructions[0],
|
||||
);
|
||||
expect(systemInstruction.fromPublicKey).toEqual(from.publicKey);
|
||||
expect(systemInstruction.toPublicKey).toEqual(newAccountPubkey);
|
||||
expect(systemInstruction.amount).toEqual(amount);
|
||||
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
||||
expect(systemInstructionType).toEqual('CreateWithSeed');
|
||||
|
||||
const stakeInstructionType = StakeInstruction.decodeInstructionType(
|
||||
createWithSeedTransaction.instructions[1],
|
||||
@ -237,6 +257,35 @@ test('live staking actions', async () => {
|
||||
'recent',
|
||||
);
|
||||
|
||||
{
|
||||
// Create Stake account without seed
|
||||
const newStakeAccount = new Account();
|
||||
let createAndInitialize = StakeProgram.createAccount({
|
||||
fromPubkey: from.publicKey,
|
||||
stakePubkey: newStakeAccount.publicKey,
|
||||
authorized: new Authorized(authorized.publicKey, authorized.publicKey),
|
||||
lockup: new Lockup(0, 0, new PublicKey('0x00')),
|
||||
lamports: minimumAmount + 42,
|
||||
});
|
||||
|
||||
await sendAndConfirmRecentTransaction(
|
||||
connection,
|
||||
createAndInitialize,
|
||||
from,
|
||||
newStakeAccount,
|
||||
);
|
||||
expect(await connection.getBalance(newStakeAccount.publicKey)).toEqual(
|
||||
minimumAmount + 42,
|
||||
);
|
||||
|
||||
let delegation = StakeProgram.delegate({
|
||||
stakePubkey: newStakeAccount.publicKey,
|
||||
authorizedPubkey: authorized.publicKey,
|
||||
votePubkey,
|
||||
});
|
||||
await sendAndConfirmRecentTransaction(connection, delegation, authorized);
|
||||
}
|
||||
|
||||
// Create Stake account with seed
|
||||
const seed = 'test string';
|
||||
const newAccountPubkey = PublicKey.createWithSeed(
|
||||
|
@ -7,6 +7,7 @@ import {
|
||||
SystemInstruction,
|
||||
SystemProgram,
|
||||
Transaction,
|
||||
TransactionInstruction,
|
||||
sendAndConfirmRecentTransaction,
|
||||
LAMPORTS_PER_SOL,
|
||||
} from '../src';
|
||||
@ -20,219 +21,129 @@ if (!mockRpcEnabled) {
|
||||
}
|
||||
|
||||
test('createAccount', () => {
|
||||
const from = new Account();
|
||||
const newAccount = new Account();
|
||||
let transaction;
|
||||
|
||||
transaction = SystemProgram.createAccount(
|
||||
from.publicKey,
|
||||
newAccount.publicKey,
|
||||
123,
|
||||
BudgetProgram.space,
|
||||
BudgetProgram.programId,
|
||||
const params = {
|
||||
fromPubkey: new Account().publicKey,
|
||||
newAccountPubkey: new Account().publicKey,
|
||||
lamports: 123,
|
||||
space: BudgetProgram.space,
|
||||
programId: BudgetProgram.programId,
|
||||
};
|
||||
const transaction = SystemProgram.createAccount(params);
|
||||
expect(transaction.instructions).toHaveLength(1);
|
||||
const [systemInstruction] = transaction.instructions;
|
||||
expect(params).toEqual(
|
||||
SystemInstruction.decodeCreateAccount(systemInstruction),
|
||||
);
|
||||
|
||||
expect(transaction.keys).toHaveLength(2);
|
||||
expect(transaction.programId).toEqual(SystemProgram.programId);
|
||||
// TODO: Validate transaction contents more
|
||||
});
|
||||
|
||||
test('transfer', () => {
|
||||
const from = new Account();
|
||||
const to = new Account();
|
||||
let transaction;
|
||||
|
||||
transaction = SystemProgram.transfer(from.publicKey, to.publicKey, 123);
|
||||
|
||||
expect(transaction.keys).toHaveLength(2);
|
||||
expect(transaction.programId).toEqual(SystemProgram.programId);
|
||||
// TODO: Validate transaction contents more
|
||||
const params = {
|
||||
fromPubkey: new Account().publicKey,
|
||||
toPubkey: new Account().publicKey,
|
||||
lamports: 123,
|
||||
};
|
||||
const transaction = SystemProgram.transfer(params);
|
||||
expect(transaction.instructions).toHaveLength(1);
|
||||
const [systemInstruction] = transaction.instructions;
|
||||
expect(params).toEqual(SystemInstruction.decodeTransfer(systemInstruction));
|
||||
});
|
||||
|
||||
test('assign', () => {
|
||||
const from = new Account();
|
||||
const to = new Account();
|
||||
let transaction;
|
||||
|
||||
transaction = SystemProgram.assign(from.publicKey, to.publicKey);
|
||||
|
||||
expect(transaction.keys).toHaveLength(1);
|
||||
expect(transaction.programId).toEqual(SystemProgram.programId);
|
||||
// TODO: Validate transaction contents more
|
||||
const params = {
|
||||
fromPubkey: new Account().publicKey,
|
||||
programId: new Account().publicKey,
|
||||
};
|
||||
const transaction = SystemProgram.assign(params);
|
||||
expect(transaction.instructions).toHaveLength(1);
|
||||
const [systemInstruction] = transaction.instructions;
|
||||
expect(params).toEqual(SystemInstruction.decodeAssign(systemInstruction));
|
||||
});
|
||||
|
||||
test('createAccountWithSeed', () => {
|
||||
const from = new Account();
|
||||
const newAccount = new Account();
|
||||
let transaction;
|
||||
|
||||
transaction = SystemProgram.createAccountWithSeed(
|
||||
from.publicKey,
|
||||
newAccount.publicKey,
|
||||
from.publicKey,
|
||||
'hi there',
|
||||
123,
|
||||
BudgetProgram.space,
|
||||
BudgetProgram.programId,
|
||||
const fromPubkey = new Account().publicKey;
|
||||
const params = {
|
||||
fromPubkey,
|
||||
newAccountPubkey: new Account().publicKey,
|
||||
basePubkey: fromPubkey,
|
||||
seed: 'hi there',
|
||||
lamports: 123,
|
||||
space: BudgetProgram.space,
|
||||
programId: BudgetProgram.programId,
|
||||
};
|
||||
const transaction = SystemProgram.createAccountWithSeed(params);
|
||||
expect(transaction.instructions).toHaveLength(1);
|
||||
const [systemInstruction] = transaction.instructions;
|
||||
expect(params).toEqual(
|
||||
SystemInstruction.decodeCreateWithSeed(systemInstruction),
|
||||
);
|
||||
|
||||
expect(transaction.keys).toHaveLength(2);
|
||||
expect(transaction.programId).toEqual(SystemProgram.programId);
|
||||
// TODO: Validate transaction contents more
|
||||
});
|
||||
|
||||
test('createNonceAccount', () => {
|
||||
const from = new Account();
|
||||
const nonceAccount = new Account();
|
||||
|
||||
const transaction = SystemProgram.createNonceAccount(
|
||||
from.publicKey,
|
||||
nonceAccount.publicKey,
|
||||
from.publicKey,
|
||||
123,
|
||||
);
|
||||
const fromPubkey = new Account().publicKey;
|
||||
const params = {
|
||||
fromPubkey,
|
||||
noncePubkey: new Account().publicKey,
|
||||
authorizedPubkey: fromPubkey,
|
||||
lamports: 123,
|
||||
};
|
||||
|
||||
const transaction = SystemProgram.createNonceAccount(params);
|
||||
expect(transaction.instructions).toHaveLength(2);
|
||||
expect(transaction.instructions[0].programId).toEqual(
|
||||
SystemProgram.programId,
|
||||
const [createInstruction, initInstruction] = transaction.instructions;
|
||||
|
||||
const createParams = {
|
||||
fromPubkey: params.fromPubkey,
|
||||
newAccountPubkey: params.noncePubkey,
|
||||
lamports: params.lamports,
|
||||
space: SystemProgram.nonceSpace,
|
||||
programId: SystemProgram.programId,
|
||||
};
|
||||
expect(createParams).toEqual(
|
||||
SystemInstruction.decodeCreateAccount(createInstruction),
|
||||
);
|
||||
expect(transaction.instructions[1].programId).toEqual(
|
||||
SystemProgram.programId,
|
||||
|
||||
const initParams = {
|
||||
noncePubkey: params.noncePubkey,
|
||||
authorizedPubkey: fromPubkey,
|
||||
};
|
||||
expect(initParams).toEqual(
|
||||
SystemInstruction.decodeNonceInitialize(initInstruction),
|
||||
);
|
||||
// TODO: Validate transaction contents more
|
||||
});
|
||||
|
||||
test('nonceAdvance', () => {
|
||||
const params = {
|
||||
noncePubkey: new Account().publicKey,
|
||||
authorizedPubkey: new Account().publicKey,
|
||||
};
|
||||
const instruction = SystemProgram.nonceAdvance(params);
|
||||
expect(params).toEqual(SystemInstruction.decodeNonceAdvance(instruction));
|
||||
});
|
||||
|
||||
test('nonceWithdraw', () => {
|
||||
const from = new Account();
|
||||
const nonceAccount = new Account();
|
||||
const to = new Account();
|
||||
|
||||
const transaction = SystemProgram.nonceWithdraw(
|
||||
nonceAccount.publicKey,
|
||||
from.publicKey,
|
||||
to.publicKey,
|
||||
123,
|
||||
);
|
||||
|
||||
expect(transaction.keys).toHaveLength(5);
|
||||
expect(transaction.programId).toEqual(SystemProgram.programId);
|
||||
// TODO: Validate transaction contents more
|
||||
const params = {
|
||||
noncePubkey: new Account().publicKey,
|
||||
authorizedPubkey: new Account().publicKey,
|
||||
toPubkey: new Account().publicKey,
|
||||
lamports: 123,
|
||||
};
|
||||
const transaction = SystemProgram.nonceWithdraw(params);
|
||||
expect(transaction.instructions).toHaveLength(1);
|
||||
const [instruction] = transaction.instructions;
|
||||
expect(params).toEqual(SystemInstruction.decodeNonceWithdraw(instruction));
|
||||
});
|
||||
|
||||
test('nonceAuthorize', () => {
|
||||
const nonceAccount = new Account();
|
||||
const authorized = new Account();
|
||||
const newAuthorized = new Account();
|
||||
const params = {
|
||||
noncePubkey: new Account().publicKey,
|
||||
authorizedPubkey: new Account().publicKey,
|
||||
newAuthorizedPubkey: new Account().publicKey,
|
||||
};
|
||||
|
||||
const transaction = SystemProgram.nonceAuthorize(
|
||||
nonceAccount.publicKey,
|
||||
authorized.publicKey,
|
||||
newAuthorized.publicKey,
|
||||
);
|
||||
|
||||
expect(transaction.keys).toHaveLength(2);
|
||||
expect(transaction.programId).toEqual(SystemProgram.programId);
|
||||
// TODO: Validate transaction contents more
|
||||
});
|
||||
|
||||
test('SystemInstruction create', () => {
|
||||
const from = new Account();
|
||||
const to = new Account();
|
||||
const program = new Account();
|
||||
const amount = 42;
|
||||
const space = 100;
|
||||
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
||||
const create = SystemProgram.createAccount(
|
||||
from.publicKey,
|
||||
to.publicKey,
|
||||
amount,
|
||||
space,
|
||||
program.publicKey,
|
||||
);
|
||||
const transaction = new Transaction({recentBlockhash}).add(create);
|
||||
|
||||
const systemInstruction = SystemInstruction.from(transaction.instructions[0]);
|
||||
expect(systemInstruction.fromPublicKey).toEqual(from.publicKey);
|
||||
expect(systemInstruction.toPublicKey).toEqual(to.publicKey);
|
||||
expect(systemInstruction.amount).toEqual(amount);
|
||||
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
||||
});
|
||||
|
||||
test('SystemInstruction transfer', () => {
|
||||
const from = new Account();
|
||||
const to = new Account();
|
||||
const amount = 42;
|
||||
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
||||
const transfer = SystemProgram.transfer(from.publicKey, to.publicKey, amount);
|
||||
const transaction = new Transaction({recentBlockhash}).add(transfer);
|
||||
transaction.sign(from);
|
||||
|
||||
const systemInstruction = SystemInstruction.from(transaction.instructions[0]);
|
||||
expect(systemInstruction.fromPublicKey).toEqual(from.publicKey);
|
||||
expect(systemInstruction.toPublicKey).toEqual(to.publicKey);
|
||||
expect(systemInstruction.amount).toEqual(amount);
|
||||
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
||||
});
|
||||
|
||||
test('SystemInstruction assign', () => {
|
||||
const from = new Account();
|
||||
const program = new Account();
|
||||
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
||||
const assign = SystemProgram.assign(from.publicKey, program.publicKey);
|
||||
const transaction = new Transaction({recentBlockhash}).add(assign);
|
||||
transaction.sign(from);
|
||||
|
||||
const systemInstruction = SystemInstruction.from(transaction.instructions[0]);
|
||||
expect(systemInstruction.fromPublicKey).toBeNull();
|
||||
expect(systemInstruction.toPublicKey).toBeNull();
|
||||
expect(systemInstruction.amount).toBeNull();
|
||||
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
||||
});
|
||||
|
||||
test('SystemInstruction createWithSeed', () => {
|
||||
const from = new Account();
|
||||
const to = new Account();
|
||||
const program = new Account();
|
||||
const amount = 42;
|
||||
const space = 100;
|
||||
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
||||
const create = SystemProgram.createAccountWithSeed(
|
||||
from.publicKey,
|
||||
to.publicKey,
|
||||
from.publicKey,
|
||||
'hi there',
|
||||
amount,
|
||||
space,
|
||||
program.publicKey,
|
||||
);
|
||||
const transaction = new Transaction({recentBlockhash}).add(create);
|
||||
|
||||
const systemInstruction = SystemInstruction.from(transaction.instructions[0]);
|
||||
expect(systemInstruction.fromPublicKey).toEqual(from.publicKey);
|
||||
expect(systemInstruction.toPublicKey).toEqual(to.publicKey);
|
||||
expect(systemInstruction.amount).toEqual(amount);
|
||||
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
||||
});
|
||||
|
||||
test('SystemInstruction nonceWithdraw', () => {
|
||||
const nonceAccount = new Account();
|
||||
const authorized = new Account();
|
||||
const to = new Account();
|
||||
const amount = 42;
|
||||
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
||||
const nonceWithdraw = SystemProgram.nonceWithdraw(
|
||||
nonceAccount.publicKey,
|
||||
authorized.publicKey,
|
||||
to.publicKey,
|
||||
amount,
|
||||
);
|
||||
const transaction = new Transaction({recentBlockhash}).add(nonceWithdraw);
|
||||
|
||||
const systemInstruction = SystemInstruction.from(transaction.instructions[0]);
|
||||
expect(systemInstruction.fromPublicKey).toEqual(nonceAccount.publicKey);
|
||||
expect(systemInstruction.toPublicKey).toEqual(to.publicKey);
|
||||
expect(systemInstruction.amount).toEqual(amount);
|
||||
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
||||
const transaction = SystemProgram.nonceAuthorize(params);
|
||||
expect(transaction.instructions).toHaveLength(1);
|
||||
const [instruction] = transaction.instructions;
|
||||
expect(params).toEqual(SystemInstruction.decodeNonceAuthorize(instruction));
|
||||
});
|
||||
|
||||
test('non-SystemInstruction error', () => {
|
||||
@ -249,7 +160,9 @@ test('non-SystemInstruction error', () => {
|
||||
data: Buffer.from([2, 0, 0, 0]),
|
||||
};
|
||||
expect(() => {
|
||||
new SystemInstruction(badProgramId, 'Create');
|
||||
SystemInstruction.decodeInstructionType(
|
||||
new TransactionInstruction(badProgramId),
|
||||
);
|
||||
}).toThrow();
|
||||
|
||||
const amount = 123;
|
||||
@ -264,12 +177,12 @@ test('non-SystemInstruction error', () => {
|
||||
transaction.sign(from);
|
||||
|
||||
expect(() => {
|
||||
SystemInstruction.from(transaction.instructions[1]);
|
||||
SystemInstruction.decodeInstructionType(transaction.instructions[1]);
|
||||
}).toThrow();
|
||||
|
||||
transaction.instructions[0].data[0] = 11;
|
||||
expect(() => {
|
||||
SystemInstruction.from(transaction.instructions[0]);
|
||||
SystemInstruction.decodeInstructionType(transaction.instructions[0]);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
@ -284,20 +197,22 @@ test('live Nonce actions', async () => {
|
||||
const from = new Account();
|
||||
const to = new Account();
|
||||
const authority = new Account();
|
||||
const newAuthority = new Account();
|
||||
await connection.requestAirdrop(from.publicKey, 2 * LAMPORTS_PER_SOL);
|
||||
await connection.requestAirdrop(authority.publicKey, LAMPORTS_PER_SOL);
|
||||
await connection.requestAirdrop(newAuthority.publicKey, LAMPORTS_PER_SOL);
|
||||
|
||||
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
|
||||
SystemProgram.nonceSpace,
|
||||
'recent',
|
||||
);
|
||||
|
||||
let createNonceAccount = SystemProgram.createNonceAccount(
|
||||
from.publicKey,
|
||||
nonceAccount.publicKey,
|
||||
from.publicKey,
|
||||
minimumAmount,
|
||||
);
|
||||
let createNonceAccount = SystemProgram.createNonceAccount({
|
||||
fromPubkey: from.publicKey,
|
||||
noncePubkey: nonceAccount.publicKey,
|
||||
authorizedPubkey: from.publicKey,
|
||||
lamports: minimumAmount,
|
||||
});
|
||||
await sendAndConfirmRecentTransaction(
|
||||
connection,
|
||||
createNonceAccount,
|
||||
@ -311,33 +226,74 @@ test('live Nonce actions', async () => {
|
||||
const nonceQuery2 = await connection.getNonce(nonceAccount.publicKey);
|
||||
expect(nonceQuery1.nonce).toEqual(nonceQuery2.nonce);
|
||||
|
||||
// Wait for blockhash to advance
|
||||
await sleep(500);
|
||||
|
||||
const advanceNonce = new Transaction().add(
|
||||
SystemProgram.nonceAdvance(nonceAccount.publicKey, from.publicKey),
|
||||
SystemProgram.nonceAdvance({
|
||||
noncePubkey: nonceAccount.publicKey,
|
||||
authorizedPubkey: from.publicKey,
|
||||
}),
|
||||
);
|
||||
await sendAndConfirmRecentTransaction(connection, advanceNonce, from);
|
||||
|
||||
const nonceQuery3 = await connection.getNonce(nonceAccount.publicKey);
|
||||
expect(nonceQuery1.nonce).not.toEqual(nonceQuery3.nonce);
|
||||
const nonce = nonceQuery3.nonce;
|
||||
|
||||
// Wait for blockhash to advance
|
||||
await sleep(500);
|
||||
|
||||
let transfer = SystemProgram.transfer(
|
||||
from.publicKey,
|
||||
to.publicKey,
|
||||
minimumAmount,
|
||||
const authorizeNonce = new Transaction().add(
|
||||
SystemProgram.nonceAuthorize({
|
||||
noncePubkey: nonceAccount.publicKey,
|
||||
authorizedPubkey: from.publicKey,
|
||||
newAuthorizedPubkey: newAuthority.publicKey,
|
||||
}),
|
||||
);
|
||||
await sendAndConfirmRecentTransaction(connection, authorizeNonce, from);
|
||||
|
||||
let transfer = SystemProgram.transfer({
|
||||
fromPubkey: from.publicKey,
|
||||
toPubkey: to.publicKey,
|
||||
lamports: minimumAmount,
|
||||
});
|
||||
transfer.nonceInfo = {
|
||||
nonce,
|
||||
nonceInstruction: SystemProgram.nonceAdvance(
|
||||
nonceAccount.publicKey,
|
||||
from.publicKey,
|
||||
),
|
||||
nonceInstruction: SystemProgram.nonceAdvance({
|
||||
noncePubkey: nonceAccount.publicKey,
|
||||
authorizedPubkey: newAuthority.publicKey,
|
||||
}),
|
||||
};
|
||||
|
||||
await sendAndConfirmRecentTransaction(connection, transfer, from);
|
||||
await sendAndConfirmRecentTransaction(
|
||||
connection,
|
||||
transfer,
|
||||
from,
|
||||
newAuthority,
|
||||
);
|
||||
const toBalance = await connection.getBalance(to.publicKey);
|
||||
expect(toBalance).toEqual(minimumAmount);
|
||||
|
||||
// Wait for blockhash to advance
|
||||
await sleep(500);
|
||||
|
||||
const withdrawAccount = new Account();
|
||||
const withdrawNonce = new Transaction().add(
|
||||
SystemProgram.nonceWithdraw({
|
||||
noncePubkey: nonceAccount.publicKey,
|
||||
authorizedPubkey: newAuthority.publicKey,
|
||||
lamports: minimumAmount,
|
||||
toPubkey: withdrawAccount.publicKey,
|
||||
}),
|
||||
);
|
||||
await sendAndConfirmRecentTransaction(
|
||||
connection,
|
||||
withdrawNonce,
|
||||
newAuthority,
|
||||
);
|
||||
expect(await connection.getBalance(nonceAccount.publicKey)).toEqual(0);
|
||||
const withdrawBalance = await connection.getBalance(
|
||||
withdrawAccount.publicKey,
|
||||
);
|
||||
expect(withdrawBalance).toEqual(minimumAmount);
|
||||
});
|
||||
|
@ -100,11 +100,11 @@ test('transaction-payer', async () => {
|
||||
},
|
||||
]);
|
||||
|
||||
const transaction = SystemProgram.transfer(
|
||||
accountFrom.publicKey,
|
||||
accountTo.publicKey,
|
||||
10,
|
||||
);
|
||||
const transaction = SystemProgram.transfer({
|
||||
fromPubkey: accountFrom.publicKey,
|
||||
toPubkey: accountTo.publicKey,
|
||||
lamports: 10,
|
||||
});
|
||||
|
||||
const signature = await connection.sendTransaction(
|
||||
transaction,
|
||||
|
@ -12,11 +12,11 @@ test('signPartial', () => {
|
||||
const account1 = new Account();
|
||||
const account2 = new Account();
|
||||
const recentBlockhash = account1.publicKey.toBase58(); // Fake recentBlockhash
|
||||
const transfer = SystemProgram.transfer(
|
||||
account1.publicKey,
|
||||
account2.publicKey,
|
||||
123,
|
||||
);
|
||||
const transfer = SystemProgram.transfer({
|
||||
fromPubkey: account1.publicKey,
|
||||
toPubkey: account2.publicKey,
|
||||
lamports: 123,
|
||||
});
|
||||
|
||||
const transaction = new Transaction({recentBlockhash}).add(transfer);
|
||||
transaction.sign(account1, account2);
|
||||
@ -33,16 +33,16 @@ test('transfer signatures', () => {
|
||||
const account1 = new Account();
|
||||
const account2 = new Account();
|
||||
const recentBlockhash = account1.publicKey.toBase58(); // Fake recentBlockhash
|
||||
const transfer1 = SystemProgram.transfer(
|
||||
account1.publicKey,
|
||||
account2.publicKey,
|
||||
123,
|
||||
);
|
||||
const transfer2 = SystemProgram.transfer(
|
||||
account2.publicKey,
|
||||
account1.publicKey,
|
||||
123,
|
||||
);
|
||||
const transfer1 = SystemProgram.transfer({
|
||||
fromPubkey: account1.publicKey,
|
||||
toPubkey: account2.publicKey,
|
||||
lamports: 123,
|
||||
});
|
||||
const transfer2 = SystemProgram.transfer({
|
||||
fromPubkey: account2.publicKey,
|
||||
toPubkey: account1.publicKey,
|
||||
lamports: 123,
|
||||
});
|
||||
|
||||
const orgTransaction = new Transaction({recentBlockhash}).add(
|
||||
transfer1,
|
||||
@ -62,16 +62,16 @@ test('dedup signatures', () => {
|
||||
const account1 = new Account();
|
||||
const account2 = new Account();
|
||||
const recentBlockhash = account1.publicKey.toBase58(); // Fake recentBlockhash
|
||||
const transfer1 = SystemProgram.transfer(
|
||||
account1.publicKey,
|
||||
account2.publicKey,
|
||||
123,
|
||||
);
|
||||
const transfer2 = SystemProgram.transfer(
|
||||
account1.publicKey,
|
||||
account2.publicKey,
|
||||
123,
|
||||
);
|
||||
const transfer1 = SystemProgram.transfer({
|
||||
fromPubkey: account1.publicKey,
|
||||
toPubkey: account2.publicKey,
|
||||
lamports: 123,
|
||||
});
|
||||
const transfer2 = SystemProgram.transfer({
|
||||
fromPubkey: account1.publicKey,
|
||||
toPubkey: account2.publicKey,
|
||||
lamports: 123,
|
||||
});
|
||||
|
||||
const orgTransaction = new Transaction({recentBlockhash}).add(
|
||||
transfer1,
|
||||
@ -88,14 +88,18 @@ test('use nonce', () => {
|
||||
|
||||
const nonceInfo = {
|
||||
nonce,
|
||||
nonceInstruction: SystemProgram.nonceAdvance(
|
||||
nonceAccount.publicKey,
|
||||
account1.publicKey,
|
||||
),
|
||||
nonceInstruction: SystemProgram.nonceAdvance({
|
||||
noncePubkey: nonceAccount.publicKey,
|
||||
authorizedPubkey: account1.publicKey,
|
||||
}),
|
||||
};
|
||||
|
||||
const transferTransaction = new Transaction({nonceInfo}).add(
|
||||
SystemProgram.transfer(account1.publicKey, account2.publicKey, 123),
|
||||
SystemProgram.transfer({
|
||||
fromPubkey: account1.publicKey,
|
||||
toPubkey: account2.publicKey,
|
||||
lamports: 123,
|
||||
}),
|
||||
);
|
||||
transferTransaction.sign(account1);
|
||||
|
||||
@ -137,7 +141,11 @@ test('parse wire format and serialize', () => {
|
||||
const recipient = new PublicKey(
|
||||
'J3dxNj7nDRRqRRXuEMynDG57DkZK4jYRuv3Garmb1i99',
|
||||
); // Arbitrary known public key
|
||||
const transfer = SystemProgram.transfer(sender.publicKey, recipient, 49);
|
||||
const transfer = SystemProgram.transfer({
|
||||
fromPubkey: sender.publicKey,
|
||||
toPubkey: recipient,
|
||||
lamports: 49,
|
||||
});
|
||||
const expectedTransaction = new Transaction({recentBlockhash}).add(transfer);
|
||||
expectedTransaction.sign(sender);
|
||||
|
||||
@ -198,7 +206,11 @@ test('serialize unsigned transaction', () => {
|
||||
const recipient = new PublicKey(
|
||||
'J3dxNj7nDRRqRRXuEMynDG57DkZK4jYRuv3Garmb1i99',
|
||||
); // Arbitrary known public key
|
||||
const transfer = SystemProgram.transfer(sender.publicKey, recipient, 49);
|
||||
const transfer = SystemProgram.transfer({
|
||||
fromPubkey: sender.publicKey,
|
||||
toPubkey: recipient,
|
||||
lamports: 49,
|
||||
});
|
||||
const expectedTransaction = new Transaction({recentBlockhash}).add(transfer);
|
||||
|
||||
const wireTransactionArray = [
|
||||
|
Reference in New Issue
Block a user