diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-09-20 02:56:41 +0800 |
---|---|---|
committer | Fred Carlsen <fred@sjelfull.no> | 2018-12-06 19:03:11 +0800 |
commit | 8e7ea4695bae8cd8c5e5bce62e7bc8b8130448a1 (patch) | |
tree | 4f9eef9a049fff5e4ffbc50de8738964dcc15aaf /packages | |
parent | cc5ea80aa346c5318a6c18d82d49368b08aae152 (diff) | |
download | dexon-sol-tools-8e7ea4695bae8cd8c5e5bce62e7bc8b8130448a1.tar dexon-sol-tools-8e7ea4695bae8cd8c5e5bce62e7bc8b8130448a1.tar.gz dexon-sol-tools-8e7ea4695bae8cd8c5e5bce62e7bc8b8130448a1.tar.bz2 dexon-sol-tools-8e7ea4695bae8cd8c5e5bce62e7bc8b8130448a1.tar.lz dexon-sol-tools-8e7ea4695bae8cd8c5e5bce62e7bc8b8130448a1.tar.xz dexon-sol-tools-8e7ea4695bae8cd8c5e5bce62e7bc8b8130448a1.tar.zst dexon-sol-tools-8e7ea4695bae8cd8c5e5bce62e7bc8b8130448a1.zip |
Add tests for etherscan events
Diffstat (limited to 'packages')
-rw-r--r-- | packages/pipeline/package.json | 7 | ||||
-rw-r--r-- | packages/pipeline/src/data-sources/etherscan/events.ts | 23 | ||||
-rw-r--r-- | packages/pipeline/src/index.ts | 3 | ||||
-rw-r--r-- | packages/pipeline/test/data-sources/etherscan/events_test.ts | 88 | ||||
-rw-r--r-- | packages/pipeline/test/utils/chai_setup.ts | 13 |
5 files changed, 124 insertions, 10 deletions
diff --git a/packages/pipeline/package.json b/packages/pipeline/package.json index b4ed33d8d..cdcd5ed18 100644 --- a/packages/pipeline/package.json +++ b/packages/pipeline/package.json @@ -31,7 +31,12 @@ "devDependencies": { "@0xproject/tslint-config": "^1.0.7", "tslint": "5.11.0", - "typescript": "3.0.1" + "typescript": "3.0.1", + "mocha": "^5.2.0", + "chai": "^4.1.2", + "chai-as-promised": "^7.1.1", + "chai-bignumber": "^2.0.2", + "dirty-chai": "^2.0.1" }, "dependencies": { "@0xproject/contract-wrappers": "^1.0.1", diff --git a/packages/pipeline/src/data-sources/etherscan/events.ts b/packages/pipeline/src/data-sources/etherscan/events.ts index 50962a266..3083af267 100644 --- a/packages/pipeline/src/data-sources/etherscan/events.ts +++ b/packages/pipeline/src/data-sources/etherscan/events.ts @@ -25,21 +25,30 @@ export interface EventsResponseResult { transactionIndex: string; } -function convertResponseToLogEntry(result: EventsResponseResult): LogEntry { - const radix = 10; +const hexRadix = 16; + +function hexToInt(hex: string): number { + return parseInt(hex.replace('0x', ''), hexRadix); +} + +// Converts a raw event response to a LogEntry +// tslint:disable-next-line:completed-docs +export function _convertResponseToLogEntry(result: EventsResponseResult): LogEntry { return { - logIndex: parseInt(result.logIndex, radix), - transactionIndex: parseInt(result.logIndex, radix), + logIndex: hexToInt(result.logIndex), + transactionIndex: hexToInt(result.transactionIndex), transactionHash: result.transactionHash, blockHash: '', - blockNumber: parseInt(result.blockNumber, radix), + blockNumber: hexToInt(result.blockNumber), address: result.address, data: result.data, topics: result.topics, }; } -function tryToDecodeLogOrNoop(log: LogEntry): LogWithDecodedArgs<DecodedLogArgs> { +// 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]); const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log); // tslint:disable-next-line:no-unnecessary-type-assertion @@ -51,4 +60,4 @@ function tryToDecodeLogOrNoop(log: LogEntry): LogWithDecodedArgs<DecodedLogArgs> * @param rawEventsResponse The raw events response from etherescan.io. * @returns Parsed and decoded events. */ -export const parseRawEventsResponse = R.pipe(R.map(convertResponseToLogEntry), R.map(tryToDecodeLogOrNoop)); +export const parseRawEventsResponse = R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry)); diff --git a/packages/pipeline/src/index.ts b/packages/pipeline/src/index.ts index c9254cc2a..baed6933e 100644 --- a/packages/pipeline/src/index.ts +++ b/packages/pipeline/src/index.ts @@ -3,6 +3,5 @@ import { Etherscan } from './data-sources/etherscan'; const etherscan = new Etherscan(process.env.ETHERSCAN_API_KEY as string); (async () => { - const events = await etherscan.getContractEventsAsync('0x4f833a24e1f95d70f028921e27040ca56e09ab0b'); - console.log(events); + await etherscan.getContractEventsAsync('0x4f833a24e1f95d70f028921e27040ca56e09ab0b'); })(); diff --git a/packages/pipeline/test/data-sources/etherscan/events_test.ts b/packages/pipeline/test/data-sources/etherscan/events_test.ts new file mode 100644 index 000000000..1151757f2 --- /dev/null +++ b/packages/pipeline/test/data-sources/etherscan/events_test.ts @@ -0,0 +1,88 @@ +import { BigNumber } from '@0xproject/utils'; +import * as chai from 'chai'; +import { DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types'; +import 'mocha'; + +import { + _convertResponseToLogEntry, + _decodeLogEntry, + EventsResponseResult, +} from '../../../src/data-sources/etherscan/events'; +import { chaiSetup } from '../../utils/chai_setup'; + +chaiSetup.configure(); +const expect = chai.expect; + +describe('etherscan#events', () => { + describe('_convertResponseToLogEntry', () => { + it('converts EventsResponseResult to LogEntry', () => { + const input: EventsResponseResult = { + address: '0x4f833a24e1f95d70f028921e27040ca56e09ab0b', + topics: [ + '0x82af639571738f4ebd4268fb0363d8957ebe1bbb9e78dba5ebd69eed39b154f0', + '0x00000000000000000000000067032ef7be8fa07c4335d0134099db0f3875e930', + '0x0000000000000000000000000000000000000000000000000000000000000000', + ], + data: '0x00000000000000000000000000000000000000000000000000000165f2d3f94d', + blockNumber: '0x61127b', + timeStamp: '0x5ba2878e', + gasPrice: '0x1a13b8600', + gasUsed: '0xd9dc', + logIndex: '0x63', + transactionHash: '0xa3f71931ddab6e758b9d1755b2715b376759f49f23fff60755f7e073367d61b5', + transactionIndex: '0x35', + }; + const expected: LogEntry = { + logIndex: 99, + transactionIndex: 53, + transactionHash: input.transactionHash, + blockHash: '', + blockNumber: 6361723, + address: input.address, + data: input.data, + topics: input.topics, + }; + const actual = _convertResponseToLogEntry(input); + expect(actual).deep.equal(expected); + }); + }); + describe('_decodeLogEntry', () => { + it('decodes LogEntry into LogWithDecodedArgs', () => { + const input: LogEntry = { + logIndex: 96, + transactionIndex: 52, + transactionHash: '0x02b59043e9b38b430c8c66abe67ab4a9e5509def8f8552b54231e88db1839831', + blockHash: '', + blockNumber: 6361723, + address: '0x4f833a24e1f95d70f028921e27040ca56e09ab0b', + data: + '0x00000000000000000000000067032ef7be8fa07c4335d0134099db0f3875e93000000000000000000000000067032ef7be8fa07c4335d0134099db0f3875e930000000000000000000000000000000000000000000000000000000174876e8000000000000000000000000000000000000000000000000000000000013ab668000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000024f47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024f47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000', + topics: [ + '0x0bcc4c97732e47d9946f229edb95f5b6323f601300e4690de719993f3c371129', + '0x0000000000000000000000003f7f832abb3be28442c0e48b7222e02b322c78f3', + '0x000000000000000000000000a258b39954cef5cb142fd567a46cddb31a670124', + '0x523404b4e6f847d9aefcf5be024be396449b4635590291fd7a28a8c940843858', + ], + }; + const expected: LogWithDecodedArgs<DecodedLogArgs> = { + ...input, + event: 'Fill', + args: { + makerAddress: '0x3f7f832abb3be28442c0e48b7222e02b322c78f3', + feeRecipientAddress: '0xa258b39954cef5cb142fd567a46cddb31a670124', + takerAddress: '0x67032ef7be8fa07c4335d0134099db0f3875e930', + senderAddress: '0x67032ef7be8fa07c4335d0134099db0f3875e930', + makerAssetFilledAmount: new BigNumber('100000000000'), + takerAssetFilledAmount: new BigNumber('330000000'), + makerFeePaid: new BigNumber('0'), + takerFeePaid: new BigNumber('0'), + orderHash: '0x523404b4e6f847d9aefcf5be024be396449b4635590291fd7a28a8c940843858', + makerAssetData: '0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498', + takerAssetData: '0xf47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', + }, + }; + const actual = _decodeLogEntry(input); + expect(actual).deep.equal(expected); + }); + }); +}); diff --git a/packages/pipeline/test/utils/chai_setup.ts b/packages/pipeline/test/utils/chai_setup.ts new file mode 100644 index 000000000..1a8733093 --- /dev/null +++ b/packages/pipeline/test/utils/chai_setup.ts @@ -0,0 +1,13 @@ +import * as chai from 'chai'; +import chaiAsPromised = require('chai-as-promised'); +import ChaiBigNumber = require('chai-bignumber'); +import * as dirtyChai from 'dirty-chai'; + +export const chaiSetup = { + configure(): void { + chai.config.includeStack = true; + chai.use(ChaiBigNumber()); + chai.use(dirtyChai); + chai.use(chaiAsPromised); + }, +}; |