feat: create instructions instead of transaction from system program (#12156)

This commit is contained in:
Justin Starry
2020-09-10 15:43:32 +08:00
committed by GitHub
parent e1abb64f41
commit 7e1682db7d
9 changed files with 269 additions and 189 deletions

16
web3.js/module.d.ts vendored
View File

@ -868,21 +868,23 @@ declare module '@solana/web3.js' {
export class SystemProgram { export class SystemProgram {
static programId: PublicKey; static programId: PublicKey;
static createAccount(params: CreateAccountParams): Transaction; static createAccount(params: CreateAccountParams): TransactionInstruction;
static createAccountWithSeed( static createAccountWithSeed(
params: CreateAccountWithSeedParams, params: CreateAccountWithSeedParams,
): Transaction; ): TransactionInstruction;
static allocate( static allocate(
params: AllocateParams | AllocateWithSeedParams, params: AllocateParams | AllocateWithSeedParams,
): Transaction; ): TransactionInstruction;
static assign(params: AssignParams | AssignWithSeedParams): Transaction; static assign(
static transfer(params: TransferParams): Transaction; params: AssignParams | AssignWithSeedParams,
): TransactionInstruction;
static transfer(params: TransferParams): TransactionInstruction;
static createNonceAccount( static createNonceAccount(
params: CreateNonceAccountParams | CreateNonceAccountWithSeedParams, params: CreateNonceAccountParams | CreateNonceAccountWithSeedParams,
): Transaction; ): Transaction;
static nonceAdvance(params: AdvanceNonceParams): TransactionInstruction; static nonceAdvance(params: AdvanceNonceParams): TransactionInstruction;
static nonceWithdraw(params: WithdrawNonceParams): Transaction; static nonceWithdraw(params: WithdrawNonceParams): TransactionInstruction;
static nonceAuthorize(params: AuthorizeNonceParams): Transaction; static nonceAuthorize(params: AuthorizeNonceParams): TransactionInstruction;
} }
export type SystemInstructionType = export type SystemInstructionType =

View File

@ -875,21 +875,23 @@ declare module '@solana/web3.js' {
declare export class SystemProgram { declare export class SystemProgram {
static programId: PublicKey; static programId: PublicKey;
static createAccount(params: CreateAccountParams): Transaction; static createAccount(params: CreateAccountParams): TransactionInstruction;
static createAccountWithSeed( static createAccountWithSeed(
params: CreateAccountWithSeedParams, params: CreateAccountWithSeedParams,
): Transaction; ): TransactionInstruction;
static allocate( static allocate(
params: AllocateParams | AllocateWithSeedParams, params: AllocateParams | AllocateWithSeedParams,
): Transaction; ): TransactionInstruction;
static assign(params: AssignParams | AssignWithSeedParams): Transaction; static assign(
static transfer(params: TransferParams): Transaction; params: AssignParams | AssignWithSeedParams,
): TransactionInstruction;
static transfer(params: TransferParams): TransactionInstruction;
static createNonceAccount( static createNonceAccount(
params: CreateNonceAccountParams | CreateNonceAccountWithSeedParams, params: CreateNonceAccountParams | CreateNonceAccountWithSeedParams,
): Transaction; ): Transaction;
static nonceAdvance(params: AdvanceNonceParams): TransactionInstruction; static nonceAdvance(params: AdvanceNonceParams): TransactionInstruction;
static nonceWithdraw(params: WithdrawNonceParams): Transaction; static nonceWithdraw(params: WithdrawNonceParams): TransactionInstruction;
static nonceAuthorize(params: AuthorizeNonceParams): Transaction; static nonceAuthorize(params: AuthorizeNonceParams): TransactionInstruction;
} }
declare export type SystemInstructionType = declare export type SystemInstructionType =

View File

@ -57,13 +57,15 @@ export class Loader {
const balanceNeeded = await connection.getMinimumBalanceForRentExemption( const balanceNeeded = await connection.getMinimumBalanceForRentExemption(
data.length, data.length,
); );
const transaction = SystemProgram.createAccount({ const transaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey, fromPubkey: payer.publicKey,
newAccountPubkey: program.publicKey, newAccountPubkey: program.publicKey,
lamports: balanceNeeded > 0 ? balanceNeeded : 1, lamports: balanceNeeded > 0 ? balanceNeeded : 1,
space: data.length, space: data.length,
programId, programId,
}); }),
);
await sendAndConfirmTransaction( await sendAndConfirmTransaction(
connection, connection,
transaction, transaction,

View File

@ -522,7 +522,9 @@ export class StakeProgram {
static createAccountWithSeed( static createAccountWithSeed(
params: CreateStakeAccountWithSeedParams, params: CreateStakeAccountWithSeedParams,
): Transaction { ): Transaction {
let transaction = SystemProgram.createAccountWithSeed({ const transaction = new Transaction();
transaction.add(
SystemProgram.createAccountWithSeed({
fromPubkey: params.fromPubkey, fromPubkey: params.fromPubkey,
newAccountPubkey: params.stakePubkey, newAccountPubkey: params.stakePubkey,
basePubkey: params.basePubkey, basePubkey: params.basePubkey,
@ -530,7 +532,8 @@ export class StakeProgram {
lamports: params.lamports, lamports: params.lamports,
space: this.space, space: this.space,
programId: this.programId, programId: this.programId,
}); }),
);
const {stakePubkey, authorized, lockup} = params; const {stakePubkey, authorized, lockup} = params;
return transaction.add(this.initialize({stakePubkey, authorized, lockup})); return transaction.add(this.initialize({stakePubkey, authorized, lockup}));
@ -540,13 +543,16 @@ export class StakeProgram {
* Generate a Transaction that creates a new Stake account * Generate a Transaction that creates a new Stake account
*/ */
static createAccount(params: CreateStakeAccountParams): Transaction { static createAccount(params: CreateStakeAccountParams): Transaction {
let transaction = SystemProgram.createAccount({ const transaction = new Transaction();
transaction.add(
SystemProgram.createAccount({
fromPubkey: params.fromPubkey, fromPubkey: params.fromPubkey,
newAccountPubkey: params.stakePubkey, newAccountPubkey: params.stakePubkey,
lamports: params.lamports, lamports: params.lamports,
space: this.space, space: this.space,
programId: this.programId, programId: this.programId,
}); }),
);
const {stakePubkey, authorized, lockup} = params; const {stakePubkey, authorized, lockup} = params;
return transaction.add(this.initialize({stakePubkey, authorized, lockup})); return transaction.add(this.initialize({stakePubkey, authorized, lockup}));
@ -648,13 +654,16 @@ export class StakeProgram {
static split(params: SplitStakeParams): Transaction { static split(params: SplitStakeParams): Transaction {
const {stakePubkey, authorizedPubkey, splitStakePubkey, lamports} = params; const {stakePubkey, authorizedPubkey, splitStakePubkey, lamports} = params;
let transaction = SystemProgram.createAccount({ const transaction = new Transaction();
transaction.add(
SystemProgram.createAccount({
fromPubkey: authorizedPubkey, fromPubkey: authorizedPubkey,
newAccountPubkey: splitStakePubkey, newAccountPubkey: splitStakePubkey,
lamports: 0, lamports: 0,
space: this.space, space: this.space,
programId: this.programId, programId: this.programId,
}); }),
);
const type = STAKE_INSTRUCTION_LAYOUTS.Split; const type = STAKE_INSTRUCTION_LAYOUTS.Split;
const data = encodeData(type, {lamports}); const data = encodeData(type, {lamports});

View File

@ -590,9 +590,9 @@ export class SystemProgram {
} }
/** /**
* Generate a Transaction that creates a new account * Generate a transaction instruction that creates a new account
*/ */
static createAccount(params: CreateAccountParams): Transaction { static createAccount(params: CreateAccountParams): TransactionInstruction {
const type = SYSTEM_INSTRUCTION_LAYOUTS.Create; const type = SYSTEM_INSTRUCTION_LAYOUTS.Create;
const data = encodeData(type, { const data = encodeData(type, {
lamports: params.lamports, lamports: params.lamports,
@ -600,7 +600,7 @@ export class SystemProgram {
programId: params.programId.toBuffer(), programId: params.programId.toBuffer(),
}); });
return new Transaction().add({ return new TransactionInstruction({
keys: [ keys: [
{pubkey: params.fromPubkey, isSigner: true, isWritable: true}, {pubkey: params.fromPubkey, isSigner: true, isWritable: true},
{pubkey: params.newAccountPubkey, isSigner: true, isWritable: true}, {pubkey: params.newAccountPubkey, isSigner: true, isWritable: true},
@ -611,13 +611,13 @@ export class SystemProgram {
} }
/** /**
* Generate a Transaction that transfers lamports from one account to another * Generate a transaction instruction that transfers lamports from one account to another
*/ */
static transfer(params: TransferParams): Transaction { static transfer(params: TransferParams): TransactionInstruction {
const type = SYSTEM_INSTRUCTION_LAYOUTS.Transfer; const type = SYSTEM_INSTRUCTION_LAYOUTS.Transfer;
const data = encodeData(type, {lamports: params.lamports}); const data = encodeData(type, {lamports: params.lamports});
return new Transaction().add({ return new TransactionInstruction({
keys: [ keys: [
{pubkey: params.fromPubkey, isSigner: true, isWritable: true}, {pubkey: params.fromPubkey, isSigner: true, isWritable: true},
{pubkey: params.toPubkey, isSigner: false, isWritable: true}, {pubkey: params.toPubkey, isSigner: false, isWritable: true},
@ -628,9 +628,11 @@ export class SystemProgram {
} }
/** /**
* Generate a Transaction that assigns an account to a program * Generate a transaction instruction that assigns an account to a program
*/ */
static assign(params: AssignParams | AssignWithSeedParams): Transaction { static assign(
params: AssignParams | AssignWithSeedParams,
): TransactionInstruction {
let data; let data;
if (params.basePubkey) { if (params.basePubkey) {
const type = SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed; const type = SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed;
@ -644,7 +646,7 @@ export class SystemProgram {
data = encodeData(type, {programId: params.programId.toBuffer()}); data = encodeData(type, {programId: params.programId.toBuffer()});
} }
return new Transaction().add({ return new TransactionInstruction({
keys: [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}], keys: [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}],
programId: this.programId, programId: this.programId,
data, data,
@ -652,12 +654,12 @@ export class SystemProgram {
} }
/** /**
* Generate a Transaction that creates a new account at * Generate a transaction instruction that creates a new account at
* an address generated with `from`, a seed, and programId * an address generated with `from`, a seed, and programId
*/ */
static createAccountWithSeed( static createAccountWithSeed(
params: CreateAccountWithSeedParams, params: CreateAccountWithSeedParams,
): Transaction { ): TransactionInstruction {
const type = SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed; const type = SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed;
const data = encodeData(type, { const data = encodeData(type, {
base: params.basePubkey.toBuffer(), base: params.basePubkey.toBuffer(),
@ -667,7 +669,7 @@ export class SystemProgram {
programId: params.programId.toBuffer(), programId: params.programId.toBuffer(),
}); });
return new Transaction().add({ return new TransactionInstruction({
keys: [ keys: [
{pubkey: params.fromPubkey, isSigner: true, isWritable: true}, {pubkey: params.fromPubkey, isSigner: true, isWritable: true},
{pubkey: params.newAccountPubkey, isSigner: false, isWritable: true}, {pubkey: params.newAccountPubkey, isSigner: false, isWritable: true},
@ -678,14 +680,15 @@ export class SystemProgram {
} }
/** /**
* Generate a Transaction that creates a new Nonce account * Generate a transaction that creates a new Nonce account
*/ */
static createNonceAccount( static createNonceAccount(
params: CreateNonceAccountParams | CreateNonceAccountWithSeedParams, params: CreateNonceAccountParams | CreateNonceAccountWithSeedParams,
): Transaction { ): Transaction {
let transaction; const transaction = new Transaction();
if (params.basePubkey && params.seed) { if (params.basePubkey && params.seed) {
transaction = SystemProgram.createAccountWithSeed({ transaction.add(
SystemProgram.createAccountWithSeed({
fromPubkey: params.fromPubkey, fromPubkey: params.fromPubkey,
newAccountPubkey: params.noncePubkey, newAccountPubkey: params.noncePubkey,
basePubkey: params.basePubkey, basePubkey: params.basePubkey,
@ -693,15 +696,18 @@ export class SystemProgram {
lamports: params.lamports, lamports: params.lamports,
space: NONCE_ACCOUNT_LENGTH, space: NONCE_ACCOUNT_LENGTH,
programId: this.programId, programId: this.programId,
}); }),
);
} else { } else {
transaction = SystemProgram.createAccount({ transaction.add(
SystemProgram.createAccount({
fromPubkey: params.fromPubkey, fromPubkey: params.fromPubkey,
newAccountPubkey: params.noncePubkey, newAccountPubkey: params.noncePubkey,
lamports: params.lamports, lamports: params.lamports,
space: NONCE_ACCOUNT_LENGTH, space: NONCE_ACCOUNT_LENGTH,
programId: this.programId, programId: this.programId,
}); }),
);
} }
const initParams = { const initParams = {
@ -762,13 +768,13 @@ export class SystemProgram {
} }
/** /**
* Generate a Transaction that withdraws lamports from a Nonce account * Generate a transaction instruction that withdraws lamports from a Nonce account
*/ */
static nonceWithdraw(params: WithdrawNonceParams): Transaction { static nonceWithdraw(params: WithdrawNonceParams): TransactionInstruction {
const type = SYSTEM_INSTRUCTION_LAYOUTS.WithdrawNonceAccount; const type = SYSTEM_INSTRUCTION_LAYOUTS.WithdrawNonceAccount;
const data = encodeData(type, {lamports: params.lamports}); const data = encodeData(type, {lamports: params.lamports});
return new Transaction().add({ return new TransactionInstruction({
keys: [ keys: [
{pubkey: params.noncePubkey, isSigner: false, isWritable: true}, {pubkey: params.noncePubkey, isSigner: false, isWritable: true},
{pubkey: params.toPubkey, isSigner: false, isWritable: true}, {pubkey: params.toPubkey, isSigner: false, isWritable: true},
@ -790,16 +796,16 @@ export class SystemProgram {
} }
/** /**
* Generate a Transaction that authorizes a new PublicKey as the authority * Generate a transaction instruction that authorizes a new PublicKey as the authority
* on a Nonce account. * on a Nonce account.
*/ */
static nonceAuthorize(params: AuthorizeNonceParams): Transaction { static nonceAuthorize(params: AuthorizeNonceParams): TransactionInstruction {
const type = SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount; const type = SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount;
const data = encodeData(type, { const data = encodeData(type, {
authorized: params.newAuthorizedPubkey.toBuffer(), authorized: params.newAuthorizedPubkey.toBuffer(),
}); });
return new Transaction().add({ return new TransactionInstruction({
keys: [ keys: [
{pubkey: params.noncePubkey, isSigner: false, isWritable: true}, {pubkey: params.noncePubkey, isSigner: false, isWritable: true},
{pubkey: params.authorizedPubkey, isSigner: true, isWritable: false}, {pubkey: params.authorizedPubkey, isSigner: true, isWritable: false},
@ -810,11 +816,11 @@ export class SystemProgram {
} }
/** /**
* Generate a Transaction that allocates space in an account without funding * Generate a transaction instruction that allocates space in an account without funding
*/ */
static allocate( static allocate(
params: AllocateParams | AllocateWithSeedParams, params: AllocateParams | AllocateWithSeedParams,
): Transaction { ): TransactionInstruction {
let data; let data;
if (params.basePubkey) { if (params.basePubkey) {
const type = SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed; const type = SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed;
@ -831,7 +837,7 @@ export class SystemProgram {
}); });
} }
return new Transaction().add({ return new TransactionInstruction({
keys: [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}], keys: [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}],
programId: this.programId, programId: this.programId,
data, data,

View File

@ -6,6 +6,7 @@ import {
Account, Account,
Connection, Connection,
SystemProgram, SystemProgram,
Transaction,
sendAndConfirmTransaction, sendAndConfirmTransaction,
LAMPORTS_PER_SOL, LAMPORTS_PER_SOL,
PublicKey, PublicKey,
@ -131,10 +132,12 @@ test('get program accounts', async () => {
}, },
]); ]);
let transaction = SystemProgram.assign({ let transaction = new Transaction().add(
SystemProgram.assign({
accountPubkey: account0.publicKey, accountPubkey: account0.publicKey,
programId: programId.publicKey, programId: programId.publicKey,
}); }),
);
mockConfirmTransaction( mockConfirmTransaction(
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
@ -156,10 +159,12 @@ test('get program accounts', async () => {
}, },
]); ]);
transaction = SystemProgram.assign({ transaction = new Transaction().add(
SystemProgram.assign({
accountPubkey: account1.publicKey, accountPubkey: account1.publicKey,
programId: programId.publicKey, programId: programId.publicKey,
}); }),
);
mockConfirmTransaction( mockConfirmTransaction(
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
@ -1746,13 +1751,15 @@ test('transaction failure', async () => {
]); ]);
const newAccount = new Account(); const newAccount = new Account();
let transaction = SystemProgram.createAccount({ let transaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: account.publicKey, fromPubkey: account.publicKey,
newAccountPubkey: newAccount.publicKey, newAccountPubkey: newAccount.publicKey,
lamports: 1000, lamports: 1000,
space: 0, space: 0,
programId: SystemProgram.programId, programId: SystemProgram.programId,
}); }),
);
mockConfirmTransaction( mockConfirmTransaction(
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk', '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
@ -1777,13 +1784,15 @@ test('transaction failure', async () => {
]); ]);
// This should fail because the account is already created // This should fail because the account is already created
transaction = SystemProgram.createAccount({ transaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: account.publicKey, fromPubkey: account.publicKey,
newAccountPubkey: newAccount.publicKey, newAccountPubkey: newAccount.publicKey,
lamports: 10, lamports: 10,
space: 0, space: 0,
programId: SystemProgram.programId, programId: SystemProgram.programId,
}); }),
);
const signature = await connection.sendTransaction( const signature = await connection.sendTransaction(
transaction, transaction,
[account, newAccount], [account, newAccount],
@ -1957,11 +1966,13 @@ test('transaction', async () => {
}, },
]); ]);
const transaction = SystemProgram.transfer({ const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: accountFrom.publicKey, fromPubkey: accountFrom.publicKey,
toPubkey: accountTo.publicKey, toPubkey: accountTo.publicKey,
lamports: 10, lamports: 10,
}); }),
);
const signature = await connection.sendTransaction( const signature = await connection.sendTransaction(
transaction, transaction,
[accountFrom], [accountFrom],
@ -1988,11 +1999,13 @@ test('transaction', async () => {
// Send again and ensure that new blockhash is used // Send again and ensure that new blockhash is used
const lastFetch = Date.now(); const lastFetch = Date.now();
const transaction2 = SystemProgram.transfer({ const transaction2 = new Transaction().add(
SystemProgram.transfer({
fromPubkey: accountFrom.publicKey, fromPubkey: accountFrom.publicKey,
toPubkey: accountTo.publicKey, toPubkey: accountTo.publicKey,
lamports: 10, lamports: 10,
}); }),
);
const signature2 = await connection.sendTransaction( const signature2 = await connection.sendTransaction(
transaction2, transaction2,
[accountFrom], [accountFrom],
@ -2017,11 +2030,13 @@ test('transaction', async () => {
]); ]);
// Send new transaction and ensure that same blockhash is used // Send new transaction and ensure that same blockhash is used
const transaction3 = SystemProgram.transfer({ const transaction3 = new Transaction().add(
SystemProgram.transfer({
fromPubkey: accountFrom.publicKey, fromPubkey: accountFrom.publicKey,
toPubkey: accountTo.publicKey, toPubkey: accountTo.publicKey,
lamports: 9, lamports: 9,
}); }),
);
const signature3 = await connection.sendTransaction( const signature3 = await connection.sendTransaction(
transaction3, transaction3,
[accountFrom], [accountFrom],
@ -2052,11 +2067,13 @@ test('transaction', async () => {
}, },
]); ]);
const transaction4 = SystemProgram.transfer({ const transaction4 = new Transaction().add(
SystemProgram.transfer({
fromPubkey: accountFrom.publicKey, fromPubkey: accountFrom.publicKey,
toPubkey: accountTo.publicKey, toPubkey: accountTo.publicKey,
lamports: 13, lamports: 13,
}); }),
);
const signature4 = await connection.sendTransaction( const signature4 = await connection.sendTransaction(
transaction4, transaction4,
@ -2150,11 +2167,15 @@ test('multi-instruction transaction', async () => {
// 1. Move(accountFrom, accountTo) // 1. Move(accountFrom, accountTo)
// 2. Move(accountTo, accountFrom) // 2. Move(accountTo, accountFrom)
const transaction = SystemProgram.transfer({ const transaction = new Transaction()
.add(
SystemProgram.transfer({
fromPubkey: accountFrom.publicKey, fromPubkey: accountFrom.publicKey,
toPubkey: accountTo.publicKey, toPubkey: accountTo.publicKey,
lamports: 100, lamports: 100,
}).add( }),
)
.add(
SystemProgram.transfer({ SystemProgram.transfer({
fromPubkey: accountTo.publicKey, fromPubkey: accountTo.publicKey,
toPubkey: accountFrom.publicKey, toPubkey: accountFrom.publicKey,
@ -2213,11 +2234,13 @@ test('account change notification', async () => {
await connection.requestAirdrop(owner.publicKey, LAMPORTS_PER_SOL); await connection.requestAirdrop(owner.publicKey, LAMPORTS_PER_SOL);
try { try {
let transaction = SystemProgram.transfer({ const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: owner.publicKey, fromPubkey: owner.publicKey,
toPubkey: programAccount.publicKey, toPubkey: programAccount.publicKey,
lamports: balanceNeeded, lamports: balanceNeeded,
}); }),
);
await sendAndConfirmTransaction(connection, transaction, [owner], { await sendAndConfirmTransaction(connection, transaction, [owner], {
commitment: 'single', commitment: 'single',
skipPreflight: true, skipPreflight: true,
@ -2280,11 +2303,13 @@ test('program account change notification', async () => {
await connection.requestAirdrop(owner.publicKey, LAMPORTS_PER_SOL); await connection.requestAirdrop(owner.publicKey, LAMPORTS_PER_SOL);
try { try {
let transaction = SystemProgram.transfer({ const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: owner.publicKey, fromPubkey: owner.publicKey,
toPubkey: programAccount.publicKey, toPubkey: programAccount.publicKey,
lamports: balanceNeeded, lamports: balanceNeeded,
}); }),
);
await sendAndConfirmTransaction(connection, transaction, [owner], { await sendAndConfirmTransaction(connection, transaction, [owner], {
commitment: 'single', commitment: 'single',
skipPreflight: true, skipPreflight: true,

View File

@ -2,7 +2,13 @@
import bs58 from 'bs58'; import bs58 from 'bs58';
import {Account, Connection, SystemProgram, PublicKey} from '../src'; import {
Account,
Connection,
SystemProgram,
Transaction,
PublicKey,
} from '../src';
import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account'; import {NONCE_ACCOUNT_LENGTH} from '../src/nonce-account';
import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch'; import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch';
import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash'; import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash';
@ -99,12 +105,14 @@ test('create and query nonce account', async () => {
}, },
]); ]);
const transaction = SystemProgram.createNonceAccount({ const transaction = new Transaction().add(
SystemProgram.createNonceAccount({
fromPubkey: from.publicKey, fromPubkey: from.publicKey,
noncePubkey: nonceAccount.publicKey, noncePubkey: nonceAccount.publicKey,
authorizedPubkey: from.publicKey, authorizedPubkey: from.publicKey,
lamports: minimumAmount, lamports: minimumAmount,
}); }),
);
const nonceSignature = await connection.sendTransaction( const nonceSignature = await connection.sendTransaction(
transaction, transaction,
[from, nonceAccount], [from, nonceAccount],
@ -228,14 +236,16 @@ test('create and query nonce account with seed', async () => {
}, },
]); ]);
const transaction = SystemProgram.createNonceAccount({ const transaction = new Transaction().add(
SystemProgram.createNonceAccount({
fromPubkey: from.publicKey, fromPubkey: from.publicKey,
noncePubkey: noncePubkey, noncePubkey: noncePubkey,
basePubkey: from.publicKey, basePubkey: from.publicKey,
seed, seed,
authorizedPubkey: from.publicKey, authorizedPubkey: from.publicKey,
lamports: minimumAmount, lamports: minimumAmount,
}); }),
);
const nonceSignature = await connection.sendTransaction(transaction, [from], { const nonceSignature = await connection.sendTransaction(transaction, [from], {
skipPreflight: true, skipPreflight: true,
}); });

View File

@ -29,7 +29,9 @@ test('createAccount', () => {
space: 0, space: 0,
programId: SystemProgram.programId, programId: SystemProgram.programId,
}; };
const transaction = SystemProgram.createAccount(params); const transaction = new Transaction().add(
SystemProgram.createAccount(params),
);
expect(transaction.instructions).toHaveLength(1); expect(transaction.instructions).toHaveLength(1);
const [systemInstruction] = transaction.instructions; const [systemInstruction] = transaction.instructions;
expect(params).toEqual( expect(params).toEqual(
@ -43,7 +45,7 @@ test('transfer', () => {
toPubkey: new Account().publicKey, toPubkey: new Account().publicKey,
lamports: 123, lamports: 123,
}; };
const transaction = SystemProgram.transfer(params); const transaction = new Transaction().add(SystemProgram.transfer(params));
expect(transaction.instructions).toHaveLength(1); expect(transaction.instructions).toHaveLength(1);
const [systemInstruction] = transaction.instructions; const [systemInstruction] = transaction.instructions;
expect(params).toEqual(SystemInstruction.decodeTransfer(systemInstruction)); expect(params).toEqual(SystemInstruction.decodeTransfer(systemInstruction));
@ -54,7 +56,7 @@ test('allocate', () => {
accountPubkey: new Account().publicKey, accountPubkey: new Account().publicKey,
space: 42, space: 42,
}; };
const transaction = SystemProgram.allocate(params); const transaction = new Transaction().add(SystemProgram.allocate(params));
expect(transaction.instructions).toHaveLength(1); expect(transaction.instructions).toHaveLength(1);
const [systemInstruction] = transaction.instructions; const [systemInstruction] = transaction.instructions;
expect(params).toEqual(SystemInstruction.decodeAllocate(systemInstruction)); expect(params).toEqual(SystemInstruction.decodeAllocate(systemInstruction));
@ -68,7 +70,7 @@ test('allocateWithSeed', () => {
space: 42, space: 42,
programId: new Account().publicKey, programId: new Account().publicKey,
}; };
const transaction = SystemProgram.allocate(params); const transaction = new Transaction().add(SystemProgram.allocate(params));
expect(transaction.instructions).toHaveLength(1); expect(transaction.instructions).toHaveLength(1);
const [systemInstruction] = transaction.instructions; const [systemInstruction] = transaction.instructions;
expect(params).toEqual( expect(params).toEqual(
@ -81,7 +83,7 @@ test('assign', () => {
accountPubkey: new Account().publicKey, accountPubkey: new Account().publicKey,
programId: new Account().publicKey, programId: new Account().publicKey,
}; };
const transaction = SystemProgram.assign(params); const transaction = new Transaction().add(SystemProgram.assign(params));
expect(transaction.instructions).toHaveLength(1); expect(transaction.instructions).toHaveLength(1);
const [systemInstruction] = transaction.instructions; const [systemInstruction] = transaction.instructions;
expect(params).toEqual(SystemInstruction.decodeAssign(systemInstruction)); expect(params).toEqual(SystemInstruction.decodeAssign(systemInstruction));
@ -94,7 +96,7 @@ test('assignWithSeed', () => {
seed: '你好', seed: '你好',
programId: new Account().publicKey, programId: new Account().publicKey,
}; };
const transaction = SystemProgram.assign(params); const transaction = new Transaction().add(SystemProgram.assign(params));
expect(transaction.instructions).toHaveLength(1); expect(transaction.instructions).toHaveLength(1);
const [systemInstruction] = transaction.instructions; const [systemInstruction] = transaction.instructions;
expect(params).toEqual( expect(params).toEqual(
@ -113,7 +115,9 @@ test('createAccountWithSeed', () => {
space: 0, space: 0,
programId: SystemProgram.programId, programId: SystemProgram.programId,
}; };
const transaction = SystemProgram.createAccountWithSeed(params); const transaction = new Transaction().add(
SystemProgram.createAccountWithSeed(params),
);
expect(transaction.instructions).toHaveLength(1); expect(transaction.instructions).toHaveLength(1);
const [systemInstruction] = transaction.instructions; const [systemInstruction] = transaction.instructions;
expect(params).toEqual( expect(params).toEqual(
@ -130,7 +134,9 @@ test('createNonceAccount', () => {
lamports: 123, lamports: 123,
}; };
const transaction = SystemProgram.createNonceAccount(params); const transaction = new Transaction().add(
SystemProgram.createNonceAccount(params),
);
expect(transaction.instructions).toHaveLength(2); expect(transaction.instructions).toHaveLength(2);
const [createInstruction, initInstruction] = transaction.instructions; const [createInstruction, initInstruction] = transaction.instructions;
@ -165,7 +171,9 @@ test('createNonceAccount with seed', () => {
lamports: 123, lamports: 123,
}; };
const transaction = SystemProgram.createNonceAccount(params); const transaction = new Transaction().add(
SystemProgram.createNonceAccount(params),
);
expect(transaction.instructions).toHaveLength(2); expect(transaction.instructions).toHaveLength(2);
const [createInstruction, initInstruction] = transaction.instructions; const [createInstruction, initInstruction] = transaction.instructions;
@ -207,7 +215,9 @@ test('nonceWithdraw', () => {
toPubkey: new Account().publicKey, toPubkey: new Account().publicKey,
lamports: 123, lamports: 123,
}; };
const transaction = SystemProgram.nonceWithdraw(params); const transaction = new Transaction().add(
SystemProgram.nonceWithdraw(params),
);
expect(transaction.instructions).toHaveLength(1); expect(transaction.instructions).toHaveLength(1);
const [instruction] = transaction.instructions; const [instruction] = transaction.instructions;
expect(params).toEqual(SystemInstruction.decodeNonceWithdraw(instruction)); expect(params).toEqual(SystemInstruction.decodeNonceWithdraw(instruction));
@ -220,7 +230,9 @@ test('nonceAuthorize', () => {
newAuthorizedPubkey: new Account().publicKey, newAuthorizedPubkey: new Account().publicKey,
}; };
const transaction = SystemProgram.nonceAuthorize(params); const transaction = new Transaction().add(
SystemProgram.nonceAuthorize(params),
);
expect(transaction.instructions).toHaveLength(1); expect(transaction.instructions).toHaveLength(1);
const [instruction] = transaction.instructions; const [instruction] = transaction.instructions;
expect(params).toEqual(SystemInstruction.decodeNonceAuthorize(instruction)); expect(params).toEqual(SystemInstruction.decodeNonceAuthorize(instruction));
@ -280,12 +292,14 @@ test('live Nonce actions', async () => {
'recent', 'recent',
); );
let createNonceAccount = SystemProgram.createNonceAccount({ let createNonceAccount = new Transaction().add(
SystemProgram.createNonceAccount({
fromPubkey: from.publicKey, fromPubkey: from.publicKey,
noncePubkey: nonceAccount.publicKey, noncePubkey: nonceAccount.publicKey,
authorizedPubkey: from.publicKey, authorizedPubkey: from.publicKey,
lamports: minimumAmount, lamports: minimumAmount,
}); }),
);
await sendAndConfirmTransaction( await sendAndConfirmTransaction(
connection, connection,
createNonceAccount, createNonceAccount,
@ -345,11 +359,13 @@ test('live Nonce actions', async () => {
skipPreflight: true, skipPreflight: true,
}); });
let transfer = SystemProgram.transfer({ let transfer = new Transaction().add(
SystemProgram.transfer({
fromPubkey: from.publicKey, fromPubkey: from.publicKey,
toPubkey: to.publicKey, toPubkey: to.publicKey,
lamports: minimumAmount, lamports: minimumAmount,
}); }),
);
transfer.nonceInfo = { transfer.nonceInfo = {
nonce, nonce,
nonceInstruction: SystemProgram.nonceAdvance({ nonceInstruction: SystemProgram.nonceAdvance({

View File

@ -1,5 +1,11 @@
// @flow // @flow
import {Account, Connection, SystemProgram, LAMPORTS_PER_SOL} from '../src'; import {
Account,
Connection,
Transaction,
SystemProgram,
LAMPORTS_PER_SOL,
} from '../src';
import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch'; import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch';
import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash'; import {mockGetRecentBlockhash} from './mockrpc/get-recent-blockhash';
import {url} from './url'; import {url} from './url';
@ -88,11 +94,13 @@ test('transaction-payer', async () => {
}, },
]); ]);
const transaction = SystemProgram.transfer({ const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: accountFrom.publicKey, fromPubkey: accountFrom.publicKey,
toPubkey: accountTo.publicKey, toPubkey: accountTo.publicKey,
lamports: 10, lamports: 10,
}); }),
);
const signature = await connection.sendTransaction( const signature = await connection.sendTransaction(
transaction, transaction,