Skip to content

Commit

Permalink
test(abigen): ensure structs in events work (gakonst#1235)
Browse files Browse the repository at this point in the history
* test(abigen): ensure structs in events work

* chore: rename vars

* chore: use ganache chain id

* chore: disable ganache test on celo
  • Loading branch information
mattsse committed May 9, 2022
1 parent fd994d7 commit 4b462e6
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
24 changes: 24 additions & 0 deletions ethers-contract/tests/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<NewPointFilter> = contract.event().from_block(0u64).query().await.unwrap();
assert_eq!(logs.len(), 1);
}
55 changes: 55 additions & 0 deletions ethers-contract/tests/solidity-contracts/StructContract.json
Original file line number Diff line number Diff line change
@@ -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"
}
15 changes: 15 additions & 0 deletions ethers-contract/tests/solidity-contracts/StructContract.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4b462e6

Please sign in to comment.