aboutsummaryrefslogtreecommitdiffstats
path: root/src/contract_wrappers
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-06-02 00:34:42 +0800
committerFabio Berger <me@fabioberger.com>2017-06-02 00:34:42 +0800
commit2aaf71e488665d53d373a17d112504a4439ab44b (patch)
tree5a41cf24e452a4fd11f174957c4a9c132c5f25d0 /src/contract_wrappers
parentde93bf1690b78a8745c392e22e01ace641fbbffe (diff)
parent520248e6787a04b2fcf45d7f185d85214a84bfd5 (diff)
downloaddexon-sol-tools-2aaf71e488665d53d373a17d112504a4439ab44b.tar
dexon-sol-tools-2aaf71e488665d53d373a17d112504a4439ab44b.tar.gz
dexon-sol-tools-2aaf71e488665d53d373a17d112504a4439ab44b.tar.bz2
dexon-sol-tools-2aaf71e488665d53d373a17d112504a4439ab44b.tar.lz
dexon-sol-tools-2aaf71e488665d53d373a17d112504a4439ab44b.tar.xz
dexon-sol-tools-2aaf71e488665d53d373a17d112504a4439ab44b.tar.zst
dexon-sol-tools-2aaf71e488665d53d373a17d112504a4439ab44b.zip
Merge branch 'fillOrderAsync' into unavailableFilledCancelled
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r--src/contract_wrappers/exchange_wrapper.ts18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts
index 7fcf91cbf..0822ef875 100644
--- a/src/contract_wrappers/exchange_wrapper.ts
+++ b/src/contract_wrappers/exchange_wrapper.ts
@@ -20,6 +20,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 = {
@@ -31,8 +32,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;
@@ -106,7 +109,7 @@ export class ExchangeWrapper extends ContractWrapper {
const senderAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync();
const exchangeInstance = await this.getExchangeContractAsync();
- this.validateFillOrder(signedOrder, fillTakerAmountInBaseUnits, senderAddress);
+ await this.validateFillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, senderAddress);
const orderAddresses: OrderAddresses = [
signedOrder.maker,
@@ -150,7 +153,7 @@ export class ExchangeWrapper extends ContractWrapper {
);
this.throwErrorLogsAsErrors(response.logs);
}
- private validateFillOrder(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, senderAddress: string) {
+ private async validateFillOrderAsync(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, senderAddress: string) {
if (fillAmount.eq(0)) {
throw new Error(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO);
}
@@ -160,6 +163,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'});