Skip to content

Commit

Permalink
Move client eth-to-cosmos to gorc (#138)
Browse files Browse the repository at this point in the history
* moving eth_to_cosmos command from client to gorc

* moving eth_to_cosmos command from client to gorc

* moving eth_to_cosmos command from client to gorc

* moving eth_to_cosmos command from client to gorc

* Merge PR #139: gorc eth-to-cosmos feedback on #138

* port #132; support user amounts verbatim

* replace etherum_key (as mnemonic) with ethereum_key (as keystore key name)

Co-authored-by: Hannydevelop <ukpaiugochiibem @gmail.com>
Co-authored-by: Levi Cook <[email protected]>
Co-authored-by: Levi Cook <[email protected]>
  • Loading branch information
4 people authored Aug 11, 2021
1 parent 2de059f commit 755156a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 5 deletions.
13 changes: 8 additions & 5 deletions orchestrator/gorc/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//! Gorc Subcommands
//! This is where you specify the subcommands of your application.
mod cosmos_to_eth;
mod deploy;
mod eth_to_cosmos;
mod keys;
mod orchestrator;
mod print_config;
mod query;
mod sign_delegate_keys;
mod tests;
mod tx;
mod version;
mod cosmos_to_eth;
mod print_config;

use crate::config::GorcConfig;
use abscissa_core::{Command, Configurable, Help, Options, Runnable};
Expand All @@ -22,13 +23,15 @@ pub const CONFIG_FILE: &str = "gorc.toml";
/// Gorc Subcommands
#[derive(Command, Debug, Options, Runnable)]
pub enum GorcCmd {

#[options(help = "Send Cosmos to Ethereum")]
CosmosToEth(cosmos_to_eth::CosmosToEthCmd),

#[options(help = "tools for contract deployment")]
Deploy(deploy::DeployCmd),

#[options(help = "Send Ethereum to Cosmos")]
EthToCosmos(eth_to_cosmos::EthToCosmosCmd),

#[options(help = "get usage information")]
Help(Help<Self>),

Expand Down Expand Up @@ -72,4 +75,4 @@ impl Configurable<GorcConfig> for GorcCmd {
None
}
}
}
}
2 changes: 2 additions & 0 deletions orchestrator/gorc/src/commands/cosmos_to_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub struct CosmosToEthCmd {
help = "cosmos-to-eth [gravity_denom] [amount] [cosmos_key] [eth_dest] [times]"
)]
pub args: Vec<String>,

#[options(help = "don't batch, send request to be sent immediately")]
pub flag_no_batch: bool,
}

Expand Down
106 changes: 106 additions & 0 deletions orchestrator/gorc/src/commands/eth_to_cosmos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::application::APP;
use abscissa_core::{status_err, Application, Command, Options, Runnable};
use clarity::Address as EthAddress;
use clarity::Uint256;
use deep_space::address::Address as CosmosAddress;
use ethereum_gravity::send_to_cosmos::send_to_cosmos;
use gravity_utils::connection_prep::{check_for_eth, create_rpc_connections};
use std::time::Duration;

const TIMEOUT: Duration = Duration::from_secs(60);

#[derive(Command, Debug, Default, Options)]
pub struct EthToCosmosCmd {
#[options(
free,
help = "eth-to-cosmos [erc20_address] [ethereum_key] [contract_address] [cosmos_dest] [amount] [times]"
)]
pub args: Vec<String>,
}

impl Runnable for EthToCosmosCmd {
fn run(&self) {
let config = APP.config();
let erc20_address = self.args.get(0).expect("erc20 address is required");
let erc20_address: EthAddress = erc20_address
.parse()
.expect("Invalid ERC20 contract address!");

let ethereum_key = self.args.get(1).expect("key is required");
let ethereum_key = config.load_clarity_key(ethereum_key.clone());

let contract_address = self.args.get(2).expect("contract address is required");
let contract_address: EthAddress =
contract_address.parse().expect("Invalid contract address!");

let cosmos_prefix = config.cosmos.prefix.trim();
let eth_rpc = config.ethereum.rpc.trim();
abscissa_tokio::run_with_actix(&APP, async {
let connections = create_rpc_connections(
cosmos_prefix.to_string(),
None,
Some(eth_rpc.to_string()),
TIMEOUT,
)
.await;
let web3 = connections.web3.unwrap();
let cosmos_dest = self.args.get(3).expect("cosmos destination is required");
let cosmos_dest: CosmosAddress = cosmos_dest.parse().unwrap();
let ethereum_public_key = ethereum_key.to_public_key().unwrap();
check_for_eth(ethereum_public_key, &web3).await;

let init_amount = self.args.get(4).expect("amount is required");
let amount: Uint256 = init_amount.parse().unwrap();

let erc20_balance = web3
.get_erc20_balance(erc20_address, ethereum_public_key)
.await
.expect("Failed to get balance, check ERC20 contract address");

let times = self.args.get(5).expect("times is required");
let times = times.parse::<usize>().expect("cannot parse times");

if erc20_balance == 0u8.into() {
panic!(
"You have zero {} tokens, please double check your sender and erc20 addresses!",
contract_address
);
} else if amount.clone() * times.into() > erc20_balance {
panic!(
"Insufficient balance {} > {}",
amount * times.into(),
erc20_balance
);
}

for _ in 0..times {
println!(
"Sending {} / {} to Cosmos from {} to {}",
init_amount.parse::<f64>().unwrap(),
erc20_address,
ethereum_public_key,
cosmos_dest
);
// we send some erc20 tokens to the gravity contract to register a deposit
let res = send_to_cosmos(
erc20_address,
contract_address,
amount.clone(),
cosmos_dest,
ethereum_key,
Some(TIMEOUT),
&web3,
vec![],
)
.await;
match res {
Ok(tx_id) => println!("Send to Cosmos txid: {:#066x}", tx_id),
Err(e) => println!("Failed to send tokens! {:?}", e),
}
}
})
.unwrap_or_else(|e| {
status_err!("executor exited with error: {}", e);
});
}
}

0 comments on commit 755156a

Please sign in to comment.