2018-09-26 09:29:24 -07:00
|
|
|
// @flow
|
|
|
|
|
2019-09-13 17:07:13 -07:00
|
|
|
import {
|
|
|
|
Account,
|
2020-01-07 16:57:56 -08:00
|
|
|
Connection,
|
2020-08-07 15:01:51 -07:00
|
|
|
StakeProgram,
|
2019-09-13 17:07:13 -07:00
|
|
|
SystemInstruction,
|
|
|
|
SystemProgram,
|
|
|
|
Transaction,
|
2020-03-03 00:05:50 -08:00
|
|
|
TransactionInstruction,
|
2020-05-20 02:13:21 -07:00
|
|
|
sendAndConfirmTransaction,
|
2020-01-07 16:57:56 -08:00
|
|
|
LAMPORTS_PER_SOL,
|
2019-09-13 17:07:13 -07:00
|
|
|
} from '../src';
|
2020-03-05 08:44:56 -08:00
|
|
|
import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account';
|
2020-01-07 16:57:56 -08:00
|
|
|
import {mockRpcEnabled} from './__mocks__/node-fetch';
|
|
|
|
import {sleep} from '../src/util/sleep';
|
|
|
|
import {url} from './url';
|
|
|
|
|
|
|
|
if (!mockRpcEnabled) {
|
|
|
|
// Testing max commitment level takes around 20s to complete
|
|
|
|
jest.setTimeout(30000);
|
|
|
|
}
|
2018-09-26 09:29:24 -07:00
|
|
|
|
|
|
|
test('createAccount', () => {
|
2020-03-03 00:05:50 -08:00
|
|
|
const params = {
|
|
|
|
fromPubkey: new Account().publicKey,
|
|
|
|
newAccountPubkey: new Account().publicKey,
|
|
|
|
lamports: 123,
|
2020-08-07 15:01:51 -07:00
|
|
|
space: 0,
|
|
|
|
programId: SystemProgram.programId,
|
2020-03-03 00:05:50 -08:00
|
|
|
};
|
|
|
|
const transaction = SystemProgram.createAccount(params);
|
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [systemInstruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(
|
|
|
|
SystemInstruction.decodeCreateAccount(systemInstruction),
|
2018-09-26 09:29:24 -07:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-04-11 18:20:22 -07:00
|
|
|
test('transfer', () => {
|
2020-03-03 00:05:50 -08:00
|
|
|
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));
|
2018-09-26 09:29:24 -07:00
|
|
|
});
|
|
|
|
|
2020-05-09 03:33:36 -07:00
|
|
|
test('allocate', () => {
|
|
|
|
const params = {
|
|
|
|
accountPubkey: new Account().publicKey,
|
|
|
|
space: 42,
|
|
|
|
};
|
|
|
|
const transaction = SystemProgram.allocate(params);
|
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [systemInstruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(SystemInstruction.decodeAllocate(systemInstruction));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('allocateWithSeed', () => {
|
|
|
|
const params = {
|
|
|
|
accountPubkey: new Account().publicKey,
|
|
|
|
basePubkey: new Account().publicKey,
|
|
|
|
seed: '你好',
|
|
|
|
space: 42,
|
|
|
|
programId: new Account().publicKey,
|
|
|
|
};
|
|
|
|
const transaction = SystemProgram.allocate(params);
|
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [systemInstruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(
|
|
|
|
SystemInstruction.decodeAllocateWithSeed(systemInstruction),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2018-09-26 09:29:24 -07:00
|
|
|
test('assign', () => {
|
2020-03-03 00:05:50 -08:00
|
|
|
const params = {
|
2020-05-09 03:33:36 -07:00
|
|
|
accountPubkey: new Account().publicKey,
|
2020-03-03 00:05:50 -08:00
|
|
|
programId: new Account().publicKey,
|
|
|
|
};
|
|
|
|
const transaction = SystemProgram.assign(params);
|
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [systemInstruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(SystemInstruction.decodeAssign(systemInstruction));
|
2018-09-26 09:29:24 -07:00
|
|
|
});
|
2019-09-13 17:07:13 -07:00
|
|
|
|
2020-05-09 03:33:36 -07:00
|
|
|
test('assignWithSeed', () => {
|
|
|
|
const params = {
|
|
|
|
accountPubkey: new Account().publicKey,
|
|
|
|
basePubkey: new Account().publicKey,
|
|
|
|
seed: '你好',
|
|
|
|
programId: new Account().publicKey,
|
|
|
|
};
|
|
|
|
const transaction = SystemProgram.assign(params);
|
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [systemInstruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(
|
|
|
|
SystemInstruction.decodeAssignWithSeed(systemInstruction),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-12-09 19:50:01 -08:00
|
|
|
test('createAccountWithSeed', () => {
|
2020-03-03 00:05:50 -08:00
|
|
|
const fromPubkey = new Account().publicKey;
|
|
|
|
const params = {
|
|
|
|
fromPubkey,
|
|
|
|
newAccountPubkey: new Account().publicKey,
|
|
|
|
basePubkey: fromPubkey,
|
|
|
|
seed: 'hi there',
|
|
|
|
lamports: 123,
|
2020-08-07 15:01:51 -07:00
|
|
|
space: 0,
|
|
|
|
programId: SystemProgram.programId,
|
2020-03-03 00:05:50 -08:00
|
|
|
};
|
|
|
|
const transaction = SystemProgram.createAccountWithSeed(params);
|
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [systemInstruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(
|
|
|
|
SystemInstruction.decodeCreateWithSeed(systemInstruction),
|
2019-12-09 19:50:01 -08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-01-02 16:58:54 -08:00
|
|
|
test('createNonceAccount', () => {
|
2020-03-03 00:05:50 -08:00
|
|
|
const fromPubkey = new Account().publicKey;
|
|
|
|
const params = {
|
|
|
|
fromPubkey,
|
|
|
|
noncePubkey: new Account().publicKey,
|
|
|
|
authorizedPubkey: fromPubkey,
|
|
|
|
lamports: 123,
|
|
|
|
};
|
2020-01-02 16:58:54 -08:00
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
const transaction = SystemProgram.createNonceAccount(params);
|
2020-01-02 16:58:54 -08:00
|
|
|
expect(transaction.instructions).toHaveLength(2);
|
2020-03-03 00:05:50 -08:00
|
|
|
const [createInstruction, initInstruction] = transaction.instructions;
|
|
|
|
|
|
|
|
const createParams = {
|
|
|
|
fromPubkey: params.fromPubkey,
|
|
|
|
newAccountPubkey: params.noncePubkey,
|
|
|
|
lamports: params.lamports,
|
2020-03-05 08:44:56 -08:00
|
|
|
space: NONCE_ACCOUNT_LENGTH,
|
2020-03-03 00:05:50 -08:00
|
|
|
programId: SystemProgram.programId,
|
|
|
|
};
|
|
|
|
expect(createParams).toEqual(
|
|
|
|
SystemInstruction.decodeCreateAccount(createInstruction),
|
2020-01-02 16:58:54 -08:00
|
|
|
);
|
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
const initParams = {
|
2020-05-04 22:30:55 -07:00
|
|
|
noncePubkey: params.noncePubkey,
|
|
|
|
authorizedPubkey: fromPubkey,
|
|
|
|
};
|
|
|
|
expect(initParams).toEqual(
|
|
|
|
SystemInstruction.decodeNonceInitialize(initInstruction),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('createNonceAccount with seed', () => {
|
|
|
|
const fromPubkey = new Account().publicKey;
|
|
|
|
const params = {
|
|
|
|
fromPubkey,
|
|
|
|
noncePubkey: new Account().publicKey,
|
|
|
|
authorizedPubkey: fromPubkey,
|
|
|
|
basePubkey: fromPubkey,
|
|
|
|
seed: 'hi there',
|
|
|
|
lamports: 123,
|
|
|
|
};
|
|
|
|
|
|
|
|
const transaction = SystemProgram.createNonceAccount(params);
|
|
|
|
expect(transaction.instructions).toHaveLength(2);
|
|
|
|
const [createInstruction, initInstruction] = transaction.instructions;
|
|
|
|
|
|
|
|
const createParams = {
|
|
|
|
fromPubkey: params.fromPubkey,
|
|
|
|
newAccountPubkey: params.noncePubkey,
|
|
|
|
basePubkey: fromPubkey,
|
|
|
|
seed: 'hi there',
|
|
|
|
lamports: params.lamports,
|
|
|
|
space: NONCE_ACCOUNT_LENGTH,
|
|
|
|
programId: SystemProgram.programId,
|
|
|
|
};
|
|
|
|
expect(createParams).toEqual(
|
|
|
|
SystemInstruction.decodeCreateWithSeed(createInstruction),
|
|
|
|
);
|
|
|
|
|
|
|
|
const initParams = {
|
2020-03-03 00:05:50 -08:00
|
|
|
noncePubkey: params.noncePubkey,
|
|
|
|
authorizedPubkey: fromPubkey,
|
|
|
|
};
|
|
|
|
expect(initParams).toEqual(
|
|
|
|
SystemInstruction.decodeNonceInitialize(initInstruction),
|
2019-09-13 17:07:13 -07:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
test('nonceAdvance', () => {
|
|
|
|
const params = {
|
|
|
|
noncePubkey: new Account().publicKey,
|
|
|
|
authorizedPubkey: new Account().publicKey,
|
|
|
|
};
|
|
|
|
const instruction = SystemProgram.nonceAdvance(params);
|
|
|
|
expect(params).toEqual(SystemInstruction.decodeNonceAdvance(instruction));
|
2019-09-13 17:07:13 -07:00
|
|
|
});
|
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
test('nonceWithdraw', () => {
|
|
|
|
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));
|
2019-12-09 19:50:01 -08:00
|
|
|
});
|
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
test('nonceAuthorize', () => {
|
|
|
|
const params = {
|
|
|
|
noncePubkey: new Account().publicKey,
|
|
|
|
authorizedPubkey: new Account().publicKey,
|
|
|
|
newAuthorizedPubkey: new Account().publicKey,
|
|
|
|
};
|
2020-01-02 16:58:54 -08:00
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
const transaction = SystemProgram.nonceAuthorize(params);
|
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [instruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(SystemInstruction.decodeNonceAuthorize(instruction));
|
2020-01-02 16:58:54 -08:00
|
|
|
});
|
|
|
|
|
2019-09-13 17:07:13 -07:00
|
|
|
test('non-SystemInstruction error', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
|
|
|
|
const badProgramId = {
|
|
|
|
keys: [
|
2019-11-06 09:42:01 -08:00
|
|
|
{pubkey: from.publicKey, isSigner: true, isWritable: true},
|
|
|
|
{pubkey: to.publicKey, isSigner: false, isWritable: true},
|
2019-09-13 17:07:13 -07:00
|
|
|
],
|
2020-08-07 15:01:51 -07:00
|
|
|
programId: StakeProgram.programId,
|
2019-09-13 17:07:13 -07:00
|
|
|
data: Buffer.from([2, 0, 0, 0]),
|
|
|
|
};
|
|
|
|
expect(() => {
|
2020-03-03 00:05:50 -08:00
|
|
|
SystemInstruction.decodeInstructionType(
|
|
|
|
new TransactionInstruction(badProgramId),
|
|
|
|
);
|
2019-09-13 17:07:13 -07:00
|
|
|
}).toThrow();
|
|
|
|
|
2020-08-07 15:01:51 -07:00
|
|
|
const stakePubkey = new Account().publicKey;
|
|
|
|
const authorizedPubkey = new Account().publicKey;
|
|
|
|
const params = {stakePubkey, authorizedPubkey};
|
|
|
|
const transaction = StakeProgram.deactivate(params);
|
2019-09-13 17:07:13 -07:00
|
|
|
|
|
|
|
expect(() => {
|
2020-03-03 00:05:50 -08:00
|
|
|
SystemInstruction.decodeInstructionType(transaction.instructions[1]);
|
2019-09-13 17:07:13 -07:00
|
|
|
}).toThrow();
|
|
|
|
|
2020-01-02 16:58:54 -08:00
|
|
|
transaction.instructions[0].data[0] = 11;
|
2019-09-13 17:07:13 -07:00
|
|
|
expect(() => {
|
2020-03-03 00:05:50 -08:00
|
|
|
SystemInstruction.decodeInstructionType(transaction.instructions[0]);
|
2019-09-13 17:07:13 -07:00
|
|
|
}).toThrow();
|
|
|
|
});
|
2020-01-07 16:57:56 -08:00
|
|
|
|
|
|
|
test('live Nonce actions', async () => {
|
|
|
|
if (mockRpcEnabled) {
|
|
|
|
console.log('non-live test skipped');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const connection = new Connection(url, 'recent');
|
|
|
|
const nonceAccount = new Account();
|
|
|
|
const from = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
const authority = new Account();
|
2020-03-03 00:05:50 -08:00
|
|
|
const newAuthority = new Account();
|
2020-01-07 16:57:56 -08:00
|
|
|
await connection.requestAirdrop(from.publicKey, 2 * LAMPORTS_PER_SOL);
|
|
|
|
await connection.requestAirdrop(authority.publicKey, LAMPORTS_PER_SOL);
|
2020-03-03 00:05:50 -08:00
|
|
|
await connection.requestAirdrop(newAuthority.publicKey, LAMPORTS_PER_SOL);
|
2020-01-07 16:57:56 -08:00
|
|
|
|
|
|
|
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
|
2020-03-05 08:44:56 -08:00
|
|
|
NONCE_ACCOUNT_LENGTH,
|
2020-01-07 16:57:56 -08:00
|
|
|
'recent',
|
|
|
|
);
|
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
let createNonceAccount = SystemProgram.createNonceAccount({
|
|
|
|
fromPubkey: from.publicKey,
|
|
|
|
noncePubkey: nonceAccount.publicKey,
|
|
|
|
authorizedPubkey: from.publicKey,
|
|
|
|
lamports: minimumAmount,
|
|
|
|
});
|
2020-05-20 02:13:21 -07:00
|
|
|
await sendAndConfirmTransaction(
|
2020-01-07 16:57:56 -08:00
|
|
|
connection,
|
|
|
|
createNonceAccount,
|
2020-05-20 02:13:21 -07:00
|
|
|
[from, nonceAccount],
|
2020-09-07 22:12:47 -07:00
|
|
|
{commitment: 'single', skipPreflight: true},
|
2020-01-07 16:57:56 -08:00
|
|
|
);
|
|
|
|
const nonceBalance = await connection.getBalance(nonceAccount.publicKey);
|
|
|
|
expect(nonceBalance).toEqual(minimumAmount);
|
|
|
|
|
|
|
|
const nonceQuery1 = await connection.getNonce(nonceAccount.publicKey);
|
2020-04-05 01:18:45 -07:00
|
|
|
if (nonceQuery1 === null) {
|
|
|
|
expect(nonceQuery1).not.toBeNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:57:56 -08:00
|
|
|
const nonceQuery2 = await connection.getNonce(nonceAccount.publicKey);
|
2020-04-05 01:18:45 -07:00
|
|
|
if (nonceQuery2 === null) {
|
|
|
|
expect(nonceQuery2).not.toBeNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:57:56 -08:00
|
|
|
expect(nonceQuery1.nonce).toEqual(nonceQuery2.nonce);
|
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
// Wait for blockhash to advance
|
2020-01-07 16:57:56 -08:00
|
|
|
await sleep(500);
|
|
|
|
|
|
|
|
const advanceNonce = new Transaction().add(
|
2020-03-03 00:05:50 -08:00
|
|
|
SystemProgram.nonceAdvance({
|
|
|
|
noncePubkey: nonceAccount.publicKey,
|
|
|
|
authorizedPubkey: from.publicKey,
|
|
|
|
}),
|
2020-01-07 16:57:56 -08:00
|
|
|
);
|
2020-06-03 04:55:42 -07:00
|
|
|
await sendAndConfirmTransaction(connection, advanceNonce, [from], {
|
2020-09-07 22:12:47 -07:00
|
|
|
commitment: 'single',
|
2020-06-03 04:55:42 -07:00
|
|
|
skipPreflight: true,
|
|
|
|
});
|
2020-01-07 16:57:56 -08:00
|
|
|
const nonceQuery3 = await connection.getNonce(nonceAccount.publicKey);
|
2020-04-05 01:18:45 -07:00
|
|
|
if (nonceQuery3 === null) {
|
|
|
|
expect(nonceQuery3).not.toBeNull();
|
|
|
|
return;
|
|
|
|
}
|
2020-01-07 16:57:56 -08:00
|
|
|
expect(nonceQuery1.nonce).not.toEqual(nonceQuery3.nonce);
|
|
|
|
const nonce = nonceQuery3.nonce;
|
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
// Wait for blockhash to advance
|
2020-01-07 16:57:56 -08:00
|
|
|
await sleep(500);
|
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
const authorizeNonce = new Transaction().add(
|
|
|
|
SystemProgram.nonceAuthorize({
|
|
|
|
noncePubkey: nonceAccount.publicKey,
|
|
|
|
authorizedPubkey: from.publicKey,
|
|
|
|
newAuthorizedPubkey: newAuthority.publicKey,
|
|
|
|
}),
|
2020-01-07 16:57:56 -08:00
|
|
|
);
|
2020-06-03 04:55:42 -07:00
|
|
|
await sendAndConfirmTransaction(connection, authorizeNonce, [from], {
|
2020-09-07 22:12:47 -07:00
|
|
|
commitment: 'single',
|
2020-06-03 04:55:42 -07:00
|
|
|
skipPreflight: true,
|
|
|
|
});
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
let transfer = SystemProgram.transfer({
|
|
|
|
fromPubkey: from.publicKey,
|
|
|
|
toPubkey: to.publicKey,
|
|
|
|
lamports: minimumAmount,
|
|
|
|
});
|
2020-01-07 16:57:56 -08:00
|
|
|
transfer.nonceInfo = {
|
|
|
|
nonce,
|
2020-03-03 00:05:50 -08:00
|
|
|
nonceInstruction: SystemProgram.nonceAdvance({
|
|
|
|
noncePubkey: nonceAccount.publicKey,
|
|
|
|
authorizedPubkey: newAuthority.publicKey,
|
|
|
|
}),
|
2020-01-07 16:57:56 -08:00
|
|
|
};
|
|
|
|
|
2020-06-03 04:55:42 -07:00
|
|
|
await sendAndConfirmTransaction(connection, transfer, [from, newAuthority], {
|
2020-09-07 22:12:47 -07:00
|
|
|
commitment: 'single',
|
2020-06-03 04:55:42 -07:00
|
|
|
skipPreflight: true,
|
|
|
|
});
|
2020-01-07 16:57:56 -08:00
|
|
|
const toBalance = await connection.getBalance(to.publicKey);
|
|
|
|
expect(toBalance).toEqual(minimumAmount);
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
}),
|
|
|
|
);
|
2020-06-03 04:55:42 -07:00
|
|
|
await sendAndConfirmTransaction(connection, withdrawNonce, [newAuthority], {
|
2020-09-07 22:12:47 -07:00
|
|
|
commitment: 'single',
|
2020-06-03 04:55:42 -07:00
|
|
|
skipPreflight: true,
|
|
|
|
});
|
2020-03-03 00:05:50 -08:00
|
|
|
expect(await connection.getBalance(nonceAccount.publicKey)).toEqual(0);
|
|
|
|
const withdrawBalance = await connection.getBalance(
|
|
|
|
withdrawAccount.publicKey,
|
|
|
|
);
|
|
|
|
expect(withdrawBalance).toEqual(minimumAmount);
|
2020-01-07 16:57:56 -08:00
|
|
|
});
|