From 4b462e6ef2350171027e52baaa8d434a1dc250c4 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 9 May 2022 18:26:36 +0200 Subject: [PATCH] test(abigen): ensure structs in events work (#1235) * test(abigen): ensure structs in events work * chore: rename vars * chore: use ganache chain id * chore: disable ganache test on celo --- ethers-contract/tests/abigen.rs | 24 ++++++++ .../solidity-contracts/StructContract.json | 55 +++++++++++++++++++ .../solidity-contracts/StructContract.sol | 15 +++++ 3 files changed, 94 insertions(+) create mode 100644 ethers-contract/tests/solidity-contracts/StructContract.json create mode 100644 ethers-contract/tests/solidity-contracts/StructContract.sol diff --git a/ethers-contract/tests/abigen.rs b/ethers-contract/tests/abigen.rs index 1c5cd110..aef9f2f6 100644 --- a/ethers-contract/tests/abigen.rs +++ b/ethers-contract/tests/abigen.rs @@ -4,8 +4,11 @@ use ethers_contract::{abigen, EthCall, EthEvent}; use ethers_core::{ abi::{AbiDecode, AbiEncode, Address, Tokenizable}, types::{transaction::eip2718::TypedTransaction, Eip1559TransactionRequest, U256}, + utils::Ganache, }; +use ethers_middleware::SignerMiddleware; use ethers_providers::{MockProvider, Provider}; +use ethers_signers::{LocalWallet, Signer}; use ethers_solc::Solc; use std::{convert::TryFrom, sync::Arc}; @@ -566,3 +569,24 @@ fn can_handle_overloaded_events() { }; let _ev2 = ActionPaused2Filter { action: "action".to_string(), pause_state: false }; } + +#[tokio::test] +#[cfg(not(feature = "celo"))] +async fn can_send_struct_param() { + abigen!(StructContract, "./tests/solidity-contracts/StructContract.json"); + + let server = Ganache::default().spawn(); + let wallet: LocalWallet = server.keys()[0].clone().into(); + let provider = Provider::try_from(server.endpoint()).unwrap(); + let client = Arc::new(SignerMiddleware::new(provider, wallet.with_chain_id(1337u64))); + + let contract = StructContract::deploy(client, ()).unwrap().legacy().send().await.unwrap(); + + let point = Point { x: 1337u64.into(), y: 0u64.into() }; + let tx = contract.submit_point(point).legacy(); + let tx = tx.send().await.unwrap().await.unwrap().unwrap(); + assert_eq!(tx.logs.len(), 1); + + let logs: Vec = contract.event().from_block(0u64).query().await.unwrap(); + assert_eq!(logs.len(), 1); +} diff --git a/ethers-contract/tests/solidity-contracts/StructContract.json b/ethers-contract/tests/solidity-contracts/StructContract.json new file mode 100644 index 00000000..23679f81 --- /dev/null +++ b/ethers-contract/tests/solidity-contracts/StructContract.json @@ -0,0 +1,55 @@ +{ + "abi": [ + { + "anonymous": false, + "inputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "x", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "y", + "type": "uint256" + } + ], + "indexed": false, + "internalType": "struct MyContract.Point", + "name": "x", + "type": "tuple" + } + ], + "name": "NewPoint", + "type": "event" + }, + { + "inputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "x", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "y", + "type": "uint256" + } + ], + "internalType": "struct MyContract.Point", + "name": "_point", + "type": "tuple" + } + ], + "name": "submitPoint", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } + ], + "bin": "608060405234801561001057600080fd5b50610268806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063b3c67bb814610030575b600080fd5b61004a600480360381019061004591906101ac565b61004c565b005b7f0a10caaea7ac3c68834bca5fb5f42a10cb68bc3866ed86e6379d62bea6d591668160405161007b9190610217565b60405180910390a150565b6000604051905090565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6100e38261009a565b810181811067ffffffffffffffff82111715610102576101016100ab565b5b80604052505050565b6000610115610086565b905061012182826100da565b919050565b6000819050919050565b61013981610126565b811461014457600080fd5b50565b60008135905061015681610130565b92915050565b60006040828403121561017257610171610095565b5b61017c604061010b565b9050600061018c84828501610147565b60008301525060206101a084828501610147565b60208301525092915050565b6000604082840312156101c2576101c1610090565b5b60006101d08482850161015c565b91505092915050565b6101e281610126565b82525050565b6040820160008201516101fe60008501826101d9565b50602082015161021160208501826101d9565b50505050565b600060408201905061022c60008301846101e8565b9291505056fea2646970667358221220ca9c95aa57bcabfe8405dcd5d993ab0920ed1e768884cae607c1b47d5c127f9564736f6c634300080a0033" +} diff --git a/ethers-contract/tests/solidity-contracts/StructContract.sol b/ethers-contract/tests/solidity-contracts/StructContract.sol new file mode 100644 index 00000000..2a617991 --- /dev/null +++ b/ethers-contract/tests/solidity-contracts/StructContract.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity >=0.8.10; + +contract MyContract { + struct Point { + uint256 x; + uint256 y; + } + + event NewPoint(Point x); + + function submitPoint(Point memory _point) public { + emit NewPoint(_point); + } +} \ No newline at end of file