account.rs

Overview

The account.rs file defines the Account struct and its associated asynchronous and synchronous methods to manage blockchain account representations, primarily focusing on smart contract interaction. It facilitates loading contract ABI files, optionally handling contract TVC (compiled contract code) files, calculating contract addresses, encoding and running local contract calls, and updating contract images with cryptographic keys. This file integrates with external modules and libraries to perform operations related to contract deployment and interaction.

Account Struct

The Account struct models a blockchain account with the following fields:

Display Trait Implementation

Implements std::fmt::Display for Account to provide a formatted string representation displaying the address, DApp ID, and keys while omitting ABI details for brevity.

Account { address: <address>, dapp_id: <dapp_id>, keys: <keys>, abi: <Abi> }

Account Methods

try_new

pub async fn try_new(
    abi_path: &str,
    tvc_path: Option<&str>,
    keys: Option<KeyPair>,
    address: Option<&str>,
) -> anyhow::Result<Self>

Creates a new Account instance by loading the ABI from a file and optionally the TVC from a file. If the TVC is provided, it updates the TVC with the keys and calculates the account address from the TVC. If no TVC is given, it requires an explicit address string.

try_new_with_abi

pub async fn try_new_with_abi(
    abi: Abi,
    tvc_path: Option<&str>,
    keys: Option<KeyPair>,
    address: Option<&str>,
) -> anyhow::Result<Self>

Similar to try_new, but receives an Abi instance directly instead of loading from a file.

address

pub fn address(&self) -> String

Returns the account's address as a String.

run_local

pub async fn run_local(
    &self,
    context: &Arc<ClientContext>,
    method: &str,
    params: Option<serde_json::Value>,
    boc: Option<String>,
) -> anyhow::Result<Value>

Executes a local call on the account's smart contract method without sending a transaction on-chain. It uses the TON Virtual Machine (TVM) to simulate the method call and returns the decoded output.

calc_address

pub async fn calc_address(tvc: &[u8]) -> anyhow::Result<String>

Calculates the blockchain address from the TVC contract binary.

update_tvc

pub fn update_tvc(tvc_bytes: &mut [u8], abi: Abi, keys: Option<KeyPair>) -> anyhow::Result<()>

Updates the TVC binary by injecting the public key and ABI data into the contract image's initialization data.

insert_pubkey_to_init_data

pub fn insert_pubkey_to_init_data(
    pubkey: &str,
    opt_init_data: Option<&str>,
) -> anyhow::Result<String>

Inserts the public key into the JSON-formatted initialization data for the contract.

load_abi

pub fn load_abi(path: &str) -> anyhow::Result<Abi>

Loads an ABI JSON file from the filesystem and returns an Abi object.

Important Implementation Details

System Interaction

Visual Diagram

classDiagram
class Account {
-address: String
-dapp_id: Option<String>
-keys: Option<KeyPair>
-abi: Abi
-_tvc: Option<Vec<u8>>
+try_new()
+try_new_with_abi()
+address()
+run_local()
+calc_address()
+update_tvc()
+insert_pubkey_to_init_data()
+load_abi()
}
Account ..> Abi : uses
Account ..> KeyPair : uses
Account ..> ClientContext : requires
Account ..> ContractImage : manipulates
Account ..> serde_json::Value : processes
Account ..> "tvm_client::net" : queries collection
Account ..> "tvm_client::tvm" : runs TVM