aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/test/utils/fill_scenarios.ts
blob: 5f3e899069454c0c358446a2bca7602bdfbb9e72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {Web3Wrapper} from '@0xproject/web3-wrapper';
import BigNumber from 'bignumber.js';

import {SignedOrder, Token, ZeroEx} from '../../src';
import {artifacts} from '../../src/artifacts';
import {DummyTokenContract} from '../../src/contract_wrappers/generated/dummy_token';
import {orderFactory} from '../utils/order_factory';

import {constants} from './constants';

const INITIAL_COINBASE_TOKEN_SUPPLY_IN_UNITS = new BigNumber(100);

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 initTokenBalancesAsync() {
        const web3Wrapper = (this.zeroEx as any)._web3Wrapper as Web3Wrapper;
        for (const token of this.tokens) {
            if (token.symbol !== 'ZRX' && token.symbol !== 'WETH') {
                const contractInstance = web3Wrapper.getContractInstance(
                    artifacts.DummyTokenArtifact.abi, token.address,
                );
                const defaults = {};
                const dummyToken = new DummyTokenContract(contractInstance, defaults);
                const tokenSupply = ZeroEx.toBaseUnitAmount(INITIAL_COINBASE_TOKEN_SUPPLY_IN_UNITS, token.decimals);
                const txHash = await dummyToken.setBalance.sendTransactionAsync(this.coinbase, tokenSupply, {
                    from: this.coinbase,
                });
                await this.zeroEx.awaitTransactionMinedAsync(txHash);
            }
        }
    }
    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,
        );
    }
}