0

I am using expectEvent from OpenZeppelin's Test Helpers, and when running a Truffle test to check the event, it's all fine for address and integer fields, but have an issue for string type.

Given a smart contract with this event and function:

  event WithdrawERC20Token(
     string indexed tokenSymbol,
     address to,
     uint256 amount
  );

  function withdrawERC20(
     string calldata tokenSymbol,
     address to,
     uint256 amount
  ) external {
     //...
     emit WithdrawERC20Token(tokenSymbol, to, amount);
  }

And this test:

const { expectEvent } = require('@openzeppelin/test-helpers');

// ...

it('should withdraw ERC20 tokens', async () => {
   //
    const txReceipt = await swapManagerContract.withdrawERC20Token(
        'UNI', 
        investor1, 
        50
    );
    expectEvent(txReceipt, 'WithdrawERC20Token', { 
        tokenSymbol: 'UNI',
        to: investor1,
        amount: new BN(50),
    });
}

I get this output error:

  expected event argument 'tokenSymbol' to have value UNI but got 0xfba01d52a7cd84480d0573725899486a0b5e55c20ff45d6628874349375d1650
  + expected - actual

  -0xfba01d52a7cd84480d0573725899486a0b5e55c20ff45d6628874349375d1650
  +UNI

It looks like the tokenSymbol is stored in kind of hexa format in the EVM. I have tried to apply several conversions to the string 'UNI' (fromAscii, asciiToHex, etc) but nothing gets this string into '0xfba01d52a7cd84480d0573725899486a0b5e55c20ff45d6628874349375d1650'.

Any clue on how to check the string from the event with expectEvent function?

2
  • as your tokenSymbol is an unbounded indexed field, it doesn't goes directly into the data section of the receipt, it goes encoded according this specification docs.soliditylang.org/en/v0.8.3/… – diega Apr 7 at 21:55
  • I see. The indexed string is hashed using keccak256, so this can be used to compare the original value in the tests. Thanks for the hint! – Sergi Juanati Apr 8 at 8:49
0

As pointed out in the comments, the string is an unbounded indexed field and therefore stored using keccak256. This can be used the other way around to compare the value in the tests.

In other words, this change worked for me:

expectEvent(txReceipt, 'WithdrawERC20Token', { 
    tokenSymbol: web3.utils.keccak256('UNI'),
    to: investor1,
    amount: new BN(50),
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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