Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
move log tests to @0x/abi-gen; delete exchange_wrapper_test (#2160)
Browse files Browse the repository at this point in the history
  • Loading branch information
xianny authored Sep 16, 2019
1 parent 50f86dd commit b3f71af
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 590 deletions.
39 changes: 27 additions & 12 deletions packages/abi-gen/test-cli/fixtures/artifacts/AbiGenDummy.json

Large diffs are not rendered by default.

24 changes: 16 additions & 8 deletions packages/abi-gen/test-cli/fixtures/contracts/AbiGenDummy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ contract AbiGenDummy
return ecrecover(prefixedHash, v, r, s);
}

event Withdrawal(address indexed _owner, uint _value);

function withdraw(uint wad) public {
emit Withdrawal(msg.sender, wad);
}

// test: generated code should normalize address inputs to lowercase
// add extra inputs to make sure it works with address in any position
function withAddressInput(address x, uint256 a, uint256 b, address y, uint256 c)
Expand All @@ -113,8 +107,6 @@ contract AbiGenDummy
return x;
}

event AnEvent(uint8 param);

function acceptsBytes(bytes memory a) public pure {}

/// @dev a method that accepts an array of bytes
Expand Down Expand Up @@ -180,6 +172,22 @@ contract AbiGenDummy
function overloadedMethod(int a) public pure {}
function overloadedMethod(string memory a) public pure {}


event Withdrawal(address indexed _owner, uint _value);

function withdraw(uint wad) public {
emit Withdrawal(msg.sender, wad);
}

event SimpleEvent(bytes someBytes, string someString);

function emitSimpleEvent() public {
emit SimpleEvent(
hex'12345678',
"lorem"
);
}

// begin tests for `decodeTransactionData`, `decodeReturnData`
/// @dev complex input is dynamic and more difficult to decode than simple input.
struct ComplexInput {
Expand Down
60 changes: 55 additions & 5 deletions packages/abi-gen/test-cli/output/python/abi_gen_dummy/__init__.py

Large diffs are not rendered by default.

170 changes: 163 additions & 7 deletions packages/abi-gen/test-cli/output/typescript/abi_gen_dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ import { assert } from '@0x/assert';
import * as ethers from 'ethers';
// tslint:enable:no-unused-variable

export type AbiGenDummyEventArgs = AbiGenDummyWithdrawalEventArgs | AbiGenDummyAnEventEventArgs;
export type AbiGenDummyEventArgs = AbiGenDummyWithdrawalEventArgs | AbiGenDummySimpleEventEventArgs;

export enum AbiGenDummyEvents {
Withdrawal = 'Withdrawal',
AnEvent = 'AnEvent',
SimpleEvent = 'SimpleEvent',
}

export interface AbiGenDummyWithdrawalEventArgs extends DecodedLogArgs {
_owner: string;
_value: BigNumber;
}

export interface AbiGenDummyAnEventEventArgs extends DecodedLogArgs {
param: number;
export interface AbiGenDummySimpleEventEventArgs extends DecodedLogArgs {
someBytes: string;
someString: string;
}

/* istanbul ignore next */
Expand Down Expand Up @@ -1809,6 +1810,147 @@ export class AbiGenDummyContract extends BaseContract {
return abiDecodedReturnData;
},
};
public emitSimpleEvent = {
/**
* Sends an Ethereum transaction executing this method with the supplied parameters. This is a read/write
* Ethereum operation and will cost gas.
* @param txData Additional data for transaction
* @returns The hash of the transaction
*/
async sendTransactionAsync(txData?: Partial<TxData> | undefined): Promise<string> {
const self = (this as any) as AbiGenDummyContract;
const encodedData = self._strictEncodeArguments('emitSimpleEvent()', []);
const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{
to: self.address,
...txData,
data: encodedData,
},
self._web3Wrapper.getContractDefaults(),
self.emitSimpleEvent.estimateGasAsync.bind(self),
);
if (txDataWithDefaults.from !== undefined) {
txDataWithDefaults.from = txDataWithDefaults.from.toLowerCase();
}

const txHash = await self._web3Wrapper.sendTransactionAsync(txDataWithDefaults);
return txHash;
},
/**
* Sends an Ethereum transaction and waits until the transaction has been successfully mined without reverting.
* If the transaction was mined, but reverted, an error is thrown.
* @param txData Additional data for transaction
* @param pollingIntervalMs Interval at which to poll for success
* @returns A promise that resolves when the transaction is successful
*/
awaitTransactionSuccessAsync(
txData?: Partial<TxData>,
pollingIntervalMs?: number,
timeoutMs?: number,
): PromiseWithTransactionHash<TransactionReceiptWithDecodedLogs> {
const self = (this as any) as AbiGenDummyContract;
const txHashPromise = self.emitSimpleEvent.sendTransactionAsync(txData);
return new PromiseWithTransactionHash<TransactionReceiptWithDecodedLogs>(
txHashPromise,
(async (): Promise<TransactionReceiptWithDecodedLogs> => {
// When the transaction hash resolves, wait for it to be mined.
return self._web3Wrapper.awaitTransactionSuccessAsync(
await txHashPromise,
pollingIntervalMs,
timeoutMs,
);
})(),
);
},
/**
* Estimates the gas cost of sending an Ethereum transaction calling this method with these arguments.
* @param txData Additional data for transaction
* @returns The hash of the transaction
*/
async estimateGasAsync(txData?: Partial<TxData> | undefined): Promise<number> {
const self = (this as any) as AbiGenDummyContract;
const encodedData = self._strictEncodeArguments('emitSimpleEvent()', []);
const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{
to: self.address,
...txData,
data: encodedData,
},
self._web3Wrapper.getContractDefaults(),
);
if (txDataWithDefaults.from !== undefined) {
txDataWithDefaults.from = txDataWithDefaults.from.toLowerCase();
}

const gas = await self._web3Wrapper.estimateGasAsync(txDataWithDefaults);
return gas;
},
async validateAndSendTransactionAsync(txData?: Partial<TxData> | undefined): Promise<string> {
await (this as any).emitSimpleEvent.callAsync(txData);
const txHash = await (this as any).emitSimpleEvent.sendTransactionAsync(txData);
return txHash;
},
/**
* Sends a read-only call to the contract method. Returns the result that would happen if one were to send an
* Ethereum transaction to this method, given the current state of the blockchain. Calls do not cost gas
* since they don't modify state.
*/
async callAsync(callData: Partial<CallData> = {}, defaultBlock?: BlockParam): Promise<void> {
assert.doesConformToSchema('callData', callData, schemas.callDataSchema, [
schemas.addressSchema,
schemas.numberSchema,
schemas.jsNumber,
]);
if (defaultBlock !== undefined) {
assert.isBlockParam('defaultBlock', defaultBlock);
}
const self = (this as any) as AbiGenDummyContract;
const encodedData = self._strictEncodeArguments('emitSimpleEvent()', []);
const callDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{
to: self.address,
...callData,
data: encodedData,
},
self._web3Wrapper.getContractDefaults(),
);
callDataWithDefaults.from = callDataWithDefaults.from
? callDataWithDefaults.from.toLowerCase()
: callDataWithDefaults.from;

const rawCallResult = await self._web3Wrapper.callAsync(callDataWithDefaults, defaultBlock);
BaseContract._throwIfRevertWithReasonCallResult(rawCallResult);
const abiEncoder = self._lookupAbiEncoder('emitSimpleEvent()');
// tslint:disable boolean-naming
const result = abiEncoder.strictDecodeReturnValue<void>(rawCallResult);
// tslint:enable boolean-naming
return result;
},
/**
* Returns the ABI encoded transaction data needed to send an Ethereum transaction calling this method. Before
* sending the Ethereum tx, this encoded tx data can first be sent to a separate signing service or can be used
* to create a 0x transaction (see protocol spec for more details).
*/
getABIEncodedTransactionData(): string {
const self = (this as any) as AbiGenDummyContract;
const abiEncodedTransactionData = self._strictEncodeArguments('emitSimpleEvent()', []);
return abiEncodedTransactionData;
},
getABIDecodedTransactionData(callData: string): void {
const self = (this as any) as AbiGenDummyContract;
const abiEncoder = self._lookupAbiEncoder('emitSimpleEvent()');
// tslint:disable boolean-naming
const abiDecodedCallData = abiEncoder.strictDecode<void>(callData);
return abiDecodedCallData;
},
getABIDecodedReturnData(returnData: string): void {
const self = (this as any) as AbiGenDummyContract;
const abiEncoder = self._lookupAbiEncoder('emitSimpleEvent()');
// tslint:disable boolean-naming
const abiDecodedReturnData = abiEncoder.strictDecodeReturnValue<void>(returnData);
return abiDecodedReturnData;
},
};
/**
* a method that returns a struct
*/
Expand Down Expand Up @@ -2712,6 +2854,15 @@ export class AbiGenDummyContract extends BaseContract {
stateMutability: 'pure',
type: 'function',
},
{
constant: false,
inputs: [],
name: 'emitSimpleEvent',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
{
constant: true,
inputs: [],
Expand Down Expand Up @@ -2822,12 +2973,17 @@ export class AbiGenDummyContract extends BaseContract {
anonymous: false,
inputs: [
{
name: 'param',
type: 'uint8',
name: 'someBytes',
type: 'bytes',
indexed: false,
},
{
name: 'someString',
type: 'string',
indexed: false,
},
],
name: 'AnEvent',
name: 'SimpleEvent',
outputs: [],
type: 'event',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { BlockchainLifecycle, devConstants, web3Factory } from '@0x/dev-utils';
import { Web3ProviderEngine } from '@0x/subproviders';
import { BigNumber, providerUtils } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';

import { BlockParamLiteral, Web3Wrapper } from '@0x/web3-wrapper';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as ChaiBigNumber from 'chai-bignumber';
import * as dirtyChai from 'dirty-chai';
import * as Sinon from 'sinon';
import { constants } from 'websocket';

import { AbiGenDummyContract, AbiGenDummyEvents, artifacts, TestLibDummyContract } from '../src';
import {
AbiGenDummyContract,
AbiGenDummyEvents,
AbiGenDummyWithdrawalEventArgs,
artifacts,
TestLibDummyContract,
} from '../src';

const txDefaults = {
from: devConstants.TESTRPC_FIRST_ADDRESS,
Expand Down Expand Up @@ -163,6 +169,45 @@ describe('AbiGenDummy Contract', () => {
});
});

describe('getLogsAsync', () => {
const blockRange = {
fromBlock: 0,
toBlock: BlockParamLiteral.Latest,
};
it('should get logs with decoded args emitted by EventWithStruct', async () => {
await abiGenDummy.emitSimpleEvent.awaitTransactionSuccessAsync();
const eventName = AbiGenDummyEvents.SimpleEvent;
const indexFilterValues = {};
const logs = await abiGenDummy.getLogsAsync(eventName, blockRange, indexFilterValues);
expect(logs).to.have.length(1);
expect(logs[0].event).to.be.equal(eventName);
});
it('should only get the logs with the correct event name', async () => {
await abiGenDummy.emitSimpleEvent.awaitTransactionSuccessAsync();
const differentEventName = AbiGenDummyEvents.Withdrawal;
const indexFilterValues = {};
const logs = await abiGenDummy.getLogsAsync(differentEventName, blockRange, indexFilterValues);
expect(logs).to.have.length(0);
});
it('should only get the logs with the correct indexed fields', async () => {
const [addressOne, addressTwo] = await web3Wrapper.getAvailableAddressesAsync();
await abiGenDummy.withdraw.awaitTransactionSuccessAsync(new BigNumber(1), { from: addressOne });
await abiGenDummy.withdraw.awaitTransactionSuccessAsync(new BigNumber(1), { from: addressTwo });
const eventName = AbiGenDummyEvents.Withdrawal;
const indexFilterValues = {
_owner: addressOne,
};
const logs = await abiGenDummy.getLogsAsync<AbiGenDummyWithdrawalEventArgs>(
eventName,
blockRange,
indexFilterValues,
);
expect(logs).to.have.length(1);
const args = logs[0].args;
expect(args._owner).to.be.equal(addressOne);
});
});

describe('withAddressInput', () => {
it('should normalize address inputs to lowercase', async () => {
const xAddress = devConstants.TESTRPC_FIRST_ADDRESS.toUpperCase();
Expand Down
10 changes: 5 additions & 5 deletions packages/contract-wrappers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@
"@0x/assert": "^2.1.5",
"@0x/contracts-test-utils": "^3.1.15",
"@0x/coordinator-server": "^0.1.3",
"@0x/dev-utils": "^2.3.2",
"@0x/fill-scenarios": "^3.0.18",
"@0x/json-schemas": "^4.0.1",
"@0x/ts-doc-gen": "^0.0.22",
"@0x/migrations": "^4.3.1",
"@0x/subproviders": "^5.0.3",
"@0x/tslint-config": "^3.0.1",
"@0x/types": "^2.4.2",
"@0x/utils": "^4.5.1",
Expand All @@ -62,7 +66,6 @@
"chai-bignumber": "^3.0.0",
"dirty-chai": "^2.0.1",
"ethereum-types": "^2.1.5",
"ethers": "~4.0.4",
"lodash": "^4.17.11",
"mocha": "^6.2.0",
"nock": "^10.0.6",
Expand All @@ -76,11 +79,8 @@
"@0x/base-contract": "^5.3.3",
"@0x/contract-addresses": "^3.1.0",
"@0x/contract-artifacts": "^2.2.1",
"@0x/dev-utils": "^2.3.2",
"@0x/fill-scenarios": "^3.0.18",
"@0x/migrations": "^4.3.1",
"@0x/order-utils": "^8.3.1",
"@0x/subproviders": "^5.0.3",
"ethers": "~4.0.4",
"http-status-codes": "^1.3.2"
},
"publishConfig": {
Expand Down
Loading

0 comments on commit b3f71af

Please sign in to comment.