From e77cc548eb61dffcd794856c10665c79a064d367 Mon Sep 17 00:00:00 2001 From: Siddharth2207 Date: Thu, 10 Aug 2023 21:30:04 +0530 Subject: [PATCH] update --- cli/Cargo.lock | 37 ++++++++++++++++++----- cli/Cargo.toml | 1 + cli/src/cli/addorder/addorder.rs | 9 ++++-- cli/src/cli/addorder/mod.rs | 2 -- cli/src/cli/deposit/deposit.rs | 34 +++------------------- cli/src/cli/deposit/mod.rs | 25 ++++++++++++---- cli/src/cli/withdraw/mod.rs | 15 ++-------- cli/src/lib.rs | 2 ++ cli/src/main.rs | 2 ++ cli/src/tokens/mod.rs | 50 ++++++++++++++++++++++++++++++++ 10 files changed, 117 insertions(+), 60 deletions(-) create mode 100644 cli/src/tokens/mod.rs diff --git a/cli/Cargo.lock b/cli/Cargo.lock index af6744158..eb50d02ae 100644 --- a/cli/Cargo.lock +++ b/cli/Cargo.lock @@ -281,6 +281,12 @@ version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3ac9f8b63eca6fd385229b3675f6cc0dc5c8a5c8a54a59d4f52ffd670d87b0c" +[[package]] +name = "byteorder" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855" + [[package]] name = "byteorder" version = "1.4.3" @@ -355,6 +361,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" +[[package]] +name = "cbor" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e56053652b4b5c0ded5ae6183c7cd547ad2dd6bcce149658bef052a4995533bd" +dependencies = [ + "byteorder 0.5.3", + "rustc-serialize", +] + [[package]] name = "cc" version = "1.0.79" @@ -496,7 +512,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "863cc93703bfc6f02f4401b42663b767783179f4080d89a0c4876766c7c0fb78" dependencies = [ "async-trait", - "byteorder", + "byteorder 1.4.3", "cfg-if", "futures", "hex", @@ -528,7 +544,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da3da6baa321ec19e1cc41d31bf599f00c783d0517095cdaf0332e3fe8d20680" dependencies = [ "ascii", - "byteorder", + "byteorder 1.4.3", "either", "memchr", "unreachable", @@ -1223,7 +1239,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" dependencies = [ - "byteorder", + "byteorder 1.4.3", "rand", "rustc-hex", "static_assertions", @@ -1406,7 +1422,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" dependencies = [ - "byteorder", + "byteorder 1.4.3", ] [[package]] @@ -2521,6 +2537,7 @@ name = "rain_cli_ob" version = "0.0.4" dependencies = [ "anyhow", + "cbor", "clap", "crossterm", "ethers", @@ -2788,6 +2805,12 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" + [[package]] name = "rustc_version" version = "0.4.0" @@ -3657,7 +3680,7 @@ version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15fba1a6d6bb030745759a9a2a588bfe8490fc8b4751a277db3a0be1c9ebbf67" dependencies = [ - "byteorder", + "byteorder 1.4.3", "bytes", "data-encoding", "http", @@ -3684,7 +3707,7 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76f64bba2c53b04fcab63c01a7d7427eadc821e3bc48c34dc9ba29c501164b52" dependencies = [ - "byteorder", + "byteorder 1.4.3", "crunchy", "hex", "static_assertions", @@ -4174,7 +4197,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "760394e246e4c28189f19d488c058bf16f564016aefac5d32bb1f3b51d5e9261" dependencies = [ "aes", - "byteorder", + "byteorder 1.4.3", "bzip2", "constant_time_eq", "crc32fast", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 168a6eeee..0bcad7e7b 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -28,3 +28,4 @@ hex = "0.4.3" ethers-signers = { version = "2.0.8", features = ["ledger"] } tui = "0.19" crossterm = "0.25" +cbor = "0.4.1" diff --git a/cli/src/cli/addorder/addorder.rs b/cli/src/cli/addorder/addorder.rs index 2d390b181..46d8a0ba6 100644 --- a/cli/src/cli/addorder/addorder.rs +++ b/cli/src/cli/addorder/addorder.rs @@ -8,6 +8,7 @@ use ethers::providers::{Provider, Http} ; use crate::cli::registry::{IOrderBookV2, IParserV1, Io, EvaluableConfig, OrderConfig}; +#[allow(unused_variables)] pub async fn add_ob_order( orderbook_address : H160, parser_address : H160, @@ -56,11 +57,13 @@ pub async fn add_ob_order( } ; let rain_magic_number = String::from("ff0a89c674ee7874") ; - + + // TODO cbor encode order_meta let meta_string = hex::decode( - format!("{}{}", + // format!("{}{}", + format!("{}", rain_magic_number, - hex::encode(order_meta) + // hex::encode(order_meta) ) ).unwrap(); diff --git a/cli/src/cli/addorder/mod.rs b/cli/src/cli/addorder/mod.rs index 78ceb4bd7..f3180f15b 100644 --- a/cli/src/cli/addorder/mod.rs +++ b/cli/src/cli/addorder/mod.rs @@ -7,7 +7,6 @@ use ethers_signers::{Ledger, HDPath}; use ethers::{providers::{Provider, Http}, types::H160} ; use anyhow::anyhow; - use self::addorder::add_ob_order; use super::registry::RainNetworkOptions; @@ -97,7 +96,6 @@ impl AddOrder{ pub async fn handle_add_order(add_order : AddOrder) -> anyhow::Result<()> { - let rpc_url = add_order.get_network_rpc().unwrap() ; let provider = Provider::::try_from(rpc_url.clone()) diff --git a/cli/src/cli/deposit/deposit.rs b/cli/src/cli/deposit/deposit.rs index 75996650b..7ab8b2304 100644 --- a/cli/src/cli/deposit/deposit.rs +++ b/cli/src/cli/deposit/deposit.rs @@ -3,9 +3,8 @@ use ethers_signers::Ledger; use std::{convert::TryFrom, sync::Arc}; use spinners::{Spinner, Spinners}; use std::str::FromStr; -use anyhow::anyhow; -use crate::cli::registry::{IERC20, IOrderBookV2, DepositConfig}; +use crate::cli::registry::{IOrderBookV2, DepositConfig}; pub async fn deposit_token( deposit_token_address : H160 , @@ -19,33 +18,6 @@ pub async fn deposit_token( let provider = Provider::::try_from(rpc_url.clone()) .expect("\n❌Could not instantiate HTTP Provider"); - let signer_address = wallet.get_address().await.unwrap() ; - - let token_contract = IERC20::new(deposit_token_address,Arc::new(provider.clone())) ; - let token_balance: U256 = token_contract.balance_of(signer_address).call().await.unwrap() ; - - if token_balance.gt(&deposit_token_amount.clone()) { - let approve_tx = token_contract.approve(orderbook_address.clone(), deposit_token_amount.clone()) ; - let mut sp = Spinner::new( - Spinners::from_str("Dots9").unwrap(), - "Approving tokens for deposit...".into(), - ); - let approve_pending_tx = approve_tx.send().await? ; - let approve_receipt = approve_pending_tx.confirmations(4).await?.unwrap(); - - let end_msg = format!( - "{}{}{}" , - String::from("\nTokens Approved for deposit !!\n#################################\n✅ Hash : "), - format!("0x{}",hex::encode(approve_receipt.transaction_hash.as_bytes().to_vec())), - String::from("\n-----------------------------------\n") - ) ; - sp.stop_with_message(end_msg.into()); - - - }else{ - return Err(anyhow!("\n ❌Insufficent balance for deposit.\nCurrent Balance : {}.",token_balance)) ; - } - let orderbook = IOrderBookV2::new(orderbook_address, Arc::new(SignerMiddleware::new(provider, wallet))); let deposit_config = DepositConfig{ @@ -64,9 +36,11 @@ pub async fn deposit_token( let depsoit_receipt = deposit_pending_tx.confirmations(3).await?.unwrap(); let deposit_msg = format!( - "{}{}{}" , + "{}{}{}{}{}" , String::from("\nTokens deposited in vault !!\n#################################\n✅ Hash : "), format!("0x{}",hex::encode(depsoit_receipt.transaction_hash.as_bytes().to_vec())), + String::from("\nVault Id : "), + deposit_vault_id , String::from("\n-----------------------------------\n") ) ; sp.stop_with_message(deposit_msg.into()); diff --git a/cli/src/cli/deposit/mod.rs b/cli/src/cli/deposit/mod.rs index 72b8ffed9..a47dcb7af 100644 --- a/cli/src/cli/deposit/mod.rs +++ b/cli/src/cli/deposit/mod.rs @@ -8,6 +8,8 @@ use ethers::{providers::{Provider, Middleware, Http}, types::{H160,U256}}; use anyhow::anyhow; use ethers_signers::{Ledger, HDPath}; +use crate::tokens::approve_tokens; + use self::deposit::deposit_token; use super::registry::RainNetworkOptions; @@ -37,7 +39,7 @@ pub struct Deposit{ #[arg(short, long)] amount : String, - /// optional vault id to deposit in + /// optional vault id to deposit in (in decimals) #[arg(short, long)] vault_id : Option , @@ -121,7 +123,7 @@ pub async fn handle_deposit(deposit : Deposit) -> anyhow::Result<()> { let vault_id = match deposit.vault_id.clone() { Some(val) => { - match U256::from_str(&val) { + match U256::from_dec_str(&val) { Ok(id) => id , Err(_) => { return Err(anyhow!("\n ❌Invalid vault id.")) ; @@ -131,15 +133,28 @@ pub async fn handle_deposit(deposit : Deposit) -> anyhow::Result<()> { None => { U256::from(H160::random().as_bytes()) } - } ; + } ; let rpc_url = deposit.get_network_rpc().unwrap() ; let provider = Provider::::try_from(rpc_url.clone()) .expect("\n❌Could not instantiate HTTP Provider"); - let chain_id = provider.get_chainid().await.unwrap().as_u64() ; - let wallet= Ledger::new(HDPath::LedgerLive(0), chain_id).await?; + let chain_id = provider.get_chainid().await.unwrap().as_u64() ; + let wallet= Ledger::new(HDPath::LedgerLive(0), chain_id.clone()).await?; + + // Approve token for deposit + let _ = approve_tokens( + token_address.clone() , + token_amount.clone(), + orderbook_address.clone() , + rpc_url.clone(), + wallet + ).await ; + + // Reinit Wallet Instance + let wallet= Ledger::new(HDPath::LedgerLive(0), chain_id).await?; + // Deposit tokens let _ = deposit_token( token_address, token_amount, diff --git a/cli/src/cli/withdraw/mod.rs b/cli/src/cli/withdraw/mod.rs index 928f8b6dc..ce265d75a 100644 --- a/cli/src/cli/withdraw/mod.rs +++ b/cli/src/cli/withdraw/mod.rs @@ -35,7 +35,7 @@ pub struct Withdraw{ #[arg(long)] amount : String, - /// vault id to withdraw from + /// decimal vault id to withdraw from #[arg(long)] vault_id : String , @@ -116,18 +116,7 @@ pub async fn handle_withdraw(withdraw : Withdraw) -> anyhow::Result<()> { } } ; - // let vault_id = match { - // Ok(id) => id , - // Err(_) => { - // return Err(anyhow!("\n ❌Invalid vault id.")) ; - // } - // } ; - - // let i_p = ethers::utils::hex::((withdraw.clone().vault_id)) ; - // // let i_p = U256::from_big_endian(&i_p) ; - // println!("i_p : {:?}",i_p) ; - - let vault_id = U256::from_str(&String::from(withdraw.clone().vault_id)).unwrap(); + let vault_id = U256::from_dec_str(&String::from(withdraw.clone().vault_id)).unwrap(); let rpc_url = withdraw.get_network_rpc().unwrap() ; diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 67353d872..59593a837 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1,2 +1,4 @@ pub mod cli; pub mod subgraph; +pub mod tokens; + diff --git a/cli/src/main.rs b/cli/src/main.rs index 260861e97..d0e237ee9 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,5 +1,7 @@ mod cli; pub(crate) mod subgraph; +pub(crate) mod tokens; + #[tokio::main] async fn main() -> anyhow::Result<()> { diff --git a/cli/src/tokens/mod.rs b/cli/src/tokens/mod.rs new file mode 100644 index 000000000..7212b5785 --- /dev/null +++ b/cli/src/tokens/mod.rs @@ -0,0 +1,50 @@ + +use anyhow::anyhow; +use ethers::{providers::{Provider, Http}, types::{H160,U256}, prelude::SignerMiddleware} ; +use std::{convert::TryFrom, sync::Arc}; +use spinners::{Spinner, Spinners}; +use std::str::FromStr; +use ethers_signers::Ledger; + +use crate::cli::registry::IERC20; + +pub async fn approve_tokens( + token_address : H160 , + token_amount : U256 , + approver_address : H160 , + rpc_url : String , + wallet : Ledger +) -> anyhow::Result<()> { + + let provider = Provider::::try_from(rpc_url.clone()) + .expect("\n❌Could not instantiate HTTP Provider"); + + let signer_address = wallet.get_address().await.unwrap() ; + + let token_contract = IERC20::new(token_address,Arc::new(SignerMiddleware::new(provider, wallet))) ; + let token_balance: U256 = token_contract.balance_of(signer_address).call().await.unwrap() ; + + if token_balance.gt(&token_amount.clone()) { + let approve_tx = token_contract.approve(approver_address.clone(), token_amount.clone()) ; + let mut sp = Spinner::new( + Spinners::from_str("Dots9").unwrap(), + "Approving tokens for deposit...".into(), + ); + let approve_pending_tx = approve_tx.send().await? ; + let approve_receipt = approve_pending_tx.confirmations(4).await?.unwrap(); + + let end_msg = format!( + "{}{}{}" , + String::from("\nTokens Approved for deposit !!\n#################################\n✅ Hash : "), + format!("0x{}",hex::encode(approve_receipt.transaction_hash.as_bytes().to_vec())), + String::from("\n-----------------------------------\n") + ) ; + sp.stop_with_message(end_msg.into()); + + + }else{ + return Err(anyhow!("\n ❌Insufficent balance for deposit.\nCurrent Balance : {}.",token_balance)) ; + } + + Ok(()) +} \ No newline at end of file