Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBolten committed Nov 20, 2021
1 parent c72e123 commit 6afba3a
Show file tree
Hide file tree
Showing 68 changed files with 621 additions and 339 deletions.
3 changes: 2 additions & 1 deletion orchestrator/cosmos_gravity/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ pub async fn contract_call_tx_confirmation_messages(

let mut msgs = Vec::new();
for logic_call in logic_calls {
let data = keccak256(encode_logic_call_confirm(gravity_id.clone(), logic_call.clone()).as_slice());
let data =
keccak256(encode_logic_call_confirm(gravity_id.clone(), logic_call.clone()).as_slice());
// Signer trait responds with a Result, but we use a LocalWallet and it
// will never throw an error
let signature = eth_client.signer().sign_message(data).await.unwrap();
Expand Down
14 changes: 8 additions & 6 deletions orchestrator/cosmos_gravity/src/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ pub async fn send_to_eth(
gas_adjustment: f64,
) -> Result<TxResponse, GravityError> {
if amount.denom != bridge_fee.denom {
return Err(GravityError::CosmosGrpcError(CosmosGrpcError::BadInput(format!(
"The amount ({}) and bridge_fee ({}) denominations do not match.",
amount.denom, bridge_fee.denom,
))))
return Err(GravityError::CosmosGrpcError(CosmosGrpcError::BadInput(
format!(
"The amount ({}) and bridge_fee ({}) denominations do not match.",
amount.denom, bridge_fee.denom,
),
)));
}

let cosmos_address = cosmos_key.to_address(&contact.get_prefix()).unwrap();
Expand Down Expand Up @@ -162,7 +164,7 @@ async fn __send_messages(

match contact.wait_for_tx(response, TIMEOUT).await {
Ok(res) => Ok(res),
Err(e) => Err(GravityError::CosmosGrpcError(e))
Err(e) => Err(GravityError::CosmosGrpcError(e)),
}
}

Expand Down Expand Up @@ -212,7 +214,7 @@ pub async fn send_messages(

match contact.wait_for_tx(response, TIMEOUT).await {
Ok(res) => Ok(res),
Err(e) => Err(GravityError::CosmosGrpcError(e))
Err(e) => Err(GravityError::CosmosGrpcError(e)),
}
}

Expand Down
19 changes: 12 additions & 7 deletions orchestrator/ethereum_gravity/src/deploy_erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ pub async fn deploy_erc20(
wait_timeout: Option<Duration>,
eth_client: EthClient,
) -> Result<TxHash, GravityError> {
let contract_call = Gravity::new(gravity_contract, eth_client.clone())
.deploy_erc20(cosmos_denom, erc20_name, erc20_symbol.clone(), decimals);
let contract_call = Gravity::new(gravity_contract, eth_client.clone()).deploy_erc20(
cosmos_denom,
erc20_name,
erc20_symbol.clone(),
decimals,
);
let gas_price = get_send_transaction_gas_price(eth_client.clone()).await?;
let contract_call = contract_call.gas_price(gas_price);

Expand All @@ -32,19 +36,20 @@ pub async fn deploy_erc20(
// TODO(bolten): ethers interval default is 7s, this mirrors what web30 was doing, should we adjust?
// additionally we are mirroring only waiting for 1 confirmation by leaving that as default
let pending_tx = pending_tx.interval(Duration::from_secs(1));
let potential_error = GravityError::GravityContractError(
format!("Did not receive transaction receipt when deploying ERC-20 {}: {}", erc20_symbol, tx_hash)
);
let potential_error = GravityError::GravityContractError(format!(
"Did not receive transaction receipt when deploying ERC-20 {}: {}",
erc20_symbol, tx_hash
));

if let Some(timeout) = wait_timeout {
match tokio::time::timeout(timeout, pending_tx).await?? {
Some(receipt) => Ok(receipt.transaction_hash),
None => Err(potential_error)
None => Err(potential_error),
}
} else {
match pending_tx.await? {
Some(receipt) => Ok(receipt.transaction_hash),
None => Err(potential_error)
None => Err(potential_error),
}
}
}
27 changes: 18 additions & 9 deletions orchestrator/ethereum_gravity/src/erc20_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ pub async fn approve_erc20_transfers(
// TODO(bolten): ethers interval default is 7s, this mirrors what web30 was doing, should we adjust?
// additionally we are mirroring only waiting for 1 confirmation by leaving that as default
let pending_tx = pending_tx.interval(Duration::from_secs(1));
let potential_error = GravityError::GravityContractError(format!("Did not receive transaction receipt when approving ERC-20 {}: {}", erc20, tx_hash));
let potential_error = GravityError::GravityContractError(format!(
"Did not receive transaction receipt when approving ERC-20 {}: {}",
erc20, tx_hash
));

if let Some(timeout) = timeout_option {
match tokio::time::timeout(timeout, pending_tx).await?? {
Some(receipt) => Ok(receipt.transaction_hash),
None => Err(potential_error)
None => Err(potential_error),
}
} else {
match pending_tx.await? {
Some(receipt) => Ok(receipt.transaction_hash),
None => Err(potential_error)
None => Err(potential_error),
}
}
}
Expand Down Expand Up @@ -83,18 +86,24 @@ pub async fn erc20_transfer(

let pending_tx = contract_call.send().await?;
let tx_hash = *pending_tx;
info!("Transferring {} ERC-20 {} from {} to {} with txid {}",
amount, erc20, eth_client.address(), destination, tx_hash
info!(
"Transferring {} ERC-20 {} from {} to {} with txid {}",
amount,
erc20,
eth_client.address(),
destination,
tx_hash
);
// TODO(bolten): ethers interval default is 7s, this mirrors what web30 was doing, should we adjust?
// additionally we are mirroring only waiting for 1 confirmation by leaving that as default
let pending_tx = pending_tx.interval(Duration::from_secs(1));
let potential_error = GravityError::GravityContractError(format!(
"Did not receive transaction receipt when transferring ERC-20 {}: {}", erc20, tx_hash)
);
"Did not receive transaction receipt when transferring ERC-20 {}: {}",
erc20, tx_hash
));

match pending_tx.await? {
Some(receipt) => Ok(receipt.transaction_hash),
None => Err(potential_error)
None => Err(potential_error),
}
}
}
2 changes: 1 addition & 1 deletion orchestrator/ethereum_gravity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use ethers::types::U256;
#[macro_use]
extern crate log;

pub mod erc20_utils;
pub mod deploy_erc20;
pub mod erc20_utils;
pub mod logic_call;
pub mod send_to_cosmos;
pub mod submit_batch;
Expand Down
72 changes: 52 additions & 20 deletions orchestrator/ethereum_gravity/src/logic_call.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{types::{EthClient, EthSignerMiddleware},
utils::{GasCost,
convert_invalidation_id_to_fixed_array,
get_logic_call_nonce,
get_send_transaction_gas_price}};
use crate::{
types::{EthClient, EthSignerMiddleware},
utils::{
convert_invalidation_id_to_fixed_array, get_logic_call_nonce,
get_send_transaction_gas_price, GasCost,
},
};
use ethers::contract::builders::ContractCall;
use ethers::prelude::*;
use ethers::types::Address as EthAddress;
Expand Down Expand Up @@ -56,10 +58,17 @@ pub async fn send_eth_logic_call(
}

let contract_call = build_send_logic_call_contract_call(
current_valset, &call, confirms, gravity_contract_address, gravity_id, eth_client.clone()
current_valset,
&call,
confirms,
gravity_contract_address,
gravity_id,
eth_client.clone(),
)?;

let contract_call = contract_call.gas(gas_cost.gas).gas_price(gas_cost.gas_price);
let contract_call = contract_call
.gas(gas_cost.gas)
.gas_price(gas_cost.gas_price);

let pending_tx = contract_call.send().await?;
let tx_hash = *pending_tx;
Expand All @@ -70,7 +79,10 @@ pub async fn send_eth_logic_call(

match tokio::time::timeout(timeout, pending_tx).await?? {
Some(_) => (),
None => error!("Did not receive transaction receipt when submitting batch: {}", tx_hash),
None => error!(
"Did not receive transaction receipt when submitting batch: {}",
tx_hash
),
}

let last_nonce = get_logic_call_nonce(
Expand Down Expand Up @@ -104,7 +116,12 @@ pub async fn estimate_logic_call_cost(
eth_client: EthClient,
) -> Result<GasCost, GravityError> {
let contract_call = build_send_logic_call_contract_call(
current_valset, &call, confirms, gravity_contract_address, gravity_id, eth_client.clone()
current_valset,
&call,
confirms,
gravity_contract_address,
gravity_id,
eth_client.clone(),
)?;

Ok(GasCost {
Expand All @@ -128,19 +145,32 @@ pub fn build_send_logic_call_contract_call(
let sig_data = current_valset.order_sigs(&hash, confirms)?;
let sig_arrays = to_arrays(sig_data);

let transfer_amounts = call.transfers.iter()
.map(|transfer| transfer.amount).collect();
let transfer_token_contracts = call.transfers.iter()
.map(|transfer| transfer.token_contract_address).collect();
let fee_amounts = call.fees.iter()
.map(|fee| fee.amount).collect();
let fee_token_contracts = call.fees.iter()
.map(|fee| fee.token_contract_address).collect();
let transfer_amounts = call
.transfers
.iter()
.map(|transfer| transfer.amount)
.collect();
let transfer_token_contracts = call
.transfers
.iter()
.map(|transfer| transfer.token_contract_address)
.collect();
let fee_amounts = call.fees.iter().map(|fee| fee.amount).collect();
let fee_token_contracts = call
.fees
.iter()
.map(|fee| fee.token_contract_address)
.collect();
let invalidation_id = convert_invalidation_id_to_fixed_array(call.invalidation_id.clone())?;

let contract_call = Gravity::new(gravity_contract_address, eth_client.clone())
.submit_logic_call(current_addresses, current_powers, current_valset_nonce.into(),
sig_arrays.v, sig_arrays.r, sig_arrays.s,
.submit_logic_call(
current_addresses,
current_powers,
current_valset_nonce.into(),
sig_arrays.v,
sig_arrays.r,
sig_arrays.s,
LogicCallArgs {
transfer_amounts,
transfer_token_contracts,
Expand All @@ -150,7 +180,9 @@ pub fn build_send_logic_call_contract_call(
payload: call.payload.clone(),
time_out: call.timeout.into(),
invalidation_id,
invalidation_nonce: call.invalidation_nonce.into(), })
invalidation_nonce: call.invalidation_nonce.into(),
},
)
.from(eth_client.address())
.value(U256::zero());

Expand Down
26 changes: 20 additions & 6 deletions orchestrator/ethereum_gravity/src/send_to_cosmos.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Helper functions for sending tokens to Cosmos

use crate::{erc20_utils::{approve_erc20_transfers, check_erc20_approved}, types::EthClient};
use crate::{
erc20_utils::{approve_erc20_transfers, check_erc20_approved},
types::EthClient,
};
use deep_space::address::Address as CosmosAddress;
use ethers::prelude::*;
use gravity_abi::gravity::*;
Expand All @@ -21,9 +24,17 @@ pub async fn send_to_cosmos(
wait_timeout: Option<Duration>,
eth_client: EthClient,
) -> Result<TxHash, GravityError> {
let approved = check_erc20_approved(erc20, gravity_contract, eth_client.address(), eth_client.clone()).await?;
let approved = check_erc20_approved(
erc20,
gravity_contract,
eth_client.address(),
eth_client.clone(),
)
.await?;
if !approved {
let txid = approve_erc20_transfers(erc20, gravity_contract, wait_timeout, eth_client.clone()).await?;
let txid =
approve_erc20_transfers(erc20, gravity_contract, wait_timeout, eth_client.clone())
.await?;
trace!("ERC-20 approval for {} finished with txid {}", erc20, txid);
}

Expand All @@ -50,17 +61,20 @@ pub async fn send_to_cosmos(
// TODO(bolten): ethers interval default is 7s, this mirrors what web30 was doing, should we adjust?
// additionally we are mirroring only waiting for 1 confirmation by leaving that as default
let pending_tx = pending_tx.interval(Duration::from_secs(1));
let potential_error = GravityError::GravityContractError(format!("Did not receive transaction receipt when sending to Cosmos: {}", tx_hash));
let potential_error = GravityError::GravityContractError(format!(
"Did not receive transaction receipt when sending to Cosmos: {}",
tx_hash
));

if let Some(timeout) = wait_timeout {
match tokio::time::timeout(timeout, pending_tx).await?? {
Some(receipt) => Ok(receipt.transaction_hash),
None => Err(potential_error)
None => Err(potential_error),
}
} else {
match pending_tx.await? {
Some(receipt) => Ok(receipt.transaction_hash),
None => Err(potential_error)
None => Err(potential_error),
}
}
}
Loading

0 comments on commit 6afba3a

Please sign in to comment.