diff --git a/target_chains/ethereum/sdk/stylus/Cargo.lock b/target_chains/ethereum/sdk/stylus/Cargo.lock index fedc1e57f..e52fabf51 100644 --- a/target_chains/ethereum/sdk/stylus/Cargo.lock +++ b/target_chains/ethereum/sdk/stylus/Cargo.lock @@ -1628,6 +1628,7 @@ dependencies = [ "alloy-sol-types", "e2e", "eyre", + "keccak-const", "mini-alloc", "pyth-stylus", "stylus-sdk", diff --git a/target_chains/ethereum/sdk/stylus/benches/src/function_calls.rs b/target_chains/ethereum/sdk/stylus/benches/src/function_calls.rs index ca8d64397..fcc1d56a8 100644 --- a/target_chains/ethereum/sdk/stylus/benches/src/function_calls.rs +++ b/target_chains/ethereum/sdk/stylus/benches/src/function_calls.rs @@ -7,11 +7,9 @@ use alloy::{ sol, sol_types::{SolCall, SolConstructor}, }; -use e2e::{receipt, Account}; +use e2e::{receipt, Account, env}; -use crate::{ - env, report::{ContractReport, FunctionReport}, CacheOpt -}; +use crate::{ report::{ContractReport, FunctionReport}, CacheOpt}; sol!( #[sol(rpc)] diff --git a/target_chains/ethereum/sdk/stylus/benches/src/proxy_calls.rs b/target_chains/ethereum/sdk/stylus/benches/src/proxy_calls.rs index f391b807e..1e7187552 100644 --- a/target_chains/ethereum/sdk/stylus/benches/src/proxy_calls.rs +++ b/target_chains/ethereum/sdk/stylus/benches/src/proxy_calls.rs @@ -5,11 +5,11 @@ use alloy::{ providers::ProviderBuilder, sol, sol_types::{ SolCall, SolConstructor} }; -use e2e::{receipt, Account}; +use e2e::{receipt, Account, env}; use pyth_stylus::pyth::mock::create_price_feed_update_data_list; use crate::{ - env, report::{ContractReport, FunctionReport}, CacheOpt + report::{ContractReport, FunctionReport}, CacheOpt }; sol!( diff --git a/target_chains/ethereum/sdk/stylus/examples/function-calls/Cargo.toml b/target_chains/ethereum/sdk/stylus/examples/function-calls/Cargo.toml index a5a32566f..62159abfa 100644 --- a/target_chains/ethereum/sdk/stylus/examples/function-calls/Cargo.toml +++ b/target_chains/ethereum/sdk/stylus/examples/function-calls/Cargo.toml @@ -8,11 +8,11 @@ version.workspace = true [dependencies] pyth-stylus.workspace = true -alloy-primitives.workspace = true +alloy-primitives = { workspace = true, features = ["tiny-keccak"] } alloy-sol-types.workspace = true stylus-sdk.workspace = true mini-alloc.workspace = true - +keccak-const.workspace = true [dev-dependencies] alloy.workspace = true diff --git a/target_chains/ethereum/sdk/stylus/examples/function-calls/tests/function-calls.rs b/target_chains/ethereum/sdk/stylus/examples/function-calls/tests/function-calls.rs index c9d874fbd..fa6554168 100644 --- a/target_chains/ethereum/sdk/stylus/examples/function-calls/tests/function-calls.rs +++ b/target_chains/ethereum/sdk/stylus/examples/function-calls/tests/function-calls.rs @@ -1,35 +1,76 @@ #![cfg(feature = "e2e")] +use std::println; + use abi::FunctionCalls; use alloy::{ - primitives::{uint, Address, U256}, + primitives::{uint, U256}, sol, }; +use alloy_primitives::{address, Address, FixedBytes}; use e2e::{ receipt, send, watch, Account, EventExt, Panic, PanicCode, ReceiptExt, - Revert, + Revert,env }; -use eyre::Result; -// use crate::FunctionCallsExample::constructorCall; +use eyre::Result; +use crate::FunctionCallsExample::constructorCall; mod abi; sol!("src/constructor.sol"); -// // ============================================================================ -// // Integration Tests: Function Calls -// // ============================================================================ - -// #[e2e::test] -// async fn constructs(alice: Account) -> Result<()> { -// let contract_addr = alice -// .as_deployer() -// .with_default_constructor::() -// .deploy() -// .await? -// .address()?; -// // // assert_eq!(true, true); - -// Ok(()) -// } +fn str_to_address(input: &str) -> Address { + let mut address_bytes = [0u8; 20]; // Initialize a 20-byte array with zeros + + // Convert the input string to bytes and copy into the 20-byte array + let input_bytes = input.as_bytes(); + let len = input_bytes.len().min(20); // Take up to 20 bytes if input is longer + + address_bytes[..len].copy_from_slice(&input_bytes[..len]); + + Address::new(address_bytes) // Create an Address from the byte array +} + +impl Default for constructorCall { + fn default() -> Self { + ctr("ETH") + } +} + + +fn ctr(key_id: &str) -> constructorCall { + let pyth_addr = env("MOCK_PYTH_ADDRESS").unwrap(); + let address_addr = str_to_address(&pyth_addr); + println!("MOCK_PYTH_ADDRESS: {:?}", pyth_addr); + let id = keccak_const::Keccak256::new().update(key_id.as_bytes()).finalize().to_vec(); + let price_id = FixedBytes::<32>::from_slice(&id); + constructorCall { + _pythAddress: address_addr, + _priceId: price_id + } +} + +#[e2e::test] +async fn constructs(alice: Account) -> Result<()> { + // Deploy contract using `alice` account + let contract_addr = alice + .as_deployer() + .with_default_constructor::() // Assuming `constructorCall` is the constructor here + .deploy() + .await? + .address()?; + + // Instantiate the contract instance + let contract = FunctionCalls::new(contract_addr, &alice.wallet); + + // Perform basic checks to ensure the contract is correctly initialized + // Calling a sample function to validate deployment (e.g., `getPriceUnsafe` as a placeholder) + let price_result = contract.getPriceUnsafe().call().await; + + // Verify that the contract was deployed successfully and the function call does not revert + price_result.expect("The contract deployment or function call failed"); + //assert!(price_result.is_ok(), "The contract deployment or function call failed"); + + Ok(()) +} diff --git a/target_chains/ethereum/sdk/stylus/examples/proxy-calls/tests/abi/mod.rs b/target_chains/ethereum/sdk/stylus/examples/proxy-calls/tests/abi/mod.rs index 34ec44e34..b187cc724 100644 --- a/target_chains/ethereum/sdk/stylus/examples/proxy-calls/tests/abi/mod.rs +++ b/target_chains/ethereum/sdk/stylus/examples/proxy-calls/tests/abi/mod.rs @@ -3,62 +3,14 @@ use alloy::sol; sol!( #[sol(rpc)] - contract Pyth { - function approve(address to, uint256 tokenId) external; - #[derive(Debug)] - function balanceOf(address owner) external view returns (uint256 balance); - #[derive(Debug)] - function getApproved(uint256 tokenId) external view returns (address approved); - #[derive(Debug)] - function isApprovedForAll(address owner, address operator) external view returns (bool approved); - #[derive(Debug)] - function ownerOf(uint256 tokenId) external view returns (address ownerOf); - function safeTransferFrom(address from, address to, uint256 tokenId) external; - function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external; - function setApprovalForAll(address operator, bool approved) external; - function totalSupply() external view returns (uint256 totalSupply); - function transferFrom(address from, address to, uint256 tokenId) external; - - function mint(address to, uint256 tokenId) external; - function burn(uint256 tokenId) external; - - function paused() external view returns (bool paused); - function pause() external; - function unpause() external; - #[derive(Debug)] - function whenPaused() external view; - #[derive(Debug)] - function whenNotPaused() external view; - - #[derive(Debug)] - function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256 tokenId); - #[derive(Debug)] - function tokenByIndex(uint256 index) external view returns (uint256 tokenId); - - function supportsInterface(bytes4 interface_id) external view returns (bool supportsInterface); - - error ERC721IncorrectOwner(address sender, uint256 tokenId, address owner); - error ERC721InsufficientApproval(address operator, uint256 tokenId); - error ERC721InvalidApprover(address approver); - error ERC721InvalidOperator(address operator); - error ERC721InvalidOwner(address owner); - error ERC721InvalidReceiver(address receiver); - error ERC721InvalidSender(address sender); - error ERC721NonexistentToken(uint256 tokenId); - error ERC721OutOfBoundsIndex(address owner, uint256 index); - error ERC721EnumerableForbiddenBatchMint(); - error EnforcedPause(); - error ExpectedPause(); - - #[derive(Debug, PartialEq)] - event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); - #[derive(Debug, PartialEq)] - event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId); - #[derive(Debug, PartialEq)] - event ApprovalForAll(address indexed owner, address indexed operator, bool approved); - #[derive(Debug, PartialEq)] - event Paused(address account); - #[derive(Debug, PartialEq)] - event Unpaused(address account); + contract ProxyCalls { + function getPriceUnsafe(bytes32 id) external; + function getEmaPriceUnsafe(bytes32 id) external; + function getPriceNoOlderThan(bytes32 id, uint age) external; + function getEmaPriceNoOlderThan(bytes32 id, uint age) external; + function getUpdateFee(bytes[] calldata updateData) external returns (uint256); + function getValidTimePeriod() external; + function updatePriceFeeds(bytes[] calldata updateData) external payable; + function updatePriceFeedsIfNecessary(bytes[] calldata updateData, bytes32[] calldata priceIds, uint64[] calldata publishTimes) external payable; } ); \ No newline at end of file diff --git a/target_chains/ethereum/sdk/stylus/lib/e2e/src/lib.rs b/target_chains/ethereum/sdk/stylus/lib/e2e/src/lib.rs index 539f71aa7..5a89cc9aa 100644 --- a/target_chains/ethereum/sdk/stylus/lib/e2e/src/lib.rs +++ b/target_chains/ethereum/sdk/stylus/lib/e2e/src/lib.rs @@ -14,6 +14,12 @@ pub use error::{Panic, PanicCode, Revert}; pub use event::EventExt; pub use receipt::ReceiptExt; pub use system::{fund_account, provider, Provider, Wallet}; +use eyre::WrapErr; + +/// Load the `name` environment variable. +pub fn env(name: &str) -> eyre::Result { + std::env::var(name).wrap_err(format!("failed to load {name}")) +} /// This macro provides a shorthand for broadcasting the transaction to the /// network. @@ -90,3 +96,4 @@ macro_rules! receipt { $crate::send!($e)?.get_receipt().await }; } +