Skip to content

Commit

Permalink
feat(cast/send): legacy txn sending
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Jan 27, 2022
1 parent 57f26c3 commit 81b8daf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
25 changes: 21 additions & 4 deletions cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ethers_core::{
token::{LenientTokenizer, Tokenizer},
Abi, AbiParser, Token,
},
types::{Chain, *},
types::{transaction::eip2718::TypedTransaction, Chain, *},
utils::{self, keccak256},
};

Expand Down Expand Up @@ -216,7 +216,7 @@ where
args: Option<(&str, Vec<String>)>,
chain: Chain,
etherscan_api_key: Option<String>,
) -> Result<(Eip1559TransactionRequest, Option<ethers_core::abi::Function>)> {
) -> Result<(TypedTransaction, Option<ethers_core::abi::Function>)> {
let from = match from.into() {
NameOrAddress::Name(ref ens_name) => self.provider.resolve_name(ens_name).await?,
NameOrAddress::Address(addr) => addr,
Expand All @@ -231,7 +231,11 @@ where
};

// make the call
let mut tx = Eip1559TransactionRequest::new().from(from).to(to);
let mut tx: TypedTransaction = if is_legacy(chain) {
TransactionRequest::new().from(from).to(to).into()
} else {
Eip1559TransactionRequest::new().from(from).to(to).into()
};

let func = if let Some((sig, args)) = args {
let func = if sig.contains('(') {
Expand All @@ -247,7 +251,7 @@ where
.await?
};
let data = encode_args(&func, &args)?;
tx = tx.data(data);
tx.set_data(data.into());
Some(func)
} else {
None
Expand Down Expand Up @@ -1042,3 +1046,16 @@ mod tests {
);
}
}

/// Helper function for checking if a chainid corresponds to a legacy chainid
/// without eip1559
fn is_legacy<T: TryInto<Chain>>(chain: T) -> bool {
let chain = match chain.try_into() {
Ok(inner) => inner,
_ => return false,
};

use Chain::*;
// TODO: Add other chains which do not support EIP1559.
matches!(chain, Optimism | OptimismKovan | Fantom | FantomTestnet)
}
1 change: 1 addition & 0 deletions cli/src/opts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct EthereumOpts {

#[clap(long, env = "ETHERSCAN_API_KEY")]
pub etherscan_api_key: Option<String>,

#[clap(long, env = "CHAIN", default_value = "mainnet")]
pub chain: Chain,
}
Expand Down

0 comments on commit 81b8daf

Please sign in to comment.