Skip to content

Commit 770c07c

Browse files
committed
SerializeOrders: Extract takerWallet to its own file
1 parent a02bab9 commit 770c07c

File tree

4 files changed

+96
-69
lines changed

4 files changed

+96
-69
lines changed

src/constants.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { BigNumber } from 'bignumber.js';
33
import { Constants } from './types';
44

55
export const constants: Constants = {
6-
EXCHANGES: {
7-
ZERO_EX: 1,
8-
KYBER: 2,
9-
TAKER_WALLET: 3,
10-
},
11-
MAX_DIGITS_IN_UNSIGNED_256_INT: 78,
12-
NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
13-
ZERO: new BigNumber(0),
14-
ZERO_EX_SNAPSHOT_EXCHANGE_ADDRESS: '0x48bacb9266a570d521063ef5dd96e61686dbe788',
6+
EXCHANGES: {
7+
ZERO_EX: 1,
8+
KYBER: 2,
9+
TAKER_WALLET: 3,
10+
},
11+
MAX_DIGITS_IN_UNSIGNED_256_INT: 78,
12+
NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
13+
ZERO: new BigNumber(0),
14+
ZERO_EX_SNAPSHOT_EXCHANGE_ADDRESS: '0x48bacb9266a570d521063ef5dd96e61686dbe788',
1515
ZERO_EX_SNAPSHOT_ERC20_PROXY_ADDRESS: '0x1dc4c1cefef38a777b15aa20260a54e584b16c48',
1616
};

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export class SetProtocolUtils {
5454
this.web3 = web3 || new Web3();
5555
}
5656

57+
/* ============ Static SetProtocolUtils Functions ============ */
58+
5759
/**
5860
* Converts an array of Buffers into Hex
5961
* @param bufferArray Array of buffers
@@ -183,6 +185,8 @@ export class SetProtocolUtils {
183185
return parseSignatureHexAsRSV(signature);
184186
}
185187

188+
/* ============ Non-Static SetProtocolUtils Functions ============ */
189+
186190
/**
187191
* Generates a byte string representing serialized exchange orders across different exchanges.
188192
* @param makerTokenAddress Address of the token used to pay for the order

src/orders.ts

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import * as _ from 'lodash';
2222
import * as Web3 from 'web3';
2323

2424
import { constants } from './constants';
25-
import { paddedBufferForPrimitive, bufferObjectWithProperties } from './encoding';
25+
import { bufferObjectWithProperties } from './encoding';
26+
import { generateTakerWalletOrdersBuffer } from './takerWallet';
2627
import { Address, Bytes32, Exchanges, IssuanceOrder, SolidityTypes, TakerWalletOrder } from './types';
2728

2829
export function generateTimestamp(minutes: number): BigNumber {
@@ -91,67 +92,11 @@ export function generateSerializedOrders(
9192
});
9293
// Loop through all exchange orders and create buffers
9394
_.forEach(exchanges, (exchangeOrders, key) => {
94-
const exchangeKey: number = constants.EXCHANGES[key];
95-
if (exchangeKey === 1) {
96-
// Handle Zero Ex
97-
} else if (exchangeKey === 2) {
98-
// Handle Kyber Network
99-
} else if (exchangeKey === 3) {
95+
if (key === 'ZERO_EX') {
96+
} else if (key === 'KYBER') {
97+
} else if (key === 'TAKER_WALLET') {
10098
orderBuffer.push(generateTakerWalletOrdersBuffer(makerTokenAddress, exchangeOrders, web3));
10199
}
102100
});
103101
return ethUtil.bufferToHex(Buffer.concat(orderBuffer));
104102
}
105-
106-
/* ============ Taker Wallet Order Functions ============ */
107-
108-
/**
109-
* Takes a taker wallet order object and turns it into a buffer.
110-
*
111-
* @param takerTokenAddress Address of the token the taker will fill in the taker wallet order
112-
* @param takerTokenAmount Amount of tokens the taker will fill in the order
113-
* @param web3 web3 instance instantiated with `new Web3(provider);`
114-
* @return Taker wallet order as a buffer
115-
*/
116-
117-
export function takerWalletOrderToBuffer(
118-
takerTokenAddress: Address,
119-
takerTokenAmount: BigNumber,
120-
web3: Web3,
121-
): Buffer {
122-
const takerWalletOrder: Buffer[] = [];
123-
takerWalletOrder.push(paddedBufferForPrimitive(takerTokenAddress));
124-
takerWalletOrder.push(paddedBufferForPrimitive(web3.toHex(takerTokenAmount)));
125-
return Buffer.concat(takerWalletOrder);
126-
}
127-
128-
/**
129-
* Takes taker wallet orders and generates a buffer representing all orders the
130-
* taker can fill directly from their wallet.
131-
*
132-
* @param makerTokenAddress Address of the token used to pay for the order
133-
* @param orders Array of TakerWalletOrders
134-
* @param web3 web3 instance instantiated with `new Web3(provider);`
135-
* @return Entire taker wallet orders data as a buffer
136-
*/
137-
138-
export function generateTakerWalletOrdersBuffer(
139-
makerTokenAddress: Address,
140-
orders: TakerWalletOrder[],
141-
web3: Web3,
142-
): Buffer {
143-
// Generate header for taker wallet order
144-
const takerOrderHeader: Buffer[] = [
145-
paddedBufferForPrimitive(constants.EXCHANGES.KYBER),
146-
paddedBufferForPrimitive(orders.length), // Include the number of orders as part of header
147-
paddedBufferForPrimitive(makerTokenAddress),
148-
paddedBufferForPrimitive(0), // Taker wallet orders do not take any maker token to execute
149-
];
150-
// Turn all taker wallet orders to buffers
151-
const takerOrderBody: Buffer[] = _.map(orders, ({takerTokenAddress, takerTokenAmount}) =>
152-
takerWalletOrderToBuffer(takerTokenAddress, takerTokenAmount, web3));
153-
return Buffer.concat([
154-
Buffer.concat(takerOrderHeader),
155-
Buffer.concat(takerOrderBody),
156-
]);
157-
}

src/takerWallet.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2018 Set Labs Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
import { BigNumber } from 'bignumber.js';
20+
import * as _ from 'lodash';
21+
import * as Web3 from 'web3';
22+
23+
import { constants } from './constants';
24+
import { paddedBufferForPrimitive } from './encoding';
25+
import { Address, TakerWalletOrder } from './types';
26+
27+
/* ============ Taker Wallet Order Functions ============ */
28+
29+
/**
30+
* Takes taker wallet orders and generates a buffer representing all orders the
31+
* taker can fill directly from their wallet.
32+
*
33+
* @param makerTokenAddress Address of the token used to pay for the order
34+
* @param orders Array of TakerWalletOrders
35+
* @param web3 web3 instance instantiated with `new Web3(provider);`
36+
* @return Entire taker wallet orders data as a buffer
37+
*/
38+
39+
export function generateTakerWalletOrdersBuffer(
40+
makerTokenAddress: Address,
41+
orders: TakerWalletOrder[],
42+
web3: Web3,
43+
): Buffer {
44+
// Generate header for taker wallet order
45+
const takerOrderHeader: Buffer[] = [
46+
paddedBufferForPrimitive(constants.EXCHANGES.KYBER),
47+
paddedBufferForPrimitive(orders.length), // Include the number of orders as part of header
48+
paddedBufferForPrimitive(makerTokenAddress),
49+
paddedBufferForPrimitive(0), // Taker wallet orders do not take any maker token to execute
50+
];
51+
// Turn all taker wallet orders to buffers
52+
const takerOrderBody: Buffer[] = _.map(orders, ({takerTokenAddress, takerTokenAmount}) =>
53+
takerWalletOrderToBuffer(takerTokenAddress, takerTokenAmount, web3));
54+
return Buffer.concat([
55+
Buffer.concat(takerOrderHeader),
56+
Buffer.concat(takerOrderBody),
57+
]);
58+
}
59+
60+
/**
61+
* Takes a taker wallet order object and turns it into a buffer.
62+
*
63+
* @param takerTokenAddress Address of the token the taker will fill in the taker wallet order
64+
* @param takerTokenAmount Amount of tokens the taker will fill in the order
65+
* @param web3 web3 instance instantiated with `new Web3(provider);`
66+
* @return Taker wallet order as a buffer
67+
*/
68+
69+
export function takerWalletOrderToBuffer(
70+
takerTokenAddress: Address,
71+
takerTokenAmount: BigNumber,
72+
web3: Web3,
73+
): Buffer {
74+
const takerWalletOrder: Buffer[] = [];
75+
takerWalletOrder.push(paddedBufferForPrimitive(takerTokenAddress));
76+
takerWalletOrder.push(paddedBufferForPrimitive(web3.toHex(takerTokenAmount)));
77+
return Buffer.concat(takerWalletOrder);
78+
}

0 commit comments

Comments
 (0)