Struct Account

Source
pub struct Account {
    pub account_id: AccountId,
    pub signer: Arc<dyn Signer>,
    pub provider: Arc<dyn Provider>,
}
Expand description

Represents a NEAR account, encapsulating account ID, signer, and provider for blockchain interaction.

Fields§

§account_id: AccountId§signer: Arc<dyn Signer>§provider: Arc<dyn Provider>

Implementations§

Source§

impl Account

Source

pub fn new( account_id: AccountId, signer: Arc<dyn Signer>, provider: Arc<dyn Provider>, ) -> Account

Constructs a new Account instance.

§Arguments
  • account_id - The unique account identifier on the NEAR blockchain.
  • signer - A signer instance for signing transactions.
  • provider - A provider instance for interacting with the blockchain.
§Returns

A new Account instance.

Examples found in repository?
examples/create_account.rs (line 30)
12async fn main() -> Result<(), Box<dyn std::error::Error>> {
13    env_logger::init();
14
15    let signer_account_id: AccountId = utils::input("Enter the signer Account ID: ")?.parse()?;
16    let signer_secret_key = utils::input("Enter the signer's private key: ")?.parse()?;
17    //To-do, implement account exist check.
18    let new_account_id: AccountId = utils::input("Enter new account name: ")?.parse()?;
19
20    let signer = InMemorySigner::from_secret_key(signer_account_id.clone(), signer_secret_key);
21
22    // Amount to transfer to the new account
23    let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR
24    let amount: Balance = 10_000_000_000_000_000_000_000; // Example amount in yoctoNEAR
25
26    let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);
27    let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
28    let signer = Arc::new(signer);
29
30    let account = Account::new(signer_account_id, signer, provider);
31
32    let contract_id: AccountId = "testnet".parse::<AccountId>()?;
33    let method_name = "create_account".to_string();
34
35    let args_json = json!({
36        "new_account_id": new_account_id,
37        "new_public_key": new_secret_key.public_key()
38    });
39
40    let result = account
41        .function_call(contract_id, method_name, args_json, gas, amount)
42        .await;
43
44    println!("response: {:#?}", result);
45    println!("New Account ID: {}", new_account_id);
46    println!("Secret Key: {}", new_secret_key);
47
48    Ok(())
49}
Source

pub async fn fetch_nonce( &self, account_id: &AccountId, public_key: &PublicKey, ) -> Result<u64, Box<dyn Error>>

Fetches the current nonce for an account’s access key.

§Arguments
  • account_id - The account ID for which to fetch the nonce.
  • public_key - The public key of the access key.
§Returns

A result containing the nonce or an error if the query failed.

Source

pub async fn create_account( &self, new_account_id: AccountId, public_key: PublicKey, amount: u128, ) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>

Creates a sub account for the signer account id.

§Arguments
  • new_account_id - The new account ID you want to create. As it will be a sub account of the signer, your new_account_id should be of the form *.signer_account_id.near/testnet
  • public_key - The public key for the new account.
  • amount - Initial balance of the new account
§Returns

A final execution outcome of the transaction.

§Note: The accounts created by this function will be of the form *.signer_account_id.near/testnet
Source

pub async fn add_key( &self, public_key: PublicKey, allowance: Option<u128>, contract_id: Option<String>, method_names: Option<Vec<String>>, ) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>

Adds a full or function call access key to an account

§Arguments
  • public_key - The new access key you want to add.
  • allowance - The allowance this new key can use
  • contract_id - Incase of function call access key, define the contract the key has access to.
  • method_names - Incase of function call access key, which define names of methods which the key will have access to. Passing an empty array [] gives you access to call functions.
§Returns

A final execution outcome of the transaction.

Source

pub async fn delete_key( &self, public_key: PublicKey, ) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>

Delete a key from an account

§Arguments
  • public_key - The access key you want to delete.
§Returns

A final execution outcome of the transaction.

Source

pub async fn deploy_contract( &self, byte_code: Vec<u8>, ) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>

Deploys a contract to the account associated with this Account instance.

§Arguments
  • byte_code - The compiled smart contract code as a vector of bytes.
§Returns

A Result containing the final execution outcome of the contract deployment or an error if the operation fails.

Source

pub async fn delete_account( &self, beneficiary_id: AccountId, ) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>

Deletes the specified account and transfers any remaining tokens to the beneficiary account.

§Arguments
  • beneficiary_id - The account ID to which the remaining balance will be transferred.
§Returns

A Result containing the final execution outcome of the account deletion or an error if the operation fails.

Source

pub async fn send_money( &self, receiver_id: AccountId, amount: u128, ) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>

Transfers a specified amount of NEAR tokens from this account to another account.

§Arguments
  • receiver_id - The account ID of the recipient.
  • amount - The amount of NEAR tokens to transfer, in yoctoNEAR.
§Returns

A Result containing the final execution outcome of the transfer or an error if the operation fails.

Source

pub async fn function_call( &self, contract_id: AccountId, method_name: String, args: Value, gas: u64, deposit: u128, ) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>

Calls a function on a smart contract deployed on the NEAR blockchain.

§Arguments
  • contract_id - The account ID of the contract.
  • method_name - The name of the function to call.
  • args - The arguments to the function call, serialized into a JSON Value.
  • gas - The amount of gas to attach to the call.
  • deposit - The amount of NEAR tokens to transfer to the contract, in yoctoNEAR.
§Returns

A Result containing the final execution outcome of the function call or an error if the operation fails.

Examples found in repository?
examples/create_account.rs (line 41)
12async fn main() -> Result<(), Box<dyn std::error::Error>> {
13    env_logger::init();
14
15    let signer_account_id: AccountId = utils::input("Enter the signer Account ID: ")?.parse()?;
16    let signer_secret_key = utils::input("Enter the signer's private key: ")?.parse()?;
17    //To-do, implement account exist check.
18    let new_account_id: AccountId = utils::input("Enter new account name: ")?.parse()?;
19
20    let signer = InMemorySigner::from_secret_key(signer_account_id.clone(), signer_secret_key);
21
22    // Amount to transfer to the new account
23    let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR
24    let amount: Balance = 10_000_000_000_000_000_000_000; // Example amount in yoctoNEAR
25
26    let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);
27    let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
28    let signer = Arc::new(signer);
29
30    let account = Account::new(signer_account_id, signer, provider);
31
32    let contract_id: AccountId = "testnet".parse::<AccountId>()?;
33    let method_name = "create_account".to_string();
34
35    let args_json = json!({
36        "new_account_id": new_account_id,
37        "new_public_key": new_secret_key.public_key()
38    });
39
40    let result = account
41        .function_call(contract_id, method_name, args_json, gas, amount)
42        .await;
43
44    println!("response: {:#?}", result);
45    println!("New Account ID: {}", new_account_id);
46    println!("Secret Key: {}", new_secret_key);
47
48    Ok(())
49}
Source

pub async fn view_function( &self, contract_id: AccountId, method_name: String, args: Value, ) -> Result<CallResult, Box<dyn Error>>

Calls a view function on a contract deployed on the NEAR blockchain.

View functions are read-only and do not modify state. They’re free to call.

§Arguments
  • contract_id - The account ID of the contract.
  • method_name - The name of the view function to call.
  • args - The arguments to the function call, typically in the form of a serialized byte array (FunctionArgs).
§Returns

A Result containing the result of the function call or an error if the operation fails.

Auto Trait Implementations§

§

impl Freeze for Account

§

impl !RefUnwindSafe for Account

§

impl !Send for Account

§

impl !Sync for Account

§

impl Unpin for Account

§

impl !UnwindSafe for Account

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,