eth-to-bnc-bridge/src/oracle/bncWatcher/bncWatcher.js

109 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-07-07 12:58:35 -07:00
const axios = require('axios')
const BN = require('bignumber.js')
const fs = require('fs')
const { computeAddress } = require('ethers').utils
2019-07-07 12:58:35 -07:00
2019-10-06 03:36:29 -07:00
const logger = require('./logger')
const redis = require('./db')
const { publicKeyToAddress } = require('./crypto')
2019-10-06 03:36:29 -07:00
const { FOREIGN_URL, PROXY_URL, FOREIGN_ASSET } = process.env
2019-07-07 12:58:35 -07:00
2019-10-31 12:43:34 -07:00
const FOREIGN_START_TIME = parseInt(process.env.FOREIGN_START_TIME)
const FOREIGN_FETCH_INTERVAL = parseInt(process.env.FOREIGN_FETCH_INTERVAL)
const FOREIGN_FETCH_BLOCK_TIME_OFFSET = parseInt(process.env.FOREIGN_FETCH_BLOCK_TIME_OFFSET)
2019-07-07 12:58:35 -07:00
const foreignHttpClient = axios.create({ baseURL: FOREIGN_URL })
const proxyHttpClient = axios.create({ baseURL: PROXY_URL })
async function initialize () {
if (await redis.get('foreignTime') === null) {
2019-10-06 03:36:29 -07:00
logger.info('Set default foreign time')
2019-10-31 12:43:34 -07:00
await redis.set('foreignTime', FOREIGN_START_TIME)
2019-07-07 12:58:35 -07:00
}
}
async function main () {
const { transactions, endTime } = await fetchNewTransactions()
if (!transactions || transactions.length === 0) {
logger.debug(`Found 0 new transactions`)
2019-10-31 12:43:34 -07:00
await new Promise(r => setTimeout(r, FOREIGN_FETCH_INTERVAL))
2019-07-07 12:58:35 -07:00
return
}
logger.info(`Found ${transactions.length} new transactions`)
logger.trace('%o', transactions)
2019-07-07 12:58:35 -07:00
for (const tx of transactions.reverse()) {
2019-07-07 12:58:35 -07:00
if (tx.memo !== 'funding') {
const publicKeyEncoded = (await getTx(tx.txHash)).signatures[0].pub_key.value
2019-07-07 12:58:35 -07:00
await proxyHttpClient
.post('/transfer', {
to: computeAddress(Buffer.from(publicKeyEncoded, 'base64')),
2019-08-23 06:48:26 -07:00
value: new BN(tx.value).multipliedBy(10 ** 18).integerValue(),
2019-07-07 12:58:35 -07:00
hash: `0x${tx.txHash}`
})
}
}
await redis.set('foreignTime', endTime)
2019-07-07 12:58:35 -07:00
}
function getTx (hash) {
return foreignHttpClient
.get(`/api/v1/tx/${hash}`, {
params: {
format: 'json'
}
})
.then(res => res.data.tx.value)
.catch(() => getTx(hash))
}
function getBlockTime () {
return foreignHttpClient
.get(`/api/v1/time`)
2019-10-31 12:43:34 -07:00
.then(res => Date.parse(res.data.block_time) - FOREIGN_FETCH_BLOCK_TIME_OFFSET)
.catch(() => getBlockTime())
}
2019-07-07 12:58:35 -07:00
async function fetchNewTransactions () {
2019-10-06 03:36:29 -07:00
logger.debug('Fetching new transactions')
2019-07-07 12:58:35 -07:00
const startTime = parseInt(await redis.get('foreignTime')) + 1
2019-10-07 06:49:20 -07:00
const address = getLastForeignAddress()
2019-10-31 12:43:34 -07:00
const endTime = Math.min(startTime + 3 * 30 * 24 * 60 * 60 * 1000, await getBlockTime())
2019-07-07 12:58:35 -07:00
if (address === null)
return {}
2019-10-06 03:36:29 -07:00
logger.debug('Sending api transactions request')
const params = {
address,
side: 'RECEIVE',
txAsset: FOREIGN_ASSET,
txType: 'TRANSFER',
startTime,
endTime,
}
try {
logger.trace('%o', params)
const transactions = (await foreignHttpClient
.get('/api/v1/transactions', { params })).data.tx
return { transactions, endTime }
} catch (e) {
return await fetchNewTransactions()
}
2019-07-07 12:58:35 -07:00
}
function getLastForeignAddress () {
const epoch = Math.max(0, ...fs.readdirSync('/keys').map(x => parseInt(x.split('.')[0].substr(4))))
if (epoch === 0)
return null
const keysFile = `/keys/keys${epoch}.store`
const publicKey = JSON.parse(fs.readFileSync(keysFile))[5]
return publicKeyToAddress(publicKey)
}
initialize().then(async () => {
while (true) {
await main()
}
})