solana_client::nonce_utils

Function data_from_account

source
pub fn data_from_account<T>(account: &T) -> Result<Data, Error>
Expand description

Deserialize the state data of a durable transaction nonce account.

§Errors

Returns an error if the account is not owned by the system program or contains no data. Returns an error if the account state is uninitialized or fails to deserialize.

§Examples

Create and sign a transaction with a durable nonce:

use solana_rpc_client_nonce_utils::nonblocking;
use solana_rpc_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::{
    message::Message,
    pubkey::Pubkey,
    signature::{Keypair, Signer},
    system_instruction,
    transaction::Transaction,
};
use std::path::Path;
use anyhow::Result;

futures::executor::block_on(async {
async fn create_transfer_tx_with_nonce(
    client: &RpcClient,
    nonce_account_pubkey: &Pubkey,
    payer: &Keypair,
    receiver: &Pubkey,
    amount: u64,
    tx_path: &Path,
) -> Result<()> {

    let instr_transfer = system_instruction::transfer(
        &payer.pubkey(),
        receiver,
        amount,
    );

    // In this example, `payer` is `nonce_account_pubkey`'s authority
    let instr_advance_nonce_account = system_instruction::advance_nonce_account(
        nonce_account_pubkey,
        &payer.pubkey(),
    );

    // The `advance_nonce_account` instruction must be the first issued in
    // the transaction.
    let message = Message::new(
        &[
            instr_advance_nonce_account,
            instr_transfer
        ],
        Some(&payer.pubkey()),
    );

    let mut tx = Transaction::new_unsigned(message);

    // Sign the tx with nonce_account's `blockhash` instead of the
    // network's latest blockhash.
    let nonce_account = client.get_account(nonce_account_pubkey).await?;
    let nonce_data = nonblocking::data_from_account(&nonce_account)?;
    let blockhash = nonce_data.blockhash();

    tx.try_sign(&[payer], blockhash)?;

    // Save the signed transaction locally for later submission.
    save_tx_to_file(&tx_path, &tx)?;

    Ok(())
}