Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add required_balance_for_deploy_token #138

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions near/nep141-locker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const MINT_TOKEN_GAS: Gas = Gas::from_tgas(5);
const SET_METADATA_GAS: Gas = Gas::from_tgas(10);
const NO_DEPOSIT: NearToken = NearToken::from_near(0);
const ONE_YOCTO: NearToken = NearToken::from_yoctonear(1);
const BRIDGE_TOKEN_INIT_BALANCE: NearToken = NearToken::from_near(3);
const SIGN_PATH: &str = "bridge-1";

#[derive(BorshSerialize, BorshStorageKey)]
Expand Down Expand Up @@ -703,7 +702,7 @@ impl Contract {
require!(self.deployed_tokens.insert(&token_id), "ERR_TOKEN_EXIST");
let required_deposit = env::storage_byte_cost()
.saturating_mul((env::storage_usage().saturating_sub(storage_usage)).into())
.saturating_add(BRIDGE_TOKEN_INIT_BALANCE);
.saturating_add(storage::BRIDGE_TOKEN_INIT_BALANCE);

require!(
attached_deposit >= required_deposit,
Expand All @@ -712,7 +711,7 @@ impl Contract {

ext_deployer::ext(deployer)
.with_static_gas(DEPLOY_TOKEN_GAS)
.with_attached_deposit(BRIDGE_TOKEN_INIT_BALANCE)
.with_attached_deposit(storage::BRIDGE_TOKEN_INIT_BALANCE)
.deploy_token(token_id, metadata)
}

Expand Down
34 changes: 27 additions & 7 deletions near/nep141-locker/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::{
OmniAddress, Promise, SdkExpect, Serialize, TransferMessage, U128,
};

pub const BRIDGE_TOKEN_INIT_BALANCE: NearToken = NearToken::from_near(3);

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Debug, Clone)]
pub struct TransferMessageStorageValue {
pub message: TransferMessage,
Expand Down Expand Up @@ -119,7 +121,7 @@ impl Contract {
}

pub fn required_balance_for_account(&self) -> NearToken {
let key_len = 64 + 4;
let key_len = Self::max_key_len_of_account_id();
let value_len = borsh::to_vec(&StorageBalance {
total: NearToken::from_yoctonear(0),
available: NearToken::from_yoctonear(0),
Expand Down Expand Up @@ -161,29 +163,47 @@ impl Contract {
.sdk_expect("ERR_BORSH")
.len() as u64;

env::storage_byte_cost().saturating_mul((Self::get_basic_storage() + key_len).into())
let storage_cost =
env::storage_byte_cost().saturating_mul((Self::get_basic_storage() + key_len).into());
let ft_transfers_cost = NearToken::from_yoctonear(2);

storage_cost.saturating_add(ft_transfers_cost)
}

pub fn required_balance_for_bind_token(&self) -> NearToken {
let max_token_id: AccountId = "a".repeat(64).parse().sdk_expect("ERR_PARSE_ACCOUNT_ID");

let key_len = borsh::to_vec(&(ChainKind::Near, &max_token_id))
.sdk_expect("ERR_BORSH")
.len() as u64
* 2;
.len() as u64;

let value_len = borsh::to_vec(&OmniAddress::Near(max_token_id))
.sdk_expect("ERR_BORSH")
.len() as u64
* 2;
.len() as u64;

env::storage_byte_cost()
.saturating_mul((Self::get_basic_storage() + key_len + value_len).into())
.saturating_mul((2 * (Self::get_basic_storage() + key_len + value_len)).into())
}

pub fn required_balance_for_deploy_token(&self) -> NearToken {
let key_len = Self::max_key_len_of_account_id();
let deployed_tokens_required_balance =
env::storage_byte_cost().saturating_mul((Self::get_basic_storage() + key_len).into());
let bind_token_required_balance = self.required_balance_for_bind_token();

bind_token_required_balance
.saturating_add(deployed_tokens_required_balance)
.saturating_add(BRIDGE_TOKEN_INIT_BALANCE)
}

fn get_basic_storage() -> u64 {
const EXTRA_BYTES_RECORD: u64 = 40;
const EXTRA_KEY_PREFIX_LEN: u64 = 1;
EXTRA_BYTES_RECORD + EXTRA_KEY_PREFIX_LEN
}

fn max_key_len_of_account_id() -> u64 {
let max_account_id: AccountId = "a".repeat(64).parse().sdk_expect("ERR_PARSE_ACCOUNT_ID");
borsh::to_vec(&max_account_id).sdk_expect("ERR_BORSH").len() as u64
}
}
Loading