diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-06-02 00:21:12 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-06-02 00:21:12 +0800 |
commit | 0554f947b12f36a1cd5f7be469123e794b467f0b (patch) | |
tree | c3d1a55af98063dadfb37cf588adc6e00dfdf3b1 /src | |
parent | 6ced554d9f6cf00d0d9e8f9ff8170af4f1cfbee3 (diff) | |
download | dexon-sol-tools-0554f947b12f36a1cd5f7be469123e794b467f0b.tar dexon-sol-tools-0554f947b12f36a1cd5f7be469123e794b467f0b.tar.gz dexon-sol-tools-0554f947b12f36a1cd5f7be469123e794b467f0b.tar.bz2 dexon-sol-tools-0554f947b12f36a1cd5f7be469123e794b467f0b.tar.lz dexon-sol-tools-0554f947b12f36a1cd5f7be469123e794b467f0b.tar.xz dexon-sol-tools-0554f947b12f36a1cd5f7be469123e794b467f0b.tar.zst dexon-sol-tools-0554f947b12f36a1cd5f7be469123e794b467f0b.zip |
Add not enough taker balance tests
Diffstat (limited to 'src')
-rw-r--r-- | src/0x.js.ts | 4 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 16 | ||||
-rw-r--r-- | src/types.ts | 4 |
3 files changed, 20 insertions, 4 deletions
diff --git a/src/0x.js.ts b/src/0x.js.ts index 336700a54..a95bc3384 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -103,9 +103,9 @@ export class ZeroEx { } constructor(web3: Web3) { this.web3Wrapper = new Web3Wrapper(web3); - this.exchange = new ExchangeWrapper(this.web3Wrapper); - this.tokenRegistry = new TokenRegistryWrapper(this.web3Wrapper); this.token = new TokenWrapper(this.web3Wrapper); + this.exchange = new ExchangeWrapper(this.web3Wrapper, this.token); + this.tokenRegistry = new TokenRegistryWrapper(this.web3Wrapper); } /** * Sets a new provider for the web3 instance used by 0x.js diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index b2d201516..dd3c3c933 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -19,6 +19,7 @@ import {signedOrderSchema} from '../schemas/order_schemas'; import {SchemaValidator} from '../utils/schema_validator'; import {ContractResponse} from '../types'; import {constants} from '../utils/constants'; +import {TokenWrapper} from './token_wrapper'; export class ExchangeWrapper extends ContractWrapper { private exchangeContractErrCodesToMsg = { @@ -30,8 +31,10 @@ export class ExchangeWrapper extends ContractWrapper { [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.ORDER_BALANCE_ALLOWANCE_ERROR, }; private exchangeContractIfExists?: ExchangeContract; - constructor(web3Wrapper: Web3Wrapper) { + private tokenWrapper: TokenWrapper; + constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) { super(web3Wrapper); + this.tokenWrapper = tokenWrapper; } public invalidateContractInstance(): void { delete this.exchangeContractIfExists; @@ -117,7 +120,7 @@ export class ExchangeWrapper extends ContractWrapper { ); this.throwErrorLogsAsErrors(response.logs); } - private validateFillOrder(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, senderAddress: string) { + private async validateFillOrder(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, senderAddress: string) { if (fillAmount.eq(0)) { throw new Error(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); } @@ -127,6 +130,15 @@ export class ExchangeWrapper extends ContractWrapper { if (signedOrder.expirationUnixTimestampSec.lessThan(Date.now() / 1000)) { throw new Error(FillOrderValidationErrs.EXPIRED); } + const makerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.makerTokenAddress, signedOrder.maker); + const takerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.takerTokenAddress, senderAddress); + const makerAllowance = await this.tokenWrapper.getProxyAllowanceAsync(signedOrder.makerTokenAddress, + signedOrder.maker); + const takerAllowance = await this.tokenWrapper.getProxyAllowanceAsync(signedOrder.takerTokenAddress, + senderAddress); + if (fillAmount.greaterThan(takerBalance)) { + throw new Error(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); + } } private throwErrorLogsAsErrors(logs: ContractEvent[]): void { const errEvent = _.find(logs, {event: 'LogError'}); diff --git a/src/types.ts b/src/types.ts index 872422af6..ad5d8fb17 100644 --- a/src/types.ts +++ b/src/types.ts @@ -89,6 +89,10 @@ export const FillOrderValidationErrs = strEnum([ 'FILL_AMOUNT_IS_ZERO', 'NOT_A_TAKER', 'EXPIRED', + 'NOT_ENOUGH_TAKER_BALANCE', + 'NOT_ENOUGH_TAKER_ALLOWANCE', + 'NOT_ENOUGH_MAKER_BALANCE', + 'NOT_ENOUGH_MAKER_ALLOWANCE', ]); export type FillOrderValidationErrs = keyof typeof FillOrderValidationErrs; |