#near #balance #account #plugin #nft #transfer #user

near-internal-balances-plugin

A library for keeping track of the amount of FTs, MTs, and NFTs deposited into a contract

1 unstable release

0.1.0 Mar 8, 2022

#48 in #balance

MIT license

78KB
1.5K SLoC

near-internal-balances-plugin builds on top of the Near Accounts library Allows for users to deposit FTs, NFTs, and MTs into the contract and keep a list of balance. I.e. say alice.near calls ft_transfer_call to a contract implementing Near internal balances, the contract will "remember" how much Alice transfers. An example use case would be with Ref Finance where the users are required to deposit tokens into the DEX smart contract in order to swap etc.

Usage is quite simple, here is a basic example

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);

As an aside, because this builds off of Near-Accounts, users need to register themselves with the smart contract in order to deal with storage. Please see The Near Accounts Documentation for more information.

Dependencies

~5.5MB
~103K SLoC