Skip to content

Commit

Permalink
feat: add a macro for creating contract call
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss committed Jul 6, 2023
1 parent b74d37f commit e9dbfc5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 70 deletions.
101 changes: 56 additions & 45 deletions src/cli/simple/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ use crate::{

pub mod silo;

#[macro_export]
macro_rules! contract_call {
($method:expr, $success_msg:expr, $error_msg:expr) => {
ContractCall {
method: $method,
success_message: &format!($success_msg),
error_message: &format!($error_msg),
}
};
}

/// Return `chain_id` of the current network.
pub async fn get_chain_id(client: Client) -> anyhow::Result<()> {
get_value::<U256>(client, "get_chain_id", None).await
Expand Down Expand Up @@ -309,22 +320,22 @@ pub async fn call(
pub async fn stage_upgrade<P: AsRef<Path> + Send>(client: Client, path: P) -> anyhow::Result<()> {
let code = std::fs::read(path)?;

ContractCall {
method: "stage_upgrade",
success_message: "The code has been saved for staged upgrade successfully",
error_message: "Error while staging code for upgrade",
}
contract_call!(
"stage_upgrade",
"The code has been saved for staged upgrade successfully",
"Error while staging code for upgrade"
)
.proceed(client, code)
.await
}

/// Deploy staged upgrade.
pub async fn deploy_upgrade(client: Client) -> anyhow::Result<()> {
ContractCall {
method: "deploy_upgrade",
success_message: "The upgrade has been applied successfully",
error_message: "Error while deploying upgrade",
}
contract_call!(
"deploy_upgrade",
"The upgrade has been applied successfully",
"Error while deploying upgrade"
)
.proceed(client, vec![])
.await
}
Expand All @@ -333,11 +344,11 @@ pub async fn deploy_upgrade(client: Client) -> anyhow::Result<()> {
pub async fn factory_update(client: Client, path: String) -> anyhow::Result<()> {
let code = std::fs::read(path)?;

ContractCall {
method: "factory_update",
success_message: "The bytecode of user's router contract has been updated successfully",
error_message: "Error while updating the bytecode of user's router contract",
}
contract_call!(
"factory_update",
"The bytecode of user's router contract has been updated successfully",
"Error while updating the bytecode of user's router contract"
)
.proceed(client, code)
.await
}
Expand All @@ -346,11 +357,11 @@ pub async fn factory_update(client: Client, path: String) -> anyhow::Result<()>
pub async fn factory_set_wnear_address(client: Client, address: String) -> anyhow::Result<()> {
let args: [u8; 20] = hex_to_arr(&address)?;

ContractCall {
method: "factory_set_wnear_address",
success_message: "The wnear address has been set successfully",
error_message: "Error while upgrading wnear address",
}
contract_call!(
"factory_set_wnear_address",
"The wnear address has been set successfully",
"Error while upgrading wnear address"
)
.proceed(client, args.to_vec())
.await
}
Expand All @@ -377,11 +388,11 @@ pub async fn fund_xcc_sub_account(
}
.try_to_vec()?;

ContractCall {
method: "fund_xcc_sub_account",
success_message: "The XCC sub-account has been funded successfully",
error_message: "Error while funding XCC sub-account",
}
contract_call!(
"fund_xcc_sub_account",
"The XCC sub-account has been funded successfully",
"Error while funding XCC sub-account"
)
.proceed_with_deposit(client, args, deposit)
.await
}
Expand All @@ -393,11 +404,11 @@ pub async fn set_owner(client: Client, account_id: String) -> anyhow::Result<()>
}
.try_to_vec()?;

ContractCall {
method: "set_owner",
success_message: "The owner has been changed successfully",
error_message: "Error while setting a new owner",
}
contract_call!(
"set_owner",
"The owner has been changed successfully",
"Error while setting a new owner"
)
.proceed(client, args)
.await
}
Expand All @@ -406,11 +417,11 @@ pub async fn set_owner(client: Client, account_id: String) -> anyhow::Result<()>
pub async fn register_relayer(client: Client, address: String) -> anyhow::Result<()> {
let args = hex_to_vec(&address)?;

ContractCall {
method: "register_relayer",
success_message: "The new relayer has been registered successfully",
error_message: "Error while registering a new relayer",
}
contract_call!(
"register_relayer",
"The new relayer has been registered successfully",
"Error while registering a new relayer"
)
.proceed(client, args)
.await
}
Expand Down Expand Up @@ -452,11 +463,11 @@ pub fn key_pair(random: bool, seed: Option<u64>) -> anyhow::Result<()> {
pub async fn pause_precompiles(client: Client, mask: u32) -> anyhow::Result<()> {
let args = PausePrecompilesCallArgs { paused_mask: mask }.try_to_vec()?;

ContractCall {
method: "pause_precompiles",
success_message: "The precompiles have been paused successfully",
error_message: "Error while pausing precompiles",
}
contract_call!(
"pause_precompiles",
"The precompiles have been paused successfully",
"Error while pausing precompiles"
)
.proceed(client, args)
.await
}
Expand All @@ -465,11 +476,11 @@ pub async fn pause_precompiles(client: Client, mask: u32) -> anyhow::Result<()>
pub async fn resume_precompiles(client: Client, mask: u32) -> anyhow::Result<()> {
let args = PausePrecompilesCallArgs { paused_mask: mask }.try_to_vec()?;

ContractCall {
method: "resume_precompiles",
success_message: "The precompiles have been resumed successfully",
error_message: "Error while resuming precompiles",
}
contract_call!(
"resume_precompiles",
"The precompiles have been resumed successfully",
"Error while resuming precompiles"
)
.proceed(client, args)
.await
}
Expand Down
52 changes: 27 additions & 25 deletions src/cli/simple/command/silo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::fmt::{Display, Formatter};
use super::{get_value, ContractCall};
use crate::cli::command::FromCallResult;
use crate::client::Client;
use crate::contract_call;
use crate::utils::hex_to_address;

/// Return fixed gas cost.
Expand All @@ -24,11 +25,11 @@ pub async fn set_fixed_gas_cost(client: Client, cost: u128) -> anyhow::Result<()
}
.try_to_vec()?;

ContractCall {
method: "set_fixed_gas_cost",
success_message: "Fixed gas cost was set successfully",
error_message: "Error while setting gas cost",
}
contract_call!(
"set_fixed_gas_cost",
"The fixed gas cost: {cost} has been set successfully",
"Error while setting gas cost"
)
.proceed(client, args)
.await
}
Expand All @@ -50,12 +51,13 @@ pub async fn set_whitelist_status(client: Client, kind: String, status: u8) -> a
active: status > 0,
}
.try_to_vec()?;
let str_status = if status == 0 { "disabled" } else { "enabled" };

ContractCall {
method: "set_whitelist_status",
success_message: "Set whitelist status successfully",
error_message: "Error while setting whitelist status",
}
contract_call!(
"set_whitelist_status",
"The whitelist has been {str_status} successfully",
"Error while setting whitelist status"
)
.proceed(client, args)
.await
}
Expand All @@ -68,11 +70,11 @@ pub async fn add_entry_to_whitelist(
) -> anyhow::Result<()> {
let args = get_whitelist_args(&kind, &entry)?;

ContractCall {
method: "add_entry_to_whitelist",
success_message: "Added entry to whitelist successfully",
error_message: "Error while adding entry to whitelist",
}
contract_call!(
"add_entry_to_whitelist",
"The entry: {entry} has been added to the whitelist successfully",
"Error while adding entry to whitelist"
)
.proceed(client, args)
.await
}
Expand All @@ -83,11 +85,11 @@ pub async fn add_entry_to_whitelist_batch(client: Client, path: String) -> anyho
.and_then(|string| serde_json::from_str::<Vec<WhitelistArgs>>(&string).map_err(Into::into))
.and_then(|entries| entries.try_to_vec())?;

ContractCall {
method: "add_entry_to_whitelist_batch",
success_message: "Added batch entry to whitelist successfully",
error_message: "Error while setting batch entry to whitelist",
}
contract_call!(
"add_entry_to_whitelist_batch",
"The batch of entries has been added to the whitelist successfully",
"Error while setting batch entry to whitelist"
)
.proceed(client, args)
.await
}
Expand All @@ -100,11 +102,11 @@ pub async fn remove_entry_from_whitelist(
) -> anyhow::Result<()> {
let args = get_whitelist_args(&kind, &entry)?;

ContractCall {
method: "remove_entry_from_whitelist",
success_message: "Removed entry to whitelist successfully",
error_message: "Error while removing entry to whitelist",
}
contract_call!(
"remove_entry_from_whitelist",
"The entry: {entry} has been removed from the whitelist successfully",
"Error while removing entry to whitelist"
)
.proceed(client, args)
.await
}
Expand Down

0 comments on commit e9dbfc5

Please sign in to comment.