pub struct StorageDeposit(Contract);Expand description
A wrapper struct that simplifies interactions with the Storage Management standard
Contracts on NEAR Protocol often implement a NEP-145 for managing storage deposits, which are required for storing data on the blockchain. This struct provides convenient methods to interact with these storage-related functions on the contract.
§Example
use near_api::*;
let storage = StorageDeposit::on_contract("contract.testnet".parse()?);
// Check storage balance
let balance = storage.view_account_storage("alice.testnet".parse()?)?.fetch_from_testnet().await?;
println!("Storage balance: {:?}", balance);
// Bob pays for Alice's storage on the contract contract.testnet
let deposit_tx = storage.deposit("alice.testnet".parse()?, NearToken::from_near(1))
.with_signer("bob.testnet".parse()?, Signer::new(Signer::from_ledger())?)?
.send_to_testnet()
.await
.unwrap();Tuple Fields§
§0: ContractImplementations§
Source§impl StorageDeposit
impl StorageDeposit
pub const fn on_contract(contract_id: AccountId) -> Self
Sourcepub const fn contract_id(&self) -> &AccountId
pub const fn contract_id(&self) -> &AccountId
Returns the underlying contract account ID for this storage deposit wrapper.
§Example
use near_api::*;
let storage = StorageDeposit::on_contract("contract.testnet".parse()?);
let contract_id = storage.contract_id();
println!("Contract ID: {}", contract_id);Sourcepub fn as_contract(&self) -> Contract
pub fn as_contract(&self) -> Contract
Converts this storage deposit wrapper to a Contract for other contract operations.
§Example
use near_api::*;
let storage = StorageDeposit::on_contract("usdt.tether-token.near".parse()?);
let contract = storage.as_contract();
// Now you can call other contract methods
let metadata: serde_json::Value = contract.call_function("ft_metadata", ())?.read_only().fetch_from_mainnet().await?.data;
println!("Token metadata: {:?}", metadata);Sourcepub fn view_account_storage(
&self,
account_id: AccountId,
) -> Result<RequestBuilder<PostprocessHandler<Data<Option<StorageBalance>>, CallResultHandler<Option<StorageBalanceInternal>>>>, BuilderError>
pub fn view_account_storage( &self, account_id: AccountId, ) -> Result<RequestBuilder<PostprocessHandler<Data<Option<StorageBalance>>, CallResultHandler<Option<StorageBalanceInternal>>>>, BuilderError>
Prepares a new contract query (storage_balance_of) for fetching the storage balance (Option<StorageBalance>) of the account on the contract.
§Example
use near_api::*;
let balance = StorageDeposit::on_contract("contract.testnet".parse()?)
.view_account_storage("alice.testnet".parse()?)?
.fetch_from_testnet()
.await?;
println!("Storage balance: {:?}", balance);Sourcepub fn deposit(
&self,
receiver_account_id: AccountId,
amount: NearToken,
) -> StorageDepositBuilder
pub fn deposit( &self, receiver_account_id: AccountId, amount: NearToken, ) -> StorageDepositBuilder
Prepares a new transaction contract call (storage_deposit) for depositing storage on the contract.
Returns a StorageDepositBuilder that allows configuring the deposit behavior
with registration_only().
§Example
use near_api::*;
// Basic deposit for another account
let tx = StorageDeposit::on_contract("contract.testnet".parse()?)
.deposit("alice.testnet".parse()?, NearToken::from_near(1))
.with_signer("bob.testnet".parse()?, Signer::new(Signer::from_ledger())?)?
.send_to_testnet()
.await?;
// Registration-only deposit (refunds excess above minimum)
let tx = StorageDeposit::on_contract("contract.testnet".parse()?)
.deposit("alice.testnet".parse()?, NearToken::from_near(1))
.registration_only()
.with_signer("bob.testnet".parse()?, Signer::new(Signer::from_ledger())?)?
.send_to_testnet()
.await?;Sourcepub fn withdraw(
&self,
account_id: AccountId,
amount: NearToken,
) -> Result<ConstructTransaction, BuilderError>
pub fn withdraw( &self, account_id: AccountId, amount: NearToken, ) -> Result<ConstructTransaction, BuilderError>
Prepares a new transaction contract call (storage_withdraw) for withdrawing storage from the contract.
§Example
use near_api::*;
let tx = StorageDeposit::on_contract("contract.testnet".parse()?)
.withdraw("alice.testnet".parse()?, NearToken::from_near(1))?
.with_signer( Signer::new(Signer::from_ledger())?)
.send_to_testnet()
.await?;Sourcepub fn unregister(&self) -> StorageUnregisterBuilder
pub fn unregister(&self) -> StorageUnregisterBuilder
Prepares a new transaction contract call (storage_unregister) for unregistering
the predecessor account and returning the storage NEAR deposit.
If the predecessor account is not registered, the function returns false without panic.
By default, the contract will panic if the caller has existing account data (such as
a positive token balance). Use force() to ignore
existing account data and force unregistering (which may burn token balances).
Note: Requires exactly 1 yoctoNEAR attached for security purposes.
§Example
use near_api::*;
// Normal unregister (fails if account has data like token balance)
let tx = StorageDeposit::on_contract("contract.testnet".parse()?)
.unregister()
.with_signer("alice.testnet".parse()?, Signer::new(Signer::from_ledger())?)?
.send_to_testnet()
.await?;
// Force unregister (burns any remaining token balance)
let tx = StorageDeposit::on_contract("contract.testnet".parse()?)
.unregister()
.force()
.with_signer("alice.testnet".parse()?, Signer::new(Signer::from_ledger())?)?
.send_to_testnet()
.await?;Trait Implementations§
Source§impl Clone for StorageDeposit
impl Clone for StorageDeposit
Source§fn clone(&self) -> StorageDeposit
fn clone(&self) -> StorageDeposit
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more