-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathContract.js
204 lines (188 loc) · 7.09 KB
/
Contract.js
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
const { utils } = require(`web3`)
const storage = require(`./storage.js`)()
const config = require(`config`)
const networks = {
mainnet: 1,
ropsten: 3,
rinkeby: 4,
kovan: 42
}
const blockScanIncrement = config.get(`rebase.blockScanIncrement`)
const maxListenerErrors = 10
class Contract {
constructor (web3, contractJson) {
this.web3 = web3
this.abi = contractJson.abi
this.contractWebsocket = new web3.websocket.eth.Contract(contractJson.abi, Contract.getAddressFromJson(contractJson))
this.contractHttp = new web3.http.eth.Contract(contractJson.abi, Contract.getAddressFromJson(contractJson))
this.transactionHash = Contract.getTransactionHashFromJson(contractJson)
if (!this.transactionHash) {
throw new Error(`Missing transactionHash in JSON`)
}
if (!contractJson.contractName) {
throw new Error(`Missing contractName in JSON`)
}
this.name = contractJson.contractName
this.errorCount = 0
}
static getNetworkFromJson (contractJson) {
if (!contractJson.networks) {
return null
}
return contractJson.networks[networks[config.get(`ethereum.network`)]]
}
static getAddressFromJson (contractJson) {
const network = Contract.getNetworkFromJson(contractJson)
return network ? network.address : null
}
static getTransactionHashFromJson (contractJson) {
const network = Contract.getNetworkFromJson(contractJson)
return network ? network.transactionHash : null
}
hasEvents () {
return this.abi.find(i => i.type === `event`)
}
listenForEvents () {
const eventEmitter = this.contractWebsocket.events.allEvents()
eventEmitter.on(`data`, event => storage.processEvents(this.name, [event]))
console.info(`${this.getLogPrefix()}: Listening for live events`)
eventEmitter.on(`error`, (error) => {
this.errorCount++
console.error(`${this.getLogPrefix()}: Error listening for events: ${error.message}, re-attaching listeners...`)
eventEmitter.removeAllListeners()
if (this.errorCount < maxListenerErrors) {
this.listenForEvents()
} else {
console.error(`${this.getLogPrefix()}: Error listening for events: ${error.message}, max re-try limit hit.`)
}
})
}
async initialize () {
const currentBlock = await this.web3.http.eth.getBlockNumber()
if (config.get(`rebase.enabled`) === true) {
this.rebase(currentBlock)
}
if (config.get(`scan.enabled`) === true) {
this.scanBlocks(currentBlock)
}
if (config.get(`listen.enabled`) === true) {
this.listenForEvents()
}
}
async rebase (currentBlock) {
const transactionReceipt = await this.web3.http.eth.getTransactionReceipt(this.transactionHash)
const birthBlock = transactionReceipt.blockNumber
let fromBlock = birthBlock
let toBlock = Math.min(birthBlock + blockScanIncrement, currentBlock)
let adjustableBlockScanIncrement = blockScanIncrement
console.log(`${this.getLogPrefix()}: Rebase: Starting from block ${birthBlock} to ${currentBlock} (${currentBlock - birthBlock} blocks)`)
while (toBlock <= currentBlock && fromBlock < toBlock) {
let events
try {
console.log(`${this.getLogPrefix()}: Rebase: Fetching from ${fromBlock} to ${toBlock} (${toBlock - fromBlock} blocks)`)
events = await this.contractHttp.getPastEvents(`allEvents`, {
fromBlock,
toBlock
})
} catch (error) {
console.error(`${this.getLogPrefix()}: Error from web3: ${error.message}. Trying again with lower increment.`)
adjustableBlockScanIncrement = Math.ceil(adjustableBlockScanIncrement / 2)
if (adjustableBlockScanIncrement === 1) {
throw new Error(`${this.getLogPrefix()}: Block scan increment reached 1 after error, probably a problem with Web3`)
}
toBlock -= adjustableBlockScanIncrement
}
if (events != null) {
console.log(`${this.getLogPrefix()}: Rebase: Found ${events.length} events in block range ${fromBlock} - ${toBlock}`)
try {
if (events.length > 0) {
/* eslint no-await-in-loop: 0 */
await storage.processEvents(this.name, events, true)
}
fromBlock = toBlock + 1
toBlock = Math.min(fromBlock + blockScanIncrement, currentBlock)
adjustableBlockScanIncrement = blockScanIncrement
} catch (error) {
console.error(`${this.getLogPrefix()}: Error persisting events in rebase: ${error.message}.`)
}
}
}
console.log(`${this.getLogPrefix()}: Rebase: Completed`)
}
async scanBlocks (currentBlock) {
const offset = config.get(`scan.blockScanOffset`)
let running = false
let fromBlock = currentBlock
this.web3.websocket.eth.subscribe(`newBlockHeaders`, (error, result) => {
if (error) {
console.log(error)
process.exit(1)
}
})
.on(`data`, async (blockHeader) => {
if (!running) {
running = true
const toBlock = blockHeader.number - offset
if (toBlock > fromBlock) {
let events
try {
events = await this.contractHttp.getPastEvents(`allEvents`, {
fromBlock,
toBlock
})
} catch (error) {
console.error(`${this.getLogPrefix()}: Error from web3: ${error.message}`)
}
if (events != null) {
console.log(`${this.getLogPrefix()}: Scan: Found ${events.length} events in block range ${fromBlock} - ${toBlock}`)
try {
if (events.length > 0) {
await storage.processEvents(this.name, events, true)
}
fromBlock = toBlock + 1
} catch (error) {
console.error(`${this.getLogPrefix()}: Error persisting events in scan: ${error.message}.`)
}
}
}
running = false
}
})
.on(`error`, (error) => {
console.error(error)
})
}
async getTokenBalances () {
const decimals = utils.toBN(await this.contractHttp.methods.decimals().call())
const events = await storage.getEvents({contractName: this.name, eventName: `Transfer`})
const zero = utils.toBN(0)
const balances = {}
events.forEach((event) => {
const { from, to } = event.returnValues
const value = utils.toBN(event.returnValues.value)
if (balances[to] == null) {
balances[to] = zero.clone()
}
if (balances[from] == null) {
balances[from] = zero.clone()
}
balances[from] = balances[from].sub(value)
balances[to] = balances[to].add(value)
})
const formattedBalances = {}
Object.keys(balances).forEach((addr) => {
const balance = balances[addr]
if (balance.gt(zero)) {
const divisor = utils.toBN(10).pow(decimals)
const beforeDecimal = balance.div(divisor)
const afterDecimal = balance.mod(divisor)
formattedBalances[addr] = `${beforeDecimal}.${afterDecimal}`
}
})
return formattedBalances
}
getLogPrefix () {
return `${new Date().toISOString()}-${this.name}`
}
}
module.exports = Contract