#account #storage #contract #smart-contracts #near #data #traits

near-account

A library for managing account data and storage on Near Smart Contracts

3 releases

0.1.2 Mar 8, 2022
0.1.1 Mar 8, 2022
0.1.0 Feb 28, 2022

#103 in #near

28 downloads per month
Used in near-internal-balances-pl…

MIT license

29KB
528 lines

near-accounts allows for keeping track of data associated with an account as well as storage management.

Usage is quite simple. Start with the required imports

use near_account::{
    impl_near_accounts_plugin, Account, AccountDeposits, Accounts, NearAccountPlugin,
    NearAccountsPluginNonExternal, NewInfo,
};

After, define a struct for what info the contract should store for each account So, for example, if the contract intends to keep track of a message associated with each user

#[derive(BorshDeserialize, BorshSerialize)]
pub struct AccountInfo {
    pub message: String,
}

Then, the contract must implement the NewInfo trait for AccountInfo, so, for example

  impl NewInfo for AccountInfo {
  fn default_from_account_id(account_id: AccountId) -> Self {
      Self {
          message: "".to_string(),
          internal_balance: UnorderedMap::new(format!("{}-bal", account_id).as_bytes()),
      }
  }
 }

Finally, all that is left to do is define the contract and call the implementing macro

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Contract {
    pub accounts: Accounts<AccountInfo>,
}

impl_near_accounts_plugin!(Contract, accounts, AccountInfo);

For documentation on externally defined methods (publicly callable), please see the NearAccountPlugin trait

For documentation on functions for internal contract use, please see the NearAccountsPluginNonExternal trait and the AccountDeposits trait

Dependencies

~4.5MB
~88K SLoC