Skip to content

Commit

Permalink
feat(simulated_transaction): Add Inner Instructions (#2756)
Browse files Browse the repository at this point in the history
* Add Inner Instructions to Simulated Transaction

* Formatting

* Nit: Change 'response' to 'simulation'

* Update Types and Structs

* Start Addressing Feedback

* Stop Overthinking and Reuse Types lol

* Format source code

* Don't widen the type of `parsed` to `any` when parsing the server response, because that makes it optional on the result

---------

Co-authored-by: steveluscher <[email protected]>
  • Loading branch information
0xIchigo and steveluscher authored Jun 29, 2024
1 parent abecbe4 commit 0936673
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/library-legacy/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,8 @@ export type SimulateTransactionConfig = {
};
/** Optional parameter used to specify the minimum block slot that can be used for simulation */
minContextSlot?: number;
/** Optional parameter used to include inner instructions in the simulation */
innerInstructions?: boolean;
};

export type SimulatedTransactionResponse = {
Expand All @@ -960,7 +962,20 @@ export type SimulatedTransactionResponse = {
accounts?: (SimulatedTransactionAccountInfo | null)[] | null;
unitsConsumed?: number;
returnData?: TransactionReturnData | null;
innerInstructions?: ParsedInnerInstruction[] | null;
};
const ParsedInstructionStruct = pick({
program: string(),
programId: PublicKeyFromString,
parsed: unknown(),
});

const PartiallyDecodedInstructionStruct = pick({
programId: PublicKeyFromString,
accounts: array(PublicKeyFromString),
data: string(),
});

const SimulatedTransactionResponseStruct = jsonRpcResultAndContext(
pick({
err: nullable(union([pick({}), string()])),
Expand Down Expand Up @@ -989,6 +1004,21 @@ const SimulatedTransactionResponseStruct = jsonRpcResultAndContext(
}),
),
),
innerInstructions: optional(
nullable(
array(
pick({
index: number(),
instructions: array(
union([
ParsedInstructionStruct,
PartiallyDecodedInstructionStruct,
]),
),
}),
),
),
),
}),
);

Expand Down Expand Up @@ -5712,6 +5742,14 @@ export class Connection {
config.commitment = this.commitment;
}

if (
configOrSigners &&
typeof configOrSigners === 'object' &&
'innerInstructions' in configOrSigners
) {
config.innerInstructions = configOrSigners.innerInstructions;
}

const args = [encodedTransaction, config];
const unsafeRes = await this._rpcRequest('simulateTransaction', args);
const res = create(unsafeRes, SimulatedTransactionResponseStruct);
Expand Down Expand Up @@ -5802,6 +5840,14 @@ export class Connection {
config.sigVerify = true;
}

if (
configOrSigners &&
typeof configOrSigners === 'object' &&
'innerInstructions' in configOrSigners
) {
config.innerInstructions = configOrSigners.innerInstructions;
}

const args = [encodedTransaction, config];
const unsafeRes = await this._rpcRequest('simulateTransaction', args);
const res = create(unsafeRes, SimulatedTransactionResponseStruct);
Expand Down

0 comments on commit 0936673

Please sign in to comment.