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

Commit

Permalink
Merge pull request #1809 from 0xProject/order-watcher-weth-events
Browse files Browse the repository at this point in the history
OrderWatcher Deposit/Withdraw events
  • Loading branch information
dekz authored May 10, 2019
2 parents 41cc523 + 0e758fa commit bd51c34
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/order-watcher/CHANGELOG.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
{
"note": "Fix race-condition bug due to async callback modifying shared state",
"pr": 1789
},
{
"note": "Fix bug where WETH deposit/withdrawal events would not trigger an order state update",
"pr": 1809
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion packages/order-watcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"lint": "tslint --format stylish --project .",
"fix": "tslint --fix --format stylish --project .",
"test:circleci": "run-s test:coverage",
"test": "",
"test": "yarn run_mocha",
"rebuild_and_test": "run-s build test",
"test:coverage": "nyc npm run test --all && yarn coverage:report:lcov",
"coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ export class CollisionResistanceAbiDecoder {
this._restAbiDecoder = new AbiDecoder(abis);
}
public tryToDecodeLogOrNoop<ArgsType extends DecodedLogArgs>(log: LogEntry): LogWithDecodedArgs<ArgsType> | RawLog {
let maybeDecodedLog = log;
if (this._knownERC20Tokens.has(log.address)) {
const maybeDecodedERC20Log = this._erc20AbiDecoder.tryToDecodeLogOrNoop(log);
return maybeDecodedERC20Log;
maybeDecodedLog = this._erc20AbiDecoder.tryToDecodeLogOrNoop(log);
} else if (this._knownERC721Tokens.has(log.address)) {
const maybeDecodedERC721Log = this._erc721AbiDecoder.tryToDecodeLogOrNoop(log);
return maybeDecodedERC721Log;
} else {
const maybeDecodedLog = this._restAbiDecoder.tryToDecodeLogOrNoop(log);
return maybeDecodedLog;
maybeDecodedLog = this._erc721AbiDecoder.tryToDecodeLogOrNoop(log);
}
// Fall back to the supplied ABIs for decoding if the ERC20/ERC721 decoding fails
// This ensures we hit events like Deposit and Withdraw given WETH can be a known ERC20Token
const isLogDecoded = ((maybeDecodedLog as any) as LogWithDecodedArgs<DecodedLogArgs>).event !== undefined;
if (!isLogDecoded) {
maybeDecodedLog = this._restAbiDecoder.tryToDecodeLogOrNoop(log);
}
return maybeDecodedLog;
}
// Hints the ABI decoder that a particular token address is ERC20 and events from it should be decoded as ERC20 events
public addERC20Token(address: string): void {
Expand Down
39 changes: 37 additions & 2 deletions packages/order-watcher/test/order_watcher_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// tslint:disable:no-unnecessary-type-assertion
import { ContractWrappers } from '@0x/contract-wrappers';
import { ContractAddresses, ContractWrappers } from '@0x/contract-wrappers';
import { tokenUtils } from '@0x/contract-wrappers/lib/test/utils/token_utils';
import { BlockchainLifecycle, callbackErrorReporter } from '@0x/dev-utils';
import { FillScenarios } from '@0x/fill-scenarios';
import { assetDataUtils, orderHashUtils } from '@0x/order-utils';
import { orderFactory } from '@0x/order-utils/lib/src/order_factory';
import {
DoneCallback,
ExchangeContractErrs,
Expand Down Expand Up @@ -51,10 +52,11 @@ describe('OrderWatcher', () => {
let feeRecipient: string;
let signedOrder: SignedOrder;
let orderWatcher: OrderWatcher;
let contractAddresses: ContractAddresses;
const decimals = constants.ZRX_DECIMALS;
const fillableAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), decimals);
before(async () => {
const contractAddresses = await migrateOnceAsync();
contractAddresses = await migrateOnceAsync();
await blockchainLifecycle.startAsync();
const networkId = constants.TESTRPC_NETWORK_ID;
const config = {
Expand Down Expand Up @@ -596,6 +598,39 @@ describe('OrderWatcher', () => {
await contractWrappers.exchange.fillOrderAsync(signedOrder, fillAmountInBaseUnits, takerAddress);
})().catch(done);
});
it('should emit orderStateInvalid when makerAddress is unfunded by withdrawing WETH', (done: DoneCallback) => {
(async () => {
const etherTokenAddress = contractAddresses.etherToken;
const wethAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddress);
await contractWrappers.erc20Token.setUnlimitedProxyAllowanceAsync(etherTokenAddress, makerAddress);
const depositAmount = fillableAmount.times(2);
await contractWrappers.etherToken.depositAsync(etherTokenAddress, depositAmount, makerAddress);
// WETH for ZRX order
signedOrder = await orderFactory.createSignedOrderAsync(
web3Wrapper.getProvider(),
makerAddress,
fillableAmount,
wethAssetData,
fillableAmount,
takerAssetData,
contractAddresses.exchange,
);
const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
await orderWatcher.addOrderAsync(signedOrder);
const callback = callbackErrorReporter.reportNodeCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.false();
const invalidOrderState = orderState as OrderStateInvalid;
expect(invalidOrderState.orderHash).to.be.equal(orderHash);
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerBalance);
});
orderWatcher.subscribe(callback);
await contractWrappers.etherToken.withdrawAsync(
contractAddresses.etherToken,
depositAmount,
makerAddress,
);
})().catch(done);
});
describe('erc721', () => {
let makerErc721AssetData: string;
let makerErc721TokenAddress: string;
Expand Down

0 comments on commit bd51c34

Please sign in to comment.