1个不稳定版本
0.1.0 | 2022年3月8日 |
---|
#51 in #balance
78KB
1.5K SLoC
near-internal-balances-plugin建立在Near Accounts库之上,允许用户将FTs、NFTs和MTs存入合约并保持余额列表。例如,当alice.near调用实现Near内部余额的合约的ft_transfer_call时,合约将“记住”Alice转发了多少。一个用例示例是在Ref Finance中,用户需要将代币存入DEX智能合约以进行交换等。
使用非常简单,以下是一个基本示例
use near_account::{
impl_near_accounts_plugin, AccountDeposits, AccountInfoTrait, Accounts, NearAccountPlugin,
NearAccountsPluginNonExternal, NewInfo,
};
use near_internal_balances_plugin::impl_near_balance_plugin;
use near_contract_standards::storage_management::StorageManagement;
use near_internal_balances_plugin::token_id::TokenId;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::{LazyOption, UnorderedMap};
use near_sdk::json_types::U128;
use near_sdk::{
assert_one_yocto, env, log, near_bindgen, AccountId, Balance, PanicOnDefault, PromiseOrValue,
};
#[derive(BorshDeserialize, BorshSerialize)]
pub struct AccountInfo {
pub internal_balance: UnorderedMap<TokenId, Balance>,
}
impl NewInfo for AccountInfo {
fn default_from_account_id(account_id: AccountId) -> Self {
Self {
internal_balance: UnorderedMap::new(format!("{}-bal", account_id).as_bytes()),
}
}
}
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Contract {
pub accounts: Accounts<AccountInfo>,
}
impl_near_accounts_plugin!(Contract, accounts, AccountInfo);
impl_near_balance_plugin!(Contract, accounts, AccountInfo, internal_balance);
顺便说一下,由于这是基于Near-Accounts,用户需要向智能合约注册自己以处理存储。有关更多信息,请参阅Near Accounts文档。
依赖项
~4.5MB
~89K SLoC