-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
2,369 additions
and
81 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use clap::Args; | ||
|
||
#[derive(Args)] | ||
pub struct TransactionArgs { | ||
#[arg(short, long, help = "Orderbook contract address")] | ||
pub orderbook_address: String, | ||
|
||
#[arg(short, long, help = "Derivation path of the Ledger wallet")] | ||
pub derivation_path: Option<usize>, | ||
|
||
#[arg(short, long, help = "Chain ID of the network")] | ||
pub chain_id: u64, | ||
|
||
#[arg(short, long, help = "RPC URL")] | ||
pub rpc_url: String, | ||
|
||
#[arg(short, long, help = "Blocknative API Key")] | ||
pub blocknative_api_key: Option<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "rain_orderbook_transactions" | ||
version = "0.0.4" | ||
edition = "2021" | ||
license = "CAL-1.0" | ||
description = "Rain Orderbook CLI." | ||
homepage = "https://github.com/rainprotocol/rain.orderbook" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
ethers = "2.0.7" | ||
ethers-signers = { version = "2.0.8", features = ["ledger"] } | ||
url = "2.5.0" | ||
reqwest = { version = "0.11.17", features = ["json"] } | ||
anyhow = "1.0.70" | ||
alloy-primitives = "0.5.4" | ||
tracing = "0.1.37" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use alloy_primitives::{Address, U256, hex}; | ||
use ethers::prelude::SignerMiddleware; | ||
use ethers::types::TransactionReceipt; | ||
use ethers::{ | ||
providers::{Http, Middleware, Provider}, | ||
types::{Eip1559TransactionRequest, H160, U64}, | ||
utils::parse_units, | ||
}; | ||
use ethers_signers::Ledger; | ||
use std::str::FromStr; | ||
use tracing::{info, warn}; | ||
|
||
use crate::gasoracle::gas_price_oracle; | ||
|
||
/// Sign and submit transaction on chain via [Ledger] wallet. | ||
/// | ||
/// # Arguments | ||
/// * `tx_data` - Abi encoded transaction data, encoded with the function selector. | ||
/// * `tx_to` - [Eip1559TransactionRequest::to] | ||
/// * `tx_value` - [Eip1559TransactionRequest::value] | ||
/// * `rpc_url` - Network RPC | ||
/// * `wallet` - [Ledger] wallet instance | ||
/// * `blocknative_api_key` - Optional Blocknative API Key. | ||
/// | ||
pub async fn execute_transaction( | ||
tx_data: Vec<u8>, | ||
tx_to: Address, | ||
tx_value: U256, | ||
rpc_url: String, | ||
wallet: Ledger, | ||
blocknative_api_key: Option<String>, | ||
) -> anyhow::Result<TransactionReceipt> { | ||
let provider = Provider::<Http>::try_from(rpc_url.clone())?; | ||
|
||
let chain_id = provider.clone().get_chainid().await.unwrap().as_u64(); | ||
let client = SignerMiddleware::new_with_provider_chain(provider, wallet).await?; | ||
|
||
let to_address = H160::from_str(&tx_to.to_string()).unwrap(); | ||
|
||
let mut tx = Eip1559TransactionRequest::new(); | ||
|
||
tx.to = Some(to_address.into()); | ||
tx.value = Some(ethers::types::U256::from_dec_str(tx_value.to_string().as_str()).unwrap()); | ||
tx.data = Some(ethers::types::Bytes::from(tx_data)); | ||
tx.chain_id = Some(U64::from_dec_str(&chain_id.to_string()).unwrap()); | ||
|
||
match gas_price_oracle(blocknative_api_key, chain_id).await { | ||
Ok((max_priority, max_fee)) => { | ||
let max_priority: ethers::types::U256 = | ||
parse_units(max_priority.to_string(), 9).unwrap().into(); | ||
let max_fee: ethers::types::U256 = parse_units(max_fee.to_string(), 9).unwrap().into(); | ||
|
||
tx.max_priority_fee_per_gas = Some(max_priority); | ||
tx.max_fee_per_gas = Some(max_fee); | ||
} | ||
Err(_) => { | ||
warn!("BLOCKNATIVE UNSUPPORTED NETWORK"); | ||
} | ||
}; | ||
|
||
let pending_tx = client.send_transaction(tx, None).await?; | ||
|
||
info!("Transaction submitted. Awaiting block confirmations..."); | ||
let tx_confirmation = pending_tx.confirmations(1).await?; | ||
|
||
let tx_receipt = match tx_confirmation { | ||
Some(receipt) => receipt, | ||
None => return Err(anyhow::anyhow!("Transaction failed")), | ||
}; | ||
info!("Transaction Confirmed!!"); | ||
info!( | ||
"✅ Hash : 0x{}", | ||
hex::encode(tx_receipt.transaction_hash.as_bytes()) | ||
); | ||
Ok(tx_receipt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use ethers::middleware::gas_oracle::GasCategory; | ||
use ethers::prelude::gas_oracle::blocknative::Response as BlockNativeResponse; | ||
use reqwest::{header::AUTHORIZATION, Client}; | ||
use url::Url; | ||
|
||
/// Bloacknative Base Url for fetching blockprices | ||
static BLOCKNATIVE_BLOCKPRICES_URL: &str = "https://api.blocknative.com/gasprices/blockprices"; | ||
|
||
/// Blocknative Gas Oracle. | ||
/// Returns max priority fee and max fee from blocknative api. | ||
/// | ||
/// # Arguments | ||
/// * `api_key` - Optional blocknative api key. | ||
/// * `chain_id` - Network Chain Id. | ||
/// | ||
pub async fn gas_price_oracle( | ||
api_key: Option<String>, | ||
chain_id: u64, | ||
) -> anyhow::Result<(f64, f64)> { | ||
let client = Client::new(); | ||
let mut url = Url::parse(BLOCKNATIVE_BLOCKPRICES_URL)?; | ||
url.set_query(Some(format!("chainid={}", chain_id).as_str())); | ||
let mut request = client.get(url); | ||
if let Some(api_key) = api_key.as_ref() { | ||
request = request.header(AUTHORIZATION, api_key); | ||
} | ||
let response: BlockNativeResponse = request.send().await?.error_for_status()?.json().await?; | ||
let fastest = response | ||
.estimate_from_category(&GasCategory::Fastest) | ||
.unwrap(); | ||
Ok((fastest.max_priority_fee_per_gas, fastest.max_fee_per_gas)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod execute; | ||
pub mod gasoracle; |