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

✨ Added provider.getLogs fn #128

Merged
merged 24 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
29fbb78
🧪 Scaffolded tests for getCode
arimgibson May 19, 2022
438826e
✨ Added getCode method
arimgibson May 19, 2022
add10e0
📝 Inline documentation for getCode
arimgibson May 19, 2022
385eb48
⬆ Post-commit packages, chains
arimgibson May 19, 2022
668271c
🧪 Fix broken test
arimgibson May 19, 2022
6ed902e
lMerge remote-tracking branch 'origin/master' into feature/getCode
dawsbot May 19, 2022
ef2dc04
🏷 Introduce new filter types
arimgibson May 20, 2022
b0bcb7b
🐛 Fix overflow issues
arimgibson May 21, 2022
594ec53
⬆ Post-commit script
arimgibson May 21, 2022
1ba355a
Merge branch 'master' into feature/getCode
arimgibson May 21, 2022
344085e
🧪 Make new test case and fix type standardizing in getBlock
arimgibson May 22, 2022
fb6a239
Merge branch 'feature/getCode' of github.com:earnifi/essential-eth in…
arimgibson May 22, 2022
f05fbc0
🔀 Merge in feature/getCode -- need access to prepBlockTag
arimgibson May 22, 2022
4ae33c6
✨ Added cleanLog class util fn
arimgibson May 22, 2022
da1e06a
🏷 Better document cleanLog
arimgibson May 22, 2022
710d8a3
Fixed getLogs
arimgibson May 22, 2022
5322323
🧪 Added initial tests, will need to cover more cases
arimgibson May 22, 2022
ee2a951
🧪 Better test coverage
arimgibson May 22, 2022
dc567e5
🏷 Added inline documentation
arimgibson May 22, 2022
a37f8f6
🏷 Export forgotten types
arimgibson May 22, 2022
c56e508
Merge branch 'master' into feature/getLogs
arimgibson May 23, 2022
02fa11b
📝 Add documentation for Filter
arimgibson May 26, 2022
337e8b9
🧪 Added etherscan links to tests
arimgibson May 26, 2022
0adf63a
Self-document smart-contract addresses
dawsbot May 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/providers/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,51 @@ export abstract class BaseProvider {
}

/**
* Returns transaction receipt event logs that match a specified filter
* May return [] if parameters are too broad, even if logs exist
arimgibson marked this conversation as resolved.
Show resolved Hide resolved
*
* * Identical to ["ethers.provider.getLogs" in ethers.js](https://docs.ethers.io/v5/api/providers/provider/#Provider-getLogs)
* * Identical to ["web3.eth.getPastLogs" in web3.js](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getpastlogs)
*
* @param filter parameters to filter the logs by
*
* @returns an array of logs matching the specified filter
*
* @example
* ```javascript
* provider.getLogs({
* address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
* topics: [
* "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
* "0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e",
* ],
* fromBlock: 14825027,
* toBlock: 14825039,
* });
*
* [
* {
* address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
* blockHash: '0x8e0dfac2f704851960f866c8708b3bef2f66c0fee0329cf25ff0261b264ca6bc',
* blockNumber: 14825029,
* data: '0x000000000000000000000000000000000000000000000000005f862ee352a38a',
* logIndex: 384,
* removed: false,
* topics: [
* '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
* '0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e',
* '0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45'
* ],
* transactionHash: '0xbd49031be16f8fd1775f4e0fe79b408ffd8ae9c65b2827ee47e3238e3f51f4c0',
* transactionIndex: 226
* }
* ]
* ```
*/
public async getLogs(
filter: Filter | FilterByBlockHash,
): Promise<Array<Log>> {
let filterByRange = filter as Filter;
const filterByRange = filter as Filter;
if (filterByRange.fromBlock)
filterByRange.fromBlock = prepBlockTag(filterByRange.fromBlock);
if (filterByRange.toBlock)
Expand Down
90 changes: 69 additions & 21 deletions src/providers/test/json-rpc-provider/get-logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import Web3 from 'web3';
import { JsonRpcProvider } from '../../../index';
import { Log } from '../../../types/Transaction.types';
import { Filter } from './../../../types/Filter.types';
import { rpcUrls } from './../rpc-urls';

const rpcUrl = rpcUrls.mainnet;
Expand All @@ -10,31 +12,77 @@ describe('provider.getLogs', () => {
const ethersProvider = new StaticJsonRpcProvider(rpcUrl);
const web3Provider = new Web3(rpcUrl);

it('should match ethers.js', async () => {
const filter = {
fromBlock: '0xE1F8F1',
toBlock: '0xE1F8F1',
address: '0xfbddadd80fe7bda00b901fbaf73803f2238ae655',
};
const filterAddressFromTo: Filter = {
address: '0xfbddadd80fe7bda00b901fbaf73803f2238ae655',
fromBlock: '0xE1F8F1',
arimgibson marked this conversation as resolved.
Show resolved Hide resolved
toBlock: '0xE1F8F1',
};

const filterAddressTopics: Filter = {
address: '0x596a0f276ee432d8a28441e55737ff55cf30d0f7',
topics: [
'0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925',
],
};

const filterAll: Filter = {
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
arimgibson marked this conversation as resolved.
Show resolved Hide resolved
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e',
],
fromBlock: 14825027,
toBlock: 14825039,
};

function testLogEquality(ee: Array<Log>, other: Array<any>) {
expect(ee).not.toBe([]); // indicates that filter needs to be updated; logs are from blocks too long ago to retrieve without full archive
other.forEach((log: any) => {
if (log.id) delete log.id;
});
expect(ee).toStrictEqual(other);
}

it('should match ethers.js -- address, from, to', async () => {
const [eeLogs, ethersLogs] = await Promise.all([
essentialEthProvider.getLogs(filterAddressFromTo),
ethersProvider.getLogs(filterAddressFromTo),
]);
testLogEquality(eeLogs, ethersLogs);
});
it('should match ethers.js -- address, topics', async () => {
const [eeLogs, ethersLogs] = await Promise.all([
essentialEthProvider.getLogs(filterAddressTopics),
ethersProvider.getLogs(filterAddressTopics),
]);
testLogEquality(eeLogs, ethersLogs);
});
it('should match ethers.js -- address, topics, from, to', async () => {
const [eeLogs, ethersLogs] = await Promise.all([
essentialEthProvider.getLogs(filter),
ethersProvider.getLogs(filter),
essentialEthProvider.getLogs(filterAll),
ethersProvider.getLogs(filterAll),
]);
expect(eeLogs).toStrictEqual(ethersLogs);
testLogEquality(eeLogs, ethersLogs);
});
it('should match web3.js', async () => {
const filter = {
fromBlock: '0xE1F8F1',
toBlock: '0xE1F8F1',
address: '0xfbddadd80fe7bda00b901fbaf73803f2238ae655',
};
it('should match web3.js -- address, from, to', async () => {
const [eeLogs, web3Logs] = await Promise.all([
essentialEthProvider.getLogs(filter),
web3Provider.eth.getPastLogs(filter),
essentialEthProvider.getLogs(filterAddressFromTo),
web3Provider.eth.getPastLogs(filterAddressFromTo),
]);
web3Logs.forEach((log: any) => {
delete log.id;
});
expect(eeLogs).toStrictEqual(web3Logs);
testLogEquality(eeLogs, web3Logs);
});
it('should match web3.js -- address, topics', async () => {
const [eeLogs, web3Logs] = await Promise.all([
essentialEthProvider.getLogs(filterAddressTopics),
web3Provider.eth.getPastLogs(filterAddressTopics),
]);
testLogEquality(eeLogs, web3Logs);
});
it('should match web3.js -- address, topics, from, to', async () => {
const [eeLogs, web3Logs] = await Promise.all([
essentialEthProvider.getLogs(filterAll),
web3Provider.eth.getPastLogs(filterAll),
]);
testLogEquality(eeLogs, web3Logs);
});
});