체인의정석

web3의 getPastEvent가 있다면ethers에는 query filter가 있다. query filter 설명 본문

블록체인/Ethers & web3

web3의 getPastEvent가 있다면ethers에는 query filter가 있다. query filter 설명

체인의정석 2023. 5. 15. 15:46
728x90
반응형

web3의 getPastEvents를 ethers에서 하려면 "query filter"를 사용하면 된다고 한다.

이걸 못찾아서 노가다를 했었는데 앞으로 이걸써야지

https://docs.ethers.org/v5/api/contract/contract/#Contract-queryFilter

 

Contract

Documentation for ethers, a complete, tiny and simple Ethereum library.

docs.ethers.org

// Get the address of the Signer
myAddress = await signer.getAddress()
// '0x8ba1f109551bD432803012645Ac136ddd64DBA72'

// Filter for all token transfers from me
filterFrom = daiContract.filters.Transfer(myAddress, null);
// {
//   address: 'dai.tokens.ethers.eth',
//   topics: [
//     '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
//     '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72'
//   ]
// }

// Filter for all token transfers to me
filterTo = daiContract.filters.Transfer(null, myAddress);
// {
//   address: 'dai.tokens.ethers.eth',
//   topics: [
//     '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
//     null,
//     '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72'
//   ]
// }

// List all transfers sent from me in a specific block range
await daiContract.queryFilter(filterFrom, 9843470, 9843480)
// [
//   {
//     address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
//     args: [
//       '0x8ba1f109551bD432803012645Ac136ddd64DBA72',
//       '0x8B3765eDA5207fB21690874B722ae276B96260E0',
//       { BigNumber: "4750000000000000000" },
//       amount: { BigNumber: "4750000000000000000" },
//       from: '0x8ba1f109551bD432803012645Ac136ddd64DBA72',
//       to: '0x8B3765eDA5207fB21690874B722ae276B96260E0'
//     ],
//     blockHash: '0x8462eb2fbcef5aa4861266f59ad5f47b9aa6525d767d713920fdbdfb6b0c0b78',
//     blockNumber: 9843476,
//     data: '0x00000000000000000000000000000000000000000000000041eb63d55b1b0000',
//     decode: [Function (anonymous)],
//     event: 'Transfer',
//     eventSignature: 'Transfer(address,address,uint256)',
//     getBlock: [Function (anonymous)],
//     getTransaction: [Function (anonymous)],
//     getTransactionReceipt: [Function (anonymous)],
//     logIndex: 69,
//     removeListener: [Function (anonymous)],
//     removed: false,
//     topics: [
//       '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
//       '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72',
//       '0x0000000000000000000000008b3765eda5207fb21690874b722ae276b96260e0'
//     ],
//     transactionHash: '0x1be23554545030e1ce47391a41098a46ff426382ed740db62d63d7676ff6fcf1',
//     transactionIndex: 81
//   },
//   {
//     address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
//     args: [
//       '0x8ba1f109551bD432803012645Ac136ddd64DBA72',
//       '0x00De4B13153673BCAE2616b67bf822500d325Fc3',
//       { BigNumber: "250000000000000000" },
//       amount: { BigNumber: "250000000000000000" },
//       from: '0x8ba1f109551bD432803012645Ac136ddd64DBA72',
//       to: '0x00De4B13153673BCAE2616b67bf822500d325Fc3'
//     ],
//     blockHash: '0x8462eb2fbcef5aa4861266f59ad5f47b9aa6525d767d713920fdbdfb6b0c0b78',
//     blockNumber: 9843476,
//     data: '0x00000000000000000000000000000000000000000000000003782dace9d90000',
//     decode: [Function (anonymous)],
//     event: 'Transfer',
//     eventSignature: 'Transfer(address,address,uint256)',
//     getBlock: [Function (anonymous)],
//     getTransaction: [Function (anonymous)],
//     getTransactionReceipt: [Function (anonymous)],
//     logIndex: 70,
//     removeListener: [Function (anonymous)],
//     removed: false,
//     topics: [
//       '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
//       '0x0000000000000000000000008ba1f109551bd432803012645ac136ddd64dba72',
//       '0x00000000000000000000000000de4b13153673bcae2616b67bf822500d325fc3'
//     ],
//     transactionHash: '0x1be23554545030e1ce47391a41098a46ff426382ed740db62d63d7676ff6fcf1',
//     transactionIndex: 81
//   }
// ]

//
// The following have had the results omitted due to the
// number of entries; but they provide some useful examples
//

// List all transfers sent in the last 10,000 blocks
await daiContract.queryFilter(filterFrom, -10000)

// List all transfers ever sent to me
await daiContract.queryFilter(filterTo)

지난번에 검색해 두었던 쿼리 필터를 드디어 쓸 시간이 왔다.

https://docs.ethers.org/v5/api/contract/example/#erc20-meta-events

 

Example: ERC-20 Contract

Documentation for ethers, a complete, tiny and simple Ethereum library.

docs.ethers.org

ethers 공식 문서의 이 예시가 잘 나와 있는것 같다.

일단 쿼리 필터에 주소를 넣게 되면 해당 주소로부터 온 해당 이벤트가 다 나온다.

따라서 특정 행위에 부합하는 이벤트만 잡으려면 그 행위에 대한 컨트렉트 주소를 filters안에 넣어서 조회해 주면 된다.

filterFrom = erc20.filters.Transfer(signer.address);
// {
//   address: '0x70ff5c5B1Ad0533eAA5489e0D5Ea01485d530674',
//   topics: [
//     '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
//     '0x00000000000000000000000046e0726ef145d92dea66d38797cf51901701926e'
//   ]
// }

필터를 만들고 나면

// Search for transfers *from* me in the last 10 blocks
logsFrom = await erc20.queryFilter(filterFrom, -10, "latest");
// [
//   {
//     address: '0x70ff5c5B1Ad0533eAA5489e0D5Ea01485d530674',
//     args: [
//       '0x46E0726Ef145d92DEA66D38797CF51901701926e',
//       '0x5555763613a12D8F3e73be831DFf8598089d3dCa',
//       { BigNumber: "1230000000000000000" },
//       amount: { BigNumber: "1230000000000000000" },
//       from: '0x46E0726Ef145d92DEA66D38797CF51901701926e',
//       to: '0x5555763613a12D8F3e73be831DFf8598089d3dCa'
//     ],
//     blockHash: '0x9340a9c7efafbb4ad9a6ebda62e3311ec5fc40ddcc733632938a988c8c62b885',
//     blockNumber: 60330,
//     data: '0x0000000000000000000000000000000000000000000000001111d67bb1bb0000',
//     decode: [Function (anonymous)],
//     event: 'Transfer',
//     eventSignature: 'Transfer(address,address,uint256)',
//     getBlock: [Function (anonymous)],
//     getTransaction: [Function (anonymous)],
//     getTransactionReceipt: [Function (anonymous)],
//     logIndex: 0,
//     removeListener: [Function (anonymous)],
//     removed: false,
//     topics: [
//       '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
//       '0x00000000000000000000000046e0726ef145d92dea66d38797cf51901701926e',
//       '0x0000000000000000000000005555763613a12d8f3e73be831dff8598089d3dca'
//     ],
//     transactionHash: '0xafdc1f7ec14e2e05a39826c134c9157e0011fc784aef623df3fdf9b9d29f3ac8',
//     transactionIndex: 0
//   }
// ]

요런식으로 블록범위를 넣어주면 된다.

-10 이건 최근 블록이지만 0, 'latest' 이건 전체 조회가 가능하다.

 

 

728x90
반응형
Comments