From 9ea1936abc9c8a503fc2226d8b23d882735f3cc3 Mon Sep 17 00:00:00 2001 From: xianny Date: Wed, 7 Aug 2019 12:27:30 -0700 Subject: [PATCH] Incorporate feedback - remove TransactionEncoder - move TokenUtils to @0x/dev-utils --- .../test/exchange_swap_quote_consumer_test.ts | 4 +- .../forwarder_swap_quote_consumer_test.ts | 4 +- .../test/swap_quote_consumer_utils_test.ts | 4 +- packages/contract-wrappers/package.json | 4 +- .../src/coordinator_wrapper.ts | 87 +++-- packages/contract-wrappers/src/index.ts | 1 - .../src/utils/getAbiEncodedTransactionData.ts | 16 + .../src/utils/transaction_encoder.ts | 297 ------------------ .../test/calldata_decoder_test.ts | 13 +- .../test/coordinator_wrapper_test.ts | 4 +- packages/dev-utils/CHANGELOG.json | 9 + packages/dev-utils/src/index.ts | 1 + .../{utils => dev-utils}/src/token_utils.ts | 0 .../test/expiration_watcher_test.ts | 4 +- .../order-watcher/test/order_watcher_test.ts | 4 +- .../order_watcher_web_socket_server_test.ts | 4 +- packages/utils/CHANGELOG.json | 4 - packages/utils/src/index.ts | 1 - 18 files changed, 117 insertions(+), 344 deletions(-) create mode 100644 packages/contract-wrappers/src/utils/getAbiEncodedTransactionData.ts delete mode 100644 packages/contract-wrappers/src/utils/transaction_encoder.ts rename packages/{utils => dev-utils}/src/token_utils.ts (100%) diff --git a/packages/asset-swapper/test/exchange_swap_quote_consumer_test.ts b/packages/asset-swapper/test/exchange_swap_quote_consumer_test.ts index 772d9ae0d2..ae0a9e1efc 100644 --- a/packages/asset-swapper/test/exchange_swap_quote_consumer_test.ts +++ b/packages/asset-swapper/test/exchange_swap_quote_consumer_test.ts @@ -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'; diff --git a/packages/asset-swapper/test/forwarder_swap_quote_consumer_test.ts b/packages/asset-swapper/test/forwarder_swap_quote_consumer_test.ts index f30e92eedc..9fef5a35b7 100644 --- a/packages/asset-swapper/test/forwarder_swap_quote_consumer_test.ts +++ b/packages/asset-swapper/test/forwarder_swap_quote_consumer_test.ts @@ -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'; diff --git a/packages/asset-swapper/test/swap_quote_consumer_utils_test.ts b/packages/asset-swapper/test/swap_quote_consumer_utils_test.ts index 62da155142..9ed535c7ce 100644 --- a/packages/asset-swapper/test/swap_quote_consumer_utils_test.ts +++ b/packages/asset-swapper/test/swap_quote_consumer_utils_test.ts @@ -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'; diff --git a/packages/contract-wrappers/package.json b/packages/contract-wrappers/package.json index 1bcd704998..423454efac 100644 --- a/packages/contract-wrappers/package.json +++ b/packages/contract-wrappers/package.json @@ -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/**/*", @@ -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" diff --git a/packages/contract-wrappers/src/coordinator_wrapper.ts b/packages/contract-wrappers/src/coordinator_wrapper.ts index 57d297c3ec..d6b323518a 100644 --- a/packages/contract-wrappers/src/coordinator_wrapper.ts +++ b/packages/contract-wrappers/src/coordinator_wrapper.ts @@ -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, @@ -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 @@ -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 } = {}; /** @@ -83,8 +82,6 @@ export class CoordinatorWrapper { this._web3Wrapper.getProvider(), this._web3Wrapper.getContractDefaults(), ); - - this._transactionEncoder = new TransactionEncoder(this._exchangeInstance); } /** @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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); @@ -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); @@ -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(); @@ -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(); @@ -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(); @@ -619,6 +658,10 @@ export class CoordinatorWrapper { return signerAddress; } + private _getAbiEncodedTransactionData(methodName: K, ...args: any): string { + return getAbiEncodedTransactionData(this._exchangeInstance, methodName, ...args); + } + private async _handleFillsAsync( data: string, takerAddress: string, diff --git a/packages/contract-wrappers/src/index.ts b/packages/contract-wrappers/src/index.ts index f539e99140..7ab661f92f 100644 --- a/packages/contract-wrappers/src/index.ts +++ b/packages/contract-wrappers/src/index.ts @@ -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'; diff --git a/packages/contract-wrappers/src/utils/getAbiEncodedTransactionData.ts b/packages/contract-wrappers/src/utils/getAbiEncodedTransactionData.ts new file mode 100644 index 0000000000..65948f3566 --- /dev/null +++ b/packages/contract-wrappers/src/utils/getAbiEncodedTransactionData.ts @@ -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(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 ''; + } +} diff --git a/packages/contract-wrappers/src/utils/transaction_encoder.ts b/packages/contract-wrappers/src/utils/transaction_encoder.ts deleted file mode 100644 index 48c0bc15c1..0000000000 --- a/packages/contract-wrappers/src/utils/transaction_encoder.ts +++ /dev/null @@ -1,297 +0,0 @@ -import { schemas } from '@0x/json-schemas'; -import { transactionHashUtils } from '@0x/order-utils'; -import { Order, SignedOrder } from '@0x/types'; -import { BigNumber } from '@0x/utils'; -import _ = require('lodash'); - -import { ExchangeContract } from '../index'; - -import { assert } from './assert'; - -/** - * Transaction Encoder. Transaction messages exist for the purpose of calling methods on the Exchange contract - * in the context of another address. For example, UserA can encode and sign a fillOrder transaction and UserB - * can submit this to the blockchain. The Exchange context executes as if UserA had directly submitted this transaction. - */ -export class TransactionEncoder { - private readonly _exchangeInstance: ExchangeContract; - constructor(exchangeInstance: ExchangeContract) { - this._exchangeInstance = exchangeInstance; - } - /** - * Hashes the transaction data for use with the Exchange contract. - * @param data The ABI Encoded 0x Exchange method. I.e fillOrder - * @param salt A random value to provide uniqueness and prevent replay attacks. - * @param signerAddress The address which will sign this transaction. - * @return The hash of the 0x transaction. - */ - public getTransactionHashHex(data: string, salt: BigNumber, signerAddress: string): string { - const exchangeAddress = this._getExchangeContract().address; - const transaction = { - verifyingContractAddress: exchangeAddress, - salt, - signerAddress, - data, - }; - const hashHex = transactionHashUtils.getTransactionHashHex(transaction); - return hashHex; - } - /** - * Encodes a fillOrder transaction. - * @param signedOrder An object that conforms to the SignedOrder interface. - * @param takerAssetFillAmount The amount of the order (in taker asset baseUnits) that you wish to fill. - * @return Hex encoded abi of the function call. - */ - public fillOrderTx(signedOrder: SignedOrder, takerAssetFillAmount: BigNumber): string { - assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); - assert.isValidBaseUnitAmount('takerAssetFillAmount', takerAssetFillAmount); - const abiEncodedData = this._getExchangeContract().fillOrder.getABIEncodedTransactionData( - signedOrder, - takerAssetFillAmount, - signedOrder.signature, - ); - return abiEncodedData; - } - /** - * Encodes a fillOrderNoThrow transaction. - * @param signedOrder An object that conforms to the SignedOrder interface. - * @param takerAssetFillAmount The amount of the order (in taker asset baseUnits) that you wish to fill. - * @return Hex encoded abi of the function call. - */ - public fillOrderNoThrowTx(signedOrder: SignedOrder, takerAssetFillAmount: BigNumber): string { - assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); - assert.isValidBaseUnitAmount('takerAssetFillAmount', takerAssetFillAmount); - const abiEncodedData = this._getExchangeContract().fillOrderNoThrow.getABIEncodedTransactionData( - signedOrder, - takerAssetFillAmount, - signedOrder.signature, - ); - return abiEncodedData; - } - /** - * Encodes a fillOrKillOrder transaction. - * @param signedOrder An object that conforms to the SignedOrder interface. - * @param takerAssetFillAmount The amount of the order (in taker asset baseUnits) that you wish to fill. - * @return Hex encoded abi of the function call. - */ - public fillOrKillOrderTx(signedOrder: SignedOrder, takerAssetFillAmount: BigNumber): string { - assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); - assert.isValidBaseUnitAmount('takerAssetFillAmount', takerAssetFillAmount); - const abiEncodedData = this._getExchangeContract().fillOrKillOrder.getABIEncodedTransactionData( - signedOrder, - takerAssetFillAmount, - signedOrder.signature, - ); - return abiEncodedData; - } - /** - * Encodes a batchFillOrders transaction. - * @param signedOrders An array of signed orders to fill. - * @param takerAssetFillAmounts The amounts of the orders (in taker asset baseUnits) that you wish to fill. - * @return Hex encoded abi of the function call. - */ - public batchFillOrdersTx(signedOrders: SignedOrder[], takerAssetFillAmounts: BigNumber[]): string { - assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); - _.forEach(takerAssetFillAmounts, takerAssetFillAmount => - assert.isBigNumber('takerAssetFillAmount', takerAssetFillAmount), - ); - const signatures = _.map(signedOrders, signedOrder => signedOrder.signature); - const abiEncodedData = this._getExchangeContract().batchFillOrders.getABIEncodedTransactionData( - signedOrders, - takerAssetFillAmounts, - signatures, - ); - return abiEncodedData; - } - /** - * Encodes a batchFillOrKillOrders transaction. - * @param signedOrders An array of signed orders to fill. - * @param takerAssetFillAmounts The amounts of the orders (in taker asset baseUnits) that you wish to fill. - * @return Hex encoded abi of the function call. - */ - public batchFillOrKillOrdersTx(signedOrders: SignedOrder[], takerAssetFillAmounts: BigNumber[]): string { - assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); - _.forEach(takerAssetFillAmounts, takerAssetFillAmount => - assert.isBigNumber('takerAssetFillAmount', takerAssetFillAmount), - ); - const signatures = _.map(signedOrders, signedOrder => signedOrder.signature); - const abiEncodedData = this._getExchangeContract().batchFillOrKillOrders.getABIEncodedTransactionData( - signedOrders, - takerAssetFillAmounts, - signatures, - ); - return abiEncodedData; - } - /** - * Encodes a batchFillOrdersNoThrow transaction. - * @param signedOrders An array of signed orders to fill. - * @param takerAssetFillAmounts The amounts of the orders (in taker asset baseUnits) that you wish to fill. - * @return Hex encoded abi of the function call. - */ - public batchFillOrdersNoThrowTx(signedOrders: SignedOrder[], takerAssetFillAmounts: BigNumber[]): string { - assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); - _.forEach(takerAssetFillAmounts, takerAssetFillAmount => - assert.isBigNumber('takerAssetFillAmount', takerAssetFillAmount), - ); - const signatures = _.map(signedOrders, signedOrder => signedOrder.signature); - const abiEncodedData = this._getExchangeContract().batchFillOrdersNoThrow.getABIEncodedTransactionData( - signedOrders, - takerAssetFillAmounts, - signatures, - ); - return abiEncodedData; - } - /** - * Encodes a batchCancelOrders transaction. - * @param signedOrders An array of orders to cancel. - * @return Hex encoded abi of the function call. - */ - public batchCancelOrdersTx(signedOrders: SignedOrder[]): string { - assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); - const abiEncodedData = this._getExchangeContract().batchCancelOrders.getABIEncodedTransactionData(signedOrders); - return abiEncodedData; - } - /** - * Encodes a cancelOrdersUpTo transaction. - * @param targetOrderEpoch Target order epoch. - * @return Hex encoded abi of the function call. - */ - public cancelOrdersUpToTx(targetOrderEpoch: BigNumber): string { - assert.isBigNumber('targetOrderEpoch', targetOrderEpoch); - const abiEncodedData = this._getExchangeContract().cancelOrdersUpTo.getABIEncodedTransactionData( - targetOrderEpoch, - ); - return abiEncodedData; - } - /** - * Encodes a cancelOrder transaction. - * @param order An object that conforms to the Order or SignedOrder interface. The order you would like to cancel. - * @return Hex encoded abi of the function call. - */ - public cancelOrderTx(order: Order | SignedOrder): string { - assert.doesConformToSchema('order', order, schemas.orderSchema); - const abiEncodedData = this._getExchangeContract().cancelOrder.getABIEncodedTransactionData(order); - return abiEncodedData; - } - /** - * Encodes a marketSellOrders transaction. - * @param signedOrders An array of signed orders to fill. - * @param takerAssetFillAmount Taker asset fill amount. - * @return Hex encoded abi of the function call. - */ - public marketSellOrdersTx(signedOrders: SignedOrder[], takerAssetFillAmount: BigNumber): string { - assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); - assert.isBigNumber('takerAssetFillAmount', takerAssetFillAmount); - const signatures = _.map(signedOrders, signedOrder => signedOrder.signature); - const abiEncodedData = this._getExchangeContract().marketSellOrders.getABIEncodedTransactionData( - signedOrders, - takerAssetFillAmount, - signatures, - ); - return abiEncodedData; - } - /** - * Encodes a marketSellOrdersNoThrow transaction. - * @param signedOrders An array of signed orders to fill. - * @param takerAssetFillAmount Taker asset fill amount. - * @return Hex encoded abi of the function call. - */ - public marketSellOrdersNoThrowTx(signedOrders: SignedOrder[], takerAssetFillAmount: BigNumber): string { - assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); - assert.isBigNumber('takerAssetFillAmount', takerAssetFillAmount); - const signatures = _.map(signedOrders, signedOrder => signedOrder.signature); - const abiEncodedData = this._getExchangeContract().marketSellOrdersNoThrow.getABIEncodedTransactionData( - signedOrders, - takerAssetFillAmount, - signatures, - ); - return abiEncodedData; - } - /** - * Encodes a maketBuyOrders transaction. - * @param signedOrders An array of signed orders to fill. - * @param makerAssetFillAmount Maker asset fill amount. - * @return Hex encoded abi of the function call. - */ - public marketBuyOrdersTx(signedOrders: SignedOrder[], makerAssetFillAmount: BigNumber): string { - assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); - assert.isBigNumber('makerAssetFillAmount', makerAssetFillAmount); - const signatures = _.map(signedOrders, signedOrder => signedOrder.signature); - const abiEncodedData = this._getExchangeContract().marketBuyOrders.getABIEncodedTransactionData( - signedOrders, - makerAssetFillAmount, - signatures, - ); - return abiEncodedData; - } - /** - * Encodes a maketBuyOrdersNoThrow transaction. - * @param signedOrders An array of signed orders to fill. - * @param makerAssetFillAmount Maker asset fill amount. - * @return Hex encoded abi of the function call. - */ - public marketBuyOrdersNoThrowTx(signedOrders: SignedOrder[], makerAssetFillAmount: BigNumber): string { - assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); - assert.isBigNumber('makerAssetFillAmount', makerAssetFillAmount); - const signatures = _.map(signedOrders, signedOrder => signedOrder.signature); - const abiEncodedData = this._getExchangeContract().marketBuyOrdersNoThrow.getABIEncodedTransactionData( - signedOrders, - makerAssetFillAmount, - signatures, - ); - return abiEncodedData; - } - /** - * Encodes a matchOrders transaction. - * @param leftOrder First order to match. - * @param rightOrder Second order to match. - * @return Hex encoded abi of the function call. - */ - public matchOrdersTx(leftOrder: SignedOrder, rightOrder: SignedOrder): string { - assert.doesConformToSchema('leftOrder', leftOrder, schemas.orderSchema); - assert.doesConformToSchema('rightOrder', rightOrder, schemas.orderSchema); - const abiEncodedData = this._getExchangeContract().matchOrders.getABIEncodedTransactionData( - leftOrder, - rightOrder, - leftOrder.signature, - rightOrder.signature, - ); - return abiEncodedData; - } - /** - * Encodes a preSign transaction. - * @param hash Hash to pre-sign - * @param signerAddress Address that should have signed the given hash. - * @param signature Proof that the hash has been signed by signer. - * @return Hex encoded abi of the function call. - */ - public preSignTx(hash: string, signerAddress: string, signature: string): string { - assert.isHexString('hash', hash); - assert.isETHAddressHex('signerAddress', signerAddress); - assert.isHexString('signature', signature); - const abiEncodedData = this._getExchangeContract().preSign.getABIEncodedTransactionData( - hash, - signerAddress, - signature, - ); - return abiEncodedData; - } - /** - * Encodes a setSignatureValidatorApproval transaction. - * @param validatorAddress Validator contract address. - * @param isApproved Boolean value to set approval to. - * @return Hex encoded abi of the function call. - */ - public setSignatureValidatorApprovalTx(validatorAddress: string, isApproved: boolean): string { - assert.isETHAddressHex('validatorAddress', validatorAddress); - assert.isBoolean('isApproved', isApproved); - const abiEncodedData = this._getExchangeContract().setSignatureValidatorApproval.getABIEncodedTransactionData( - validatorAddress, - isApproved, - ); - return abiEncodedData; - } - private _getExchangeContract(): ExchangeContract { - return this._exchangeInstance; - } -} diff --git a/packages/contract-wrappers/test/calldata_decoder_test.ts b/packages/contract-wrappers/test/calldata_decoder_test.ts index d9c0147f75..3657d52511 100644 --- a/packages/contract-wrappers/test/calldata_decoder_test.ts +++ b/packages/contract-wrappers/test/calldata_decoder_test.ts @@ -7,7 +7,8 @@ import * as chai from 'chai'; import * as _ from 'lodash'; import 'mocha'; -import { ContractAddresses, ContractWrappers, TransactionEncoder } from '../src'; +import { ContractAddresses, ContractWrappers } from '../src'; +import { getAbiEncodedTransactionData } from '../src/utils/getAbiEncodedTransactionData'; import { chaiSetup } from './utils/chai_setup'; import { migrateOnceAsync } from './utils/migrate'; @@ -80,8 +81,14 @@ describe('ABI Decoding Calldata', () => { blockPollingIntervalMs: 10, }; contractWrappers = new ContractWrappers(provider, config); - const transactionEncoder = new TransactionEncoder(contractWrappers.exchange); - matchOrdersTxData = transactionEncoder.matchOrdersTx(signedOrderLeft, signedOrderRight); + matchOrdersTxData = getAbiEncodedTransactionData( + contractWrappers.exchange, + 'matchOrders', + signedOrderLeft, + signedOrderRight, + signedOrderLeft.signature, + signedOrderRight.signature, + ); }); describe('decode', () => { diff --git a/packages/contract-wrappers/test/coordinator_wrapper_test.ts b/packages/contract-wrappers/test/coordinator_wrapper_test.ts index b21fd62d5b..c26f81e7a8 100644 --- a/packages/contract-wrappers/test/coordinator_wrapper_test.ts +++ b/packages/contract-wrappers/test/coordinator_wrapper_test.ts @@ -1,11 +1,11 @@ import { CoordinatorRegistryContract } from '@0x/abi-gen-wrappers'; import { constants } from '@0x/contracts-test-utils'; import { defaultOrmConfig, getAppAsync } from '@0x/coordinator-server'; -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 { SignedOrder } from '@0x/types'; -import { BigNumber, fetchAsync, logUtils, tokenUtils } from '@0x/utils'; +import { BigNumber, fetchAsync, logUtils } from '@0x/utils'; import * as chai from 'chai'; import * as http from 'http'; import 'mocha'; diff --git a/packages/dev-utils/CHANGELOG.json b/packages/dev-utils/CHANGELOG.json index b585824eef..8f4e259934 100644 --- a/packages/dev-utils/CHANGELOG.json +++ b/packages/dev-utils/CHANGELOG.json @@ -1,4 +1,13 @@ [ + { + "version": "2.3.0", + "changes": [ + { + "note": "Move `tokenUtils` here from `@0x/contract-wrappers`", + "pr": 2037 + } + ] + }, { "timestamp": 1564604963, "version": "2.2.6", diff --git a/packages/dev-utils/src/index.ts b/packages/dev-utils/src/index.ts index d4c19f1bf2..5372501f2c 100644 --- a/packages/dev-utils/src/index.ts +++ b/packages/dev-utils/src/index.ts @@ -3,3 +3,4 @@ export { web3Factory } from './web3_factory'; export { constants as devConstants } from './constants'; export { env, EnvVars } from './env'; export { callbackErrorReporter } from './callback_error_reporter'; +export { tokenUtils } from './token_utils'; diff --git a/packages/utils/src/token_utils.ts b/packages/dev-utils/src/token_utils.ts similarity index 100% rename from packages/utils/src/token_utils.ts rename to packages/dev-utils/src/token_utils.ts diff --git a/packages/order-watcher/test/expiration_watcher_test.ts b/packages/order-watcher/test/expiration_watcher_test.ts index 9c301b6e32..885994ce8f 100644 --- a/packages/order-watcher/test/expiration_watcher_test.ts +++ b/packages/order-watcher/test/expiration_watcher_test.ts @@ -1,8 +1,8 @@ -import { BlockchainLifecycle, callbackErrorReporter } from '@0x/dev-utils'; +import { BlockchainLifecycle, callbackErrorReporter, tokenUtils } from '@0x/dev-utils'; import { FillScenarios } from '@0x/fill-scenarios'; import { assetDataUtils, orderHashUtils } from '@0x/order-utils'; import { DoneCallback } from '@0x/types'; -import { BigNumber, tokenUtils } from '@0x/utils'; +import { BigNumber } from '@0x/utils'; import * as chai from 'chai'; import * as _ from 'lodash'; import 'mocha'; diff --git a/packages/order-watcher/test/order_watcher_test.ts b/packages/order-watcher/test/order_watcher_test.ts index 00c8993a23..6776ee0b4c 100644 --- a/packages/order-watcher/test/order_watcher_test.ts +++ b/packages/order-watcher/test/order_watcher_test.ts @@ -1,6 +1,6 @@ // tslint:disable:no-unnecessary-type-assertion import { ContractAddresses, ContractWrappers } from '@0x/contract-wrappers'; -import { BlockchainLifecycle, callbackErrorReporter } from '@0x/dev-utils'; +import { BlockchainLifecycle, callbackErrorReporter, tokenUtils } 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'; @@ -12,7 +12,7 @@ import { OrderStateValid, SignedOrder, } from '@0x/types'; -import { BigNumber, tokenUtils } from '@0x/utils'; +import { BigNumber } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import * as chai from 'chai'; import * as _ from 'lodash'; diff --git a/packages/order-watcher/test/order_watcher_web_socket_server_test.ts b/packages/order-watcher/test/order_watcher_web_socket_server_test.ts index c0f885658a..60a3204a98 100644 --- a/packages/order-watcher/test/order_watcher_web_socket_server_test.ts +++ b/packages/order-watcher/test/order_watcher_web_socket_server_test.ts @@ -1,10 +1,10 @@ import { ContractAddresses } from '@0x/contract-addresses'; import { 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, orderHashUtils } from '@0x/order-utils'; import { ExchangeContractErrs, OrderStateInvalid, SignedOrder } from '@0x/types'; -import { BigNumber, logUtils, tokenUtils } from '@0x/utils'; +import { BigNumber, logUtils } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import * as chai from 'chai'; import 'mocha'; diff --git a/packages/utils/CHANGELOG.json b/packages/utils/CHANGELOG.json index 5ecfc39153..74af4d2f58 100644 --- a/packages/utils/CHANGELOG.json +++ b/packages/utils/CHANGELOG.json @@ -9,10 +9,6 @@ { "note": "Throw exception when trying to decode beyond boundaries of calldata", "pr": 2018 - }, - { - "note": "Move `tokenUtils` here from `@0x/contract-wrappers`", - "pr": 2037 } ] }, diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 18f7e0308c..b757439a2f 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -11,7 +11,6 @@ export { abiUtils } from './abi_utils'; export { NULL_BYTES } from './constants'; export { errorUtils } from './error_utils'; export { fetchAsync } from './fetch_async'; -export { tokenUtils } from './token_utils'; export { signTypedDataUtils } from './sign_typed_data_utils'; export import AbiEncoder = require('./abi_encoder'); export * from './types';