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

Commit

Permalink
Incorporate feedback
Browse files Browse the repository at this point in the history
- remove TransactionEncoder
- move TokenUtils to @0x/dev-utils
  • Loading branch information
xianny committed Aug 7, 2019
1 parent e57d29a commit 9ea1936
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 344 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ContractAddresses, ContractWrappers } from '@0x/contract-wrappers';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { BlockchainLifecycle, tokenUtils } from '@0x/dev-utils';
import { FillScenarios } from '@0x/fill-scenarios';
import { assetDataUtils } from '@0x/order-utils';
import { MarketOperation, SignedOrder } from '@0x/types';
import { BigNumber, tokenUtils } from '@0x/utils';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import 'mocha';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ContractAddresses, ContractWrappers } from '@0x/contract-wrappers';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { BlockchainLifecycle, tokenUtils } from '@0x/dev-utils';
import { assetDataUtils } from '@0x/order-utils';
import { MarketOperation, SignedOrder } from '@0x/types';
import { BigNumber, tokenUtils } from '@0x/utils';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import 'mocha';

Expand Down
4 changes: 2 additions & 2 deletions packages/asset-swapper/test/swap_quote_consumer_utils_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ContractAddresses, ContractWrappers } from '@0x/contract-wrappers';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { BlockchainLifecycle, tokenUtils } from '@0x/dev-utils';
import { assetDataUtils } from '@0x/order-utils';
import { MarketOperation, SignedOrder } from '@0x/types';
import { BigNumber, tokenUtils } from '@0x/utils';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import 'mocha';

Expand Down
4 changes: 2 additions & 2 deletions packages/contract-wrappers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test": "test"
},
"scripts": {
"build": "yarn pre_build && tsc -b && yarn docs",
"build": "yarn pre_build && tsc -b",
"build:ci": "yarn build",
"lint": "tslint --format stylish --project . --exclude **/lib/**/*",
"fix": "tslint --fix --format stylish --project .--exclude **/lib/**/*",
Expand All @@ -31,7 +31,7 @@
"prettier_contract_wrappers": "prettier --write src/generated-wrappers/* --config ../../.prettierrc",
"clean": "shx rm -rf lib src/generated-wrappers",
"generate_contract_wrappers": "abi-gen --abis ${npm_package_config_abis} --template ../../node_modules/@0x/abi-gen-templates/contract.handlebars --partials '../../node_modules/@0x/abi-gen-templates/partials/**/*.handlebars' --output src/generated-wrappers --backend ethers",
"docs": "typedoc --excludePrivate --excludeExternals --target ES5 --tsconfig typedoc-tsconfig.json --out generated_docs ./src/generated-wrappers/*"
"docs_test": "typedoc --excludePrivate --excludeExternals --target ES5 --tsconfig typedoc-tsconfig.json --out generated_docs ./src/generated-wrappers/*"
},
"config": {
"abis": "../contract-artifacts/artifacts/@(AssetProxyOwner|DutchAuction|DummyERC20Token|DummyERC721Token|ERC20Proxy|ERC20Token|ERC721Proxy|ERC721Token|Exchange|Forwarder|IAssetProxy|IValidator|IWallet|MultiAssetProxy|OrderValidator|WETH9|ZRXToken|Coordinator|CoordinatorRegistry|EthBalanceChecker).json"
Expand Down
87 changes: 65 additions & 22 deletions packages/contract-wrappers/src/coordinator_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import { ContractAbi } from 'ethereum-types';
import * as HttpStatus from 'http-status-codes';
import { flatten } from 'lodash';

import { CoordinatorContract, CoordinatorRegistryContract, ExchangeContract } from './index';

import { orderTxOptsSchema } from './schemas/order_tx_opts_schema';
import { txOptsSchema } from './schemas/tx_opts_schema';
import { CoordinatorTransaction, OrderTransactionOpts } from './types';
import { assert } from './utils/assert';
import {
CoordinatorServerApprovalRawResponse,
Expand All @@ -21,10 +24,7 @@ import {
CoordinatorServerResponse,
} from './utils/coordinator_server_types';
import { decorators } from './utils/decorators';
import { TransactionEncoder } from './utils/transaction_encoder';

import { CoordinatorContract, CoordinatorRegistryContract, ExchangeContract } from './index';
import { CoordinatorTransaction, OrderTransactionOpts } from './types';
import { getAbiEncodedTransactionData } from './utils/getAbiEncodedTransactionData';

/**
* This class includes all the functionality related to filling or cancelling orders through
Expand All @@ -40,7 +40,6 @@ export class CoordinatorWrapper {
private readonly _contractInstance: CoordinatorContract;
private readonly _registryInstance: CoordinatorRegistryContract;
private readonly _exchangeInstance: ExchangeContract;
private readonly _transactionEncoder: TransactionEncoder;
private readonly _feeRecipientToEndpoint: { [feeRecipient: string]: string } = {};

/**
Expand Down Expand Up @@ -83,8 +82,6 @@ export class CoordinatorWrapper {
this._web3Wrapper.getProvider(),
this._web3Wrapper.getContractDefaults(),
);

this._transactionEncoder = new TransactionEncoder(this._exchangeInstance);
}

/**
Expand Down Expand Up @@ -113,7 +110,12 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.fillOrderTx(signedOrder, takerAssetFillAmount);
const data = this._getAbiEncodedTransactionData(
'fillOrder',
signedOrder,
takerAssetFillAmount,
signedOrder.signature,
);
const txHash = await this._handleFillsAsync(data, takerAddress, [signedOrder], orderTransactionOpts);
return txHash;
}
Expand All @@ -140,7 +142,12 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.fillOrderNoThrowTx(signedOrder, takerAssetFillAmount);
const data = this._getAbiEncodedTransactionData(
'fillOrderNoThrow',
signedOrder,
takerAssetFillAmount,
signedOrder.signature,
);
const txHash = await this._handleFillsAsync(data, takerAddress, [signedOrder], orderTransactionOpts);
return txHash;
}
Expand Down Expand Up @@ -168,7 +175,12 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.fillOrKillOrderTx(signedOrder, takerAssetFillAmount);
const data = this._getAbiEncodedTransactionData(
'fillOrKillOrder',
signedOrder,
takerAssetFillAmount,
signedOrder.signature,
);
const txHash = await this._handleFillsAsync(data, takerAddress, [signedOrder], orderTransactionOpts);
return txHash;
}
Expand Down Expand Up @@ -202,7 +214,8 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.batchFillOrdersTx(signedOrders, takerAssetFillAmounts);
const signatures = signedOrders.map(o => o.signature);
const data = this._getAbiEncodedTransactionData('batchFillOrders', signedOrders, takerAssetFillAmounts, signatures);
const txHash = await this._handleFillsAsync(data, takerAddress, signedOrders, orderTransactionOpts);
return txHash;
}
Expand Down Expand Up @@ -231,7 +244,13 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.batchFillOrdersNoThrowTx(signedOrders, takerAssetFillAmounts);
const signatures = signedOrders.map(o => o.signature);
const data = this._getAbiEncodedTransactionData(
'batchFillOrdersNoThrow',
signedOrders,
takerAssetFillAmounts,
signatures,
);
const txHash = await this._handleFillsAsync(data, takerAddress, signedOrders, orderTransactionOpts);
return txHash;
}
Expand Down Expand Up @@ -260,7 +279,13 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.batchFillOrKillOrdersTx(signedOrders, takerAssetFillAmounts);
const signatures = signedOrders.map(o => o.signature);
const data = this._getAbiEncodedTransactionData(
'batchFillOrKillOrders',
signedOrders,
takerAssetFillAmounts,
signatures,
);
const txHash = await this._handleFillsAsync(data, takerAddress, signedOrders, orderTransactionOpts);
return txHash;
}
Expand Down Expand Up @@ -292,7 +317,8 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.marketBuyOrdersTx(signedOrders, makerAssetFillAmount);
const signatures = signedOrders.map(o => o.signature);
const data = this._getAbiEncodedTransactionData('marketBuyOrders', signedOrders, makerAssetFillAmount, signatures);
const txHash = await this._handleFillsAsync(data, takerAddress, signedOrders, orderTransactionOpts);
return txHash;
}
Expand Down Expand Up @@ -324,7 +350,8 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.marketSellOrdersTx(signedOrders, takerAssetFillAmount);
const signatures = signedOrders.map(o => o.signature);
const data = this._getAbiEncodedTransactionData('marketSellOrders', signedOrders, takerAssetFillAmount, signatures);
const txHash = await this._handleFillsAsync(data, takerAddress, signedOrders, orderTransactionOpts);
return txHash;
}
Expand All @@ -351,7 +378,13 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.marketBuyOrdersNoThrowTx(signedOrders, makerAssetFillAmount);
const signatures = signedOrders.map(o => o.signature);
const data = this._getAbiEncodedTransactionData(
'marketBuyOrdersNoThrow',
signedOrders,
makerAssetFillAmount,
signatures,
);
const txHash = await this._handleFillsAsync(data, takerAddress, signedOrders, orderTransactionOpts);
return txHash;
}
Expand All @@ -378,7 +411,13 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper);

const data = this._transactionEncoder.marketSellOrdersNoThrowTx(signedOrders, takerAssetFillAmount);
const signatures = signedOrders.map(o => o.signature);
const data = this._getAbiEncodedTransactionData(
'marketSellOrdersNoThrow',
signedOrders,
takerAssetFillAmount,
signatures,
);
const txHash = await this._handleFillsAsync(data, takerAddress, signedOrders, orderTransactionOpts);
return txHash;
}
Expand All @@ -395,7 +434,7 @@ export class CoordinatorWrapper {
assert.isETHAddressHex('feeRecipientAddress', order.feeRecipientAddress);
assert.isSenderAddressAsync('makerAddress', order.makerAddress, this._web3Wrapper);

const data = this._transactionEncoder.cancelOrderTx(order);
const data = this._getAbiEncodedTransactionData('cancelOrder', order);
const transaction = await this._generateSignedZeroExTransactionAsync(data, order.makerAddress);
const endpoint = await this._getServerEndpointOrThrowAsync(order.feeRecipientAddress);

Expand Down Expand Up @@ -429,7 +468,7 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orders', orders, schemas.ordersSchema);
const makerAddress = getMakerAddressOrThrow(orders);
assert.isSenderAddressAsync('makerAddress', makerAddress, this._web3Wrapper);
const data = this._transactionEncoder.batchCancelOrdersTx(orders);
const data = this._getAbiEncodedTransactionData('batchCancelOrders', orders);

const serverEndpointsToOrders = await this._mapServerEndpointsToOrdersAsync(orders);

Expand Down Expand Up @@ -487,7 +526,7 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('makerAddress', order.makerAddress, this._web3Wrapper);

const data = this._transactionEncoder.cancelOrderTx(order);
const data = this._getAbiEncodedTransactionData('cancelOrder', order);
const transaction = await this._generateSignedZeroExTransactionAsync(data, order.makerAddress);

const approvalSignatures = new Array();
Expand Down Expand Up @@ -520,7 +559,7 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('makerAddress', makerAddress, this._web3Wrapper);

const data = this._transactionEncoder.batchCancelOrdersTx(orders);
const data = this._getAbiEncodedTransactionData('batchCancelOrders', orders);
const transaction = await this._generateSignedZeroExTransactionAsync(data, makerAddress);

const approvalSignatures = new Array();
Expand Down Expand Up @@ -555,7 +594,7 @@ export class CoordinatorWrapper {
assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]);
await assert.isSenderAddressAsync('senderAddress', senderAddress, this._web3Wrapper);

const data = this._transactionEncoder.cancelOrdersUpToTx(targetOrderEpoch);
const data = this._getAbiEncodedTransactionData('cancelOrdersUpTo', targetOrderEpoch);
const transaction = await this._generateSignedZeroExTransactionAsync(data, senderAddress);

const approvalSignatures = new Array();
Expand Down Expand Up @@ -619,6 +658,10 @@ export class CoordinatorWrapper {
return signerAddress;
}

private _getAbiEncodedTransactionData<K extends keyof ExchangeContract>(methodName: K, ...args: any): string {
return getAbiEncodedTransactionData(this._exchangeInstance, methodName, ...args);
}

private async _handleFillsAsync(
data: string,
takerAddress: string,
Expand Down
1 change: 0 additions & 1 deletion packages/contract-wrappers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ export * from '@0x/contract-addresses';

export { ContractWrappers } from './contract_wrappers';
export { CoordinatorWrapper } from './coordinator_wrapper';
export { TransactionEncoder } from './utils/transaction_encoder';
export { OrderStatus } from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ExchangeContract } from '../index';

// HACK (xianny): we haven't formalised contract method functions into a type interface, and would have to
// differentiate contract method members from other class members to get this to work non-hackily

export function getAbiEncodedTransactionData<K extends keyof ExchangeContract>(contractInstance: ExchangeContract, methodName: K, ...params: any): string {
const method = (contractInstance[methodName] as any) as {
getAbiEncodedTransactionData: (...args: any) => string;
};
if (method.getAbiEncodedTransactionData) {
const abiEncodedData = method.getAbiEncodedTransactionData(params);
return abiEncodedData;
} else {
return '';
}
}
Loading

0 comments on commit 9ea1936

Please sign in to comment.