/
index.js
73 lines (62 loc) · 2.07 KB
/
index.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
import debug from 'debug'
import aggregator from 'xrp-price-aggregator'
import Conn from 'rippled-ws-client'
import Sign from 'rippled-ws-client-sign'
import dotenv from 'dotenv'
const log = debug('oracle:persist')
const timeoutSec = (process.env.TIMEOUT_SECONDS || 55)
const timeout = setTimeout(() => {
log(`Error, killed by timeout after ${timeoutSec} seconds`)
process.exit(1)
}, timeoutSec * 1000)
export default (async () => {
dotenv.config()
const Connection = new Conn(process.env.ENDPOINT)
log(`START (timeout at ${timeoutSec}), GO GET DATA!`)
const data = await aggregator
log('GOT DATA')
log({data})
await Connection
const Memos = Object.keys(data.rawResultsNamed).map(k => {
return {
Memo: {
MemoData: Buffer.from(data.rawResultsNamed[k].map(_v => String(_v)).join(';'), 'utf-8').toString('hex').toUpperCase(),
MemoFormat: Buffer.from('text/csv', 'utf-8').toString('hex').toUpperCase(),
MemoType: Buffer.from('rates:' + k, 'utf-8').toString('hex').toUpperCase()
}
}
})
const Tx = {
TransactionType: 'TrustSet',
Account: process.env.XRPL_SOURCE_ACCOUNT,
Fee: '10',
Flags: 131072,
LimitAmount: {
currency: 'USD',
issuer: process.env.XRPL_DESTINATION_ACCOUNT,
value: String(data.filteredMedian)
},
Memos
}
log('SIGN & SUBMIT')
try {
const Signed = await new Sign(Object.assign({}, Tx), process.env.XRPL_SOURCE_ACCOUNT_SECRET, await Connection)
log({Signed})
} catch (e) {
log(`Error signing / submitting: ${e.message}`)
}
if (typeof process.env.ENDPOINT_TESTNET !== 'undefined') {
log('SIGN & SUBMIT TESTNET')
const ConnectionTestnet = await new Conn(process.env.ENDPOINT_TESTNET)
try {
const SignedTestnet = await new Sign(Object.assign({}, Tx), process.env.XRPL_SOURCE_ACCOUNT_SECRET, await ConnectionTestnet)
log({SignedTestnet})
} catch (e) {
log(`Error signing / submitting @ Testnet: ${e.message}`)
}
;(await ConnectionTestnet).close()
}
log('WRAP UP')
;(await Connection).close()
clearTimeout(timeout)
})()