1

I am trying to simulate a transaction on Solana ( I am not sure if this is the right approach to what I want to achieve). I want to be able to determine if a trade would be profitable without actually having to spend any money. I don't want to use the test net, as I want to be able to use actual token account. Currently I am using the Raydium SDK to build and send the swap. Here are my functions:

export async function simulateSendTx(
  connection: Connection,
  payer: Keypair | Signer,
  txs: (VersionedTransaction | Transaction)[],
): Promise<SimulatedTransactionResponse[]> {
 
  const tokenAccounts = await getTokenAccountsByOwner(
   connection,
   wallet.publicKey
  )
  const tokenAccountsArray = tokenAccounts.map((ta) => ta.pubkey.toString())


const config: SimulateTransactionConfig = {
  
  /** Optional parameter used to enable signature verification before simulation */
  sigVerify: true,
  /** Optional parameter used to replace the simulated transaction's recent blockhash with the latest blockhash */
  replaceRecentBlockhash: false,
  /** Optional parameter used to set the commitment level when selecting the latest block */
  commitment: 'processed',
  /** Optional parameter used to specify a list of account addresses to return post simulation state for */
  accounts: {
    encoding: 'base64',
    addresses: tokenAccountsArray,
  },
}

  const simulatedResponses: SimulatedTransactionResponse[] = []

  for (const iTx of txs) {
    if (iTx instanceof VersionedTransaction) {
      iTx.sign([payer])
      const response = await connection.simulateTransaction(iTx,config )
      const simulatedResponse = response.value as SimulatedTransactionResponse
      simulatedResponses.push(simulatedResponse)
    } else {
      //@ts-ignore
      const response = await connection.simulateTransaction(iTx, config )
      const simulatedResponse = response.value as SimulatedTransactionResponse
      simulatedResponses.push(simulatedResponse)
    }
  }

  return simulatedResponses
}

The issue is this : the token accounts always return as NULL.

Transaction Return [
  {
    accounts: [ null ],
    err: null,
    logs: [
      'Program 11111111111111111111111111111111 invoke [1]',
      'Program 11111111111111111111111111111111 success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]',
      'Program log: Instruction: InitializeAccount',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 3443 of 999850 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL invoke [1]',
      'Program log: Create',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: GetAccountDataSize',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1569 of 986540 compute units',
      'Program return: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA pQAAAAAAAAA=',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program 11111111111111111111111111111111 invoke [2]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Initialize the associated token account',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: InitializeImmutableOwner',
      'Program log: Please upgrade to SPL Token 2022 for immutable owner support',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1405 of 979953 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: InitializeAccount3',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4188 of 976071 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL consumed 24807 of 996407 compute units',
      'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL success',
      'Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 invoke [1]',
      'Program log: ray_log: AxAnAAAAAAAAXWcAAAAAAAABAAAAAAAAABAnAAAAAAAA+s6ich4BAAA+UxcdZwAAAD5sAAAAAAAA',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: Transfer',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4736 of 953483 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: Transfer',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4645 of 945766 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 consumed 31282 of 971600 compute units',
      'Program 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]',
      'Program log: Instruction: CloseAccount',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2915 of 940318 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success'
    ],
    returnData: null,
    unitsConsumed: 62597
  }
]

I want to be able to see whether not the trade was profitable. Is this possible on Solana?

1 Answer 1

1

This approach is correct: you simulate the transaction and then check the state of the token accounts afterwards to see how much was gained or lost.

You'll need to specify to include the accounts on your simulation, so be sure to run:

      const response = await connection.simulateTransaction(iTx, config, true)

That last parameter specifies to includeAccounts in the return data.

1
  • Thanks a lot, will try and implement what you have said
    – Ajar
    Commented Feb 8 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.