Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth2207 committed Aug 10, 2023
1 parent 22f2ccc commit e77cc54
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 60 deletions.
37 changes: 30 additions & 7 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 6 additions & 3 deletions cli/src/cli/addorder/addorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 0 additions & 2 deletions cli/src/cli/addorder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Http>::try_from(rpc_url.clone())
Expand Down
34 changes: 4 additions & 30 deletions cli/src/cli/deposit/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ,
Expand All @@ -19,33 +18,6 @@ pub async fn deposit_token(
let provider = Provider::<Http>::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{
Expand All @@ -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());
Expand Down
25 changes: 20 additions & 5 deletions cli/src/cli/deposit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> ,

Expand Down Expand Up @@ -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.")) ;
Expand All @@ -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::<Http>::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,
Expand Down
15 changes: 2 additions & 13 deletions cli/src/cli/withdraw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ,

Expand Down Expand Up @@ -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() ;

Expand Down
2 changes: 2 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod cli;
pub mod subgraph;
pub mod tokens;

2 changes: 2 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod cli;
pub(crate) mod subgraph;
pub(crate) mod tokens;


#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand Down
50 changes: 50 additions & 0 deletions cli/src/tokens/mod.rs
Original file line number Diff line number Diff line change
@@ -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::<Http>::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(())
}

0 comments on commit e77cc54

Please sign in to comment.