[web3/web3.js]getPastLogs 缺少日志

2023-12-09 2 views
7
import Web3 from 'web3'; // 4.1.2

const url = ''; // your rpc url
const web3 = new Web3(url);
const syncEvent = web3.utils.sha3('Sync(uint112,uint112)');

async function getSync({ transactionHash, chain, logIndex, address, blockNumber }) {
    try {
        const logs = await web3.eth.getPastLogs({
            fromBlock: blockNumber,
            toBlock: blockNumber,
            address: address, // even without this parameter it fails
            topics: [
                [syncEvent]
            ]
        });
        // find the specific syncEvent = swapEvent logIndex  -1
        const sync = logs.find(d => d.address.toLowerCase() === address.toLowerCase() && parseInt(d.logIndex) === (logIndex--)); 
        const decoded = web3.eth.abi.decodeParameters(['uint112','uint112'], sync.data);
        return { reserve0: decoded[0], reserve1: decoded[1] };
    } catch (err){
        console.log(err)
    };
}

const sync = await getSync({ 
    transactionHash: '0xa4a2709219742f4152888a6e9bf64fa225d32cf4a48b00be0c288b0cbb277f31', 
    chain: 'ETH', 
    logIndex: 215, 
    address: '0xef9ef6e07602e1e0419a5788f1d85e0698eab077', 
    blockNumber: 18484534 
});

console.log(sync)

const receipt = await web3.eth.getTransactionReceipt('0xa4a2709219742f4152888a6e9bf64fa225d32cf4a48b00be0c288b0cbb277f31');
console.log(receipt)

上面的代码将无法从 getPastLogs 获取同步事件,但是收据中会显示我们要查找的特定日志确实存在于收据中。

我认为如果日志存在于收据中,它应该显示在 getPastLogs 中。

回答

3

我尝试重现该问题,但似乎效果很好:以下是示例:

  1. 我已经创建了一个包含类型为 uint112 的事件的合约
    
    // SPDX-License-Identifier: GNU

杂注可靠性^0.8.16;

合约 Basic2 { uint112 intValue; 字符串字符串值;布尔布尔值;

event StringEvent(string str);
event MultiValueEvent(string str, uint112 val, bool flag);
event MultiValueIndexedEvent(string str, uint112 indexed val, bool indexed flag);
event MultiValueIndexedEventWithStringIndexed(
    string indexed str,
    uint112 indexed val,
    bool indexed flag
);

constructor(uint112 _val, string memory _stringValue) {
    intValue = _val;
    stringValue = _stringValue;
}

function getStringValue() public view returns (string memory) {
    return stringValue;
}

function getIntValue() public view returns (uint112) {
    return intValue;
}

function getBoolValue() public view returns (bool) {
    return boolValue;
}

function getValues()
    public
    view
    returns (
        uint112,
        string memory,
        bool
    )
{
    return (intValue, stringValue, boolValue);
}

function setValues(
    uint112 _value,
    string memory _stringValue,
    bool _boolValue
) public {
    intValue = _value;
    stringValue = _stringValue;
    boolValue = _boolValue;
}

function requireWithoutReason() public pure {
    require(false);
}

function requireWithReason() public pure {
    require(false, 'REVERTED WITH REQUIRE');
}

function reverts() public pure {
    revert('REVERTED WITH REVERT');
}

function firesMultiValueEvent(
    string memory str,
    uint112 val,
    bool flag
) public {
    emit MultiValueEvent(str, val, flag);
}

function firesMultiValueIndexedEvent(
    string memory str,
    uint112 val,
    bool flag
) public {
    emit MultiValueIndexedEvent(str, val, flag);
}

function firesStringEvent(string memory _str) public {
    emit StringEvent(_str);
}

function firesMultiValueIndexedEventWithStringIndexed(
    string calldata str,
    uint112 val,
    bool flag
) public {
    emit MultiValueIndexedEventWithStringIndexed(str, val, flag);
}

}


2. compile it:
```typescript
export const Basic2Abi = [
    {
        inputs: [
            { internalType: 'uint112', name: '_val', type: 'uint112' },
            { internalType: 'string', name: '_stringValue', type: 'string' },
        ],
        stateMutability: 'nonpayable',
        type: 'constructor',
    },
    {
        anonymous: false,
        inputs: [
            { indexed: false, internalType: 'string', name: 'str', type: 'string' },
            { indexed: false, internalType: 'uint112', name: 'val', type: 'uint112' },
            { indexed: false, internalType: 'bool', name: 'flag', type: 'bool' },
        ],
        name: 'MultiValueEvent',
        type: 'event',
    },
    {
        anonymous: false,
        inputs: [
            { indexed: false, internalType: 'string', name: 'str', type: 'string' },
            { indexed: true, internalType: 'uint112', name: 'val', type: 'uint112' },
            { indexed: true, internalType: 'bool', name: 'flag', type: 'bool' },
        ],
        name: 'MultiValueIndexedEvent',
        type: 'event',
    },
    {
        anonymous: false,
        inputs: [
            { indexed: true, internalType: 'string', name: 'str', type: 'string' },
            { indexed: true, internalType: 'uint112', name: 'val', type: 'uint112' },
            { indexed: true, internalType: 'bool', name: 'flag', type: 'bool' },
        ],
        name: 'MultiValueIndexedEventWithStringIndexed',
        type: 'event',
    },
    {
        anonymous: false,
        inputs: [{ indexed: false, internalType: 'string', name: 'str', type: 'string' }],
        name: 'StringEvent',
        type: 'event',
    },
    {
        inputs: [
            { internalType: 'string', name: 'str', type: 'string' },
            { internalType: 'uint112', name: 'val', type: 'uint112' },
            { internalType: 'bool', name: 'flag', type: 'bool' },
        ],
        name: 'firesMultiValueEvent',
        outputs: [],
        stateMutability: 'nonpayable',
        type: 'function',
    },
    {
        inputs: [
            { internalType: 'string', name: 'str', type: 'string' },
            { internalType: 'uint112', name: 'val', type: 'uint112' },
            { internalType: 'bool', name: 'flag', type: 'bool' },
        ],
        name: 'firesMultiValueIndexedEvent',
        outputs: [],
        stateMutability: 'nonpayable',
        type: 'function',
    },
    {
        inputs: [
            { internalType: 'string', name: 'str', type: 'string' },
            { internalType: 'uint112', name: 'val', type: 'uint112' },
            { internalType: 'bool', name: 'flag', type: 'bool' },
        ],
        name: 'firesMultiValueIndexedEventWithStringIndexed',
        outputs: [],
        stateMutability: 'nonpayable',
        type: 'function',
    },
    {
        inputs: [{ internalType: 'string', name: '_str', type: 'string' }],
        name: 'firesStringEvent',
        outputs: [],
        stateMutability: 'nonpayable',
        type: 'function',
    },
    {
        inputs: [],
        name: 'getBoolValue',
        outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
        stateMutability: 'view',
        type: 'function',
    },
    {
        inputs: [],
        name: 'getIntValue',
        outputs: [{ internalType: 'uint112', name: '', type: 'uint112' }],
        stateMutability: 'view',
        type: 'function',
    },
    {
        inputs: [],
        name: 'getStringValue',
        outputs: [{ internalType: 'string', name: '', type: 'string' }],
        stateMutability: 'view',
        type: 'function',
    },
    {
        inputs: [],
        name: 'getValues',
        outputs: [
            { internalType: 'uint112', name: '', type: 'uint112' },
            { internalType: 'string', name: '', type: 'string' },
            { internalType: 'bool', name: '', type: 'bool' },
        ],
        stateMutability: 'view',
        type: 'function',
    },
    {
        inputs: [],
        name: 'requireWithReason',
        outputs: [],
        stateMutability: 'pure',
        type: 'function',
    },
    {
        inputs: [],
        name: 'requireWithoutReason',
        outputs: [],
        stateMutability: 'pure',
        type: 'function',
    },
    { inputs: [], name: 'reverts', outputs: [], stateMutability: 'pure', type: 'function' },
    {
        inputs: [
            { internalType: 'uint112', name: '_value', type: 'uint112' },
            { internalType: 'string', name: '_stringValue', type: 'string' },
            { internalType: 'bool', name: '_boolValue', type: 'bool' },
        ],
        name: 'setValues',
        outputs: [],
        stateMutability: 'nonpayable',
        type: 'function',
    },
] as const;
export const Basic2Bytecode =
    '0x60806040523480156200001157600080fd5b506040516200151838038062001518833981810160405281019062000037919062000263565b816000806101000a8154816dffffffffffffffffffffffffffff02191690836dffffffffffffffffffffffffffff16021790555080600190816200007c919062000514565b505050620005fb565b6000604051905090565b600080fd5b600080fd5b60006dffffffffffffffffffffffffffff82169050919050565b620000be8162000099565b8114620000ca57600080fd5b50565b600081519050620000de81620000b3565b92915050565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6200013982620000ee565b810181811067ffffffffffffffff821117156200015b576200015a620000ff565b5b80604052505050565b60006200017062000085565b90506200017e82826200012e565b919050565b600067ffffffffffffffff821115620001a157620001a0620000ff565b5b620001ac82620000ee565b9050602081019050919050565b60005b83811015620001d9578082015181840152602081019050620001bc565b60008484015250505050565b6000620001fc620001f68462000183565b62000164565b9050828152602081018484840111156200021b576200021a620000e9565b5b62000228848285620001b9565b509392505050565b600082601f830112620002485762000247620000e4565b5b81516200025a848260208601620001e5565b91505092915050565b600080604083850312156200027d576200027c6200008f565b5b60006200028d85828601620000cd565b925050602083015167ffffffffffffffff811115620002b157620002b062000094565b5b620002bf8582860162000230565b9150509250929050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200031c57607f821691505b602082108103620003325762000331620002d4565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026200039c7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826200035d565b620003a886836200035d565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b6000620003f5620003ef620003e984620003c0565b620003ca565b620003c0565b9050919050565b6000819050919050565b6200041183620003d4565b620004296200042082620003fc565b8484546200036a565b825550505050565b600090565b6200044062000431565b6200044d81848462000406565b505050565b5b8181101562000475576200046960008262000436565b60018101905062000453565b5050565b601f821115620004c4576200048e8162000338565b62000499846200034d565b81016020851015620004a9578190505b620004c1620004b8856200034d565b83018262000452565b50505b505050565b600082821c905092915050565b6000620004e960001984600802620004c9565b1980831691505092915050565b6000620005048383620004d6565b9150826002028217905092915050565b6200051f82620002c9565b67ffffffffffffffff8111156200053b576200053a620000ff565b5b62000547825462000303565b6200055482828562000479565b600060209050601f8311600181146200058c576000841562000577578287015190505b620005838582620004f6565b865550620005f3565b601f1984166200059c8662000338565b60005b82811015620005c6578489015182556001820191506020850194506020810190506200059f565b86831015620005e65784890151620005e2601f891682620004d6565b8355505b6001600288020188555050505b505050505050565b610f0d806200060b6000396000f3fe608060405234801561001057600080fd5b50600436106100b45760003560e01c8063844d6a3211610071578063844d6a3214610157578063986c189a14610161578063a34e65b31461017d578063ae012ede14610199578063b7a8238a146101a3578063e8256ea0146101bf576100b4565b806319eb4a90146100b95780632bfc4c69146100d95780633bccbbc9146100f757806351ef13781461010157806352244cfb1461011d578063828909bd14610139575b600080fd5b6100c16101dd565b6040516100d09392919061065e565b60405180910390f35b6100e16102a8565b6040516100ee919061069c565b60405180910390f35b6100ff61033a565b005b61011b6004803603810190610116919061085f565b610375565b005b6101376004803603810190610132919061092e565b6103b5565b005b610141610413565b60405161014e91906109a2565b60405180910390f35b61015f610436565b005b61017b600480360381019061017691906109bd565b610479565b005b6101976004803603810190610192919061085f565b6104dc565b005b6101a161052c565b005b6101bd60048036038101906101b89190610a2c565b610539565b005b6101c7610573565b6040516101d49190610a75565b60405180910390f35b6000606060008060009054906101000a90046dffffffffffffffffffffffffffff166001600260009054906101000a900460ff1681805461021d90610abf565b80601f016020809104026020016040519081016040528092919081815260200182805461024990610abf565b80156102965780601f1061026b57610100808354040283529160200191610296565b820191906000526020600020905b81548152906001019060200180831161027957829003601f168201915b50505050509150925092509250909192565b6060600180546102b790610abf565b80601f01602080910402602001604051908101604052809291908181526020018280546102e390610abf565b80156103305780601f1061030557610100808354040283529160200191610330565b820191906000526020600020905b81548152906001019060200180831161031357829003601f168201915b5050505050905090565b6040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161036c90610b3c565b60405180910390fd5b7f93956dc96ca430ae0ad7e3b43e1bb9cbb04e72b53dab0bdd2b168ee276c21c7a8383836040516103a893929190610b5c565b60405180910390a1505050565b801515826dffffffffffffffffffffffffffff1685856040516103d9929190610bca565b60405180910390207fd67f6c7f45f6103f234c72956b7a16f29628058ac0b5ee09a8d6b892f819374960405160405180910390a450505050565b60008060009054906101000a90046dffffffffffffffffffffffffffff16905090565b6000610477576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161046e90610c2f565b60405180910390fd5b565b826000806101000a8154816dffffffffffffffffffffffffffff02191690836dffffffffffffffffffffffffffff16021790555081600190816104bc9190610e05565b5080600260006101000a81548160ff021916908315150217905550505050565b801515826dffffffffffffffffffffffffffff167fa51d8baf1342c2755103fc3e44254acc0242e28a3153a27f6718fffb1a709e228560405161051f919061069c565b60405180910390a3505050565b600061053757600080fd5b565b7f617cf8a4400dd7963ed519ebe655a16e8da1282bb8fea36a21f634af912f54ab81604051610568919061069c565b60405180910390a150565b6000600260009054906101000a900460ff16905090565b60006dffffffffffffffffffffffffffff82169050919050565b6105ad8161058a565b82525050565b600081519050919050565b600082825260208201905092915050565b60005b838110156105ed5780820151818401526020810190506105d2565b60008484015250505050565b6000601f19601f8301169050919050565b6000610615826105b3565b61061f81856105be565b935061062f8185602086016105cf565b610638816105f9565b840191505092915050565b60008115159050919050565b61065881610643565b82525050565b600060608201905061067360008301866105a4565b8181036020830152610685818561060a565b9050610694604083018461064f565b949350505050565b600060208201905081810360008301526106b6818461060a565b905092915050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b610714826105f9565b810181811067ffffffffffffffff82111715610733576107326106dc565b5b80604052505050565b60006107466106be565b9050610752828261070b565b919050565b600067ffffffffffffffff821115610772576107716106dc565b5b61077b826105f9565b9050602081019050919050565b82818337600083830152505050565b60006107aa6107a584610757565b61073c565b9050828152602081018484840111156107c6576107c56106d7565b5b6107d1848285610788565b509392505050565b600082601f8301126107ee576107ed6106d2565b5b81356107fe848260208601610797565b91505092915050565b6108108161058a565b811461081b57600080fd5b50565b60008135905061082d81610807565b92915050565b61083c81610643565b811461084757600080fd5b50565b60008135905061085981610833565b92915050565b600080600060608486031215610878576108776106c8565b5b600084013567ffffffffffffffff811115610896576108956106cd565b5b6108a2868287016107d9565b93505060206108b38682870161081e565b92505060406108c48682870161084a565b9150509250925092565b600080fd5b600080fd5b60008083601f8401126108ee576108ed6106d2565b5b8235905067ffffffffffffffff81111561090b5761090a6108ce565b5b602083019150836001820283011115610927576109266108d3565b5b9250929050565b60008060008060608587031215610948576109476106c8565b5b600085013567ffffffffffffffff811115610966576109656106cd565b5b610972878288016108d8565b945094505060206109858782880161081e565b92505060406109968782880161084a565b91505092959194509250565b60006020820190506109b760008301846105a4565b92915050565b6000806000606084860312156109d6576109d56106c8565b5b60006109e48682870161081e565b935050602084013567ffffffffffffffff811115610a0557610a046106cd565b5b610a11868287016107d9565b9250506040610a228682870161084a565b9150509250925092565b600060208284031215610a4257610a416106c8565b5b600082013567ffffffffffffffff811115610a6057610a5f6106cd565b5b610a6c848285016107d9565b91505092915050565b6000602082019050610a8a600083018461064f565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680610ad757607f821691505b602082108103610aea57610ae9610a90565b5b50919050565b7f5245564552544544205749544820524556455254000000000000000000000000600082015250565b6000610b266014836105be565b9150610b3182610af0565b602082019050919050565b60006020820190508181036000830152610b5581610b19565b9050919050565b60006060820190508181036000830152610b76818661060a565b9050610b8560208301856105a4565b610b92604083018461064f565b949350505050565b600081905092915050565b6000610bb18385610b9a565b9350610bbe838584610788565b82840190509392505050565b6000610bd7828486610ba5565b91508190509392505050565b7f5245564552544544205749544820524551554952450000000000000000000000600082015250565b6000610c196015836105be565b9150610c2482610be3565b602082019050919050565b60006020820190508181036000830152610c4881610c0c565b9050919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302610cb17fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82610c74565b610cbb8683610c74565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b6000610d02610cfd610cf884610cd3565b610cdd565b610cd3565b9050919050565b6000819050919050565b610d1c83610ce7565b610d30610d2882610d09565b848454610c81565b825550505050565b600090565b610d45610d38565b610d50818484610d13565b505050565b5b81811015610d7457610d69600082610d3d565b600181019050610d56565b5050565b601f821115610db957610d8a81610c4f565b610d9384610c64565b81016020851015610da2578190505b610db6610dae85610c64565b830182610d55565b50505b505050565b600082821c905092915050565b6000610ddc60001984600802610dbe565b1980831691505092915050565b6000610df58383610dcb565b9150826002028217905092915050565b610e0e826105b3565b67ffffffffffffffff811115610e2757610e266106dc565b5b610e318254610abf565b610e3c828285610d78565b600060209050601f831160018114610e6f5760008415610e5d578287015190505b610e678582610de9565b865550610ecf565b601f198416610e7d86610c4f565b60005b82811015610ea557848901518255600182019150602085019450602081019050610e80565b86831015610ec25784890151610ebe601f891682610dcb565b8355505b6001600288020188555050505b50505050505056fea2646970667358221220f02d25981ec4e151954fc39affbf306481dae9dbbd9acde1eee43c8967af6f5a64736f6c63430008100033';
  1. 运行此代码来检查:

    
    const contract = new Contract(Basic2Abi, undefined, {
                provider: clientUrl,
            });
    
            contractDeployed = await contract
                .deploy({
                    data: Basic2Bytecode,
                    arguments: [10, 'string init value'],
                })
                .send({ from: tempAcc.address, gas: '1000000' });
    
            const receipt = await contractDeployed.methods
                ?.firesMultiValueEvent('some text here', 4567, true)
                .send(sendOptions);
    
            console.log('receipt logs', receipt.logs);
            // receipt logs [
            //     {
            //         address: '0xd132113e556165789f48def6843ead6bb7733d8d',
            //         blockHash: '0xf6c6a79ca973cb8ea12ced2d808c508ac2c21a98b15fa00937a19b73d113b03b',
            //         blockNumber: 57n,
            //         data: '0x000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000011d70000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000e736f6d6520746578742068657265000000000000000000000000000000000000',
            //         logIndex: 0n,
            //         removed: false,
            //         topics: [
            //             '0x93956dc96ca430ae0ad7e3b43e1bb9cbb04e72b53dab0bdd2b168ee276c21c7a'
            //         ],
            //         transactionHash: '0x0a633886d3363e91b4c30a80f2381e337f2bfb09150b73f527765531fd833d8b',
            //         transactionIndex: 0n
            //     }
            // ]
    
            const someEvent = String(sha3('MultiValueEvent(string,uint112,bool)'));
            console.log('someEvent', someEvent);
            // someEvent 0x93956dc96ca430ae0ad7e3b43e1bb9cbb04e72b53dab0bdd2b168ee276c21c7a
    
            const res: Array<any> = await web3Eth.getPastLogs({
                address: contractDeployed.options.address as string,
                topics: [someEvent],
            });
    
            const decoded = decodeParameters(['string', 'uint112', 'bool'], res[0].data);
            console.log('decoded', decoded);
            // decoded { '0': 'some text here', '1': 4567n, '2': true, __length__: 3 }


So, everything works correctly. 
I think the problem in your code is this condition `parseInt(d.logIndex) === (logIndex--)`
Could you please debug and check what is the value of `const logs = await web3.eth.getPastLogs(...)` in your code?
@Sv3nskie
5

我的第一篇文章包含应该重现问题的示例代码。

logIndex--只是为了从 uniswap v2 池上的交换事件中查找同步事件。

5

在你的情况下,它是不正确的 blockNumber。

您的交易哈希值https://etherscan.io/tx/0xa4a2709219742f4152888a6e9bf64fa225d32cf4a48b00be0c288b0cbb277f31是 blockNumber = 18484533n

如果你只设置这个块号 - 一切都会正常

我还删除了这段代码parseInt(d.logIndex) === (logIndex--)

async function getSync({ logIndex, address, blockNumber }) {
        try {
            const logs: Array<any> = await web3.eth.getPastLogs({
                fromBlock: blockNumber,
                toBlock: blockNumber,
                address: address, // even without this parameter it fails
                // @ts-ignore
                topics: [syncEvent],
            });

            const sync = logs.find(d => d.address.toLowerCase() === address.toLowerCase());
            console.log('sync',sync)
            // sync {
            //     address: '0xef9ef6e07602e1e0419a5788f1d85e0698eab077',
            //         blockHash: '0x0920227a73f293bc41ea41754737cf0b2b9ec002fbeeca12e381f9b75fd5a70d',
            //         blockNumber: 18484533n,
            //         data: '0x0000000000000000000000000000000000000000003d1c614516bd8c19b7173a000000000000000000000000000000000000000000000004b65d765cb10ace66',
            //         logIndex: 214n,
            //         removed: false,
            //         topics: [
            //         '0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1'
            //     ],
            //         transactionHash: '0xa4a2709219742f4152888a6e9bf64fa225d32cf4a48b00be0c288b0cbb277f31',
            //         transactionIndex: 108n
            // }
            const decoded = decodeParameters(['uint112', 'uint112'], sync!.data);
            return { reserve0: decoded[0], reserve1: decoded[1] };
            // sync {
            //     reserve0: 73878495570562727078729530n,
            //     reserve1: 86927765723054526054n
            // }
        } catch (err) {
            console.log(err);
            return undefined;
        }
    }
    const sync = await getSync({
        logIndex: 215,
        address: '0xef9ef6e07602e1e0419a5788f1d85e0698eab077',
        blockNumber: 18484533
    });