diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-06-29 16:48:18 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-06-29 22:52:54 +0800 |
commit | f7b3fc58bc24decc37f7c64b15573a5ec90e1ceb (patch) | |
tree | 169b1d821a6b48065f436b0d262de6f11852f1d2 /packages/contract-wrappers | |
parent | de4049733075d4750617c5129f3c650cea941295 (diff) | |
download | dexon-0x-contracts-f7b3fc58bc24decc37f7c64b15573a5ec90e1ceb.tar dexon-0x-contracts-f7b3fc58bc24decc37f7c64b15573a5ec90e1ceb.tar.gz dexon-0x-contracts-f7b3fc58bc24decc37f7c64b15573a5ec90e1ceb.tar.bz2 dexon-0x-contracts-f7b3fc58bc24decc37f7c64b15573a5ec90e1ceb.tar.lz dexon-0x-contracts-f7b3fc58bc24decc37f7c64b15573a5ec90e1ceb.tar.xz dexon-0x-contracts-f7b3fc58bc24decc37f7c64b15573a5ec90e1ceb.tar.zst dexon-0x-contracts-f7b3fc58bc24decc37f7c64b15573a5ec90e1ceb.zip |
Add assertions for txOpts and methodOpts
Diffstat (limited to 'packages/contract-wrappers')
4 files changed, 75 insertions, 38 deletions
diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts index 315b0314a..014e96fa3 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts @@ -5,6 +5,8 @@ import { ContractAbi, LogWithDecodedArgs } from 'ethereum-types'; import * as _ from 'lodash'; import { artifacts } from '../artifacts'; +import { methodOptsSchema } from '../schemas/method_opts_schema'; +import { txOptsSchema } from '../schemas/tx_opts_schema'; import { BlockRange, ContractWrappersError, @@ -49,6 +51,9 @@ export class ERC20TokenWrapper extends ContractWrapper { ): Promise<BigNumber> { assert.isETHAddressHex('ownerAddress', ownerAddress); assert.isETHAddressHex('tokenAddress', tokenAddress); + if (!_.isUndefined(methodOpts)) { + assert.doesConformToSchema('methodOpts', methodOpts, methodOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedOwnerAddress = ownerAddress.toLowerCase(); @@ -81,6 +86,9 @@ export class ERC20TokenWrapper extends ContractWrapper { assert.isETHAddressHex('spenderAddress', spenderAddress); assert.isETHAddressHex('tokenAddress', tokenAddress); await assert.isSenderAddressAsync('ownerAddress', ownerAddress, this._web3Wrapper); + if (!_.isUndefined(txOpts)) { + assert.doesConformToSchema('txOpts', txOpts, txOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedSpenderAddress = spenderAddress.toLowerCase(); const normalizedOwnerAddress = ownerAddress.toLowerCase(); @@ -112,16 +120,10 @@ export class ERC20TokenWrapper extends ContractWrapper { spenderAddress: string, txOpts: TransactionOpts = {}, ): Promise<string> { - assert.isETHAddressHex('ownerAddress', ownerAddress); - assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.isETHAddressHex('spenderAddress', spenderAddress); - const normalizedTokenAddress = tokenAddress.toLowerCase(); - const normalizedOwnerAddress = ownerAddress.toLowerCase(); - const normalizedSpenderAddress = spenderAddress.toLowerCase(); const txHash = await this.setAllowanceAsync( - normalizedTokenAddress, - normalizedOwnerAddress, - normalizedSpenderAddress, + tokenAddress, + ownerAddress, + spenderAddress, this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS, txOpts, ); @@ -144,6 +146,9 @@ export class ERC20TokenWrapper extends ContractWrapper { assert.isETHAddressHex('ownerAddress', ownerAddress); assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('spenderAddress', spenderAddress); + if (!_.isUndefined(methodOpts)) { + assert.doesConformToSchema('methodOpts', methodOpts, methodOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedOwnerAddress = ownerAddress.toLowerCase(); const normalizedSpenderAddress = spenderAddress.toLowerCase(); @@ -172,18 +177,8 @@ export class ERC20TokenWrapper extends ContractWrapper { ownerAddress: string, methodOpts?: MethodOpts, ): Promise<BigNumber> { - assert.isETHAddressHex('ownerAddress', ownerAddress); - assert.isETHAddressHex('tokenAddress', tokenAddress); - const normalizedTokenAddress = tokenAddress.toLowerCase(); - const normalizedOwnerAddress = ownerAddress.toLowerCase(); - const proxyAddress = this._erc20ProxyWrapper.getContractAddress(); - const allowanceInBaseUnits = await this.getAllowanceAsync( - normalizedTokenAddress, - normalizedOwnerAddress, - proxyAddress, - methodOpts, - ); + const allowanceInBaseUnits = await this.getAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, methodOpts); return allowanceInBaseUnits; } /** @@ -202,16 +197,10 @@ export class ERC20TokenWrapper extends ContractWrapper { amountInBaseUnits: BigNumber, txOpts: TransactionOpts = {}, ): Promise<string> { - assert.isETHAddressHex('ownerAddress', ownerAddress); - assert.isETHAddressHex('tokenAddress', tokenAddress); - const normalizedTokenAddress = tokenAddress.toLowerCase(); - const normalizedOwnerAddress = ownerAddress.toLowerCase(); - assert.isValidBaseUnitAmount('amountInBaseUnits', amountInBaseUnits); - const proxyAddress = this._erc20ProxyWrapper.getContractAddress(); const txHash = await this.setAllowanceAsync( - normalizedTokenAddress, - normalizedOwnerAddress, + tokenAddress, + ownerAddress, proxyAddress, amountInBaseUnits, txOpts, @@ -234,13 +223,9 @@ export class ERC20TokenWrapper extends ContractWrapper { ownerAddress: string, txOpts: TransactionOpts = {}, ): Promise<string> { - assert.isETHAddressHex('ownerAddress', ownerAddress); - assert.isETHAddressHex('tokenAddress', tokenAddress); - const normalizedTokenAddress = tokenAddress.toLowerCase(); - const normalizedOwnerAddress = ownerAddress.toLowerCase(); const txHash = await this.setProxyAllowanceAsync( - normalizedTokenAddress, - normalizedOwnerAddress, + tokenAddress, + ownerAddress, this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS, txOpts, ); @@ -265,6 +250,9 @@ export class ERC20TokenWrapper extends ContractWrapper { assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('toAddress', toAddress); await assert.isSenderAddressAsync('fromAddress', fromAddress, this._web3Wrapper); + if (!_.isUndefined(txOpts)) { + assert.doesConformToSchema('txOpts', txOpts, txOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedFromAddress = fromAddress.toLowerCase(); const normalizedToAddress = toAddress.toLowerCase(); @@ -310,6 +298,9 @@ export class ERC20TokenWrapper extends ContractWrapper { assert.isETHAddressHex('fromAddress', fromAddress); assert.isETHAddressHex('tokenAddress', tokenAddress); await assert.isSenderAddressAsync('senderAddress', senderAddress, this._web3Wrapper); + if (!_.isUndefined(txOpts)) { + assert.doesConformToSchema('txOpts', txOpts, txOptsSchema); + } const normalizedToAddress = toAddress.toLowerCase(); const normalizedFromAddress = fromAddress.toLowerCase(); const normalizedTokenAddress = tokenAddress.toLowerCase(); diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts index 8164e3df3..f32827191 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts @@ -6,6 +6,8 @@ import * as _ from 'lodash'; import { constants } from '../../test/utils/constants'; import { artifacts } from '../artifacts'; +import { methodOptsSchema } from '../schemas/method_opts_schema'; +import { txOptsSchema } from '../schemas/tx_opts_schema'; import { BlockRange, ContractWrappersError, @@ -49,6 +51,9 @@ export class ERC721TokenWrapper extends ContractWrapper { ): Promise<BigNumber> { assert.isETHAddressHex('ownerAddress', ownerAddress); assert.isETHAddressHex('tokenAddress', tokenAddress); + if (!_.isUndefined(methodOpts)) { + assert.doesConformToSchema('methodOpts', methodOpts, methodOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedOwnerAddress = ownerAddress.toLowerCase(); @@ -71,6 +76,9 @@ export class ERC721TokenWrapper extends ContractWrapper { public async getOwnerOfAsync(tokenAddress: string, tokenId: BigNumber, methodOpts?: MethodOpts): Promise<string> { assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isBigNumber('tokenId', tokenId); + if (!_.isUndefined(methodOpts)) { + assert.doesConformToSchema('methodOpts', methodOpts, methodOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const tokenContract = await this._getTokenContractAsync(normalizedTokenAddress); @@ -100,6 +108,9 @@ export class ERC721TokenWrapper extends ContractWrapper { assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('ownerAddress', ownerAddress); assert.isETHAddressHex('operatorAddress', operatorAddress); + if (!_.isUndefined(methodOpts)) { + assert.doesConformToSchema('methodOpts', methodOpts, methodOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedOwnerAddress = ownerAddress.toLowerCase(); const normalizedOperatorAddress = operatorAddress.toLowerCase(); @@ -127,10 +138,17 @@ export class ERC721TokenWrapper extends ContractWrapper { ownerAddress: string, methodOpts?: MethodOpts, ): Promise<boolean> { + assert.isETHAddressHex('tokenAddress', tokenAddress); + assert.isETHAddressHex('ownerAddress', ownerAddress); + if (!_.isUndefined(methodOpts)) { + assert.doesConformToSchema('methodOpts', methodOpts, methodOptsSchema); + } + const normalizedTokenAddress = tokenAddress.toLowerCase(); + const normalizedOwnerAddress = ownerAddress.toLowerCase(); const proxyAddress = this._erc721ProxyWrapper.getContractAddress(); const isProxyApprovedForAll = await this.isApprovedForAllAsync( - tokenAddress, - ownerAddress, + normalizedTokenAddress, + normalizedOwnerAddress, proxyAddress, methodOpts, ); @@ -151,6 +169,9 @@ export class ERC721TokenWrapper extends ContractWrapper { ): Promise<string | undefined> { assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isBigNumber('tokenId', tokenId); + if (!_.isUndefined(methodOpts)) { + assert.doesConformToSchema('methodOpts', methodOpts, methodOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const tokenContract = await this._getTokenContractAsync(normalizedTokenAddress); @@ -199,9 +220,12 @@ export class ERC721TokenWrapper extends ContractWrapper { txOpts: TransactionOpts = {}, ): Promise<string> { assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.isETHAddressHex('ownerAddress', ownerAddress); + await assert.isSenderAddressAsync('ownerAddress', ownerAddress, this._web3Wrapper); assert.isETHAddressHex('operatorAddress', operatorAddress); assert.isBoolean('isApproved', isApproved); + if (!_.isUndefined(txOpts)) { + assert.doesConformToSchema('txOpts', txOpts, txOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedOwnerAddress = ownerAddress.toLowerCase(); const normalizedOperatorAddress = operatorAddress.toLowerCase(); @@ -260,11 +284,15 @@ export class ERC721TokenWrapper extends ContractWrapper { assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('approvedAddress', approvedAddress); assert.isBigNumber('tokenId', tokenId); + if (!_.isUndefined(txOpts)) { + assert.doesConformToSchema('txOpts', txOpts, txOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedApprovedAddress = approvedAddress.toLowerCase(); const tokenContract = await this._getTokenContractAsync(normalizedTokenAddress); const tokenOwnerAddress = await tokenContract.ownerOf.callAsync(tokenId); + await assert.isSenderAddressAsync('tokenOwnerAddress', tokenOwnerAddress, this._web3Wrapper); const txHash = await tokenContract.approve.sendTransactionAsync(normalizedApprovedAddress, tokenId, { gas: txOpts.gasLimit, gasPrice: txOpts.gasPrice, @@ -311,7 +339,10 @@ export class ERC721TokenWrapper extends ContractWrapper { ): Promise<string> { assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('receiverAddress', receiverAddress); - assert.isETHAddressHex('senderAddress', senderAddress); + await assert.isSenderAddressAsync('senderAddress', senderAddress, this._web3Wrapper); + if (!_.isUndefined(txOpts)) { + assert.doesConformToSchema('txOpts', txOpts, txOptsSchema); + } const normalizedTokenAddress = tokenAddress.toLowerCase(); const normalizedReceiverAddress = receiverAddress.toLowerCase(); const normalizedSenderAddress = senderAddress.toLowerCase(); diff --git a/packages/contract-wrappers/src/schemas/method_opts_schema.ts b/packages/contract-wrappers/src/schemas/method_opts_schema.ts new file mode 100644 index 000000000..ef434070a --- /dev/null +++ b/packages/contract-wrappers/src/schemas/method_opts_schema.ts @@ -0,0 +1,7 @@ +export const methodOptsSchema = { + id: '/MethodOpts', + properties: { + defaultBlock: { $ref: '/BlockParam' }, + }, + type: 'object', +}; diff --git a/packages/contract-wrappers/src/schemas/tx_opts_schema.ts b/packages/contract-wrappers/src/schemas/tx_opts_schema.ts new file mode 100644 index 000000000..bddc33b6c --- /dev/null +++ b/packages/contract-wrappers/src/schemas/tx_opts_schema.ts @@ -0,0 +1,8 @@ +export const txOptsSchema = { + id: '/TxOpts', + properties: { + gasPrice: { $ref: '/Number' }, + gasLimit: { type: 'number' }, + }, + type: 'object', +}; |