Expand description

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.

Re-exports

pub use token_id::TokenId;

Modules

Macros

Structs

Traits

Implemented by the macro and is used to get balances

The following methods get implemented by the contract struct and are exposed as external or internal methods

NearInternalBalance gets implemented onto the contract struct with the addition of the impl_near_balance_plugin! macro

The following methods get implemented by the contract struct but are not exposed as external, callable methods.