Skip to content

Commit

Permalink
refactor: Refactored NEAR tokens usages to use a strictly typed near-…
Browse files Browse the repository at this point in the history
…token crate (#253)
  • Loading branch information
FroVolod authored Oct 30, 2023
1 parent 809d628 commit 5962e2b
Show file tree
Hide file tree
Showing 27 changed files with 376 additions and 683 deletions.
464 changes: 227 additions & 237 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ near-jsonrpc-primitives = "0.17.0"
near-gas = { version = "0.2.3", features = [
"serde",
"borsh",
"schemars",
"interactive-clap",
] }
near-token = { version = "0.2.0", features = [
"serde",
"borsh",
"interactive-clap",
] }

keyring = "2.0.5"
interactive-clap = "0.2.7"
interactive-clap-derive = "0.2.7"

near-socialdb-client = "0.1.0"
near-socialdb-client = "0.2.1"

[features]
default = ["ledger", "self-update"]
Expand Down
14 changes: 7 additions & 7 deletions src/commands/account/add_key/access_key_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl From<FullAccessTypeContext> for AccessTypeContext {
pub struct FunctionCallType {
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
allowance: Option<crate::common::NearBalance>,
allowance: Option<near_token::NearToken>,
#[interactive_clap(long)]
/// Enter a receiver to use by this access key to pay for function call gas and transaction fees:
receiver_account_id: crate::types::account_id::AccountId,
Expand All @@ -69,7 +69,7 @@ pub struct FunctionCallType {
pub struct FunctionCallTypeContext {
global_context: crate::GlobalContext,
signer_account_id: near_primitives::types::AccountId,
allowance: Option<crate::common::NearBalance>,
allowance: Option<near_token::NearToken>,
receiver_account_id: crate::types::account_id::AccountId,
method_names: crate::types::vec_string::VecString,
}
Expand All @@ -82,7 +82,7 @@ impl FunctionCallTypeContext {
Ok(Self {
global_context: previous_context.global_context,
signer_account_id: previous_context.owner_account_id.into(),
allowance: scope.allowance.clone(),
allowance: scope.allowance,
receiver_account_id: scope.receiver_account_id.clone(),
method_names: scope.method_names.clone(),
})
Expand All @@ -96,7 +96,7 @@ impl From<FunctionCallTypeContext> for AccessTypeContext {
signer_account_id: item.signer_account_id,
permission: near_primitives::account::AccessKeyPermission::FunctionCall(
near_primitives::account::FunctionCallPermission {
allowance: item.allowance.map(|allowance| allowance.to_yoctonear()),
allowance: item.allowance.map(|allowance| allowance.as_yoctonear()),
receiver_id: item.receiver_account_id.to_string(),
method_names: item.method_names.into(),
},
Expand Down Expand Up @@ -126,7 +126,7 @@ impl interactive_clap::FromCli for FunctionCallType {
Err(err) => return interactive_clap::ResultFromCli::Err(Some(clap_variant), err),
};
}
let allowance = clap_variant.allowance.clone();
let allowance = clap_variant.allowance;
if clap_variant.receiver_account_id.is_none() {
clap_variant.receiver_account_id = match Self::input_receiver_account_id(&context) {
Ok(Some(first_receiver_account_id)) => Some(first_receiver_account_id),
Expand Down Expand Up @@ -217,7 +217,7 @@ impl FunctionCallType {

pub fn input_allowance(
_context: &super::AddKeyCommandContext,
) -> color_eyre::eyre::Result<Option<crate::common::NearBalance>> {
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
eprintln!();
#[derive(strum_macros::Display)]
enum ConfirmOptions {
Expand All @@ -232,7 +232,7 @@ impl FunctionCallType {
)
.prompt()?;
if let ConfirmOptions::Yes = select_choose_input {
let allowance_near_balance: crate::common::NearBalance =
let allowance_near_balance: near_token::NearToken =
CustomType::new("Enter an allowance which is a balance limit to use by this access key to pay for function call gas and transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):")
.prompt()?;
Ok(Some(allowance_near_balance))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct NewAccount {
new_account_id: crate::types::account_id::AccountId,
#[interactive_clap(skip_default_input_arg)]
/// Enter the amount for the account:
initial_balance: crate::common::NearBalance,
initial_balance: near_token::NearToken,
#[interactive_clap(subcommand)]
access_key_mode: add_key::AccessKeyMode,
}
Expand All @@ -25,7 +25,7 @@ pub struct NewAccount {
pub struct NewAccountContext {
global_context: crate::GlobalContext,
new_account_id: near_primitives::types::AccountId,
initial_balance: crate::common::NearBalance,
initial_balance: near_token::NearToken,
}

impl NewAccountContext {
Expand All @@ -36,7 +36,7 @@ impl NewAccountContext {
Ok(Self {
global_context: previous_context,
new_account_id: scope.new_account_id.clone().into(),
initial_balance: scope.initial_balance.clone(),
initial_balance: scope.initial_balance,
})
}
}
Expand Down Expand Up @@ -130,9 +130,9 @@ impl NewAccount {

fn input_initial_balance(
_context: &crate::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::common::NearBalance>> {
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
eprintln!();
match crate::common::NearBalance::from_str(&Text::new("Enter the amount of the NEAR tokens you want to fund the new account with (example: 10NEAR or 0.5near or 10000yoctonear):")
match near_token::NearToken::from_str(&Text::new("Enter the amount of the NEAR tokens you want to fund the new account with (example: 10NEAR or 0.5near or 10000yoctonear):")
.with_initial_value("0.1 NEAR")
.prompt()?
) {
Expand All @@ -156,5 +156,5 @@ pub struct AccountPropertiesContext {
pub struct AccountProperties {
pub new_account_id: near_primitives::types::AccountId,
pub public_key: near_crypto::PublicKey,
pub initial_balance: crate::common::NearBalance,
pub initial_balance: near_token::NearToken,
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl From<SignerAccountIdContext> for crate::commands::ActionContext {
),
near_primitives::transaction::Action::Transfer(
near_primitives::transaction::TransferAction {
deposit: item.account_properties.initial_balance.to_yoctonear(),
deposit: item.account_properties.initial_balance.as_yoctonear(),
},
),
near_primitives::transaction::Action::AddKey(
Expand Down Expand Up @@ -101,7 +101,7 @@ impl From<SignerAccountIdContext> for crate::commands::ActionContext {
deposit: item
.account_properties
.initial_balance
.to_yoctonear(),
.as_yoctonear(),
},
)],
linkdrop_account_id.clone(),
Expand Down
10 changes: 5 additions & 5 deletions src/commands/account/storage_management/storage_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct DepositArgs {
/// Which account ID do you want to add a deposit to?
receiver_account_id: crate::types::account_id::AccountId,
/// Enter the amount to deposit into the storage (example: 10NEAR or 0.5near or 10000yoctonear):
deposit: crate::common::NearBalance,
deposit: near_token::NearToken,
#[interactive_clap(named_arg)]
/// What is the signer account ID?
sign_as: SignerAccountId,
Expand All @@ -19,7 +19,7 @@ pub struct DepositArgsContext {
global_context: crate::GlobalContext,
get_contract_account_id: super::GetContractAccountId,
receiver_account_id: near_primitives::types::AccountId,
deposit: crate::common::NearBalance,
deposit: near_token::NearToken,
}

impl DepositArgsContext {
Expand All @@ -31,7 +31,7 @@ impl DepositArgsContext {
global_context: previous_context.global_context,
get_contract_account_id: previous_context.get_contract_account_id,
receiver_account_id: scope.receiver_account_id.clone().into(),
deposit: scope.deposit.clone(),
deposit: scope.deposit,
})
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@ impl SignerAccountIdContext {
scope.signer_account_id.clone().into();
let receiver_account_id = previous_context.receiver_account_id.clone();
let get_contract_account_id = previous_context.get_contract_account_id.clone();
let deposit = previous_context.deposit.clone();
let deposit = previous_context.deposit;

move |network_config| {
Ok(crate::commands::PrepopulatedTransaction {
Expand All @@ -121,7 +121,7 @@ impl SignerAccountIdContext {
.to_string()
.into_bytes(),
gas: crate::common::NearGas::from_tgas(50).as_gas(),
deposit: deposit.to_yoctonear(),
deposit: deposit.as_yoctonear(),
},
)],
})
Expand Down
13 changes: 6 additions & 7 deletions src/commands/account/storage_management/storage_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[interactive_clap(output_context = WithdrawArgsContext)]
pub struct WithdrawArgs {
/// Enter the amount to withdraw from the storage (example: 10NEAR or 0.5near or 10000yoctonear):
amount: crate::common::NearBalance,
amount: near_token::NearToken,
#[interactive_clap(named_arg)]
/// What is the signer account ID?
sign_as: SignerAccountId,
Expand All @@ -13,7 +13,7 @@ pub struct WithdrawArgs {
pub struct WithdrawArgsContext {
global_context: crate::GlobalContext,
get_contract_account_id: super::GetContractAccountId,
amount: crate::common::NearBalance,
amount: near_token::NearToken,
}

impl WithdrawArgsContext {
Expand All @@ -24,7 +24,7 @@ impl WithdrawArgsContext {
Ok(Self {
global_context: previous_context.global_context,
get_contract_account_id: previous_context.get_contract_account_id,
amount: scope.amount.clone(),
amount: scope.amount,
})
}
}
Expand Down Expand Up @@ -54,7 +54,7 @@ impl SignerAccountIdContext {
let signer_account_id: near_primitives::types::AccountId =
scope.signer_account_id.clone().into();
let get_contract_account_id = previous_context.get_contract_account_id.clone();
let amount = previous_context.amount.clone();
let amount = previous_context.amount;

move |network_config| {
Ok(crate::commands::PrepopulatedTransaction {
Expand All @@ -64,13 +64,12 @@ impl SignerAccountIdContext {
near_primitives::transaction::FunctionCallAction {
method_name: "storage_withdraw".to_string(),
args: serde_json::json!({
"amount": amount.clone().to_yoctonear().to_string()
"amount": amount.clone().as_yoctonear().to_string()
})
.to_string()
.into_bytes(),
gas: crate::common::NearGas::from_tgas(50).as_gas(),
deposit: crate::common::NearBalance::from_yoctonear(1)
.to_yoctonear(),
deposit: near_token::NearToken::from_yoctonear(1).as_yoctonear(),
},
)],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ impl AccountContext {
eprintln!(" {:<13} {:>10} ({} [{:>28} yoctoNEAR])",
"available:",
bytesize::ByteSize(u64::try_from(storage_balance.available / STORAGE_COST_PER_BYTE).unwrap()),
crate::common::NearBalance::from_yoctonear(storage_balance.available),
near_token::NearToken::from_yoctonear(storage_balance.available),
storage_balance.available
);
eprintln!(" {:<13} {:>10} ({} [{:>28} yoctoNEAR])",
"total:",
bytesize::ByteSize(u64::try_from(storage_balance.total / STORAGE_COST_PER_BYTE).unwrap()),
crate::common::NearBalance::from_yoctonear(storage_balance.total),
near_token::NearToken::from_yoctonear(storage_balance.total),
storage_balance.total
);

Expand Down
6 changes: 3 additions & 3 deletions src/commands/account/update_social_profile/sign_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl From<SignerContext> for crate::commands::ActionContext {
method_name: "set".to_string(),
args,
gas: crate::common::NearGas::from_tgas(300).as_gas(),
deposit: deposit.to_yoctonear(),
deposit: deposit.as_yoctonear(),
},
)];

Expand All @@ -139,9 +139,9 @@ impl From<SignerContext> for crate::commands::ActionContext {
&account_id,
"profile",
&prepopulated_unsigned_transaction.receiver_id,
crate::common::NearBalance::from_yoctonear(action.deposit),
near_token::NearToken::from_yoctonear(action.deposit),
))?
.to_yoctonear();
.as_yoctonear();
Ok(())
} else {
color_eyre::eyre::bail!("Unexpected action to change components",);
Expand Down
14 changes: 7 additions & 7 deletions src/commands/contract/call_function/as_transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl PrepaidGas {
pub struct Deposit {
#[interactive_clap(skip_default_input_arg)]
/// Enter deposit for a function call:
deposit: crate::common::NearBalance,
deposit: near_token::NearToken,
#[interactive_clap(named_arg)]
/// What is the signer account ID?
sign_as: SignerAccountId,
Expand All @@ -145,7 +145,7 @@ pub struct DepositContext {
function_name: String,
function_args: Vec<u8>,
gas: crate::common::NearGas,
deposit: crate::common::NearBalance,
deposit: near_token::NearToken,
}

impl DepositContext {
Expand All @@ -159,17 +159,17 @@ impl DepositContext {
function_name: previous_context.function_name,
function_args: previous_context.function_args,
gas: previous_context.gas,
deposit: scope.deposit.clone(),
deposit: scope.deposit,
})
}
}

impl Deposit {
fn input_deposit(
_context: &PrepaidGasContext,
) -> color_eyre::eyre::Result<Option<crate::common::NearBalance>> {
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
eprintln!();
match crate::common::NearBalance::from_str(
match near_token::NearToken::from_str(
&Text::new(
"Enter deposit for a function call (example: 10NEAR or 0.5near or 10000yoctonear):",
)
Expand Down Expand Up @@ -201,7 +201,7 @@ pub struct SignerAccountIdContext {
function_name: String,
function_args: Vec<u8>,
gas: crate::common::NearGas,
deposit: crate::common::NearBalance,
deposit: near_token::NearToken,
signer_account_id: near_primitives::types::AccountId,
}

Expand Down Expand Up @@ -238,7 +238,7 @@ impl From<SignerAccountIdContext> for crate::commands::ActionContext {
method_name: item.function_name.clone(),
args: item.function_args.clone(),
gas: item.gas.as_gas(),
deposit: item.deposit.to_yoctonear(),
deposit: item.deposit.as_yoctonear(),
},
)],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl PrepaidGas {
pub struct Deposit {
#[interactive_clap(skip_default_input_arg)]
/// Enter deposit for a function call:
deposit: crate::common::NearBalance,
deposit: near_token::NearToken,
#[interactive_clap(named_arg)]
/// Select network
network_config: crate::network_for_transaction::NetworkForTransactionArgs,
Expand All @@ -146,7 +146,7 @@ impl DepositContext {
previous_context: PrepaidGasContext,
scope: &<Deposit as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
let deposit = scope.deposit.clone();
let deposit = scope.deposit;

let on_after_getting_network_callback: crate::commands::OnAfterGettingNetworkCallback =
std::sync::Arc::new({
Expand All @@ -168,7 +168,7 @@ impl DepositContext {
method_name: previous_context.function_name.clone(),
args: previous_context.function_args.clone(),
gas: previous_context.gas.as_gas(),
deposit: deposit.to_yoctonear(),
deposit: deposit.as_yoctonear(),
},
),
],
Expand Down Expand Up @@ -205,9 +205,9 @@ impl From<DepositContext> for crate::commands::ActionContext {
impl Deposit {
fn input_deposit(
_context: &PrepaidGasContext,
) -> color_eyre::eyre::Result<Option<crate::common::NearBalance>> {
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
eprintln!();
match crate::common::NearBalance::from_str(
match near_token::NearToken::from_str(
&Text::new(
"Enter deposit for a function call (example: 10NEAR or 0.5near or 10000yoctonear):",
)
Expand Down
Loading

0 comments on commit 5962e2b

Please sign in to comment.