0

I need to retrieve data to front end from event:

eventSignature: 'ProposalIsCreated(bytes32,address,address,address,string,uint256,uint256,bytes32))'

I use defaultAbiCoder from ethers.js and this function throws an error, probably because I use array syntax, but I have no other ideas how to represent struct and make it work:

ethers.utils.defaultAbiCoder.decode(
    [
      "bytes32",
      "address",
      ["address", "address", "string", "uint256", "uint256", "bytes32"],
    ],
    event.data
  );

Data example:

"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000a0bf3617915aec7a04b5fb59ee63c9df1975b677000000000000000000000000ec5596dffbb8ca06d83532bc4428bbae4ca1b76e00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001692048e9a8062420fae2d6ea18d56a76e4c5f5b6255e9047b8ec624ffdbd15f200000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000"

1 Answer 1

2

you can do it this way your

//rpc
const connection = new ethers.providers.JsonRpcProvider('https://rpc-mumbai.maticvigil.com/', { chainId: 80001 });

//set address
let contractAddress = "address";
   //set private-key
   const privateKey="key";
 
 //connect wallet
 var signer = new ethers.Wallet(privateKey, connection)
 //create signer
 const txSigner = contract.connect(signer);

 //instance of struct
 const Ranger = "(uint256 idNumber,uint256 dateOfBirth,string lastName,string userAddress,string gender,string  firstName,string middleName,uint256 accountNumber,bool isActive,uint256 phone; )";

//human readable abi
const ABI = [
 `function addRanger(uint256 _id,string _userAdd,string  _gender,uint256 _dob,uint _phone,string _middleName,string _firstName,string _lastName,uint256  _account) public`,
`function getAllUsers() public`,
`event AddedRanger(${Ranger}  created)`];

 //create contract instance
const contract = new ethers.Contract(contractAddress, ABI, connection);

 contract.on("AddedRanger",(Ranger,event) => {
        console.log({
            value: Ranger.toString(),
            data: event
        });
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.