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

98 lines
2.7 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
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-07 06:49:20 -07:00
await redis.set('foreignTime', Date.now() - 2 * 30 * 24 * 60 * 60 * 1000)
2019-07-07 12:58:35 -07:00
}
}
async function main () {
const newTransactions = await fetchNewTransactions()
if (newTransactions === null || newTransactions.length === 0) {
await new Promise(r => setTimeout(r, 5000))
return
}
if (newTransactions.length) {
2019-10-06 03:36:29 -07:00
logger.info(`Found ${newTransactions.length} new transactions`)
logger.trace('%o', newTransactions)
} else {
2019-10-06 03:36:29 -07:00
logger.debug(`Found 0 new transactions`)
}
2019-07-07 12:58:35 -07:00
for (const tx of newTransactions.reverse()) {
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', Date.parse(tx.timeStamp))
}
}
function getTx (hash) {
return foreignHttpClient
.get(`/api/v1/tx/${hash}`, {
params: {
format: 'json'
}
})
.then(res => res.data.tx.value)
.catch(() => getTx(hash))
}
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-07-07 12:58:35 -07:00
if (address === null)
return null
2019-10-06 03:36:29 -07:00
logger.debug('Sending api transactions request')
2019-07-07 12:58:35 -07:00
return foreignHttpClient
.get('/api/v1/transactions', {
params: {
address,
side: 'RECEIVE',
txAsset: FOREIGN_ASSET,
txType: 'TRANSFER',
startTime,
endTime: startTime + 3 * 30 * 24 * 60 * 60 * 1000,
}
})
.then(res => res.data.tx)
.catch(() => 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()
}
})