-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
135 lines (128 loc) · 5.51 KB
/
app.js
File metadata and controls
135 lines (128 loc) · 5.51 KB
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
const Exchange = artifacts.require('Exchange');
const ZeroEx = require('0x.js');
const rp = require('request-promise');
const appConfig = require('./config.js');
const tokenContractABI = require('./tokenABI.js');
const BigNumber = require('@0xproject/utils').BigNumber;
module.exports = function (callback) {
web3.eth.getAccounts((error, addresses) => {
let configs = {
networkId: 1,
};
let zeroEx = new ZeroEx.ZeroEx(web3.currentProvider, configs);
let followingAddress = appConfig.followingAddress;
if (followingAddress === '' || followingAddress === undefined || followingAddress === addresses[0]) {// Invalid address
return;
}
let exchange = Exchange.at(zeroEx.exchange.getContractAddress());
// Get transaction event of all trade transaction on contract.
// Filter only following address to copy the transaction.
let fillEvent = exchange.LogFill();
fillEvent.watch(function (err, orderLog) {
if (err) {
console.log(err);
return;
}
console.dir(orderLog);
let isMaker = false;
let contractAddress = '';
let tokenTradeAmount = 0;
if (orderLog.args.maker === followingAddress) {
isMaker = true;
contractAddress = orderLog.args.makerToken;
tokenTradeAmount = orderLog.args.filledMakerTokenAmount;
} else if (orderLog.args.taker === followingAddress) {
isMaker = false;
contractAddress = orderLog.args.takerToken;
tokenTradeAmount = orderLog.args.filledTakerTokenAmount;
} else { // Not following address.
return;
}
let tokenContract = web3.eth.contract(tokenContractABI).at(contractAddress);
tokenContract.balanceOf(followingAddress, function (error, followingBalance) {
tokenContract.balanceOf(addresses[0], function (error, followerBalance) {
let tokenBalanceBeforeTrade = followingBalance.add(tokenTradeAmount);
let tradePart = tokenTradeAmount.div(tokenBalanceBeforeTrade); // Calculate part of traded.
let rate = orderLog.args.filledMakerTokenAmount.div(orderLog.args.filledTakerTokenAmount);
let followerMakerAmount = 0;
let followerTakerAmount = 0;
let makerOrderToken = orderLog.args.makerToken;
let takerOrderToken = orderLog.args.takerToken;
if (isMaker) {
followerMakerAmount = followerBalance.mul(tradePart).mul(appConfig.portionOfFund);
followerTakerAmount = followerMakerAmount.dividedToIntegerBy(rate);
} else {
followerTakerAmount = followerBalance.mul(tradePart).mul(appConfig.portionOfFund);
followerMakerAmount = followerTakerAmount.mul(rate);
makerOrderToken = orderLog.args.takerToken;
takerOrderToken = orderLog.args.makerToken;
}
if (followerMakerAmount === 0 || followerMakerAmount === 0) { // Check value
return;
}
// Order will be valid 1 hour.
let duration = 3600;
let order = {
// The default web3 account address
maker: addresses[0],
// Anyone may fill the order
taker: '0x0000000000000000000000000000000000000000',
makerTokenAddress: makerOrderToken,
takerTokenAddress: takerOrderToken,
makerTokenAmount: followerMakerAmount,
takerTokenAmount: followerTakerAmount,
// Add the duration (above) to the current time to get the unix
// timestamp
expirationUnixTimestampSec: parseInt(
(new Date().getTime() / 1000) + duration
).toString(),
// We need a random salt to distinguish different orders made by
// the same user for the same quantities of the same tokens.
salt: ZeroEx.ZeroEx.generatePseudoRandomSalt()
};
order.exchangeContractAddress = zeroEx.exchange.getContractAddress();
let relayBaseURL = appConfig.relayBaseURL;
rp({
method: 'POST',
uri: relayBaseURL + '/v0/fees',
body: order,
json: true,
}).then((feeResponse) => {
// Convert the makerFee and takerFee into BigNumbers
order.makerFee = new BigNumber(feeResponse.makerFee);
order.takerFee = new BigNumber(feeResponse.takerFee);
// The fee API tells us what feeRecipient to specify
order.feeRecipient = feeResponse.feeRecipient;
// Once those promises have resolved, our order is ready to be signed
let orderHash = ZeroEx.ZeroEx.getOrderHashHex(order);
return zeroEx.signOrderHashAsync(orderHash, order.maker, false);
}).then((signature) => {
order.ecSignature = signature;
console.dir(order);
return zeroEx.token.setProxyAllowanceAsync(
order.makerTokenAddress,
order.maker,
new BigNumber(order.makerTokenAmount)
)
}).then((tokenAllowance) => {
return zeroEx.token.setProxyAllowanceAsync(
'0xe41d2489571d322189246dafa5ebde1f4699f498',
order.maker,
order.makerFee
)
}).then((feeAllowance) => {
return rp({
method: 'POST',
uri: relayBaseURL + '/v0/order',
body: order,
json: true,
})
}).then((orderPromise) => {
}).catch((something) => {
console.dir(something);
})
});
})
});
});
};