2019-07-07 12:58:35 -07:00
|
|
|
const axios = require('axios')
|
|
|
|
const BN = require('bignumber.js')
|
|
|
|
const fs = require('fs')
|
2019-07-15 12:41:02 -07:00
|
|
|
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')
|
2019-10-08 10:45:28 -07:00
|
|
|
const redis = require('./db')
|
|
|
|
const { publicKeyToAddress } = require('./crypto')
|
2019-10-06 03:36:29 -07:00
|
|
|
|
2019-07-15 12:41:02 -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 () {
|
2019-10-31 08:46:14 -07:00
|
|
|
const { transactions, endTime } = await fetchNewTransactions()
|
|
|
|
if (!transactions || transactions.length === 0) {
|
|
|
|
logger.debug(`Found 0 new transactions`)
|
2019-07-07 12:58:35 -07:00
|
|
|
await new Promise(r => setTimeout(r, 5000))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-31 08:46:14 -07:00
|
|
|
logger.info(`Found ${transactions.length} new transactions`)
|
|
|
|
logger.trace('%o', transactions)
|
2019-07-07 12:58:35 -07:00
|
|
|
|
2019-10-31 08:46:14 -07:00
|
|
|
for (const tx of transactions.reverse()) {
|
2019-07-07 12:58:35 -07:00
|
|
|
if (tx.memo !== 'funding') {
|
2019-07-15 12:41:02 -07:00
|
|
|
const publicKeyEncoded = (await getTx(tx.txHash)).signatures[0].pub_key.value
|
2019-07-07 12:58:35 -07:00
|
|
|
await proxyHttpClient
|
|
|
|
.post('/transfer', {
|
2019-07-15 12:41:02 -07:00
|
|
|
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}`
|
|
|
|
})
|
|
|
|
}
|
2019-10-31 08:46:14 -07:00
|
|
|
//await redis.set('foreignTime', Date.parse(tx.timeStamp))
|
2019-07-07 12:58:35 -07:00
|
|
|
}
|
2019-10-31 08:46:14 -07:00
|
|
|
await redis.set('foreignTime', endTime)
|
2019-07-07 12:58:35 -07:00
|
|
|
}
|
|
|
|
|
2019-10-31 02:50:03 -07:00
|
|
|
function getTx (hash) {
|
2019-07-15 12:41:02 -07:00
|
|
|
return foreignHttpClient
|
|
|
|
.get(`/api/v1/tx/${hash}`, {
|
|
|
|
params: {
|
|
|
|
format: 'json'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(res => res.data.tx.value)
|
|
|
|
.catch(() => getTx(hash))
|
|
|
|
}
|
|
|
|
|
2019-10-31 08:46:14 -07:00
|
|
|
function getBlockTime () {
|
|
|
|
return foreignHttpClient
|
|
|
|
.get(`/api/v1/time`)
|
|
|
|
.then(res => Date.parse(res.data.block_time) - 10 * 1000)
|
|
|
|
.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 08:46:14 -07:00
|
|
|
const endTime = await getBlockTime()
|
2019-07-07 12:58:35 -07:00
|
|
|
if (address === null)
|
2019-10-31 08:46:14 -07:00
|
|
|
return {}
|
2019-10-06 03:36:29 -07:00
|
|
|
logger.debug('Sending api transactions request')
|
2019-10-31 08:46:14 -07:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|