aboutsummaryrefslogtreecommitdiffstats
path: root/src/types.ts
blob: e9b7f0c10a89d188b08e05c9daae845d5672a469 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import * as _ from 'lodash';
import * as Web3 from 'web3';

// Utility function to create a K:V from a list of strings
// Adapted from: https://basarat.gitbooks.io/typescript/content/docs/types/literal-types.html
export interface StringEnum {
    [key: string]: string;
}
function strEnum(values: string[]): StringEnum {
    return _.reduce(values, (result, key) => {
        result[key] = key;
        return result;
    }, Object.create(null));
}

export const ZeroExError = strEnum([
    'CONTRACT_DOES_NOT_EXIST',
    'EXCHANGE_CONTRACT_DOES_NOT_EXIST',
    'UNHANDLED_ERROR',
    'USER_HAS_NO_ASSOCIATED_ADDRESSES',
    'INVALID_SIGNATURE',
    'CONTRACT_NOT_DEPLOYED_ON_NETWORK',
    'ZRX_NOT_IN_TOKEN_REGISTRY',
    'INSUFFICIENT_ALLOWANCE_FOR_TRANSFER',
    'INSUFFICIENT_BALANCE_FOR_TRANSFER',
    'INSUFFICIENT_ETH_BALANCE_FOR_DEPOSIT',
    'INSUFFICIENT_WETH_BALANCE_FOR_WITHDRAWAL',
    'INVALID_JUMP',
    'OUT_OF_GAS',
]);
export type ZeroExError = keyof typeof ZeroExError;

/**
 * Elliptic Curve signature
 */
export interface ECSignature {
    v: number;
    r: string;
    s: string;
}

export type OrderAddresses = [string, string, string, string, string];

export type OrderValues = [BigNumber.BigNumber, BigNumber.BigNumber, BigNumber.BigNumber,
                           BigNumber.BigNumber, BigNumber.BigNumber, BigNumber.BigNumber];

export type EventCallbackAsync = (err: Error, event: ContractEvent) => Promise<void>;
export type EventCallbackSync = (err: Error, event: ContractEvent) => void;
export type EventCallback = EventCallbackSync|EventCallbackAsync;
export interface ContractEventObj {
    watch: (eventWatch: EventCallback) => void;
    stopWatching: () => void;
}
export type CreateContractEvent = (indexFilterValues: IndexedFilterValues,
                                   subscriptionOpts: SubscriptionOpts) => ContractEventObj;
export interface ExchangeContract extends ContractInstance {
    isValidSignature: {
        call: (signerAddressHex: string, dataHex: string, v: number, r: string, s: string,
               txOpts?: TxOpts) => Promise<boolean>;
    };
    LogFill: CreateContractEvent;
    LogCancel: CreateContractEvent;
    LogError: CreateContractEvent;
    ZRX_TOKEN_CONTRACT: {
        call: () => Promise<string>;
    };
    getUnavailableTakerTokenAmount: {
        call: (orderHash: string) => BigNumber.BigNumber;
    };
    isRoundingError: {
        call: (takerTokenAmount: BigNumber.BigNumber, fillTakerAmount: BigNumber.BigNumber,
               makerTokenAmount: BigNumber.BigNumber, txOpts?: TxOpts) => Promise<boolean>;
    };
    fillOrder: {
        (orderAddresses: OrderAddresses, orderValues: OrderValues, fillTakerTokenAmount: BigNumber.BigNumber,
         shouldThrowOnInsufficientBalanceOrAllowance: boolean,
         v: number, r: string, s: string, txOpts?: TxOpts): ContractResponse;
        estimateGas: (orderAddresses: OrderAddresses, orderValues: OrderValues,
                      fillTakerTokenAmount: BigNumber.BigNumber,
                      shouldThrowOnInsufficientBalanceOrAllowance: boolean,
                      v: number, r: string, s: string, txOpts?: TxOpts) => number;
    };
    batchFillOrders: {
        (orderAddresses: OrderAddresses[], orderValues: OrderValues[], fillTakerTokenAmounts: BigNumber.BigNumber[],
         shouldThrowOnInsufficientBalanceOrAllowance: boolean,
         v: number[], r: string[], s: string[], txOpts?: TxOpts): ContractResponse;
        estimateGas: (orderAddresses: OrderAddresses[], orderValues: OrderValues[],
                      fillTakerTokenAmounts: BigNumber.BigNumber[],
                      shouldThrowOnInsufficientBalanceOrAllowance: boolean,
                      v: number[], r: string[], s: string[], txOpts?: TxOpts) => number;
    };
    fillOrdersUpTo: {
        (orderAddresses: OrderAddresses[], orderValues: OrderValues[], fillTakerTokenAmount: BigNumber.BigNumber,
         shouldThrowOnInsufficientBalanceOrAllowance: boolean,
         v: number[], r: string[], s: string[], txOpts?: TxOpts): ContractResponse;
        estimateGas: (orderAddresses: OrderAddresses[], orderValues: OrderValues[],
                      fillTakerTokenAmount: BigNumber.BigNumber,
                      shouldThrowOnInsufficientBalanceOrAllowance: boolean,
                      v: number[], r: string[], s: string[], txOpts?: TxOpts) => number;
    };
    cancelOrder: {
        (orderAddresses: OrderAddresses, orderValues: OrderValues, canceltakerTokenAmount: BigNumber.BigNumber,
         txOpts?: TxOpts): ContractResponse;
        estimateGas: (orderAddresses: OrderAddresses, orderValues: OrderValues,
                      canceltakerTokenAmount: BigNumber.BigNumber,
                      txOpts?: TxOpts) => number;
    };
    batchCancelOrders: {
        (orderAddresses: OrderAddresses[], orderValues: OrderValues[], cancelTakerTokenAmounts: BigNumber.BigNumber[],
         txOpts?: TxOpts): ContractResponse;
        estimateGas: (orderAddresses: OrderAddresses[], orderValues: OrderValues[],
                      cancelTakerTokenAmounts: BigNumber.BigNumber[],
                      txOpts?: TxOpts) => number;
    };
    fillOrKill: {
        (orderAddresses: OrderAddresses, orderValues: OrderValues, fillAmount: BigNumber.BigNumber,
         v: number, r: string, s: string, txOpts?: TxOpts): ContractResponse;
        estimateGas: (orderAddresses: OrderAddresses, orderValues: OrderValues, fillAmount: BigNumber.BigNumber,
                      v: number, r: string, s: string, txOpts?: TxOpts) => number;
    };
    batchFillOrKillOrders: {
        (orderAddresses: OrderAddresses[], orderValues: OrderValues[], fillTakerTokenAmounts: BigNumber.BigNumber[],
         v: number[], r: string[], s: string[], txOpts: TxOpts): ContractResponse;
        estimateGas: (orderAddresses: OrderAddresses[], orderValues: OrderValues[],
                      fillTakerTokenAmounts: BigNumber.BigNumber[],
                      v: number[], r: string[], s: string[], txOpts?: TxOpts) => number;
    };
    filled: {
        call: (orderHash: string) => BigNumber.BigNumber;
    };
    cancelled: {
        call: (orderHash: string) => BigNumber.BigNumber;
    };
    getOrderHash: {
        call: (orderAddresses: OrderAddresses, orderValues: OrderValues) => string;
    };
}

export interface TokenContract extends ContractInstance {
    Transfer: CreateContractEvent;
    Approval: CreateContractEvent;
    balanceOf: {
        call: (address: string) => Promise<BigNumber.BigNumber>;
    };
    allowance: {
        call: (ownerAddress: string, allowedAddress: string) => Promise<BigNumber.BigNumber>;
    };
    transfer: (toAddress: string, amountInBaseUnits: BigNumber.BigNumber, txOpts?: TxOpts) => Promise<boolean>;
    transferFrom: (fromAddress: string, toAddress: string, amountInBaseUnits: BigNumber.BigNumber,
                   txOpts?: TxOpts) => Promise<boolean>;
    approve: (proxyAddress: string, amountInBaseUnits: BigNumber.BigNumber, txOpts?: TxOpts) => void;
}

export interface TokenRegistryContract extends ContractInstance {
    getTokenMetaData: {
        call: (address: string) => Promise<TokenMetadata>;
    };
    getTokenAddresses: {
        call: () => Promise<string[]>;
    };
}

export interface EtherTokenContract extends ContractInstance {
    deposit: (txOpts: TxOpts) => Promise<void>;
    withdraw: (amount: BigNumber.BigNumber, txOpts: TxOpts) => Promise<void>;
}

export interface ProxyContract extends ContractInstance {
    getAuthorizedAddresses: {
        call: () => Promise<string[]>;
    };
    authorized: {
        call: (address: string) => Promise<boolean>;
    };
}

export const SolidityTypes = strEnum([
    'address',
    'uint256',
]);
export type SolidityTypes = keyof typeof SolidityTypes;

export enum ExchangeContractErrCodes {
    ERROR_FILL_EXPIRED, // Order has already expired
    ERROR_FILL_NO_VALUE, // Order has already been fully filled or cancelled
    ERROR_FILL_TRUNCATION, // Rounding error too large
    ERROR_FILL_BALANCE_ALLOWANCE, // Insufficient balance or allowance for token transfer
    ERROR_CANCEL_EXPIRED, // Order has already expired
    ERROR_CANCEL_NO_VALUE, // Order has already been fully filled or cancelled
}

export const ExchangeContractErrs = strEnum([
    'ORDER_FILL_EXPIRED',
    'ORDER_CANCEL_EXPIRED',
    'ORDER_CANCEL_AMOUNT_ZERO',
    'ORDER_ALREADY_CANCELLED_OR_FILLED',
    'ORDER_REMAINING_FILL_AMOUNT_ZERO',
    'ORDER_FILL_ROUNDING_ERROR',
    'FILL_BALANCE_ALLOWANCE_ERROR',
    'INSUFFICIENT_TAKER_BALANCE',
    'INSUFFICIENT_TAKER_ALLOWANCE',
    'INSUFFICIENT_MAKER_BALANCE',
    'INSUFFICIENT_MAKER_ALLOWANCE',
    'INSUFFICIENT_TAKER_FEE_BALANCE',
    'INSUFFICIENT_TAKER_FEE_ALLOWANCE',
    'INSUFFICIENT_MAKER_FEE_BALANCE',
    'INSUFFICIENT_MAKER_FEE_ALLOWANCE',
    'TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER',
    'MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED',
    'INSUFFICIENT_REMAINING_FILL_AMOUNT',
    'MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED',
    'BATCH_ORDERS_MUST_HAVE_SAME_EXCHANGE_ADDRESS',
]);
export type ExchangeContractErrs = keyof typeof ExchangeContractErrs;

export interface ContractResponse {
    logs: ContractEvent[];
}

export interface ContractEvent {
    logIndex: number;
    transactionIndex: number;
    transactionHash: string;
    blockHash: string;
    blockNumber: number;
    address: string;
    type: string;
    event: string;
    args: ContractEventArgs;
}

export interface LogFillContractEventArgs {
    maker: string;
    taker: string;
    feeRecipient: string;
    tokenM: string;
    tokenT: string;
    filledValueM: BigNumber.BigNumber;
    filledValueT: BigNumber.BigNumber;
    feeMPaid: BigNumber.BigNumber;
    feeTPaid: BigNumber.BigNumber;
    tokens: string;
    orderHash: string;
}
export interface LogCancelContractEventArgs {
    maker: string;
    feeRecipient: string;
    tokenM: string;
    tokenT: string;
    cancelledValueM: BigNumber.BigNumber;
    cancelledValueT: BigNumber.BigNumber;
    tokens: string;
    orderHash: string;
}
export interface LogErrorContractEventArgs {
    errorId: BigNumber.BigNumber;
    orderHash: string;
}
export type ExchangeContractEventArgs = LogFillContractEventArgs|LogCancelContractEventArgs|LogErrorContractEventArgs;
export interface TransferContractEventArgs {
    _from: string;
    _to: string;
    _value: BigNumber.BigNumber;
}
export interface ApprovalContractEventArgs {
    _owner: string;
    _spender: string;
    _value: BigNumber.BigNumber;
}
export type TokenContractEventArgs = TransferContractEventArgs|ApprovalContractEventArgs;
export type ContractEventArgs = ExchangeContractEventArgs|TokenContractEventArgs;
export type ContractEventArg = string|BigNumber.BigNumber;

export interface Order {
    maker: string;
    taker: string;
    makerFee: BigNumber.BigNumber;
    takerFee: BigNumber.BigNumber;
    makerTokenAmount: BigNumber.BigNumber;
    takerTokenAmount: BigNumber.BigNumber;
    makerTokenAddress: string;
    takerTokenAddress: string;
    salt: BigNumber.BigNumber;
    exchangeContractAddress: string;
    feeRecipient: string;
    expirationUnixTimestampSec: BigNumber.BigNumber;
}

export interface SignedOrder extends Order {
    ecSignature: ECSignature;
}

//                          [address, name, symbol, projectUrl, decimals, ipfsHash, swarmHash]
export type TokenMetadata = [string, string, string, string, BigNumber.BigNumber, string, string];

export interface Token {
    name: string;
    address: string;
    symbol: string;
    decimals: number;
    url: string;
}

export interface TxOpts {
    from: string;
    gas?: number;
    value?: BigNumber.BigNumber;
}

export interface TokenAddressBySymbol {
    [symbol: string]: string;
}

export const ExchangeEvents = strEnum([
    'LogFill',
    'LogCancel',
    'LogError',
]);
export type ExchangeEvents = keyof typeof ExchangeEvents;

export const TokenEvents = strEnum([
    'Transfer',
    'Approval',
]);
export type TokenEvents = keyof typeof TokenEvents;

export interface IndexedFilterValues {
    [index: string]: ContractEventArg;
}

export type BlockParam = 'latest'|'earliest'|'pending'|number;

export interface SubscriptionOpts {
    fromBlock: BlockParam;
    toBlock: BlockParam;
}

export type DoneCallback = (err?: Error) => void;

export interface OrderFillOrKillRequest {
    signedOrder: SignedOrder;
    fillTakerAmount: BigNumber.BigNumber;
}

export interface OrderCancellationRequest {
    order: Order|SignedOrder;
    takerTokenCancelAmount: BigNumber.BigNumber;
}

export interface OrderFillRequest {
    signedOrder: SignedOrder;
    takerTokenFillAmount: BigNumber.BigNumber;
}

export type AsyncMethod = (...args: any[]) => Promise<any>;

export interface ContractInstance {
    address: string;
}

export interface Artifact {
    networks: {[networkId: number]: any};
}

export interface ContractEventEmitter {
    watch: (eventCallback: EventCallback) => void;
    stopWatchingAsync: () => Promise<void>;
}

/**
 * We re-export the `Web3.Provider` type specified in the Web3 Typescript typings
 * since it is the type of the `provider` argument to the `ZeroEx` constructor.
 * It is however a `Web3` library type, not a native `0x.js` type.
 */
export type Web3Provider = Web3.Provider;

export interface ExchangeContractByAddress {
    [address: string]: ExchangeContract;
}

export interface ContractArtifact {
    networks: {
        [networkId: number]: {
            address: string;
        };
    };
}