-
Notifications
You must be signed in to change notification settings - Fork 3
/
ContractService.ts
275 lines (257 loc) · 8.36 KB
/
ContractService.ts
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
import {
Constants,
Constants as WrapperConstants,
DualClassWrapper,
EsplanadeWrapper,
MagiWrapper,
VivaldiWrapper,
Web3Wrapper
} from '@finbook/duo-contract-wrapper';
import { IOption } from '../common/types';
import dbUtil from '../utils/dbUtil';
import eventUtil from '../utils/eventUtil';
import keyUtil from '../utils/keyUtil';
import priceUtil from '../utils/priceUtil';
import util from '../utils/util';
export default class ContractService {
public web3Wrapper: Web3Wrapper;
public tool: string;
public option: IOption;
public key: string = '';
public address: string = '';
constructor(tool: string, option: IOption) {
this.option = option;
this.tool = tool;
this.web3Wrapper = new Web3Wrapper(null, option.provider, '', option.live);
}
public createDuoWrappers(): {
[type: string]: { [tenor: string]: DualClassWrapper | VivaldiWrapper };
} {
return {
Beethoven: {
Perpetual: new DualClassWrapper(
this.web3Wrapper,
this.web3Wrapper.contractAddresses.Custodians.Beethoven.Perpetual.custodian.address
),
M19: new DualClassWrapper(
this.web3Wrapper,
this.web3Wrapper.contractAddresses.Custodians.Beethoven.M19.custodian.address
)
},
Mozart: {
Perpetual: new DualClassWrapper(
this.web3Wrapper,
this.web3Wrapper.contractAddresses.Custodians.Mozart.Perpetual.custodian.address
),
M19: new DualClassWrapper(
this.web3Wrapper,
this.web3Wrapper.contractAddresses.Custodians.Mozart.M19.custodian.address
)
},
Vivaldi: {
'100C-3H': new VivaldiWrapper(
this.web3Wrapper,
this.web3Wrapper.contractAddresses.Custodians.Vivaldi[
'100C-3H'
].custodian.address
)
}
};
}
public createMagiWrapper() {
return new MagiWrapper(
this.web3Wrapper,
this.web3Wrapper.contractAddresses.Oracles[0].address
);
}
public createEsplanadeWrapper() {
return new EsplanadeWrapper(
this.web3Wrapper,
this.web3Wrapper.contractAddresses.MultiSigManagers[0].address
);
}
public async fetchKey() {
this.key = (await keyUtil.getKey(this.tool, this.option)).privateKey;
this.web3Wrapper = new Web3Wrapper(null, this.option.provider, this.key, this.option.live);
this.address = this.web3Wrapper.web3.eth.accounts.privateKeyToAccount(
this.key.startsWith('0x') ? this.key : '0x' + this.key
).address;
util.logInfo('loaded key for ' + this.address);
}
public async trigger() {
await this.fetchKey();
const duoWrappers = this.createDuoWrappers();
const VivaldiWrappers: VivaldiWrapper[] = [];
for (const tenor in duoWrappers.Vivaldi) VivaldiWrappers.push(duoWrappers.Vivaldi[tenor] as VivaldiWrapper);
eventUtil.trigger(
this.address,
[
duoWrappers.Beethoven.Perpetual as DualClassWrapper,
duoWrappers.Beethoven.M19 as DualClassWrapper,
duoWrappers.Mozart.Perpetual as DualClassWrapper,
duoWrappers.Mozart.M19 as DualClassWrapper,
...VivaldiWrappers
],
this.option.event
);
}
public async commitPrice() {
await this.fetchKey();
const magiWrapper = this.createMagiWrapper();
priceUtil.startCommitPrices(this.address, magiWrapper, this.option.pair);
global.setInterval(() => dbUtil.insertHeartbeat(), 30000);
}
public async fetchPrice() {
await this.fetchKey();
const dualClassCustodianWrappers = this.createDuoWrappers();
const magiWrapper = this.createMagiWrapper();
priceUtil.fetchPrice(
this.address,
[
dualClassCustodianWrappers.Beethoven.Perpetual as DualClassWrapper,
dualClassCustodianWrappers.Beethoven.M19 as DualClassWrapper,
dualClassCustodianWrappers.Mozart.Perpetual as DualClassWrapper,
dualClassCustodianWrappers.Mozart.M19 as DualClassWrapper
],
magiWrapper
);
global.setInterval(() => dbUtil.insertHeartbeat(), 30000);
}
public async checkRound(contractWrapper: VivaldiWrapper, magiWrapper: MagiWrapper) {
await dbUtil.insertHeartbeat();
const states = await contractWrapper.getStates();
const magiPrice = await magiWrapper.getLastPrice();
if (states.lastPriceTime === 0 && states.state === Constants.CTD_TRADING)
contractWrapper.startRound(this.address);
else if (util.getUTCNowTimestamp() - states.lastPriceTime > states.period * 1.5)
return Promise.reject(' option contract has skipped one period!');
else if (states.state !== Constants.CTD_TRADING) {
util.logDebug('contract not in trading state!');
return;
} else if (states.lastPriceTime < states.resetPriceTime)
if (states.priceFetchCoolDown === 0) contractWrapper.startRound(this.address);
else {
const requiredTime = states.resetPriceTime + states.priceFetchCoolDown;
if (
util.getUTCNowTimestamp() > requiredTime &&
magiPrice.timestamp > requiredTime &&
magiPrice.price > 0
)
contractWrapper.startRound(this.address);
}
else if (states.lastPriceTime >= states.resetPriceTime) {
const miniMumTime = states.resetPriceTime + states.period;
if (
util.getUTCNowTimestamp() >= miniMumTime &&
magiPrice.timestamp === miniMumTime &&
magiPrice.price > 0
)
contractWrapper.endRound(this.address);
}
return;
}
public async round(option: IOption) {
await this.fetchKey();
const custodianContract = this.createDuoWrappers();
const contractWrapper: DualClassWrapper | VivaldiWrapper =
custodianContract[option.contractType][option.tenor];
const magiWrapper = this.createMagiWrapper();
await this.checkRound(contractWrapper as VivaldiWrapper, magiWrapper);
global.setInterval(
() => this.checkRound(contractWrapper as VivaldiWrapper, magiWrapper),
30 * 1000
);
}
public async startCustodian(option: IOption) {
let kovanManagerAccount: {
[type: string]: { operator: { privateKey: string; address: string } };
} = {
Beethoven: {
operator: {
address: 'account',
privateKey: ''
}
},
Mozart: {
operator: {
address: 'account',
privateKey: ''
}
},
Vivaldi: {
operator: {
address: 'account',
privateKey: ''
}
}
};
try {
kovanManagerAccount = require('../static/kovanManagerAccount.json');
} catch (error) {
console.log(error);
}
this.web3Wrapper = new Web3Wrapper(
null,
this.option.provider,
kovanManagerAccount[option.contractType].operator.privateKey,
this.option.live
);
const account = kovanManagerAccount.Beethoven.operator.address;
const type = this.option.contractType;
const tenor = this.option.tenor;
if (
![
WrapperConstants.BEETHOVEN,
WrapperConstants.MOZART,
WrapperConstants.VIVALDI
].includes(type) ||
![WrapperConstants.TENOR_PPT, WrapperConstants.TENOR_M19, '100C-3H'].includes(tenor)
) {
util.logDebug('no contract type or tenor specified');
return;
}
const custodianContract = this.createDuoWrappers();
const contractWrapper: DualClassWrapper | VivaldiWrapper = custodianContract[type][tenor];
if (option.contractType === WrapperConstants.VIVALDI) {
util.logInfo(`start custodian of vivaldi`);
(contractWrapper as VivaldiWrapper).startCustodian(
account,
contractWrapper.web3Wrapper.contractAddresses.Custodians[type][tenor].aToken
.address,
contractWrapper.web3Wrapper.contractAddresses.Custodians[type][tenor].bToken
.address,
contractWrapper.web3Wrapper.contractAddresses.Oracles[0].address,
1.0,
true,
true
);
} else
(contractWrapper as DualClassWrapper).startCustodian(
account,
contractWrapper.web3Wrapper.contractAddresses.Custodians[type][tenor].aToken
.address,
contractWrapper.web3Wrapper.contractAddresses.Custodians[type][tenor].bToken
.address,
contractWrapper.web3Wrapper.contractAddresses.Oracles[0].address
);
}
public fetchEvent() {
const duoWrappers = this.createDuoWrappers();
const magiWrapper = this.createMagiWrapper();
const esplanadeWrapper = this.createEsplanadeWrapper();
const VivaldiWrappers = [];
for (const tenor in duoWrappers.Vivaldi) VivaldiWrappers.push(duoWrappers.Vivaldi[tenor]);
eventUtil.fetch(
[
duoWrappers.Beethoven.Perpetual,
duoWrappers.Beethoven.M19,
duoWrappers.Mozart.Perpetual,
duoWrappers.Mozart.M19,
magiWrapper,
esplanadeWrapper,
...VivaldiWrappers
],
this.option.force
);
}
}