Skip to content

Commit

Permalink
Support for returnData in simulate transaction
Browse files Browse the repository at this point in the history
 * simulate transaction will populate return data
   generated by instruction in returnData field
  • Loading branch information
atharmohammad committed Jul 8, 2022
1 parent b8b5215 commit a283c34
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
14 changes: 14 additions & 0 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,11 +711,17 @@ export type SimulatedTransactionAccountInfo = {
rentEpoch?: number;
};

export type SimulatedTransactionReturnData = {
programId: string;
data: string[];
};

export type SimulatedTransactionResponse = {
err: TransactionError | string | null;
logs: Array<string> | null;
accounts?: (SimulatedTransactionAccountInfo | null)[] | null;
unitsConsumed?: number;
returnData?: SimulatedTransactionReturnData | null;
};

const SimulatedTransactionResponseStruct = jsonRpcResultAndContext(
Expand All @@ -738,6 +744,14 @@ const SimulatedTransactionResponseStruct = jsonRpcResultAndContext(
),
),
unitsConsumed: optional(number()),
returnData: optional(
nullable(
pick({
programId: string(),
data: array(string()),
}),
),
),
}),
);

Expand Down
24 changes: 24 additions & 0 deletions web3.js/test/bpf-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,30 @@ if (process.env.TEST_LIVE) {
);
});

it('simulate transaction with returnData', async () => {
const simulatedTransaction = new Transaction().add({
keys: [
{pubkey: payerAccount.publicKey, isSigner: true, isWritable: true},
],
programId: program.publicKey,
});
const {err, returnData} = (
await connection.simulateTransaction(simulatedTransaction, [
payerAccount,
])
).value;
const expected_return_data = new Uint8Array([1, 2, 3]);
var decoded_data = new Uint8Array(
atob(returnData.data[0])
.split('')
.map(function (c) {
return c.charCodeAt(0);
}),
);
expect(err).to.be.null;
expect(decoded_data).to.eql(expected_return_data);
});

it('deprecated - simulate transaction without signature verification', async () => {
const simulatedTransaction = new Transaction().add({
keys: [
Expand Down
47 changes: 47 additions & 0 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3756,6 +3756,53 @@ describe('Connection', function () {
verifySignatureStatus(response, expectedErr);
});

if (mockServer) {
it('returnData on simulateTransaction', async () => {
const tx = new Transaction();
tx.feePayer = Keypair.generate().publicKey;

const getLatestBlockhashResponse = {
method: 'getLatestBlockhash',
params: [],
value: {
blockhash: 'CSymwgTNX1j3E4qhKfJAUE41nBWEwXufoYryPbkde5RR',
feeCalculator: {
lamportsPerSignature: 5000,
},
lastValidBlockHeight: 51,
},
withContext: true,
};
const simulateTransactionResponse = {
method: 'simulateTransaction',
params: [],
value: {
err: null,
accounts: null,
logs: [
'Program 83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri invoke [1]',
'Program 83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri consumed 2366 of 1400000 compute units',
'Program return: 83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri KgAAAAAAAAA=',
'Program 83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri success',
],
returnData: {
data: ['Kg==', 'base64'],
programId: '83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri',
},
unitsConsumed: 2366,
},
withContext: true,
};
await mockRpcResponse(getLatestBlockhashResponse);
await mockRpcResponse(simulateTransactionResponse);
const response = (await connection.simulateTransaction(tx)).value;
expect(response.returnData).to.eql({
data: ['Kg==', 'base64'],
programId: '83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri',
});
});
}

if (process.env.TEST_LIVE) {
it('simulate transaction with message', async () => {
connection._commitment = 'confirmed';
Expand Down
Binary file modified web3.js/test/fixtures/noop-program/solana_bpf_rust_noop.so
Binary file not shown.
9 changes: 8 additions & 1 deletion web3.js/test/fixtures/noop-program/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Example Rust-based BPF program that prints out the parameters passed to it
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, log::*, msg, pubkey::Pubkey,
account_info::AccountInfo, entrypoint::ProgramResult, log::*, msg, program::set_return_data,
pubkey::Pubkey,
};

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -55,6 +56,12 @@ fn process_instruction(
panic!();
}

{
// return data in simulate transaction
let return_data: &[u8] = &[1, 2, 3];
set_return_data(return_data);
}

Ok(())
}

Expand Down

0 comments on commit a283c34

Please sign in to comment.