Skip to main content

Transactions

Pay attention

It is the developer's responsibility to only execute transactions as often as is necessary. Typically, this means after user input such as a click or keypress. DO NOT send transactions every frame of the game loop.

For any Solana transaction, you will need the following:

  1. Pubkey of the program you are interacting with
  2. List of AccountMeta for accounts involved in the transaction
  3. Data for Instructions you are including in the transaction

sign_and_send_transaction

Signs a transaction and sends it to the configured RPC endpoint.

turbo::solana::rpc
pub fn sign_and_send_transaction(tx: &Transaction) -> bool

Using Turbo's Anchor Module

This approach is preferred when interacting with an Anchor program.

First, ensure the required dependencies are in scope:

use turbo::solana::{
self,
anchor::Program,
solana_sdk::{
instruction::AccountMeta,
pubkey::Pubkey
}
};

...then, somewhere in your game loop:

// 1. Create the Transaction
let program_id: Pubkey = ...;
let instruction_name = "name_of_anchor_instruction";
let accounts: Vec<AccountMeta> = vec![...];
let args = my_anchor_program::instructions::DoSomething { ... };
let tx = Program::new(program_id)
.instruction(instruction_name)
.accounts(accounts)
.args(args)
.transaction();

// 2. Send the transaction
let did_send = solana::rpc::sign_and_send_transaction(&tx);
note

If you are not directly importing an Anchor program crate, you will have to manually construct instruction args.

Using Solana SDK

This approach is preferred when interacting with a non-Anchor program. It relies on the Solana SDK which Turbo re-exports.

First, ensure the required dependencies are in scope:

Dependencies
use turbo::solana::{
self,
solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
transaction::Transaction
};
};

...then, somewhere in your game loop:

Usage
// 1. Create the Transaction
let program_id: Pubkey = ...;
let accounts: Vec<AccountMeta> = vec![...];
let data = vec![...];
let instruction = Instruction {
program_id,
accounts,
data
};
let tx = Transaction::new_with_payer(&[instruction], None);

// 2. Send the transaction
let did_send = solana::rpc::sign_and_send_transaction(&tx);