Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexed parameters are shifted by one when decoding logs #2368

Closed
mistersandman opened this issue Feb 10, 2019 · 7 comments · Fixed by #2391
Closed

indexed parameters are shifted by one when decoding logs #2368

mistersandman opened this issue Feb 10, 2019 · 7 comments · Fixed by #2391
Labels
Bug Addressing a bug

Comments

@mistersandman
Copy link

Expected behavior

decodeLog in AbiCoder.js (here) should return a map from all event parameter names to their decoded data with respect to the type specified in the ABI.

Actual behavior

The data used to decode indexed parameters of events is shifted by one topic (i.e. the first indexed parameter uses the data from topics[0] when it should in fact use topics[1]). Note that the first topic (topics[0]) is always defined to be the hash of the event signature.

Steps to reproduce the behavior

Use getPastEvents to query for an event with at least one indexed parameter and compare the result value of the event with the raw value.

Error Logs

no error

Versions

web3.js 1.0.0-beta.46

@nivida nivida added Bug Addressing a bug Needs Clarification Requires additional input labels Feb 11, 2019
@mistersandman
Copy link
Author

mistersandman commented Feb 11, 2019

Small example to reproduce, querying past Transfer events from block 7205796 of the Wrapped ETH (WETH) contract:

const Web3 = require('web3');

let web3 = new Web3('https://mainnet.infura.io/metamask');

let wethAbi = [{ "anonymous": false, "inputs": [{ "indexed": true, "name": "src", "type": "address" }, { "indexed": true, "name": "dst", "type": "address" }, { "indexed": false, "name": "wad", "type": "uint256" }], "name": "Transfer", "type": "event" }];
let wethAddress = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
let wethContract = new web3.eth.Contract(wethAbi, wethAddress);

wethContract.getPastEvents('Transfer', { fromBlock: 7205796, toBlock: 7205796 }).then((events) => {
    console.log(events[0].transactionHash);
    console.log(events[0].returnValues.src);
    console.log(events[0].returnValues.dst);
    console.log(events[0].raw.topics);
});

The output is:

0x72cbb7c4f9c3990b2f7d3351a8ea733fed9681c0f09169b65400a571089568e9 // tx hash
0xFC378dAA952ba7f163c4a11628f55a4df523b3EF // src address
0x260e92D7b25c9315d068b2c3Ca5f234D0Cf9e505 // dst address
0: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" // topics array with 3 entries
1: "0x000000000000000000000000260e92d7b25c9315d068b2c3ca5f234d0cf9e505"
2: "0x0000000000000000000000000681e844593a051e2882ec897ecd5444efe19ff2"

Compare this with the transfer information on Etherscan here. The transfer goes from 260e92d7b25c9315d068b2c3ca5f234d0cf9e505 to 0681e844593a051e2882ec897ecd5444efe19ff2, however the returnValues of the event gives the source address as destination address and 0xFC378dAA952ba7f163c4a11628f55a4df523b3EF as the source address, which is the last 256 bits of events[0].raw.topics[0]. Note that source and destination address parameters in the Transfer event are indexed. Decoding indexed parameter arguments erroneously starts with the first entry in the topics array. It should start with the second entry, since the first entry is the hash of the event signature.

@pixelmatrix
Copy link

I'm seeing something similar with beta.46.

When I subscribe to Transfer events, i'm seeing the to and from values being swapped. The event signature is:

event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);

The raw event log is:

topics: [
  "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
  "0x000000000000000000000000c20c5ccb44e6092c712a1b895df6d72ea53b40f5",
  "0x000000000000000000000000f30c09bc05d3d4c5b4070c989cc7d268265d0a3c",
  "0x2a057186f17ec59a722759e94470b239925c609effd8e5936702b0729f2cfaaa"
]

Decoded return values:

returnValues: {
  0: "0xFC378dAA952ba7f163c4a11628f55a4df523b3EF"
  1: "0xc20c5CCB44E6092C712A1B895DF6D72eA53B40F5"
  2: "1387553214243979768995578582844225663590104631868"
  from: "0xFC378dAA952ba7f163c4a11628f55a4df523b3EF"
  to: "0xc20c5CCB44E6092C712A1B895DF6D72eA53B40F5"
  tokenId: "1387553214243979768995578582844225663590104631868"
}

@chiro-hiro
Copy link

chiro-hiro commented Feb 18, 2019

I have the same issue:

//New offer
event NewOffer(
    address indexed maker,
    uint256 indexed offerId,
    address indexed tokenAddress,
    uint256 offerType,
    uint128 startDate,
    uint128 endDate,
    uint256 amount,
    uint256 price,
    uint256 minimum
);

Expected result:

{
    '0': '0x12583F7627099D8C89A68112C15B83628E72CBE7',
    '1': '0',
    '2': '0x5562779B00A8Ad6344586E23F940d9e4E202F849',
    '3': '13',
    '4': '1550123168',
    '5': '2414123168',
    '6': '1000000',
    '7': '1000',
    '8': '100',
    maker: '0x12583F7627099D8C89A68112C15B83628E72CBE7',
    offerId: '0',
    tokenAddress: '0x5562779B00A8Ad6344586E23F940d9e4E202F849',
    offerType: '13',
    startDate: '1550123168',
    endDate: '2414123168',
    amount: '1000000',
    price: '1000',
    minimum: '100'
}

Which was I received:

{
    '0': '0x5562779B00A8Ad6344586E23F940d9e4E202F849',
    '1': '104729827731522621819118954356228975471770127335',
    '2': '0x0000000000000000000000000000000000000000',
    '3': '13',
    '4': '1550123168',
    '5': '2414123168',
    '6': '1000000',
    '7': '1000',
    '8': '100',
    maker: '0x5562779B00A8Ad6344586E23F940d9e4E202F849',
    offerId: '104729827731522621819118954356228975471770127335',
    tokenAddress: '0x0000000000000000000000000000000000000000',
    offerType: '13',
    startDate: '1550123168',
    endDate: '2414123168',
    amount: '1000000',
    price: '1000',
    minimum: '100'
}

To me, it was happened to both 1.0.0beta45 and 1.0.0beta46.

@nivida
Copy link
Contributor

nivida commented Feb 18, 2019

Thanks all for the detailed information! I will fix and release it asap.

@nivida nivida removed the Needs Clarification Requires additional input label Feb 18, 2019
@nivida nivida mentioned this issue Feb 19, 2019
12 tasks
@kevholder
Copy link

I think I'm still seeing this error in web3 1.2.6

const data = '0x000000000000000000000000000000000000000000000004eee0f1dc808c0000';
const inputs = [ { indexed: true, name: 'user', type: 'address' },
  { indexed: false, name: 'amount', type: 'uint256' },
  { indexed: true, name: 'depositIndex', type: 'uint256' } ];
const topics = [ 'd9acabe6e09d178728ba5c366661c5be0621b4770f216305b059ec175b37e0b4',
  '000000000000000000000000a8a07f09def5e6a4462df90068c11abf6224e865',
  '000000000000000000000000000000000000000000000000000000000000000f' ];
const res = web3.eth.abi.decodeLog(inputs, data, topics);
console.log(res);
/*
Result {
  '0': '0x6661C5bE0621b4770F216305B059EC175B37e0B4',
  '1': '91000000000000000000',
  '2': '962689635349389981377523555802656161741058402405',
  __length__: 3,
  user: '0x6661C5bE0621b4770F216305B059EC175B37e0B4',
  amount: '91000000000000000000',
  depositIndex: '962689635349389981377523555802656161741058402405' }
*/

// depositIndex should be a small integer...

@pseudonaut
Copy link

pseudonaut commented May 5, 2020

Error is still occuring for me as well on web3 1.2.6 using web3.eth.abi.decodeLog()...

0: "17"
1: "40736905663348235180449726993715947561663541068933497753637381325270891332134"
2: "1000000000000000000000"
3: "0x0000000000000000000000000000000000000011"
4: "0x6B175474E89094C44Da98b954EedeAC495271d0F"
5: "1250"
6: "5"
7: "1591287895"
8: "0"
9: "0"
length: 10
_amount: "1000000000000000000000"
_apr: "1250"
_asset: "0x6B175474E89094C44Da98b954EedeAC495271d0F"
_borrower: "0x0000000000000000000000000000000000000011"
_collateralPercentageRequirement: "0"
_communityID: "17"
_fee: "0"
_firstPaymentUnix: "1591287895"
_loanID: "40736905663348235180449726993715947561663541068933497753637381325270891332134"
_monthlyPayments: "5"

_loanID / _communityID / _borrower are the 3 indexed params

@pseudonaut
Copy link

I fixed above error by adjusting the order of logs sent into the decodeLog in 3rd param:

result.logs[0].topics[1],
result.logs[0].topics[2],
result.logs[0].topics[3],
result.logs[0].topics[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Addressing a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants