From b1f0ff7e90c77d468c12f36ddebba36e56fd4c8b Mon Sep 17 00:00:00 2001 From: 0xNeshi Date: Fri, 13 Dec 2024 11:51:07 +0100 Subject: [PATCH] chore: add benches --- benches/src/erc20_flash_mint.rs | 105 ++++++++++++++++++++++++++++++++ benches/src/lib.rs | 1 + benches/src/main.rs | 5 +- 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 benches/src/erc20_flash_mint.rs diff --git a/benches/src/erc20_flash_mint.rs b/benches/src/erc20_flash_mint.rs new file mode 100644 index 000000000..c73eb9af5 --- /dev/null +++ b/benches/src/erc20_flash_mint.rs @@ -0,0 +1,105 @@ +use alloy::{ + network::{AnyNetwork, EthereumWallet}, + primitives::Address, + providers::ProviderBuilder, + sol, + sol_types::{SolCall, SolConstructor}, + uint, +}; +use alloy_primitives::{address, U256}; +use e2e::{receipt, Account}; + +use crate::{ + report::{ContractReport, FunctionReport}, + CacheOpt, +}; + +sol!( + #[sol(rpc)] + contract Erc20FlashMint { + function maxFlashLoan(address token) external view returns (uint256 maxLoan); + function flashFee(address token, uint256 amount) external view returns (uint256 fee); + function flashLoan(address receiver, address token, uint256 amount, bytes calldata data) external returns (bool); + } +); + +sol!("../examples/erc20-flash-mint/src/constructor.sol"); +sol!("../examples/erc20-flash-mint/src/ERC3156FlashBorrowerMock.sol"); + +const FEE_RECEIVER: Address = + address!("F4EaCDAbEf3c8f1EdE91b6f2A6840bc2E4DD3526"); +const FLASH_FEE_AMOUNT: U256 = uint!(100_U256); + +pub async fn bench() -> eyre::Result { + let reports = run_with(CacheOpt::None).await?; + let report = reports + .into_iter() + .try_fold(ContractReport::new("Erc20FlashMint"), ContractReport::add)?; + + let cached_reports = run_with(CacheOpt::Bid(0)).await?; + let report = cached_reports + .into_iter() + .try_fold(report, ContractReport::add_cached)?; + + Ok(report) +} + +pub async fn run_with( + cache_opt: CacheOpt, +) -> eyre::Result> { + let alice = Account::new().await?; + let alice_wallet = ProviderBuilder::new() + .network::() + .with_recommended_fillers() + .wallet(EthereumWallet::from(alice.signer.clone())) + .on_http(alice.url().parse()?); + + let contract_addr = deploy(&alice, cache_opt.clone()).await?; + + // TODO: uncomment once it's possible to call ERC20 functions from + // within `ERC3156FlashBorrowerMock`. + // let receiver_addr = deploy_receiver(&alice, cache_opt).await?; + + let contract = Erc20FlashMint::new(contract_addr, &alice_wallet); + + let amount = uint!(100_U256); + + // IMPORTANT: Order matters! + use Erc20FlashMint::*; + #[rustfmt::skip] + let receipts = vec![ + (maxFlashLoanCall::SIGNATURE, receipt!(contract.maxFlashLoan(contract_addr))?), + (flashFeeCall::SIGNATURE, receipt!(contract.flashFee(contract_addr, amount))?), + // (flashLoanCall::SIGNATURE, receipt!(contract.flashLoan(receiver_addr, contract_addr, amount, vec![].into()))?), + ]; + + receipts + .into_iter() + .map(FunctionReport::new) + .collect::>>() +} + +async fn deploy( + account: &Account, + cache_opt: CacheOpt, +) -> eyre::Result
{ + let args = Erc20FlashMintExample::constructorCall { + flashFeeAmount_: FLASH_FEE_AMOUNT, + flashFeeReceiverAddress_: FEE_RECEIVER, + }; + let args = alloy::hex::encode(args.abi_encode()); + crate::deploy(account, "erc20-flash-mint", Some(args), cache_opt).await +} + +// async fn deploy_receiver( +// account: &Account, +// cache_opt: CacheOpt, +// ) -> eyre::Result
{ +// let args = ERC3156FlashBorrowerMock::constructorCall { +// enableApprove: true, +// enableReturn: true, +// }; +// let args = alloy::hex::encode(args.abi_encode()); +// crate::deploy(account, "erc3156-flash-borrower-mock", Some(args), +// cache_opt) .await +// } diff --git a/benches/src/lib.rs b/benches/src/lib.rs index bdad0189c..cd2306c17 100644 --- a/benches/src/lib.rs +++ b/benches/src/lib.rs @@ -18,6 +18,7 @@ pub mod erc1155; pub mod erc1155_metadata_uri; pub mod erc1155_supply; pub mod erc20; +pub mod erc20_flash_mint; pub mod erc721; pub mod merkle_proofs; pub mod ownable; diff --git a/benches/src/main.rs b/benches/src/main.rs index ba06fd578..78b6dd3b5 100644 --- a/benches/src/main.rs +++ b/benches/src/main.rs @@ -1,6 +1,6 @@ use benches::{ - access_control, erc1155, erc1155_metadata_uri, erc20, erc721, - merkle_proofs, ownable, report::BenchmarkReport, + access_control, erc1155, erc1155_metadata_uri, erc20, erc20_flash_mint, + erc721, merkle_proofs, ownable, report::BenchmarkReport, }; use futures::FutureExt; use itertools::Itertools; @@ -10,6 +10,7 @@ async fn main() -> eyre::Result<()> { let benchmarks = [ access_control::bench().boxed(), erc20::bench().boxed(), + erc20_flash_mint::bench().boxed(), erc721::bench().boxed(), merkle_proofs::bench().boxed(), ownable::bench().boxed(),