2018-09-26 09:29:24 -07:00
|
|
|
// @flow
|
|
|
|
|
2019-09-13 18:07:13 -06:00
|
|
|
import {
|
|
|
|
Account,
|
2020-01-07 17:57:56 -07:00
|
|
|
Connection,
|
2021-01-14 09:59:31 -07:00
|
|
|
PublicKey,
|
2020-08-07 16:01:51 -06:00
|
|
|
StakeProgram,
|
2019-09-13 18:07:13 -06:00
|
|
|
SystemInstruction,
|
|
|
|
SystemProgram,
|
|
|
|
Transaction,
|
2020-03-03 16:05:50 +08:00
|
|
|
TransactionInstruction,
|
2020-05-20 17:13:21 +08:00
|
|
|
sendAndConfirmTransaction,
|
2020-01-07 17:57:56 -07:00
|
|
|
LAMPORTS_PER_SOL,
|
2019-09-13 18:07:13 -06:00
|
|
|
} from '../src';
|
2020-03-05 09:44:56 -07:00
|
|
|
import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account';
|
2020-01-07 17:57:56 -07:00
|
|
|
import {mockRpcEnabled} from './__mocks__/node-fetch';
|
2020-12-24 10:43:45 -08:00
|
|
|
import {newAccountWithLamports} from './new-account-with-lamports';
|
2020-01-07 17:57:56 -07:00
|
|
|
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 16:05:50 +08:00
|
|
|
const params = {
|
|
|
|
fromPubkey: new Account().publicKey,
|
|
|
|
newAccountPubkey: new Account().publicKey,
|
|
|
|
lamports: 123,
|
2020-08-07 16:01:51 -06:00
|
|
|
space: 0,
|
|
|
|
programId: SystemProgram.programId,
|
2020-03-03 16:05:50 +08:00
|
|
|
};
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
SystemProgram.createAccount(params),
|
|
|
|
);
|
2020-03-03 16:05:50 +08:00
|
|
|
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 16:05:50 +08:00
|
|
|
const params = {
|
|
|
|
fromPubkey: new Account().publicKey,
|
|
|
|
toPubkey: new Account().publicKey,
|
|
|
|
lamports: 123,
|
|
|
|
};
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(SystemProgram.transfer(params));
|
2020-03-03 16:05:50 +08:00
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [systemInstruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(SystemInstruction.decodeTransfer(systemInstruction));
|
2018-09-26 09:29:24 -07:00
|
|
|
});
|
|
|
|
|
2021-01-14 09:59:31 -07:00
|
|
|
test('transferWithSeed', () => {
|
|
|
|
const params = {
|
|
|
|
fromPubkey: new Account().publicKey,
|
|
|
|
basePubkey: new Account().publicKey,
|
|
|
|
toPubkey: new Account().publicKey,
|
|
|
|
lamports: 123,
|
|
|
|
seed: '你好',
|
|
|
|
programId: new Account().publicKey,
|
|
|
|
};
|
|
|
|
const transaction = new Transaction().add(SystemProgram.transfer(params));
|
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [systemInstruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(
|
|
|
|
SystemInstruction.decodeTransferWithSeed(systemInstruction),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-05-09 18:33:36 +08:00
|
|
|
test('allocate', () => {
|
|
|
|
const params = {
|
|
|
|
accountPubkey: new Account().publicKey,
|
|
|
|
space: 42,
|
|
|
|
};
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(SystemProgram.allocate(params));
|
2020-05-09 18:33:36 +08:00
|
|
|
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,
|
|
|
|
};
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(SystemProgram.allocate(params));
|
2020-05-09 18:33:36 +08:00
|
|
|
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 16:05:50 +08:00
|
|
|
const params = {
|
2020-05-09 18:33:36 +08:00
|
|
|
accountPubkey: new Account().publicKey,
|
2020-03-03 16:05:50 +08:00
|
|
|
programId: new Account().publicKey,
|
|
|
|
};
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(SystemProgram.assign(params));
|
2020-03-03 16:05:50 +08:00
|
|
|
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 18:07:13 -06:00
|
|
|
|
2020-05-09 18:33:36 +08:00
|
|
|
test('assignWithSeed', () => {
|
|
|
|
const params = {
|
|
|
|
accountPubkey: new Account().publicKey,
|
|
|
|
basePubkey: new Account().publicKey,
|
|
|
|
seed: '你好',
|
|
|
|
programId: new Account().publicKey,
|
|
|
|
};
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(SystemProgram.assign(params));
|
2020-05-09 18:33:36 +08:00
|
|
|
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 16: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 16:01:51 -06:00
|
|
|
space: 0,
|
|
|
|
programId: SystemProgram.programId,
|
2020-03-03 16:05:50 +08:00
|
|
|
};
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
SystemProgram.createAccountWithSeed(params),
|
|
|
|
);
|
2020-03-03 16:05:50 +08:00
|
|
|
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 17:58:54 -07:00
|
|
|
test('createNonceAccount', () => {
|
2020-03-03 16:05:50 +08:00
|
|
|
const fromPubkey = new Account().publicKey;
|
|
|
|
const params = {
|
|
|
|
fromPubkey,
|
|
|
|
noncePubkey: new Account().publicKey,
|
|
|
|
authorizedPubkey: fromPubkey,
|
|
|
|
lamports: 123,
|
|
|
|
};
|
2020-01-02 17:58:54 -07:00
|
|
|
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
SystemProgram.createNonceAccount(params),
|
|
|
|
);
|
2020-01-02 17:58:54 -07:00
|
|
|
expect(transaction.instructions).toHaveLength(2);
|
2020-03-03 16:05:50 +08:00
|
|
|
const [createInstruction, initInstruction] = transaction.instructions;
|
|
|
|
|
|
|
|
const createParams = {
|
|
|
|
fromPubkey: params.fromPubkey,
|
|
|
|
newAccountPubkey: params.noncePubkey,
|
|
|
|
lamports: params.lamports,
|
2020-03-05 09:44:56 -07:00
|
|
|
space: NONCE_ACCOUNT_LENGTH,
|
2020-03-03 16:05:50 +08:00
|
|
|
programId: SystemProgram.programId,
|
|
|
|
};
|
|
|
|
expect(createParams).toEqual(
|
|
|
|
SystemInstruction.decodeCreateAccount(createInstruction),
|
2020-01-02 17:58:54 -07:00
|
|
|
);
|
|
|
|
|
2020-03-03 16:05:50 +08:00
|
|
|
const initParams = {
|
2020-05-05 13:30:55 +08: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,
|
|
|
|
};
|
|
|
|
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
SystemProgram.createNonceAccount(params),
|
|
|
|
);
|
2020-05-05 13:30:55 +08:00
|
|
|
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 16:05:50 +08:00
|
|
|
noncePubkey: params.noncePubkey,
|
|
|
|
authorizedPubkey: fromPubkey,
|
|
|
|
};
|
|
|
|
expect(initParams).toEqual(
|
|
|
|
SystemInstruction.decodeNonceInitialize(initInstruction),
|
2019-09-13 18:07:13 -06:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-03-03 16: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 18:07:13 -06:00
|
|
|
});
|
|
|
|
|
2020-03-03 16:05:50 +08:00
|
|
|
test('nonceWithdraw', () => {
|
|
|
|
const params = {
|
|
|
|
noncePubkey: new Account().publicKey,
|
|
|
|
authorizedPubkey: new Account().publicKey,
|
|
|
|
toPubkey: new Account().publicKey,
|
|
|
|
lamports: 123,
|
|
|
|
};
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
SystemProgram.nonceWithdraw(params),
|
|
|
|
);
|
2020-03-03 16:05:50 +08:00
|
|
|
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 16:05:50 +08:00
|
|
|
test('nonceAuthorize', () => {
|
|
|
|
const params = {
|
|
|
|
noncePubkey: new Account().publicKey,
|
|
|
|
authorizedPubkey: new Account().publicKey,
|
|
|
|
newAuthorizedPubkey: new Account().publicKey,
|
|
|
|
};
|
2020-01-02 17:58:54 -07:00
|
|
|
|
2020-09-10 15:43:32 +08:00
|
|
|
const transaction = new Transaction().add(
|
|
|
|
SystemProgram.nonceAuthorize(params),
|
|
|
|
);
|
2020-03-03 16:05:50 +08:00
|
|
|
expect(transaction.instructions).toHaveLength(1);
|
|
|
|
const [instruction] = transaction.instructions;
|
|
|
|
expect(params).toEqual(SystemInstruction.decodeNonceAuthorize(instruction));
|
2020-01-02 17:58:54 -07:00
|
|
|
});
|
|
|
|
|
2019-09-13 18:07:13 -06:00
|
|
|
test('non-SystemInstruction error', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
|
|
|
|
const badProgramId = {
|
|
|
|
keys: [
|
2019-11-06 10:42:01 -07:00
|
|
|
{pubkey: from.publicKey, isSigner: true, isWritable: true},
|
|
|
|
{pubkey: to.publicKey, isSigner: false, isWritable: true},
|
2019-09-13 18:07:13 -06:00
|
|
|
],
|
2020-08-07 16:01:51 -06:00
|
|
|
programId: StakeProgram.programId,
|
2019-09-13 18:07:13 -06:00
|
|
|
data: Buffer.from([2, 0, 0, 0]),
|
|
|
|
};
|
|
|
|
expect(() => {
|
2020-03-03 16:05:50 +08:00
|
|
|
SystemInstruction.decodeInstructionType(
|
|
|
|
new TransactionInstruction(badProgramId),
|
|
|
|
);
|
2019-09-13 18:07:13 -06:00
|
|
|
}).toThrow();
|
|
|
|
|
2020-08-07 16:01:51 -06:00
|
|
|
const stakePubkey = new Account().publicKey;
|
|
|
|
const authorizedPubkey = new Account().publicKey;
|
|
|
|
const params = {stakePubkey, authorizedPubkey};
|
|
|
|
const transaction = StakeProgram.deactivate(params);
|
2019-09-13 18:07:13 -06:00
|
|
|
|
|
|
|
expect(() => {
|
2020-03-03 16:05:50 +08:00
|
|
|
SystemInstruction.decodeInstructionType(transaction.instructions[1]);
|
2019-09-13 18:07:13 -06:00
|
|
|
}).toThrow();
|
|
|
|
|
2020-01-02 17:58:54 -07:00
|
|
|
transaction.instructions[0].data[0] = 11;
|
2019-09-13 18:07:13 -06:00
|
|
|
expect(() => {
|
2020-03-03 16:05:50 +08:00
|
|
|
SystemInstruction.decodeInstructionType(transaction.instructions[0]);
|
2019-09-13 18:07:13 -06:00
|
|
|
}).toThrow();
|
|
|
|
});
|
2020-01-07 17:57:56 -07:00
|
|
|
|
|
|
|
test('live Nonce actions', async () => {
|
|
|
|
if (mockRpcEnabled) {
|
|
|
|
console.log('non-live test skipped');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-24 10:43:45 -08:00
|
|
|
const connection = new Connection(url, 'singleGossip');
|
2020-01-07 17:57:56 -07:00
|
|
|
const nonceAccount = new Account();
|
2020-12-24 10:43:45 -08:00
|
|
|
const from = await newAccountWithLamports(connection, 2 * LAMPORTS_PER_SOL);
|
2020-01-07 17:57:56 -07:00
|
|
|
const to = new Account();
|
2020-12-24 10:43:45 -08:00
|
|
|
const newAuthority = await newAccountWithLamports(
|
|
|
|
connection,
|
|
|
|
LAMPORTS_PER_SOL,
|
|
|
|
);
|
2020-01-07 17:57:56 -07:00
|
|
|
|
|
|
|
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
|
2020-03-05 09:44:56 -07:00
|
|
|
NONCE_ACCOUNT_LENGTH,
|
2020-01-07 17:57:56 -07:00
|
|
|
);
|
|
|
|
|
2020-09-10 15:43:32 +08:00
|
|
|
let createNonceAccount = new Transaction().add(
|
|
|
|
SystemProgram.createNonceAccount({
|
|
|
|
fromPubkey: from.publicKey,
|
|
|
|
noncePubkey: nonceAccount.publicKey,
|
|
|
|
authorizedPubkey: from.publicKey,
|
|
|
|
lamports: minimumAmount,
|
|
|
|
}),
|
|
|
|
);
|
2020-05-20 17:13:21 +08:00
|
|
|
await sendAndConfirmTransaction(
|
2020-01-07 17:57:56 -07:00
|
|
|
connection,
|
|
|
|
createNonceAccount,
|
2020-05-20 17:13:21 +08:00
|
|
|
[from, nonceAccount],
|
2020-12-24 10:43:45 -08:00
|
|
|
{commitment: 'singleGossip', preflightCommitment: 'singleGossip'},
|
2020-01-07 17:57:56 -07:00
|
|
|
);
|
|
|
|
const nonceBalance = await connection.getBalance(nonceAccount.publicKey);
|
|
|
|
expect(nonceBalance).toEqual(minimumAmount);
|
|
|
|
|
|
|
|
const nonceQuery1 = await connection.getNonce(nonceAccount.publicKey);
|
2020-04-05 16:18:45 +08:00
|
|
|
if (nonceQuery1 === null) {
|
|
|
|
expect(nonceQuery1).not.toBeNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-07 17:57:56 -07:00
|
|
|
const nonceQuery2 = await connection.getNonce(nonceAccount.publicKey);
|
2020-04-05 16:18:45 +08:00
|
|
|
if (nonceQuery2 === null) {
|
|
|
|
expect(nonceQuery2).not.toBeNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-07 17:57:56 -07:00
|
|
|
expect(nonceQuery1.nonce).toEqual(nonceQuery2.nonce);
|
|
|
|
|
2020-03-03 16:05:50 +08:00
|
|
|
// Wait for blockhash to advance
|
2020-01-07 17:57:56 -07:00
|
|
|
await sleep(500);
|
|
|
|
|
|
|
|
const advanceNonce = new Transaction().add(
|
2020-03-03 16:05:50 +08:00
|
|
|
SystemProgram.nonceAdvance({
|
|
|
|
noncePubkey: nonceAccount.publicKey,
|
|
|
|
authorizedPubkey: from.publicKey,
|
|
|
|
}),
|
2020-01-07 17:57:56 -07:00
|
|
|
);
|
2020-06-03 19:55:42 +08:00
|
|
|
await sendAndConfirmTransaction(connection, advanceNonce, [from], {
|
2020-12-24 10:43:45 -08:00
|
|
|
commitment: 'singleGossip',
|
|
|
|
preflightCommitment: 'singleGossip',
|
2020-06-03 19:55:42 +08:00
|
|
|
});
|
2020-01-07 17:57:56 -07:00
|
|
|
const nonceQuery3 = await connection.getNonce(nonceAccount.publicKey);
|
2020-04-05 16:18:45 +08:00
|
|
|
if (nonceQuery3 === null) {
|
|
|
|
expect(nonceQuery3).not.toBeNull();
|
|
|
|
return;
|
|
|
|
}
|
2020-01-07 17:57:56 -07:00
|
|
|
expect(nonceQuery1.nonce).not.toEqual(nonceQuery3.nonce);
|
|
|
|
const nonce = nonceQuery3.nonce;
|
|
|
|
|
2020-03-03 16:05:50 +08:00
|
|
|
// Wait for blockhash to advance
|
2020-01-07 17:57:56 -07:00
|
|
|
await sleep(500);
|
|
|
|
|
2020-03-03 16:05:50 +08:00
|
|
|
const authorizeNonce = new Transaction().add(
|
|
|
|
SystemProgram.nonceAuthorize({
|
|
|
|
noncePubkey: nonceAccount.publicKey,
|
|
|
|
authorizedPubkey: from.publicKey,
|
|
|
|
newAuthorizedPubkey: newAuthority.publicKey,
|
|
|
|
}),
|
2020-01-07 17:57:56 -07:00
|
|
|
);
|
2020-06-03 19:55:42 +08:00
|
|
|
await sendAndConfirmTransaction(connection, authorizeNonce, [from], {
|
2020-12-24 10:43:45 -08:00
|
|
|
commitment: 'singleGossip',
|
|
|
|
preflightCommitment: 'singleGossip',
|
2020-06-03 19:55:42 +08:00
|
|
|
});
|
2020-03-03 16:05:50 +08:00
|
|
|
|
2020-09-10 15:43:32 +08:00
|
|
|
let transfer = new Transaction().add(
|
|
|
|
SystemProgram.transfer({
|
|
|
|
fromPubkey: from.publicKey,
|
|
|
|
toPubkey: to.publicKey,
|
|
|
|
lamports: minimumAmount,
|
|
|
|
}),
|
|
|
|
);
|
2020-01-07 17:57:56 -07:00
|
|
|
transfer.nonceInfo = {
|
|
|
|
nonce,
|
2020-03-03 16:05:50 +08:00
|
|
|
nonceInstruction: SystemProgram.nonceAdvance({
|
|
|
|
noncePubkey: nonceAccount.publicKey,
|
|
|
|
authorizedPubkey: newAuthority.publicKey,
|
|
|
|
}),
|
2020-01-07 17:57:56 -07:00
|
|
|
};
|
|
|
|
|
2020-06-03 19:55:42 +08:00
|
|
|
await sendAndConfirmTransaction(connection, transfer, [from, newAuthority], {
|
2020-12-24 10:43:45 -08:00
|
|
|
commitment: 'singleGossip',
|
|
|
|
preflightCommitment: 'singleGossip',
|
2020-06-03 19:55:42 +08:00
|
|
|
});
|
2020-01-07 17:57:56 -07:00
|
|
|
const toBalance = await connection.getBalance(to.publicKey);
|
|
|
|
expect(toBalance).toEqual(minimumAmount);
|
2020-03-03 16: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 19:55:42 +08:00
|
|
|
await sendAndConfirmTransaction(connection, withdrawNonce, [newAuthority], {
|
2020-12-24 10:43:45 -08:00
|
|
|
commitment: 'singleGossip',
|
|
|
|
preflightCommitment: 'singleGossip',
|
2020-06-03 19:55:42 +08:00
|
|
|
});
|
2020-03-03 16: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 17:57:56 -07:00
|
|
|
});
|
2021-01-14 09:59:31 -07:00
|
|
|
|
|
|
|
test('live withSeed actions', async () => {
|
|
|
|
if (mockRpcEnabled) {
|
|
|
|
console.log('non-live test skipped');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const connection = new Connection(url, 'singleGossip');
|
|
|
|
const baseAccount = await newAccountWithLamports(
|
|
|
|
connection,
|
|
|
|
2 * LAMPORTS_PER_SOL,
|
|
|
|
);
|
|
|
|
const basePubkey = baseAccount.publicKey;
|
|
|
|
const seed = 'hi there';
|
|
|
|
const programId = new Account().publicKey;
|
|
|
|
const createAccountWithSeedAddress = await PublicKey.createWithSeed(
|
|
|
|
basePubkey,
|
|
|
|
seed,
|
|
|
|
programId,
|
|
|
|
);
|
|
|
|
const space = 0;
|
|
|
|
|
|
|
|
const minimumAmount = await connection.getMinimumBalanceForRentExemption(
|
|
|
|
space,
|
|
|
|
);
|
|
|
|
|
2021-01-14 11:26:27 -07:00
|
|
|
// Test CreateAccountWithSeed
|
2021-01-14 09:59:31 -07:00
|
|
|
const createAccountWithSeedParams = {
|
|
|
|
fromPubkey: basePubkey,
|
|
|
|
newAccountPubkey: createAccountWithSeedAddress,
|
|
|
|
basePubkey,
|
|
|
|
seed,
|
|
|
|
lamports: minimumAmount,
|
|
|
|
space,
|
|
|
|
programId,
|
|
|
|
};
|
|
|
|
const createAccountWithSeedTransaction = new Transaction().add(
|
|
|
|
SystemProgram.createAccountWithSeed(createAccountWithSeedParams),
|
|
|
|
);
|
|
|
|
await sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
createAccountWithSeedTransaction,
|
|
|
|
[baseAccount],
|
|
|
|
{commitment: 'singleGossip', preflightCommitment: 'singleGossip'},
|
|
|
|
);
|
|
|
|
const createAccountWithSeedBalance = await connection.getBalance(
|
|
|
|
createAccountWithSeedAddress,
|
|
|
|
);
|
|
|
|
expect(createAccountWithSeedBalance).toEqual(minimumAmount);
|
|
|
|
|
2021-01-14 11:26:27 -07:00
|
|
|
// Transfer to a derived address to prep for TransferWithSeed
|
2021-01-14 09:59:31 -07:00
|
|
|
const programId2 = new Account().publicKey;
|
|
|
|
const transferWithSeedAddress = await PublicKey.createWithSeed(
|
|
|
|
basePubkey,
|
|
|
|
seed,
|
|
|
|
programId2,
|
|
|
|
);
|
|
|
|
await sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
new Transaction().add(
|
|
|
|
SystemProgram.transfer({
|
|
|
|
fromPubkey: baseAccount.publicKey,
|
|
|
|
toPubkey: transferWithSeedAddress,
|
|
|
|
lamports: 3 * minimumAmount,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
[baseAccount],
|
|
|
|
{commitment: 'singleGossip', preflightCommitment: 'singleGossip'},
|
|
|
|
);
|
|
|
|
let transferWithSeedAddressBalance = await connection.getBalance(
|
|
|
|
transferWithSeedAddress,
|
|
|
|
);
|
|
|
|
expect(transferWithSeedAddressBalance).toEqual(3 * minimumAmount);
|
|
|
|
|
|
|
|
// Test TransferWithSeed
|
|
|
|
const programId3 = new Account();
|
|
|
|
const toPubkey = await PublicKey.createWithSeed(
|
|
|
|
basePubkey,
|
|
|
|
seed,
|
|
|
|
programId3.publicKey,
|
|
|
|
);
|
|
|
|
const transferWithSeedParams = {
|
|
|
|
fromPubkey: transferWithSeedAddress,
|
|
|
|
basePubkey,
|
|
|
|
toPubkey,
|
|
|
|
lamports: 2 * minimumAmount,
|
|
|
|
seed,
|
|
|
|
programId: programId2,
|
|
|
|
};
|
|
|
|
const transferWithSeedTransaction = new Transaction().add(
|
|
|
|
SystemProgram.transfer(transferWithSeedParams),
|
|
|
|
);
|
|
|
|
await sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
transferWithSeedTransaction,
|
|
|
|
[baseAccount],
|
|
|
|
{commitment: 'singleGossip', preflightCommitment: 'singleGossip'},
|
|
|
|
);
|
|
|
|
const toBalance = await connection.getBalance(toPubkey);
|
|
|
|
expect(toBalance).toEqual(2 * minimumAmount);
|
|
|
|
transferWithSeedAddressBalance = await connection.getBalance(
|
|
|
|
createAccountWithSeedAddress,
|
|
|
|
);
|
|
|
|
expect(transferWithSeedAddressBalance).toEqual(minimumAmount);
|
|
|
|
|
|
|
|
// Test AllocateWithSeed
|
|
|
|
const allocateWithSeedParams = {
|
|
|
|
accountPubkey: toPubkey,
|
|
|
|
basePubkey,
|
|
|
|
seed,
|
|
|
|
space: 10,
|
|
|
|
programId: programId3.publicKey,
|
|
|
|
};
|
|
|
|
const allocateWithSeedTransaction = new Transaction().add(
|
|
|
|
SystemProgram.allocate(allocateWithSeedParams),
|
|
|
|
);
|
|
|
|
await sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
allocateWithSeedTransaction,
|
|
|
|
[baseAccount],
|
|
|
|
{commitment: 'singleGossip', preflightCommitment: 'singleGossip'},
|
|
|
|
);
|
|
|
|
let account = await connection.getAccountInfo(toPubkey);
|
|
|
|
if (account === null) {
|
|
|
|
expect(account).not.toBeNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
expect(account.data).toHaveLength(10);
|
|
|
|
|
|
|
|
// Test AssignWithSeed
|
|
|
|
const assignWithSeedParams = {
|
|
|
|
accountPubkey: toPubkey,
|
|
|
|
basePubkey,
|
|
|
|
seed,
|
|
|
|
programId: programId3.publicKey,
|
|
|
|
};
|
|
|
|
const assignWithSeedTransaction = new Transaction().add(
|
|
|
|
SystemProgram.assign(assignWithSeedParams),
|
|
|
|
);
|
|
|
|
await sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
assignWithSeedTransaction,
|
|
|
|
[baseAccount],
|
|
|
|
{commitment: 'singleGossip', preflightCommitment: 'singleGossip'},
|
|
|
|
);
|
|
|
|
account = await connection.getAccountInfo(toPubkey);
|
|
|
|
if (account === null) {
|
|
|
|
expect(account).not.toBeNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
expect(account.owner).toEqual(programId3.publicKey);
|
|
|
|
});
|