Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transactions): transaction examples #4

Merged
merged 8 commits into from
Mar 25, 2024
Merged

feat(transactions): transaction examples #4

merged 8 commits into from
Mar 25, 2024

Conversation

yash-atreya
Copy link
Member

Added the following examples

  • decode_input
  • transfer_eth
  • transfer_erc20
  • trace_transaction
  • trace_call - Uses trace_call as debug_traceCall is pending implementation
  • gas_price_usd

@yash-atreya yash-atreya self-assigned this Mar 14, 2024
@zerosnacks
Copy link
Member

Pulled in the latest upstream changes and made a few slight stylistic changes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see if we can make this a bit simpler after merging, either by flattening or simplifying the example.

When we look at flattening the repo structure I would like to have a dedicated folder (fixtures?) to store these kind of assets in. That allows for re-use across examples.

async fn main() -> Result<()> {
let (provider, _anvil) = init();

let call = latestAnswerCall {}.abi_encode();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no fields should not require {} imo -- @DaniPopes, also in this case it could be made const but doubt it fits in abstraction

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this must be an instance, in order to decode it, so this is fine imo

examples/transactions/examples/gas_price_usd.rs Outdated Show resolved Hide resolved

fn init() -> (HttpProvider<Ethereum>, AnvilInstance) {
let anvil = Anvil::new().fork("https://eth.llamarpc.com").spawn();
let url = anvil.endpoint().parse().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make anvil return Url here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that would be ideal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add this as a new function?

fn endpoint_url?

Comment on lines +58 to +64
fn usd_value(amount: U256, price_usd: U256) -> Result<f64> {
let base: U256 = U256::from(10).pow(U256::from(ETH_DECIMALS));
let value: U256 = amount * price_usd / base;
let usd_price_decimals: u8 = 8;
let f: String = format_units(value, usd_price_decimals)?;
Ok(f.parse::<f64>()?)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we wanna add this as a general helper? :P

#[tokio::main]
async fn main() -> Result<()> {
let (provider, _anvil) = init();
let hash = fixed_bytes!("97a02abf405d36939e5b232a5d4ef5206980c5a6661845436058f30600c52df7"); // Hash of the tx we want to trace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont love fixed_bytes, too long, do we have an alias for it

(HttpProvider::new(RpcClient::new(http, true)), anvil)
}

async fn deploy_token_contract(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace with ERC20Example::deploy(...)

..Default::default()
};

let pending_tx = provider.send_transaction(transfer_tx).await?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't this take a block tag? maybe no longer in the new api

Comment on lines +28 to +30
let input = ERC20Example::transferCall { to, amount: U256::from(100) }.abi_encode();
// Convert to Bytes
let input = Bytes::from(input);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should abi_encode always return Bytes maybe? maybe not

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few more pedantic nits

async fn main() -> Result<()> {
let (provider, _anvil) = init();

let call = latestAnswerCall {}.abi_encode();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this must be an instance, in order to decode it, so this is fine imo


fn init() -> (HttpProvider<Ethereum>, AnvilInstance) {
let anvil = Anvil::new().fork("https://eth.llamarpc.com").spawn();
let url = anvil.endpoint().parse().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add this as a new function?

fn endpoint_url?

examples/transactions/examples/trace_call.rs Outdated Show resolved Hide resolved
examples/transactions/examples/trace_call.rs Outdated Show resolved Hide resolved
examples/transactions/examples/trace_transaction.rs Outdated Show resolved Hide resolved
examples/transactions/examples/transfer_erc20.rs Outdated Show resolved Hide resolved
examples/transactions/Cargo.toml Outdated Show resolved Hide resolved
@yash-atreya yash-atreya merged commit e329e7d into main Mar 25, 2024
2 checks passed
@zerosnacks zerosnacks deleted the tx_examples branch March 27, 2024 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants