-
Notifications
You must be signed in to change notification settings - Fork 13
/
blockchainConnexion.js
297 lines (269 loc) · 8.38 KB
/
blockchainConnexion.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
var Web3 = require('web3')
const {
web3UrlBep20,
web3UrlBTT,
web3Url,
web3PolygonUrl,
web3UrlArthera,
CampaignConstants,
OracleConstants,
ArtheraNetworkConstant,
} = require('./conf/const')
const { Campaigns, Event } = require('./model/index')
const { TronConstant } = require('./conf/const')
const options = {
timeout: 30000,
clientConfig: {
// Useful if requests are large
maxReceivedFrameSize: 100000000, // bytes - default: 1MiB
maxReceivedMessageSize: 100000000, // bytes - default: 8MiB
// Useful to keep a connection alive
keepalive: true,
keepaliveInterval: 60000, // ms
},
// Enable auto reconnection
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false,
},
}
const networks = [
{ name: 'ERC20', providerUrl: process.env.WEB3_URL },
{ name: 'BEP20', providerUrl: process.env.WEB3_URL_BEP20 },
{ name: 'POLYGON', providerUrl: process.env.WEB3_URL_POLYGON },
{ name: 'BTTC', providerUrl: process.env.WEB3_URL_BTT },
{ name: 'ARTHERA', providerUrl: process.env.WEB3_URL_ARTHERA },
]
exports.artheraConnexion = async () => {
try {
let Web3 = require('web3')
let web3 = await new Web3(
new Web3.providers.HttpProvider(web3UrlArthera, options)
)
await web3.eth.getNodeInfo()
return web3
} catch (err) {
console.log('error', err)
return null
}
}
exports.bep20Connexion = async () => {
try {
let Web3 = require('web3')
let web3 = await new Web3(
new Web3.providers.HttpProvider(web3UrlBep20, options)
)
await web3.eth.getNodeInfo()
return web3
} catch (err) {
console.log('error', err)
return null
}
}
exports.erc20Connexion = async () => {
try {
let Web3 = require('web3')
let web3 = await new Web3(
new Web3.providers.HttpProvider(web3Url, options)
)
try {
await web3.eth.getNodeInfo()
// .then((result) => console.log('result: ', result))
} catch (error) {
console.log('error: ', error)
}
return web3
} catch (err) {
return null
}
}
exports.polygonConnexion = async () => {
try {
let Web3 = require('web3')
let web3 = await new Web3(
new Web3.providers.HttpProvider(web3PolygonUrl, options)
)
await web3.eth.getNodeInfo()
return web3
} catch (err) {
return null
}
}
exports.bttConnexion = async () => {
try {
let Web3 = require('web3')
let web3 = await new Web3(
new Web3.providers.HttpProvider(web3UrlBTT, options)
)
await web3.eth.getNodeInfo()
return web3
} catch (err) {
return null
}
}
exports.webTronInstance = async () => {
try {
const TronWeb = require('tronweb')
const tronWeb = new TronWeb({
fullHost: process.env.TRON_NETWORK_URL,
headers: { 'TRON-PRO-API-KEY': process.env.TRON_PRO_API_KEY },
})
return tronWeb
} catch (err) {}
}
exports.getContractByNetwork = async (credentials) => {
try {
var contract = new credentials.WEB3.eth.Contract(
CampaignConstants[credentials.network.toUpperCase()].abi,
CampaignConstants[credentials.network.toUpperCase()].address
)
contract.getGasPrice = credentials.WEB3.eth.getGasPrice
return contract
} catch (err) {}
}
exports.getProviderUrl = async (networkName) => {
const network = networks.find((net) => net.name === networkName)
return network ? network.providerUrl : null
}
exports.getContractByNetworkExternal = async (credentials) => {
try {
let web3 = await new Web3(
new Web3.providers.HttpProvider(
await exports.getProviderUrl(credentials.network),
options
)
)
var contract = new web3.eth.Contract(
CampaignConstants[credentials.network.toUpperCase()].abi,
CampaignConstants[credentials.network.toUpperCase()].address
)
contract.getGasPrice = web3.eth.getGasPrice
return contract
} catch (err) {
console.log(err)
}
}
exports.getCampaignContractByHashCampaign = async (
hash,
credentials = false,
tronWeb = null
) => {
try {
var ctr
var campaign = await Campaigns.findOne({ hash })
if (campaign?.contract) {
if (!!tronWeb) {
ctr = await tronWeb.contract(
TronConstant.campaign.abi,
TronConstant.campaign.address
)
return ctr
} else {
if (campaign.token.type === 'ARTHERA') {
ctr = new credentials.Web3ARTHERA.eth.Contract(
CampaignConstants[ArtheraNetworkConstant].abi,
CampaignConstants[ArtheraNetworkConstant].address
)
} else {
credentials.network = campaign.token.type
ctr = this.getContractByNetwork(credentials)
}
return ctr
}
}
} catch (err) {}
}
exports.getCampaignContractByHashCampaignExternal = async (
hash,
credentials = false,
tronWeb = null
) => {
try {
var campaign = await Campaigns.findOne({ hash })
if (campaign?.contract) {
credentials.network = campaign.token.type
return this.getContractByNetworkExternal(credentials)
}
} catch (err) {}
}
exports.getPromContract = async (idProm, credentials = false) => {
try {
var prom = await Event.findOne(
{ prom: idProm },
{ contract: 1, _id: 0 }
)
return this.getContractByNetwork(credentials)
} catch (err) {}
}
exports.getPromContractExternal = async (idProm, credentials = false) => {
try {
var prom = await Event.findOne(
{ prom: idProm },
{ contract: 1, _id: 0 }
)
return this.getContractByNetworkExternal(credentials)
} catch (err) {}
}
exports.getCampaignOwnerAddr = async (idProm) => {
try {
var prom = await Event.findOne(
{ prom: idProm },
{ contract: 1, _id: 0 }
)
if (!prom.contract) return
return process.env.CAMPAIGN_OWNER
} catch (err) {}
}
exports.getOracleContractByCampaignContract = async (credentials = false) => {
try {
var contract
if (!!credentials.Web3ARTHERA) {
contract = new credentials.Web3ARTHERA.eth.Contract(
OracleConstants[ArtheraNetworkConstant].abi,
OracleConstants[ArtheraNetworkConstant].address
)
} else {
contract = new credentials.WEB3.eth.Contract(
OracleConstants[credentials.network.toUpperCase()].abi,
OracleConstants[credentials.network.toUpperCase()].address
)
contract.getGasPrice = credentials.WEB3.eth.getGasPrice
}
return contract
} catch (err) {}
}
exports.getOracleContractByCampaignContractExternal = async (
credentials = false
) => {
try {
let web3 = await new Web3(
new Web3.providers.HttpProvider(
await exports.getProviderUrl(credentials.network),
options
)
)
var contract = new web3.eth.Contract(
OracleConstants[credentials.network.toUpperCase()].abi,
OracleConstants[credentials.network.toUpperCase()].address
)
contract.getGasPrice = web3.eth.getGasPrice
return contract
} catch (err) {}
}
module.exports.tronBalances = async (_) => {
// let tronWallets = await Wallet.find({tronAddress :{ $exists: true }},{tronAddress :1})
let tronWallets = require('./tronWallets').walletsTron
let tronWeb = await this.webTronInstance()
let walletBalances = []
for (let wallet of tronWallets) {
let amount = await tronWeb.trx.getBalance(wallet.tronAddress)
amount && walletBalances.push(wallet.tronAddress)
}
console.log('tron wallets that have trx balance', walletBalances.length)
return responseHandler.makeResponseData(res, 200, 'success', {
count: walletBalances.length,
walletBalances,
})
}