Simplified the API by removing the generics in favour of explicit Coin type

This commit is contained in:
Jędrzej Stuczyński
2022-05-30 16:09:37 +01:00
parent 766c31544e
commit de1df74eb5
3 changed files with 107 additions and 146 deletions
@@ -8,7 +8,7 @@ use crate::nymd::cosmwasm_client::types::*;
use crate::nymd::error::NymdError;
use crate::nymd::fee::{Fee, DEFAULT_SIMULATED_GAS_MULTIPLIER};
use crate::nymd::wallet::DirectSecp256k1HdWallet;
use crate::nymd::{GasPrice, TxResponse};
use crate::nymd::{Coin, GasPrice, TxResponse};
use async_trait::async_trait;
use cosmrs::bank::MsgSend;
use cosmrs::distribution::MsgWithdrawDelegatorReward;
@@ -294,38 +294,17 @@ pub trait SigningCosmWasmClient: CosmWasmClient {
})
}
// a helper to automatically infer required types so that you wouldn't need the turbofish
// for normal `execute` if you're not sending any funds with your transaction
async fn fundless_execute<M>(
async fn execute<M>(
&self,
sender_address: &AccountId,
contract_address: &AccountId,
msg: &M,
fee: Fee,
memo: impl Into<String> + Send + 'static,
funds: Vec<Coin>,
) -> Result<ExecuteResult, NymdError>
where
M: ?Sized + Serialize + Sync,
{
self.execute::<_, CosmosCoin, _>(sender_address, contract_address, msg, fee, memo, vec![])
.await
}
// the memo had to be extracted into an explicit generic due to: https://github.com/rust-lang/rust/issues/83701
async fn execute<M, T, S>(
&self,
sender_address: &AccountId,
contract_address: &AccountId,
msg: &M,
fee: Fee,
memo: S,
funds: Vec<T>,
) -> Result<ExecuteResult, NymdError>
where
M: ?Sized + Serialize + Sync,
// this allows you to use both CosmosCoin and Coin
T: Into<CosmosCoin> + Send,
S: Into<String> + Send + 'static,
{
let execute_msg = cosmwasm::MsgExecuteContract {
sender: sender_address.clone(),
@@ -351,7 +330,7 @@ pub trait SigningCosmWasmClient: CosmWasmClient {
})
}
async fn execute_multiple<I, M, T>(
async fn execute_multiple<I, M>(
&self,
sender_address: &AccountId,
contract_address: &AccountId,
@@ -360,9 +339,7 @@ pub trait SigningCosmWasmClient: CosmWasmClient {
memo: impl Into<String> + Send + 'static,
) -> Result<ExecuteResult, NymdError>
where
// this allows you to use both CosmosCoin and Coin
T: Into<CosmosCoin> + Send,
I: IntoIterator<Item = (M, Vec<T>)> + Send,
I: IntoIterator<Item = (M, Vec<Coin>)> + Send,
M: Serialize,
{
let messages = msgs
@@ -394,18 +371,14 @@ pub trait SigningCosmWasmClient: CosmWasmClient {
})
}
async fn send_tokens<T>(
async fn send_tokens(
&self,
sender_address: &AccountId,
recipient_address: &AccountId,
amount: Vec<T>,
amount: Vec<Coin>,
fee: Fee,
memo: impl Into<String> + Send + 'static,
) -> Result<TxResponse, NymdError>
where
// this allows you to use both CosmosCoin and Coin
T: Into<CosmosCoin> + Send,
{
) -> Result<TxResponse, NymdError> {
let send_msg = MsgSend {
from_address: sender_address.clone(),
to_address: recipient_address.clone(),
@@ -419,7 +392,7 @@ pub trait SigningCosmWasmClient: CosmWasmClient {
.check_response()
}
async fn send_tokens_multiple<I, T>(
async fn send_tokens_multiple<I>(
&self,
sender_address: &AccountId,
msgs: I,
@@ -427,9 +400,7 @@ pub trait SigningCosmWasmClient: CosmWasmClient {
memo: impl Into<String> + Send + 'static,
) -> Result<TxResponse, NymdError>
where
// this allows you to use both CosmosCoin and Coin
T: Into<CosmosCoin> + Send,
I: IntoIterator<Item = (AccountId, Vec<T>)> + Send,
I: IntoIterator<Item = (AccountId, Vec<Coin>)> + Send,
{
let messages = msgs
.into_iter()
@@ -654,17 +654,15 @@ impl<C> NymdClient<C> {
}
/// Send funds from one address to another
pub async fn send<T>(
pub async fn send(
&self,
recipient: &AccountId,
amount: Vec<T>,
amount: Vec<Coin>,
memo: impl Into<String> + Send + 'static,
fee: Option<Fee>,
) -> Result<TxResponse, NymdError>
where
C: SigningCosmWasmClient + Sync,
// this allows you to use both CosmosCoin and Coin
T: Into<CosmosCoin> + Send,
{
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
self.client
@@ -673,16 +671,14 @@ impl<C> NymdClient<C> {
}
/// Send funds from one address to multiple others
pub async fn send_multiple<T>(
pub async fn send_multiple(
&self,
msgs: Vec<(AccountId, Vec<T>)>,
msgs: Vec<(AccountId, Vec<Coin>)>,
memo: impl Into<String> + Send + 'static,
fee: Option<Fee>,
) -> Result<TxResponse, NymdError>
where
C: SigningCosmWasmClient + Sync,
// this allows you to use both CosmosCoin and Coin
T: Into<CosmosCoin> + Send,
{
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
self.client
@@ -690,18 +686,16 @@ impl<C> NymdClient<C> {
.await
}
pub async fn execute<M, T>(
pub async fn execute<M>(
&self,
contract_address: &AccountId,
msg: &M,
fee: Fee,
memo: impl Into<String> + Send + 'static,
funds: Vec<T>,
funds: Vec<Coin>,
) -> Result<ExecuteResult, NymdError>
where
C: SigningCosmWasmClient + Sync,
// this allows you to use both CosmosCoin and Coin
T: Into<CosmosCoin> + Send,
M: ?Sized + Serialize + Sync,
{
self.client
@@ -709,7 +703,7 @@ impl<C> NymdClient<C> {
.await
}
pub async fn execute_multiple<I, M, T>(
pub async fn execute_multiple<I, M>(
&self,
contract_address: &AccountId,
msgs: I,
@@ -718,9 +712,7 @@ impl<C> NymdClient<C> {
) -> Result<ExecuteResult, NymdError>
where
C: SigningCosmWasmClient + Sync,
// this allows you to use both CosmosCoin and Coin
T: Into<CosmosCoin> + Send,
I: IntoIterator<Item = (M, Vec<CosmosCoin>)> + Send,
I: IntoIterator<Item = (M, Vec<Coin>)> + Send,
M: Serialize,
{
self.client
@@ -916,12 +908,13 @@ impl<C> NymdClient<C> {
let req = ExecuteMsg::UnbondMixnode {};
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Unbonding mixnode from rust!",
vec![],
)
.await
}
@@ -939,12 +932,13 @@ impl<C> NymdClient<C> {
let req = ExecuteMsg::UnbondMixnodeOnBehalf { owner };
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Unbonding mixnode on behalf from rust!",
vec![],
)
.await
}
@@ -964,12 +958,13 @@ impl<C> NymdClient<C> {
profit_margin_percent,
};
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Updating mixnode configuration from rust!",
vec![],
)
.await
}
@@ -1081,12 +1076,13 @@ impl<C> NymdClient<C> {
mix_identity: mix_identity.to_string(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Removing mixnode delegation from rust!",
vec![],
)
.await
}
@@ -1108,12 +1104,13 @@ impl<C> NymdClient<C> {
delegate: delegate.to_string(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Removing mixnode delegation on behalf from rust!",
vec![],
)
.await
}
@@ -1223,12 +1220,13 @@ impl<C> NymdClient<C> {
let req = ExecuteMsg::UnbondGateway {};
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Unbonding gateway from rust!",
vec![],
)
.await
}
@@ -1247,12 +1245,13 @@ impl<C> NymdClient<C> {
let req = ExecuteMsg::UnbondGatewayOnBehalf { owner };
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Unbonding gateway on behalf from rust!",
vec![],
)
.await
}
@@ -1269,12 +1268,13 @@ impl<C> NymdClient<C> {
let req = ExecuteMsg::UpdateContractStateParams(new_params);
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Updating contract state from rust!",
vec![],
)
.await
}
@@ -1287,12 +1287,13 @@ impl<C> NymdClient<C> {
let req = ExecuteMsg::AdvanceCurrentEpoch {};
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Advance current epoch",
vec![],
)
.await
}
@@ -1305,12 +1306,13 @@ impl<C> NymdClient<C> {
let req = ExecuteMsg::ReconcileDelegations {};
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Reconciling delegation events",
vec![],
)
.await
}
@@ -1323,12 +1325,13 @@ impl<C> NymdClient<C> {
let req = ExecuteMsg::CheckpointMixnodes {};
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Snapshotting mixnodes",
vec![],
)
.await
}
@@ -1349,12 +1352,13 @@ impl<C> NymdClient<C> {
expected_active_set_size,
};
self.client
.fundless_execute(
.execute(
self.address(),
self.mixnet_contract_address()?,
&req,
fee,
"Writing rewarded set",
vec![],
)
.await
}
@@ -4,10 +4,8 @@
pub use crate::nymd::cosmwasm_client::signing_client::SigningCosmWasmClient;
use crate::nymd::cosmwasm_client::types::ExecuteResult;
use crate::nymd::error::NymdError;
use crate::nymd::{Fee, NymdClient};
use crate::nymd::{Coin, Fee, NymdClient};
use async_trait::async_trait;
use cosmrs::Coin as CosmosCoin;
use cosmwasm_std::Coin as CosmWasmCoin;
use mixnet_contract_common::{Gateway, IdentityKey, IdentityKeyRef, MixNode};
use vesting_contract_common::messages::{ExecuteMsg as VestingExecuteMsg, VestingSpecification};
@@ -25,57 +23,57 @@ pub trait VestingSigningClient {
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
async fn vesting_bond_gateway<T: Into<CosmWasmCoin> + Send>(
async fn vesting_bond_gateway(
&self,
gateway: Gateway,
owner_signature: &str,
pledge: T,
pledge: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
async fn vesting_unbond_gateway(&self, fee: Option<Fee>) -> Result<ExecuteResult, NymdError>;
async fn vesting_track_unbond_gateway<T: Into<CosmWasmCoin> + Send>(
async fn vesting_track_unbond_gateway(
&self,
owner: &str,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
async fn vesting_bond_mixnode<T: Into<CosmWasmCoin> + Send>(
async fn vesting_bond_mixnode(
&self,
mix_node: MixNode,
owner_signature: &str,
pledge: T,
pledge: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
async fn vesting_unbond_mixnode(&self, fee: Option<Fee>) -> Result<ExecuteResult, NymdError>;
async fn vesting_track_unbond_mixnode<T: Into<CosmWasmCoin> + Send>(
async fn vesting_track_unbond_mixnode(
&self,
owner: &str,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
async fn withdraw_vested_coins<T: Into<CosmWasmCoin> + Send>(
async fn withdraw_vested_coins(
&self,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
async fn vesting_track_undelegation<T: Into<CosmWasmCoin> + Send>(
async fn vesting_track_undelegation(
&self,
address: &str,
mix_identity: IdentityKey,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
async fn vesting_delegate_to_mixnode<'a, T: Into<CosmWasmCoin> + Send>(
async fn vesting_delegate_to_mixnode<'a>(
&self,
mix_identity: IdentityKeyRef<'a>,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
@@ -85,12 +83,12 @@ pub trait VestingSigningClient {
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
async fn create_periodic_vesting_account<T: Into<CosmosCoin> + Send>(
async fn create_periodic_vesting_account(
&self,
owner_address: &str,
staking_address: Option<String>,
vesting_spec: Option<VestingSpecification>,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>;
}
@@ -107,12 +105,13 @@ impl<C: SigningCosmWasmClient + Sync + Send> VestingSigningClient for NymdClient
profit_margin_percent,
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::UpdateMixnetConfig",
vec![],
)
.await
}
@@ -127,26 +126,24 @@ impl<C: SigningCosmWasmClient + Sync + Send> VestingSigningClient for NymdClient
address: address.to_string(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::UpdateMixnetAddress",
vec![],
)
.await
}
async fn vesting_bond_gateway<T>(
async fn vesting_bond_gateway(
&self,
gateway: Gateway,
owner_signature: &str,
pledge: T,
pledge: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>
where
T: Into<CosmWasmCoin> + Send,
{
) -> Result<ExecuteResult, NymdError> {
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::BondGateway {
gateway,
@@ -154,12 +151,13 @@ impl<C: SigningCosmWasmClient + Sync + Send> VestingSigningClient for NymdClient
amount: pledge.into(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::BondGateway",
vec![],
)
.await
}
@@ -168,51 +166,47 @@ impl<C: SigningCosmWasmClient + Sync + Send> VestingSigningClient for NymdClient
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::UnbondGateway {};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::UnbondGateway",
vec![],
)
.await
}
async fn vesting_track_unbond_gateway<T>(
async fn vesting_track_unbond_gateway(
&self,
owner: &str,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>
where
T: Into<CosmWasmCoin> + Send,
{
) -> Result<ExecuteResult, NymdError> {
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::TrackUnbondGateway {
owner: owner.to_string(),
amount: amount.into(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::TrackUnbondGateway",
vec![],
)
.await
}
async fn vesting_bond_mixnode<T>(
async fn vesting_bond_mixnode(
&self,
mix_node: MixNode,
owner_signature: &str,
pledge: T,
pledge: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>
where
T: Into<CosmWasmCoin> + Send,
{
) -> Result<ExecuteResult, NymdError> {
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::BondMixnode {
mix_node,
@@ -220,12 +214,13 @@ impl<C: SigningCosmWasmClient + Sync + Send> VestingSigningClient for NymdClient
amount: pledge.into(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::BondMixnode",
vec![],
)
.await
}
@@ -234,72 +229,66 @@ impl<C: SigningCosmWasmClient + Sync + Send> VestingSigningClient for NymdClient
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::UnbondMixnode {};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::UnbondMixnode",
vec![],
)
.await
}
async fn vesting_track_unbond_mixnode<T>(
async fn vesting_track_unbond_mixnode(
&self,
owner: &str,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>
where
T: Into<CosmWasmCoin> + Send,
{
) -> Result<ExecuteResult, NymdError> {
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::TrackUnbondMixnode {
owner: owner.to_string(),
amount: amount.into(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::TrackUnbondMixnode",
vec![],
)
.await
}
async fn withdraw_vested_coins<T>(
async fn withdraw_vested_coins(
&self,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>
where
T: Into<CosmWasmCoin> + Send,
{
) -> Result<ExecuteResult, NymdError> {
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::WithdrawVestedCoins {
amount: amount.into(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::WithdrawVested",
vec![],
)
.await
}
async fn vesting_track_undelegation<T>(
async fn vesting_track_undelegation(
&self,
address: &str,
mix_identity: IdentityKey,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>
where
T: Into<CosmWasmCoin> + Send,
{
) -> Result<ExecuteResult, NymdError> {
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::TrackUndelegation {
owner: address.to_string(),
@@ -307,36 +296,35 @@ impl<C: SigningCosmWasmClient + Sync + Send> VestingSigningClient for NymdClient
amount: amount.into(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::TrackUndelegation",
vec![],
)
.await
}
async fn vesting_delegate_to_mixnode<'a, T>(
async fn vesting_delegate_to_mixnode<'a>(
&self,
mix_identity: IdentityKeyRef<'a>,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>
where
T: Into<CosmWasmCoin> + Send,
{
) -> Result<ExecuteResult, NymdError> {
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::DelegateToMixnode {
mix_identity: mix_identity.into(),
amount: amount.into(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::DelegateToMixnode",
vec![],
)
.await
}
@@ -351,27 +339,25 @@ impl<C: SigningCosmWasmClient + Sync + Send> VestingSigningClient for NymdClient
mix_identity: mix_identity.into(),
};
self.client
.fundless_execute(
.execute(
self.address(),
self.vesting_contract_address()?,
&req,
fee,
"VestingContract::UndelegateFromMixnode",
vec![],
)
.await
}
async fn create_periodic_vesting_account<T>(
async fn create_periodic_vesting_account(
&self,
owner_address: &str,
staking_address: Option<String>,
vesting_spec: Option<VestingSpecification>,
amount: T,
amount: Coin,
fee: Option<Fee>,
) -> Result<ExecuteResult, NymdError>
where
T: Into<CosmosCoin> + Send,
{
) -> Result<ExecuteResult, NymdError> {
let fee = fee.unwrap_or(Fee::Auto(Some(self.simulated_gas_multiplier)));
let req = VestingExecuteMsg::CreateAccount {
owner_address: owner_address.to_string(),