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
impl Account
Sourcepub fn new(
account_id: AccountId,
signer: Arc<dyn Signer>,
provider: Arc<dyn Provider>,
) -> Account
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?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let signer_account_id: AccountId = utils::input("Enter the signer Account ID: ")?.parse()?;
let signer_secret_key = utils::input("Enter the signer's private key: ")?.parse()?;
//To-do, implement account exist check.
let new_account_id: AccountId = utils::input("Enter new account name: ")?.parse()?;
let signer = InMemorySigner::from_secret_key(signer_account_id.clone(), signer_secret_key);
// Amount to transfer to the new account
let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR
let amount: Balance = 10_000_000_000_000_000_000_000; // Example amount in yoctoNEAR
let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);
let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
let signer = Arc::new(signer);
let account = Account::new(signer_account_id, signer, provider);
let contract_id: AccountId = "testnet".parse::<AccountId>()?;
let method_name = "create_account".to_string();
let args_json = json!({
"new_account_id": new_account_id,
"new_public_key": new_secret_key.public_key()
});
let result = account
.function_call(contract_id, method_name, args_json, gas, amount)
.await;
println!("response: {:#?}", result);
println!("New Account ID: {}", new_account_id);
println!("Secret Key: {}", new_secret_key);
Ok(())
}
Sourcepub async fn fetch_nonce(
&self,
account_id: &AccountId,
public_key: &PublicKey,
) -> Result<u64, Box<dyn Error>>
pub async fn fetch_nonce( &self, account_id: &AccountId, public_key: &PublicKey, ) -> Result<u64, Box<dyn Error>>
Sourcepub async fn create_account(
&self,
new_account_id: AccountId,
public_key: PublicKey,
amount: u128,
) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>
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/testnetpublic_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
Sourcepub 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>>
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 usecontract_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.
Sourcepub async fn delete_key(
&self,
public_key: PublicKey,
) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>
pub async fn delete_key( &self, public_key: PublicKey, ) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>
Sourcepub async fn deploy_contract(
&self,
byte_code: Vec<u8>,
) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>
pub async fn deploy_contract( &self, byte_code: Vec<u8>, ) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>
Sourcepub async fn delete_account(
&self,
beneficiary_id: AccountId,
) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>
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.
Sourcepub async fn send_money(
&self,
receiver_id: AccountId,
amount: u128,
) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>
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.
Sourcepub async fn function_call(
&self,
contract_id: AccountId,
method_name: String,
args: Value,
gas: u64,
deposit: u128,
) -> Result<FinalExecutionOutcomeView, Box<dyn Error>>
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 JSONValue
.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?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let signer_account_id: AccountId = utils::input("Enter the signer Account ID: ")?.parse()?;
let signer_secret_key = utils::input("Enter the signer's private key: ")?.parse()?;
//To-do, implement account exist check.
let new_account_id: AccountId = utils::input("Enter new account name: ")?.parse()?;
let signer = InMemorySigner::from_secret_key(signer_account_id.clone(), signer_secret_key);
// Amount to transfer to the new account
let gas: Gas = 100_000_000_000_000; // Example amount in yoctoNEAR
let amount: Balance = 10_000_000_000_000_000_000_000; // Example amount in yoctoNEAR
let new_secret_key = near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519);
let provider = Arc::new(JsonRpcProvider::new("https://rpc.testnet.near.org"));
let signer = Arc::new(signer);
let account = Account::new(signer_account_id, signer, provider);
let contract_id: AccountId = "testnet".parse::<AccountId>()?;
let method_name = "create_account".to_string();
let args_json = json!({
"new_account_id": new_account_id,
"new_public_key": new_secret_key.public_key()
});
let result = account
.function_call(contract_id, method_name, args_json, gas, amount)
.await;
println!("response: {:#?}", result);
println!("New Account ID: {}", new_account_id);
println!("Secret Key: {}", new_secret_key);
Ok(())
}
Sourcepub async fn view_function(
&self,
contract_id: AccountId,
method_name: String,
args: Value,
) -> Result<CallResult, Box<dyn Error>>
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.