|
| 1 | +/* |
| 2 | +This file is part of web3.js. |
| 3 | +
|
| 4 | +web3.js is free software: you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU Lesser General Public License as published by |
| 6 | +the Free Software Foundation, either version 3 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +
|
| 9 | +web3.js is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU Lesser General Public License |
| 15 | +along with web3.js. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | + |
| 18 | +import { Web3Context } from 'web3-core'; |
| 19 | +import { ethRpcMethods } from 'web3-rpc-methods'; |
| 20 | +import { DataFormat, EthExecutionAPI, Numbers, Log, FilterParams } from 'web3-types'; |
| 21 | +import { format, numberToHex } from 'web3-utils'; |
| 22 | +import { isNullish } from 'web3-validator'; |
| 23 | +import { logSchema } from './schemas.js'; |
| 24 | + |
| 25 | +/** |
| 26 | + * View additional documentations here: {@link Web3Eth.createNewPendingTransactionFilter} |
| 27 | + * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc. |
| 28 | + * @param returnFormat ({@link DataFormat}) Return format |
| 29 | + */ |
| 30 | +export async function createNewPendingTransactionFilter<ReturnFormat extends DataFormat>( |
| 31 | + web3Context: Web3Context<EthExecutionAPI>, |
| 32 | + returnFormat: ReturnFormat, |
| 33 | +) { |
| 34 | + const response = await ethRpcMethods.newPendingTransactionFilter(web3Context.requestManager); |
| 35 | + |
| 36 | + return format( |
| 37 | + { format: 'uint' }, |
| 38 | + response as Numbers, |
| 39 | + returnFormat ?? web3Context.defaultReturnFormat, |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * View additional documentations here: {@link Web3Eth.createNewFilter} |
| 45 | + * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc. |
| 46 | + * @param filter ({@link FilterParam}) Filter param optional having from-block to-block address or params |
| 47 | + * @param returnFormat ({@link DataFormat}) Return format |
| 48 | + */ |
| 49 | +export async function createNewFilter<ReturnFormat extends DataFormat>( |
| 50 | + web3Context: Web3Context<EthExecutionAPI>, |
| 51 | + filter: FilterParams, |
| 52 | + returnFormat: ReturnFormat, |
| 53 | +) { |
| 54 | + // format type bigint or number toBlock and fromBlock to hexstring. |
| 55 | + let { toBlock, fromBlock } = filter; |
| 56 | + if (!isNullish(toBlock)) { |
| 57 | + if (typeof toBlock === 'number' || typeof toBlock === 'bigint') { |
| 58 | + toBlock = numberToHex(toBlock); |
| 59 | + } |
| 60 | + } |
| 61 | + if (!isNullish(fromBlock)) { |
| 62 | + if (typeof fromBlock === 'number' || typeof fromBlock === 'bigint') { |
| 63 | + fromBlock = numberToHex(fromBlock); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const formattedFilter = { ...filter, fromBlock, toBlock }; |
| 68 | + |
| 69 | + const response = await ethRpcMethods.newFilter(web3Context.requestManager, formattedFilter); |
| 70 | + |
| 71 | + return format( |
| 72 | + { format: 'uint' }, |
| 73 | + response as Numbers, |
| 74 | + returnFormat ?? web3Context.defaultReturnFormat, |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * View additional documentations here: {@link Web3Eth.createNewBlockFilter} |
| 80 | + * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc. |
| 81 | + * @param returnFormat ({@link DataFormat}) Return format |
| 82 | + */ |
| 83 | +export async function createNewBlockFilter<ReturnFormat extends DataFormat>( |
| 84 | + web3Context: Web3Context<EthExecutionAPI>, |
| 85 | + returnFormat: ReturnFormat, |
| 86 | +) { |
| 87 | + const response = await ethRpcMethods.newBlockFilter(web3Context.requestManager); |
| 88 | + |
| 89 | + return format( |
| 90 | + { format: 'uint' }, |
| 91 | + response as Numbers, |
| 92 | + returnFormat ?? web3Context.defaultReturnFormat, |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * View additional documentations here: {@link Web3Eth.uninstallFilter} |
| 98 | + * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc. |
| 99 | + * @param filterIdentifier ({@link Numbers}) filter id |
| 100 | + */ |
| 101 | +export async function uninstallFilter( |
| 102 | + web3Context: Web3Context<EthExecutionAPI>, |
| 103 | + filterIdentifier: Numbers, |
| 104 | +) { |
| 105 | + const response = await ethRpcMethods.uninstallFilter( |
| 106 | + web3Context.requestManager, |
| 107 | + numberToHex(filterIdentifier), |
| 108 | + ); |
| 109 | + |
| 110 | + return response; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * View additional documentations here: {@link Web3Eth.getFilterChanges} |
| 115 | + * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc. |
| 116 | + * @param filterIdentifier ({@link Numbers}) filter id |
| 117 | + */ |
| 118 | +export async function getFilterChanges<ReturnFormat extends DataFormat>( |
| 119 | + web3Context: Web3Context<EthExecutionAPI>, |
| 120 | + filterIdentifier: Numbers, |
| 121 | + returnFormat: ReturnFormat, |
| 122 | +) { |
| 123 | + const response = await ethRpcMethods.getFilterChanges( |
| 124 | + web3Context.requestManager, |
| 125 | + numberToHex(filterIdentifier), |
| 126 | + ); |
| 127 | + |
| 128 | + const result = response.map(res => { |
| 129 | + if (typeof res === 'string') { |
| 130 | + return res; |
| 131 | + } |
| 132 | + |
| 133 | + return format( |
| 134 | + logSchema, |
| 135 | + res as unknown as Log, |
| 136 | + returnFormat ?? web3Context.defaultReturnFormat, |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + return result; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * View additional documentations here: {@link Web3Eth.getFilterLogs} |
| 145 | + * @param web3Context ({@link Web3Context}) Web3 configuration object that contains things such as the provider, request manager, wallet, etc. |
| 146 | + * @param filterIdentifier ({@link Numbers}) filter id |
| 147 | + */ |
| 148 | +export async function getFilterLogs<ReturnFormat extends DataFormat>( |
| 149 | + web3Context: Web3Context<EthExecutionAPI>, |
| 150 | + filterIdentifier: Numbers, |
| 151 | + returnFormat: ReturnFormat, |
| 152 | +) { |
| 153 | + const response = await ethRpcMethods.getFilterLogs( |
| 154 | + web3Context.requestManager, |
| 155 | + numberToHex(filterIdentifier), |
| 156 | + ); |
| 157 | + |
| 158 | + const result = response.map(res => { |
| 159 | + if (typeof res === 'string') { |
| 160 | + return res; |
| 161 | + } |
| 162 | + |
| 163 | + return format( |
| 164 | + logSchema, |
| 165 | + res as unknown as Log, |
| 166 | + returnFormat ?? web3Context.defaultReturnFormat, |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + return result; |
| 171 | +} |
0 commit comments