Make contractAbi a parameter of getContractEventsAsync
This commit is contained in:
committed by
Fred Carlsen
parent
8e7ea4695b
commit
a6e5d126c3
@@ -1,9 +1,7 @@
|
||||
import { AbiDecoder } from '@0xproject/utils';
|
||||
import { DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types';
|
||||
import { AbiDefinition, DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types';
|
||||
import * as R from 'ramda';
|
||||
|
||||
import { artifacts } from '../../artifacts';
|
||||
|
||||
// Raw events response from etherescan.io
|
||||
export interface EventsResponse {
|
||||
status: string;
|
||||
@@ -48,16 +46,26 @@ export function _convertResponseToLogEntry(result: EventsResponseResult): LogEnt
|
||||
|
||||
// Decodes a LogEntry into a LogWithDecodedArgs
|
||||
// tslint:disable-next-line:completed-docs
|
||||
export function _decodeLogEntry(log: LogEntry): LogWithDecodedArgs<DecodedLogArgs> {
|
||||
const abiDecoder = new AbiDecoder([artifacts.Exchange.compilerOutput.abi]);
|
||||
export const _decodeLogEntry = R.curry((contractAbi: AbiDefinition[], log: LogEntry): LogWithDecodedArgs<
|
||||
DecodedLogArgs
|
||||
> => {
|
||||
const abiDecoder = new AbiDecoder([contractAbi]);
|
||||
const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log);
|
||||
// tslint:disable-next-line:no-unnecessary-type-assertion
|
||||
return logWithDecodedArgs as LogWithDecodedArgs<DecodedLogArgs>;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Parses and abi-decodes the raw events response from etherscan.io.
|
||||
* @param contractAbi The ABI for the contract that the events where emited from.
|
||||
* @param rawEventsResponse The raw events response from etherescan.io.
|
||||
* @returns Parsed and decoded events.
|
||||
*/
|
||||
export const parseRawEventsResponse = R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry));
|
||||
export function parseRawEventsResponse(
|
||||
contractAbi: AbiDefinition[],
|
||||
rawEventsResponse: EventsResponse,
|
||||
): Array<LogWithDecodedArgs<DecodedLogArgs>> {
|
||||
return R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry(contractAbi)))(rawEventsResponse.result);
|
||||
}
|
||||
|
||||
// export const parseRawEventsResponse = R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { default as axios } from 'axios';
|
||||
import { BlockParam, BlockParamLiteral, DecodedLogArgs, LogWithDecodedArgs } from 'ethereum-types';
|
||||
import { AbiDefinition, BlockParam, BlockParamLiteral, DecodedLogArgs, LogWithDecodedArgs } from 'ethereum-types';
|
||||
|
||||
import { EventsResponse, parseRawEventsResponse } from './events';
|
||||
|
||||
@@ -14,12 +14,14 @@ export class Etherscan {
|
||||
/**
|
||||
* Gets the decoded events for a specific contract and block range.
|
||||
* @param contractAddress The address of the contract to get the events for.
|
||||
* @param constractAbi The ABI of the contract.
|
||||
* @param fromBlock The start of the block range to get events for (inclusive).
|
||||
* @param toBlock The end of the block range to get events for (inclusive).
|
||||
* @returns A list of decoded events.
|
||||
*/
|
||||
public async getContractEventsAsync(
|
||||
contractAddress: string,
|
||||
contractAbi: AbiDefinition[],
|
||||
fromBlock: BlockParam = BlockParamLiteral.Earliest,
|
||||
toBlock: BlockParam = BlockParamLiteral.Latest,
|
||||
): Promise<Array<LogWithDecodedArgs<DecodedLogArgs>>> {
|
||||
@@ -28,7 +30,7 @@ export class Etherscan {
|
||||
}`;
|
||||
const resp = await axios.get<EventsResponse>(fullURL);
|
||||
// TODO(albrow): Check response code.
|
||||
const decodedEvents = parseRawEventsResponse(resp.data.result);
|
||||
const decodedEvents = parseRawEventsResponse(contractAbi, resp.data);
|
||||
return decodedEvents;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { Etherscan } from './data-sources/etherscan';
|
||||
|
||||
import { artifacts } from './artifacts';
|
||||
|
||||
const etherscan = new Etherscan(process.env.ETHERSCAN_API_KEY as string);
|
||||
|
||||
(async () => {
|
||||
await etherscan.getContractEventsAsync('0x4f833a24e1f95d70f028921e27040ca56e09ab0b');
|
||||
const events = await etherscan.getContractEventsAsync(
|
||||
'0x4f833a24e1f95d70f028921e27040ca56e09ab0b',
|
||||
artifacts.Exchange.compilerOutput.abi,
|
||||
);
|
||||
console.log(events);
|
||||
})();
|
||||
|
||||
@@ -3,6 +3,8 @@ import * as chai from 'chai';
|
||||
import { DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types';
|
||||
import 'mocha';
|
||||
|
||||
import { artifacts } from '../../../src/artifacts';
|
||||
|
||||
import {
|
||||
_convertResponseToLogEntry,
|
||||
_decodeLogEntry,
|
||||
@@ -81,7 +83,7 @@ describe('etherscan#events', () => {
|
||||
takerAssetData: '0xf47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
|
||||
},
|
||||
};
|
||||
const actual = _decodeLogEntry(input);
|
||||
const actual = _decodeLogEntry(artifacts.Exchange.compilerOutput.abi, input);
|
||||
expect(actual).deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user