2020-10-06 14:20:26 +03:00
|
|
|
const { instances, netId } = require('./config')
|
2020-09-28 05:28:34 +03:00
|
|
|
const { poseidon } = require('circomlib')
|
2020-10-05 17:22:52 +03:00
|
|
|
const { toBN, toChecksumAddress, BN } = require('web3-utils')
|
2020-10-02 15:09:33 +03:00
|
|
|
|
2021-03-17 15:39:34 +03:00
|
|
|
const TOKENS = {
|
|
|
|
torn: {
|
|
|
|
tokenAddress: '0x77777FeDdddFfC19Ff86DB637967013e6C6A116C',
|
|
|
|
symbol: 'TORN',
|
|
|
|
decimals: 18,
|
|
|
|
},
|
2021-02-09 20:14:37 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:26:05 +03:00
|
|
|
const sleep = ms => new Promise(res => setTimeout(res, ms))
|
2020-09-29 06:17:42 +03:00
|
|
|
|
2020-09-28 05:28:34 +03:00
|
|
|
function getInstance(address) {
|
2020-10-01 19:37:25 +03:00
|
|
|
address = toChecksumAddress(address)
|
2020-09-28 05:28:34 +03:00
|
|
|
const inst = instances[`netId${netId}`]
|
|
|
|
for (const currency of Object.keys(inst)) {
|
|
|
|
for (const amount of Object.keys(inst[currency].instanceAddress)) {
|
|
|
|
if (inst[currency].instanceAddress[amount] === address) {
|
|
|
|
return { currency, amount }
|
2019-12-02 15:49:24 +03:00
|
|
|
}
|
2019-09-19 22:56:45 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 05:28:34 +03:00
|
|
|
return null
|
2019-12-18 22:25:09 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 13:26:05 +03:00
|
|
|
const poseidonHash = items => toBN(poseidon(items).toString())
|
2020-09-28 05:28:34 +03:00
|
|
|
const poseidonHash2 = (a, b) => poseidonHash([a, b])
|
2019-11-15 12:01:59 +03:00
|
|
|
|
2020-09-28 05:28:34 +03:00
|
|
|
function setSafeInterval(func, interval) {
|
2020-09-29 23:13:45 +03:00
|
|
|
func()
|
|
|
|
.catch(console.error)
|
|
|
|
.finally(() => {
|
|
|
|
setTimeout(() => setSafeInterval(func, interval), interval)
|
|
|
|
})
|
2019-12-02 15:49:24 +03:00
|
|
|
}
|
|
|
|
|
2020-09-29 06:17:42 +03:00
|
|
|
/**
|
|
|
|
* A promise that resolves when the source emits specified event
|
|
|
|
*/
|
|
|
|
function when(source, event) {
|
2020-09-29 23:13:45 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
source
|
2020-10-02 13:26:05 +03:00
|
|
|
.once(event, payload => {
|
2020-09-29 23:13:45 +03:00
|
|
|
resolve(payload)
|
|
|
|
})
|
2020-10-02 13:26:05 +03:00
|
|
|
.on('error', error => {
|
2020-09-29 23:13:45 +03:00
|
|
|
reject(error)
|
|
|
|
})
|
2020-09-29 06:17:42 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-05 17:22:52 +03:00
|
|
|
function getArgsForOracle() {
|
2021-02-09 20:14:37 +03:00
|
|
|
const tokens = {
|
|
|
|
...instances.netId1,
|
2021-03-17 15:39:34 +03:00
|
|
|
...TOKENS,
|
2021-02-09 20:14:37 +03:00
|
|
|
}
|
2020-10-05 17:22:52 +03:00
|
|
|
const tokenAddresses = []
|
|
|
|
const oneUintAmount = []
|
|
|
|
const currencyLookup = {}
|
|
|
|
Object.entries(tokens).map(([currency, data]) => {
|
|
|
|
if (currency !== 'eth') {
|
|
|
|
tokenAddresses.push(data.tokenAddress)
|
|
|
|
oneUintAmount.push(toBN('10').pow(toBN(data.decimals.toString())).toString())
|
|
|
|
currencyLookup[data.tokenAddress] = currency
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return { tokenAddresses, oneUintAmount, currencyLookup }
|
|
|
|
}
|
|
|
|
|
|
|
|
function fromDecimals(value, decimals) {
|
|
|
|
value = value.toString()
|
|
|
|
let ether = value.toString()
|
|
|
|
const base = new BN('10').pow(new BN(decimals))
|
|
|
|
const baseLength = base.toString(10).length - 1 || 1
|
|
|
|
|
|
|
|
const negative = ether.substring(0, 1) === '-'
|
|
|
|
if (negative) {
|
|
|
|
ether = ether.substring(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ether === '.') {
|
|
|
|
throw new Error('[ethjs-unit] while converting number ' + value + ' to wei, invalid value')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split it into a whole and fractional part
|
|
|
|
const comps = ether.split('.')
|
|
|
|
if (comps.length > 2) {
|
|
|
|
throw new Error('[ethjs-unit] while converting number ' + value + ' to wei, too many decimal points')
|
|
|
|
}
|
|
|
|
|
|
|
|
let whole = comps[0]
|
|
|
|
let fraction = comps[1]
|
|
|
|
|
|
|
|
if (!whole) {
|
|
|
|
whole = '0'
|
|
|
|
}
|
|
|
|
if (!fraction) {
|
|
|
|
fraction = '0'
|
|
|
|
}
|
|
|
|
if (fraction.length > baseLength) {
|
|
|
|
throw new Error('[ethjs-unit] while converting number ' + value + ' to wei, too many decimal places')
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fraction.length < baseLength) {
|
|
|
|
fraction += '0'
|
|
|
|
}
|
|
|
|
|
|
|
|
whole = new BN(whole)
|
|
|
|
fraction = new BN(fraction)
|
|
|
|
let wei = whole.mul(base).add(fraction)
|
|
|
|
|
|
|
|
if (negative) {
|
|
|
|
wei = wei.mul(negative)
|
|
|
|
}
|
|
|
|
|
|
|
|
return new BN(wei.toString(10), 10)
|
|
|
|
}
|
|
|
|
|
2020-08-04 10:39:56 +03:00
|
|
|
module.exports = {
|
2020-09-28 05:28:34 +03:00
|
|
|
getInstance,
|
|
|
|
setSafeInterval,
|
|
|
|
poseidonHash2,
|
2020-09-29 06:17:42 +03:00
|
|
|
sleep,
|
|
|
|
when,
|
2020-10-05 17:22:52 +03:00
|
|
|
getArgsForOracle,
|
|
|
|
fromDecimals,
|
2020-08-04 10:39:56 +03:00
|
|
|
}
|