diff options
author | Fabio Berger <me@fabioberger.com> | 2017-11-13 11:17:18 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-11-13 11:17:18 +0800 |
commit | c4ee2d73865a1444c079b9e2836b7630a0adf03e (patch) | |
tree | b9c7794e7022fb189675d914f5fe58dcabd67dec /packages/0x.js/test/utils/fill_scenarios.ts | |
parent | a74ec0effa818a86233fe64cb0dad2c61bbb4bb6 (diff) | |
download | dexon-sol-tools-c4ee2d73865a1444c079b9e2836b7630a0adf03e.tar.gz dexon-sol-tools-c4ee2d73865a1444c079b9e2836b7630a0adf03e.tar.zst dexon-sol-tools-c4ee2d73865a1444c079b9e2836b7630a0adf03e.zip |
Switch over to Lerna + Yarn Workspaces setup for a mono-repo approach
Diffstat (limited to 'packages/0x.js/test/utils/fill_scenarios.ts')
-rw-r--r-- | packages/0x.js/test/utils/fill_scenarios.ts | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/packages/0x.js/test/utils/fill_scenarios.ts b/packages/0x.js/test/utils/fill_scenarios.ts new file mode 100644 index 000000000..a0632b12c --- /dev/null +++ b/packages/0x.js/test/utils/fill_scenarios.ts @@ -0,0 +1,114 @@ +import BigNumber from 'bignumber.js'; +import {ZeroEx, Token, SignedOrder} from '../../src'; +import {orderFactory} from '../utils/order_factory'; +import {constants} from './constants'; + +export class FillScenarios { + private zeroEx: ZeroEx; + private userAddresses: string[]; + private tokens: Token[]; + private coinbase: string; + private zrxTokenAddress: string; + private exchangeContractAddress: string; + constructor(zeroEx: ZeroEx, userAddresses: string[], + tokens: Token[], zrxTokenAddress: string, exchangeContractAddress: string) { + this.zeroEx = zeroEx; + this.userAddresses = userAddresses; + this.tokens = tokens; + this.coinbase = userAddresses[0]; + this.zrxTokenAddress = zrxTokenAddress; + this.exchangeContractAddress = exchangeContractAddress; + } + public async createFillableSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, + makerAddress: string, takerAddress: string, + fillableAmount: BigNumber, + expirationUnixTimestampSec?: BigNumber): + Promise<SignedOrder> { + return this.createAsymmetricFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, + fillableAmount, fillableAmount, expirationUnixTimestampSec, + ); + } + public async createFillableSignedOrderWithFeesAsync( + makerTokenAddress: string, takerTokenAddress: string, + makerFee: BigNumber, takerFee: BigNumber, + makerAddress: string, takerAddress: string, + fillableAmount: BigNumber, + feeRecepient: string, expirationUnixTimestampSec?: BigNumber, + ): Promise<SignedOrder> { + return this.createAsymmetricFillableSignedOrderWithFeesAsync( + makerTokenAddress, takerTokenAddress, makerFee, takerFee, makerAddress, takerAddress, + fillableAmount, fillableAmount, feeRecepient, expirationUnixTimestampSec, + ); + } + public async createAsymmetricFillableSignedOrderAsync( + makerTokenAddress: string, takerTokenAddress: string, makerAddress: string, takerAddress: string, + makerFillableAmount: BigNumber, takerFillableAmount: BigNumber, + expirationUnixTimestampSec?: BigNumber): Promise<SignedOrder> { + const makerFee = new BigNumber(0); + const takerFee = new BigNumber(0); + const feeRecepient = constants.NULL_ADDRESS; + return this.createAsymmetricFillableSignedOrderWithFeesAsync( + makerTokenAddress, takerTokenAddress, makerFee, takerFee, makerAddress, takerAddress, + makerFillableAmount, takerFillableAmount, feeRecepient, expirationUnixTimestampSec, + ); + } + public async createPartiallyFilledSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, + takerAddress: string, fillableAmount: BigNumber, + partialFillAmount: BigNumber) { + const [makerAddress] = this.userAddresses; + const signedOrder = await this.createAsymmetricFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, + fillableAmount, fillableAmount, + ); + const shouldThrowOnInsufficientBalanceOrAllowance = false; + await this.zeroEx.exchange.fillOrderAsync( + signedOrder, partialFillAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + ); + return signedOrder; + } + private async createAsymmetricFillableSignedOrderWithFeesAsync( + makerTokenAddress: string, takerTokenAddress: string, + makerFee: BigNumber, takerFee: BigNumber, + makerAddress: string, takerAddress: string, + makerFillableAmount: BigNumber, takerFillableAmount: BigNumber, + feeRecepient: string, expirationUnixTimestampSec?: BigNumber): Promise<SignedOrder> { + + await Promise.all([ + this.increaseBalanceAndAllowanceAsync(makerTokenAddress, makerAddress, makerFillableAmount), + this.increaseBalanceAndAllowanceAsync(takerTokenAddress, takerAddress, takerFillableAmount), + ]); + await Promise.all([ + this.increaseBalanceAndAllowanceAsync(this.zrxTokenAddress, makerAddress, makerFee), + this.increaseBalanceAndAllowanceAsync(this.zrxTokenAddress, takerAddress, takerFee), + ]); + + const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, + makerAddress, takerAddress, makerFee, takerFee, + makerFillableAmount, makerTokenAddress, takerFillableAmount, takerTokenAddress, + this.exchangeContractAddress, feeRecepient, expirationUnixTimestampSec); + return signedOrder; + } + private async increaseBalanceAndAllowanceAsync( + tokenAddress: string, address: string, amount: BigNumber): Promise<void> { + if (amount.isZero() || address === ZeroEx.NULL_ADDRESS) { + return; // noop + } + await Promise.all([ + this.increaseBalanceAsync(tokenAddress, address, amount), + this.increaseAllowanceAsync(tokenAddress, address, amount), + ]); + } + private async increaseBalanceAsync( + tokenAddress: string, address: string, amount: BigNumber): Promise<void> { + await this.zeroEx.token.transferAsync(tokenAddress, this.coinbase, address, amount); + } + private async increaseAllowanceAsync( + tokenAddress: string, address: string, amount: BigNumber): Promise<void> { + const oldMakerAllowance = await this.zeroEx.token.getProxyAllowanceAsync(tokenAddress, address); + const newMakerAllowance = oldMakerAllowance.plus(amount); + await this.zeroEx.token.setProxyAllowanceAsync( + tokenAddress, address, newMakerAllowance, + ); + } +} |