diff options
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r-- | src/contract_wrappers/contract_wrapper.ts | 47 | ||||
-rw-r--r-- | src/contract_wrappers/ether_token_wrapper.ts | 4 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 76 | ||||
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 4 | ||||
-rw-r--r-- | src/contract_wrappers/token_transfer_proxy_wrapper.ts | 4 | ||||
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 4 |
6 files changed, 54 insertions, 85 deletions
diff --git a/src/contract_wrappers/contract_wrapper.ts b/src/contract_wrappers/contract_wrapper.ts index 28df82cee..3de26148f 100644 --- a/src/contract_wrappers/contract_wrapper.ts +++ b/src/contract_wrappers/contract_wrapper.ts @@ -1,7 +1,7 @@ import * as _ from 'lodash'; -import contract = require('truffle-contract'); +import * as Web3 from 'web3'; import {Web3Wrapper} from '../web3_wrapper'; -import {ZeroExError, Artifact, ContractInstance} from '../types'; +import {ZeroExError} from '../types'; import {utils} from '../utils/utils'; export class ContractWrapper { @@ -11,43 +11,10 @@ export class ContractWrapper { this._web3Wrapper = web3Wrapper; this._gasPrice = gasPrice; } - protected async _instantiateContractIfExistsAsync(artifact: Artifact, address?: string): Promise<ContractInstance> { - const c = await contract(artifact); - c.defaults({ - gasPrice: this._gasPrice, - }); - const providerObj = this._web3Wrapper.getCurrentProvider(); - c.setProvider(providerObj); - - const networkIdIfExists = await this._web3Wrapper.getNetworkIdIfExistsAsync(); - const artifactNetworkConfigs = _.isUndefined(networkIdIfExists) ? - undefined : - artifact.networks[networkIdIfExists]; - let contractAddress; - if (!_.isUndefined(address)) { - contractAddress = address; - } else if (!_.isUndefined(artifactNetworkConfigs)) { - contractAddress = artifactNetworkConfigs.address.toLowerCase(); - } - - if (!_.isUndefined(contractAddress)) { - const doesContractExist = await this._web3Wrapper.doesContractExistAtAddressAsync(contractAddress); - if (!doesContractExist) { - throw new Error(ZeroExError.ContractDoesNotExist); - } - } - - try { - const contractInstance = _.isUndefined(address) ? await c.deployed() : await c.at(address); - return contractInstance; - } catch (err) { - const errMsg = `${err}`; - if (_.includes(errMsg, 'not been deployed to detected network')) { - throw new Error(ZeroExError.ContractDoesNotExist); - } else { - utils.consoleLog(`Notice: Error encountered: ${err} ${err.stack}`); - throw new Error(ZeroExError.UnhandledError); - } - } + protected async _instantiateContractIfExistsAsync<A extends Web3.ContractInstance>(artifact: Artifact, + address?: string): Promise<A> { + const contractInstance = + await this._web3Wrapper.getContractInstanceFromArtifactAsync<A>(artifact, address); + return contractInstance; } } diff --git a/src/contract_wrappers/ether_token_wrapper.ts b/src/contract_wrappers/ether_token_wrapper.ts index 3c282510f..a2486b15e 100644 --- a/src/contract_wrappers/ether_token_wrapper.ts +++ b/src/contract_wrappers/ether_token_wrapper.ts @@ -71,7 +71,9 @@ export class EtherTokenWrapper extends ContractWrapper { if (!_.isUndefined(this._etherTokenContractIfExists)) { return this._etherTokenContractIfExists; } - const contractInstance = await this._instantiateContractIfExistsAsync((EtherTokenArtifacts as any)); + const contractInstance = await this._instantiateContractIfExistsAsync<EtherTokenContract>( + EtherTokenArtifacts as any as Artifact, + ); this._etherTokenContractIfExists = contractInstance as EtherTokenContract; return this._etherTokenContractIfExists; } diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index d09df236b..1196394e9 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -141,12 +141,12 @@ export class ExchangeWrapper extends ContractWrapper { * @param takerAddress The user Ethereum address who would like to fill this order. * Must be available via the supplied Web3.Provider * passed to 0x.js. - * @return The amount of the order that was filled (in taker token baseUnits). + * @return Transaction hash. */ @decorators.contractCallErrorHandler public async fillOrderAsync(signedOrder: SignedOrder, fillTakerTokenAmount: BigNumber.BigNumber, shouldThrowOnInsufficientBalanceOrAllowance: boolean, - takerAddress: string): Promise<BigNumber.BigNumber> { + takerAddress: string): Promise<string> { assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); assert.isBigNumber('fillTakerTokenAmount', fillTakerTokenAmount); assert.isBoolean('shouldThrowOnInsufficientBalanceOrAllowance', shouldThrowOnInsufficientBalanceOrAllowance); @@ -169,7 +169,7 @@ export class ExchangeWrapper extends ContractWrapper { from: takerAddress, }, ); - const response: ContractResponse = await exchangeInstance.fillOrder( + const txHash: string = await exchangeInstance.fillOrder( orderAddresses, orderValues, fillTakerTokenAmount, @@ -182,10 +182,7 @@ export class ExchangeWrapper extends ContractWrapper { gas, }, ); - this._throwErrorLogsAsErrors(response.logs); - const logFillArgs = response.logs[0].args as LogFillContractEventArgs; - const filledTakerTokenAmount = new BigNumber(logFillArgs.filledTakerTokenAmount); - return filledTakerTokenAmount; + return txHash; } /** * Sequentially and atomically fills signedOrders up to the specified takerTokenFillAmount. @@ -201,12 +198,12 @@ export class ExchangeWrapper extends ContractWrapper { * @param takerAddress The user Ethereum address who would like to fill these * orders. Must be available via the supplied Web3.Provider * passed to 0x.js. - * @return The amount of the orders that was filled (in taker token baseUnits). + * @return Transaction hash. */ @decorators.contractCallErrorHandler public async fillOrdersUpToAsync(signedOrders: SignedOrder[], fillTakerTokenAmount: BigNumber.BigNumber, shouldThrowOnInsufficientBalanceOrAllowance: boolean, - takerAddress: string): Promise<BigNumber.BigNumber> { + takerAddress: string): Promise<string> { assert.doesConformToSchema('signedOrders', signedOrders, schemas.signedOrdersSchema); const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress); assert.hasAtMostOneUniqueValue(takerTokenAddresses, @@ -222,7 +219,7 @@ export class ExchangeWrapper extends ContractWrapper { signedOrder, fillTakerTokenAmount, takerAddress); } if (_.isEmpty(signedOrders)) { - return new BigNumber(0); // no-op + throw new Error(ExchangeContractErrs.BatchOrdersMustHaveAtLeastOneItem); } const orderAddressesValuesAndSignatureArray = _.map(signedOrders, signedOrder => { @@ -251,7 +248,7 @@ export class ExchangeWrapper extends ContractWrapper { from: takerAddress, }, ); - const response: ContractResponse = await exchangeInstance.fillOrdersUpTo( + const txHash = await exchangeInstance.fillOrdersUpTo( orderAddressesArray, orderValuesArray, fillTakerTokenAmount, @@ -264,13 +261,7 @@ export class ExchangeWrapper extends ContractWrapper { gas, }, ); - this._throwErrorLogsAsErrors(response.logs); - let filledTakerTokenAmount = new BigNumber(0); - _.each(response.logs, log => { - filledTakerTokenAmount = filledTakerTokenAmount.plus( - (log.args as LogFillContractEventArgs).filledTakerTokenAmount); - }); - return filledTakerTokenAmount; + return txHash; } /** * Batch version of fillOrderAsync. @@ -288,11 +279,12 @@ export class ExchangeWrapper extends ContractWrapper { * @param takerAddress The user Ethereum address who would like to fill * these orders. Must be available via the supplied * Web3.Provider passed to 0x.js. + * @return Transaction hash. */ @decorators.contractCallErrorHandler public async batchFillOrdersAsync(orderFillRequests: OrderFillRequest[], shouldThrowOnInsufficientBalanceOrAllowance: boolean, - takerAddress: string): Promise<void> { + takerAddress: string): Promise<string> { assert.doesConformToSchema('orderFillRequests', orderFillRequests, schemas.orderFillRequestsSchema); const exchangeContractAddresses = _.map( orderFillRequests, @@ -307,7 +299,7 @@ export class ExchangeWrapper extends ContractWrapper { orderFillRequest.signedOrder, orderFillRequest.takerTokenFillAmount, takerAddress); } if (_.isEmpty(orderFillRequests)) { - return; // no-op + throw new Error(ExchangeContractErrs.BatchOrdersMustHaveAtLeastOneItem); } const orderAddressesValuesAmountsAndSignatureArray = _.map(orderFillRequests, orderFillRequest => { @@ -337,7 +329,7 @@ export class ExchangeWrapper extends ContractWrapper { from: takerAddress, }, ); - const response: ContractResponse = await exchangeInstance.batchFillOrders( + const txHash = await exchangeInstance.batchFillOrders( orderAddressesArray, orderValuesArray, fillTakerTokenAmounts, @@ -350,7 +342,7 @@ export class ExchangeWrapper extends ContractWrapper { gas, }, ); - this._throwErrorLogsAsErrors(response.logs); + return txHash; } /** * Attempts to fill a specific amount of an order. If the entire amount specified cannot be filled, @@ -360,10 +352,11 @@ export class ExchangeWrapper extends ContractWrapper { * @param fillTakerTokenAmount The total amount of the takerTokens you would like to fill. * @param takerAddress The user Ethereum address who would like to fill this order. * Must be available via the supplied Web3.Provider passed to 0x.js. + * @return Transaction hash. */ @decorators.contractCallErrorHandler public async fillOrKillOrderAsync(signedOrder: SignedOrder, fillTakerTokenAmount: BigNumber.BigNumber, - takerAddress: string): Promise<void> { + takerAddress: string): Promise<string> { assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); assert.isBigNumber('fillTakerTokenAmount', fillTakerTokenAmount); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); @@ -385,7 +378,7 @@ export class ExchangeWrapper extends ContractWrapper { from: takerAddress, }, ); - const response: ContractResponse = await exchangeInstance.fillOrKillOrder( + const txHash = await exchangeInstance.fillOrKillOrder( orderAddresses, orderValues, fillTakerTokenAmount, @@ -397,7 +390,7 @@ export class ExchangeWrapper extends ContractWrapper { gas, }, ); - this._throwErrorLogsAsErrors(response.logs); + return txHash; } /** * Batch version of fillOrKill. Allows a taker to specify a batch of orders that will either be atomically @@ -405,10 +398,11 @@ export class ExchangeWrapper extends ContractWrapper { * @param orderFillOrKillRequests An array of objects that conform to the OrderFillOrKillRequest interface. * @param takerAddress The user Ethereum address who would like to fill there orders. * Must be available via the supplied Web3.Provider passed to 0x.js. + * @return Transaction hash. */ @decorators.contractCallErrorHandler public async batchFillOrKillAsync(orderFillOrKillRequests: OrderFillOrKillRequest[], - takerAddress: string): Promise<void> { + takerAddress: string): Promise<string> { assert.doesConformToSchema('orderFillOrKillRequests', orderFillOrKillRequests, schemas.orderFillOrKillRequestsSchema); const exchangeContractAddresses = _.map( @@ -419,7 +413,7 @@ export class ExchangeWrapper extends ContractWrapper { ExchangeContractErrs.BatchOrdersMustHaveSameExchangeAddress); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); if (_.isEmpty(orderFillOrKillRequests)) { - return; // no-op + throw new Error(ExchangeContractErrs.BatchOrdersMustHaveAtLeastOneItem); } const exchangeInstance = await this._getExchangeContractAsync(); for (const request of orderFillOrKillRequests) { @@ -452,7 +446,7 @@ export class ExchangeWrapper extends ContractWrapper { from: takerAddress, }, ); - const response: ContractResponse = await exchangeInstance.batchFillOrKillOrders( + const txHash = await exchangeInstance.batchFillOrKillOrders( orderAddresses, orderValues, fillTakerTokenAmounts, @@ -464,18 +458,18 @@ export class ExchangeWrapper extends ContractWrapper { gas, }, ); - this._throwErrorLogsAsErrors(response.logs); + return txHash; } /** * Cancel a given fill amount of an order. Cancellations are cumulative. * @param order An object that conforms to the Order or SignedOrder interface. * The order you would like to cancel. * @param cancelTakerTokenAmount The amount (specified in taker tokens) that you would like to cancel. - * @return The amount of the order that was cancelled (in taker token baseUnits). + * @return Transaction hash. */ @decorators.contractCallErrorHandler public async cancelOrderAsync( - order: Order|SignedOrder, cancelTakerTokenAmount: BigNumber.BigNumber): Promise<BigNumber.BigNumber> { + order: Order|SignedOrder, cancelTakerTokenAmount: BigNumber.BigNumber): Promise<string> { assert.doesConformToSchema('order', order, schemas.orderSchema); assert.isBigNumber('takerTokenCancelAmount', cancelTakerTokenAmount); await assert.isSenderAddressAsync('order.maker', order.maker, this._web3Wrapper); @@ -492,7 +486,7 @@ export class ExchangeWrapper extends ContractWrapper { from: order.maker, }, ); - const response: ContractResponse = await exchangeInstance.cancelOrder( + const txHash = await exchangeInstance.cancelOrder( orderAddresses, orderValues, cancelTakerTokenAmount, @@ -501,19 +495,17 @@ export class ExchangeWrapper extends ContractWrapper { gas, }, ); - this._throwErrorLogsAsErrors(response.logs); - const logFillArgs = response.logs[0].args as LogCancelContractEventArgs; - const cancelledTakerTokenAmount = new BigNumber(logFillArgs.cancelledTakerTokenAmount); - return cancelledTakerTokenAmount; + return txHash; } /** * Batch version of cancelOrderAsync. Atomically cancels multiple orders in a single transaction. * All orders must be from the same maker. * @param orderCancellationRequests An array of objects that conform to the OrderCancellationRequest * interface. + * @return Transaction hash. */ @decorators.contractCallErrorHandler - public async batchCancelOrdersAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise<void> { + public async batchCancelOrdersAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise<string> { assert.doesConformToSchema('orderCancellationRequests', orderCancellationRequests, schemas.orderCancellationRequestsSchema); const exchangeContractAddresses = _.map( @@ -532,7 +524,7 @@ export class ExchangeWrapper extends ContractWrapper { ); } if (_.isEmpty(orderCancellationRequests)) { - return; // no-op + throw new Error(ExchangeContractErrs.BatchOrdersMustHaveAtLeastOneItem); } const exchangeInstance = await this._getExchangeContractAsync(); const orderAddressesValuesAndTakerTokenCancelAmounts = _.map(orderCancellationRequests, cancellationRequest => { @@ -552,7 +544,7 @@ export class ExchangeWrapper extends ContractWrapper { from: maker, }, ); - const response: ContractResponse = await exchangeInstance.batchCancelOrders( + const txHash = await exchangeInstance.batchCancelOrders( orderAddresses, orderValues, cancelTakerTokenAmounts, @@ -561,7 +553,7 @@ export class ExchangeWrapper extends ContractWrapper { gas, }, ); - this._throwErrorLogsAsErrors(response.logs); + return txHash; } /** * Subscribe to an event type emitted by the Exchange smart contract @@ -730,7 +722,9 @@ export class ExchangeWrapper extends ContractWrapper { if (!_.isUndefined(this._exchangeContractIfExists)) { return this._exchangeContractIfExists; } - const contractInstance = await this._instantiateContractIfExistsAsync((ExchangeArtifacts as any)); + const contractInstance = await this._instantiateContractIfExistsAsync<ExchangeContract>( + (ExchangeArtifacts as any), + ); this._exchangeContractIfExists = contractInstance as ExchangeContract; return this._exchangeContractIfExists; } diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts index 822e69460..1550bfa3e 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -101,7 +101,9 @@ export class TokenRegistryWrapper extends ContractWrapper { if (!_.isUndefined(this._tokenRegistryContractIfExists)) { return this._tokenRegistryContractIfExists; } - const contractInstance = await this._instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); + const contractInstance = await this._instantiateContractIfExistsAsync<TokenRegistryContract>( + TokenRegistryArtifacts as any as Artifact, + ); this._tokenRegistryContractIfExists = contractInstance as TokenRegistryContract; return this._tokenRegistryContractIfExists; } diff --git a/src/contract_wrappers/token_transfer_proxy_wrapper.ts b/src/contract_wrappers/token_transfer_proxy_wrapper.ts index da17d79ff..2b4b32961 100644 --- a/src/contract_wrappers/token_transfer_proxy_wrapper.ts +++ b/src/contract_wrappers/token_transfer_proxy_wrapper.ts @@ -44,7 +44,9 @@ export class TokenTransferProxyWrapper extends ContractWrapper { if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) { return this._tokenTransferProxyContractIfExists; } - const contractInstance = await this._instantiateContractIfExistsAsync((TokenTransferProxyArtifacts as any)); + const contractInstance = await this._instantiateContractIfExistsAsync<TokenTransferProxyContract>( + TokenTransferProxyArtifacts as any as Artifact, + ); this._tokenTransferProxyContractIfExists = contractInstance as TokenTransferProxyContract; return this._tokenTransferProxyContractIfExists; } diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index f7070f1f4..e98744cbc 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -267,7 +267,9 @@ export class TokenWrapper extends ContractWrapper { if (!_.isUndefined(tokenContract)) { return tokenContract; } - const contractInstance = await this._instantiateContractIfExistsAsync((TokenArtifacts as any), tokenAddress); + const contractInstance = await this._instantiateContractIfExistsAsync<TokenContract>( + TokenArtifacts as any as Artifact, tokenAddress, + ); tokenContract = contractInstance as TokenContract; this._tokenContractsByAddress[tokenAddress] = tokenContract; return tokenContract; |