web3.js: add accounts support to simulateTransaction (#19590)

* feat: add accounts support to simulateTransaction

* feat: introduce test for simulateTransaction on Message objects

* feat: populate transaction from message defaults to no signatures

* fix: remove unused constant

* fix: small formatting error

* fix: eslint and prettier were fighting over ternary indentation

* fix: make simulated transaction result accounts nullable
This commit is contained in:
Josh
2021-09-16 14:10:28 -07:00
committed by GitHub
parent 1a91621c29
commit 49d3d79459
4 changed files with 150 additions and 6 deletions

View File

@ -17,6 +17,7 @@ import {
StakeProgram,
sendAndConfirmTransaction,
Keypair,
Message,
} from '../src';
import invariant from '../src/util/assert';
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from '../src/timing';
@ -2818,6 +2819,65 @@ describe('Connection', () => {
});
if (process.env.TEST_LIVE) {
it('simulate transaction with message', async () => {
connection._commitment = 'confirmed';
const account1 = Keypair.generate();
const account2 = Keypair.generate();
await helpers.airdrop({
connection,
address: account1.publicKey,
amount: LAMPORTS_PER_SOL,
});
await helpers.airdrop({
connection,
address: account2.publicKey,
amount: LAMPORTS_PER_SOL,
});
const recentBlockhash = await (
await helpers.recentBlockhash({connection})
).blockhash;
const message = new Message({
accountKeys: [
account1.publicKey.toString(),
account2.publicKey.toString(),
'Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo',
],
header: {
numReadonlySignedAccounts: 1,
numReadonlyUnsignedAccounts: 2,
numRequiredSignatures: 1,
},
instructions: [
{
accounts: [0, 1],
data: bs58.encode(Buffer.alloc(5).fill(9)),
programIdIndex: 2,
},
],
recentBlockhash,
});
const results1 = await connection.simulateTransaction(
message,
[account1],
true,
);
expect(results1.value.accounts).lengthOf(2);
const results2 = await connection.simulateTransaction(
message,
[account1],
[account1.publicKey],
);
expect(results2.value.accounts).lengthOf(1);
}).timeout(10000);
it('transaction', async () => {
connection._commitment = 'confirmed';