solana/docs/src/api/methods/_simulateTransaction.mdx

175 lines
5.4 KiB
Plaintext

import {
DocBlock,
DocSideBySide,
CodeParams,
Parameter,
Field,
Values,
CodeSnippets,
} from "../../../components/CodeDocBlock";
<DocBlock>
## simulateTransaction
Simulate sending a transaction
<DocSideBySide>
<CodeParams>
### Parameters:
<Parameter type={"string"} required={true}>
Transaction, as an encoded string.
:::note
The transaction must have a valid blockhash, but is not required to be signed.
:::
</Parameter>
<Parameter type={"object"} optional={true}>
Configuration object containing the following fields:
<Field
name="commitment"
type="string"
defaultValue="finalized"
optional={true}
href="/api/http#configuring-state-commitment"
>
Commitment level to simulate the transaction at
</Field>
<Field name="sigVerify" type="bool" optional={true} defaultValue={false}>
if `true` the transaction signatures will be verified (conflicts with
`replaceRecentBlockhash`)
</Field>
<Field
name="replaceRecentBlockhash"
type="bool"
optional={true}
defaultValue={false}
>
if `true` the transaction recent blockhash will be replaced with the most
recent blockhash. (conflicts with `sigVerify`)
</Field>
<Field name="minContextSlot" type="number" optional={true}>
the minimum slot that the request can be evaluated at
</Field>
<Field name="encoding" type="string" defaultValue="base58" optional={true}>
Encoding used for the transaction data.
Values: `base58` (_slow_, **DEPRECATED**), or `base64`.
</Field>
<Field name="accounts" type={"object"} optional={true}>
Accounts configuration object containing the following fields:
<Field name="addresses" type="array">
An `array` of accounts to return, as base-58 encoded strings
</Field>
<Field name="encoding" type="string" defaultValue="base64">
encoding for returned Account data
<Values values={["base64", "base58", "base64+zstd", "jsonParsed"]} />
<details>
- `jsonParsed` encoding attempts to use program-specific state
parsers to return more human-readable and explicit account state data.
- If `jsonParsed` is requested but a [parser cannot be found](https://github.com/solana-labs/solana/blob/cfd0a00ae2ba85a6d76757df8b4fa38ed242d185/account-decoder/src/parse_account_data.rs#L98-L100), the field falls
back to `base64` encoding, detectable when the returned `accounts.data` field is type `string`.
</details>
</Field>
</Field>
</Parameter>
### Result:
The result will be an RpcResponse JSON object with `value` set to a JSON object with the following fields:
- `err: <object|string|null>` - Error if transaction failed, null if transaction succeeded. [TransactionError definitions](https://github.com/solana-labs/solana/blob/c0c60386544ec9a9ec7119229f37386d9f070523/sdk/src/transaction/error.rs#L13)
- `logs: <array|null>` - Array of log messages the transaction instructions output during execution, null if simulation failed before the transaction was able to execute (for example due to an invalid blockhash or signature verification failure)
- `accounts: <array|null>` - array of accounts with the same length as the `accounts.addresses` array in the request
- `<null>` - if the account doesn't exist or if `err` is not null
- `<object>` - otherwise, a JSON object containing:
- `lamports: <u64>` - number of lamports assigned to this account, as a u64
- `owner: <string>` - base-58 encoded Pubkey of the program this account has been assigned to
- `data: <[string, encoding]|object>` - data associated with the account, either as encoded binary data or JSON format `{<program>: <state>}` - depending on encoding parameter
- `executable: <bool>` - boolean indicating if the account contains a program \(and is strictly read-only\)
- `rentEpoch: <u64>` - the epoch at which this account will next owe rent, as u64
- `unitsConsumed: <u64|undefined>` - The number of compute budget units consumed during the processing of this transaction
- `returnData: <object|null>` - the most-recent return data generated by an instruction in the transaction, with the following fields:
- `programId: <string>` - the program that generated the return data, as base-58 encoded Pubkey
- `data: <[string, encoding]>` - the return data itself, as base-64 encoded binary data
</CodeParams>
<CodeSnippets>
### Code sample:
```bash
curl http://localhost:8899 -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0",
"id": 1,
"method": "simulateTransaction",
"params": [
"AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDArczbMia1tLmq7zz4DinMNN0pJ1JtLdqIJPUw3YrGCzYAMHBsgN27lcgB6H2WQvFgyZuJYHa46puOQo9yQ8CVQbd9uHXZaGT2cvhRs7reawctIXtX1s3kTqM9YV+/wCp20C7Wj2aiuk5TReAXo+VTVg8QTHjs0UjNMMKCvpzZ+ABAgEBARU=",
{
"encoding":"base64",
}
]
}
'
```
### Response:
```json
{
"jsonrpc": "2.0",
"result": {
"context": {
"slot": 218
},
"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
}
},
"id": 1
}
```
</CodeSnippets>
</DocSideBySide>
</DocBlock>