-
Notifications
You must be signed in to change notification settings - Fork 0
/
MagiWrapper.ts
148 lines (135 loc) · 4.7 KB
/
MagiWrapper.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
import BaseContractWrapper from './BaseContractWrapper';
import * as CST from './constants';
import magiAbi from './static/Magi.json';
import { IContractPrice, IMagiAddresses, IMagiStates, ITransactionOption } from './types';
import Web3Wrapper from './Web3Wrapper';
export class MagiWrapper extends BaseContractWrapper {
public readonly events = [
CST.EVENT_UPDATE_ROLE_MANAGER,
CST.EVENT_UPDATE_OPERATOR,
CST.EVENT_COMMIT_PRICE,
CST.EVENT_ACCEPT_PRICE,
CST.EVENT_SET_VALUE,
CST.EVENT_UPDATE_PRICE_FEED
];
constructor(web3Wrapper: Web3Wrapper, address: string) {
super(web3Wrapper, magiAbi.abi, address);
}
public async isStarted(): Promise<boolean> {
return await this.contract.methods.started().call();
}
public async getStates(): Promise<IMagiStates> {
const firstPrice = await this.contract.methods.firstPrice().call();
const secondPrice = await this.contract.methods.secondPrice().call();
return {
isStarted: await this.isStarted(),
firstPrice: {
price: Web3Wrapper.fromWei(firstPrice[0].valueOf()),
timestamp: firstPrice[1].valueOf() * 1000,
source: firstPrice[2].valueOf()
},
secondPrice: {
price: Web3Wrapper.fromWei(secondPrice[0].valueOf()),
timestamp: secondPrice[1].valueOf() * 1000,
source: secondPrice[2].valueOf()
},
priceTolerance: (await this.contract.methods.priceTolInBP().call()) / 10000,
priceFeedTolerance: (await this.contract.methods.priceFeedTolInBP().call()) / 10000,
priceFeedTimeTolerance: (await this.contract.methods.priceFeedTimeTol().call()) * 1000,
priceUpdateCoolDown: (await this.contract.methods.priceUpdateCoolDown().call()) * 1000,
numOfPrices: Number(await this.contract.methods.numOfPrices().call()),
lastOperationTime: (await this.contract.methods.lastOperationTime().call()) * 1000,
operationCoolDown: (await this.contract.methods.operationCoolDown().call()) * 1000
};
}
public async getAddresses(): Promise<IMagiAddresses> {
return {
priceFeed: [
await this.contract.methods.priceFeed1().call(),
await this.contract.methods.priceFeed2().call(),
await this.contract.methods.priceFeed3().call()
],
operator: await this.contract.methods.operator().call(),
roleManagerAddress: await this.contract.methods.roleManagerAddress().call()
};
}
public async getLastPrice(): Promise<IContractPrice> {
const lastPrice = await this.contract.methods.getLastPrice().call();
return {
price: Web3Wrapper.fromWei(lastPrice[0].valueOf()),
timestamp: Number(lastPrice[1].valueOf()) * 1000
};
}
public async startMagi(
account: string,
price: number,
timeInSecond: number,
option: ITransactionOption = {}
) {
if (!this.web3Wrapper.isLocal()) return this.web3Wrapper.wrongEnvReject();
return this.contract.methods
.startOracle(Web3Wrapper.toWei(price), timeInSecond)
.send(await this.web3Wrapper.getTransactionOption(account, CST.START_MAGI_GAS, option));
}
public async commitPrice(
account: string,
price: number,
timeInSecond: number,
option: ITransactionOption = {}
) {
if (!this.web3Wrapper.isLocal()) return this.web3Wrapper.wrongEnvReject();
return this.contract.methods
.commitPrice(Web3Wrapper.toWei(price), timeInSecond)
.send(
await this.web3Wrapper.getTransactionOption(account, CST.COMMIT_PRICE_GAS, option)
);
}
public async updatePriceFeed(account: string, index: number, option: ITransactionOption = {}) {
if (this.web3Wrapper.isReadOnly()) return this.web3Wrapper.readOnlyReject();
return this.contract.methods
.updatePriceFeed(index)
.send(
await this.web3Wrapper.getTransactionOption(account, CST.UPDATE_PRICE_FEED, option)
);
}
public async setValue(
account: string,
index: number,
newValue: number,
option: ITransactionOption = {}
) {
if (this.web3Wrapper.isReadOnly()) return this.web3Wrapper.readOnlyReject();
return this.contract.methods
.setValue(index, newValue)
.send(await this.web3Wrapper.getTransactionOption(account, CST.MAGI_SET_VALUE_GAS, option));
}
public async updateRoleManager(
account: string,
newManagerAddr: string,
option: ITransactionOption = {}
) {
if (this.web3Wrapper.isReadOnly()) return this.web3Wrapper.readOnlyReject();
return this.contract.methods
.updateRoleManager(newManagerAddr)
.send(
await this.web3Wrapper.getTransactionOption(
account,
CST.UPDATE_ROLE_MANAGER_GAS,
option
)
);
}
public async updateOperator(account: string, option: ITransactionOption = {}) {
if (this.web3Wrapper.isReadOnly()) return this.web3Wrapper.readOnlyReject();
return this.contract.methods
.updateOperator()
.send(
await this.web3Wrapper.getTransactionOption(
account,
CST.UPDATE_OPERATOR,
option
)
);
}
}
export default MagiWrapper;