#!/usr/bin/env node const BN = require('bignumber.js') const fs = require('fs') const log4js = require('log4js') const logger = require("@ledgerhq/logs"); const path = require('path') const pm2 = require('pm2') const {promisify} = require('es6-promisify') const baseDir = __dirname + '/' process.chdir(baseDir) process.env.NODE_CONFIG_DIR = (process.env.NODE_CONFIG_DIR ? process.env.NODE_CONFIG_DIR + require('path').delimiter : "") + baseDir + "config/" + require('path').delimiter + baseDir + "config/secrets/" + require('path').delimiter + "config/radix/" const config = require('config') const ut = require('./utils/util') const {argParse} = require('./utils/argparse.js') const aave = require('./utils/eth/aave.js') const curve = require('./utils/eth/curve.js') const radix = require('./utils/radix.js') const walletconnect = require('./utils/eth/walletconnect.js') const addresses = require('./utils/ledger/addresses.js') const maker = require('./utils/eth/maker.js') const exch = require('./utils/exchange.js') const tx = require('./utils/eth/tx.js') logger.listen(log => console.log(log.type + ": " + log.message)) BN.set({DECIMAL_PLACES: 45, ROUNDING_MODE: 1/*round down*/}) log4js.configure( { appenders: { out: {type: 'stdout', layout: { type: 'pattern', pattern: '%[[%d] [%p] [%f{1}#%l] -%] %m', }, }, }, categories: {default: {appenders: ['out'], level: 'info', enableCallStack: true}}, } ) const log = log4js.getLogger() log.level = config.get('loglevel') if (process && (process.version < 'v15.0.0' || process.version.match(/^v\d\./))) { log.error('Please update nodejs to v15.0.0 or higher.') process.exit(1) } if (require.main === module) { var args = argParse() if (!args.getYargsCompletions) log.debug(args) //fs.writeFileSync("/home/user/cceb/args.txt", JSON.stringify({args})); let exchange if ('exchange' === args._[0]) { const exchangeType = config.get(`keys.${args.exchange}.type`) if (exchangeType === 'centralized') { exchange = ut.getExchange(args.exchange) } } (async () => { await processArgs(exchange, args) })() } async function processArgs (exchange, args) { try { switch (args._[0]) { case 'exchange': switch (args._[1]) { case 'listorders': await exch.dispListOrders(exchange, args, undefined) break case 'add': await exch.dispAddOrder(exchange, args, undefined) break case 'rm': await exch.removeOrder(exchange, args, undefined) break case 'rmall': await exch.removeAllOrders(exchange, args, undefined) break case 'trickle': await exch.dispAddTrickleOrder(exchange, args, undefined) break case 'markets': await exch.dispMarkets(exchange, args) break case 'price': // TODO: implement dispPrice() // eslint-disable-next-line await exch.dispPrice(exchange, args) break case 'orderbook': await exch.dispOrderbook(exchange, args) break case 'withdraw': await exch.withdraw(exchange, args) break case 'deposit': await exch.dispDeposit(exchange, args) break case 'listbalances': await exch.dispListBalances(exchange, args, null) break } break case 'eth': switch (args._[1]) { case 'tx': await tx.dispEthTransaction(args) break case 'abi': await tx.dispEthTransaction(args) break case 'source': await tx.dispEthTransaction(args) break case 'send': await tx.dispEthTransaction(args) break case 'address': await tx.dispEthTransaction(args) break case 'import': await tx.dispEthTransaction(args) break case 'nonce': await tx.dispEthTransaction(args) break case 'web3': await tx.dispEthTransaction(args) break case 'walletconnect': switch (args._[2]) { case 'connect': await walletconnect.displayConnect(args) break } break case 'maker': switch (args._[2]) { case 'info': await maker.dispVaultInfo(args) break case 'open': await maker.dispVaultOpen(args) break case 'deposit': await maker.dispVaultDeposit(args) break case 'withdraw': await maker.dispVaultWithdraw(args) break case 'generate': await maker.dispVaultGenerate(args) break case 'payback': await maker.dispVaultPayback(args) break case 'estimate': await maker.dispVaultEstimate(args) // --Maker MKR acution commands --- break case 'auction': await maker.dispFlop(args) break case 'tick': await maker.dispTick(args) break case 'dent': await maker.dispDent(args) break case 'deal': await maker.dispDeal(args) break case 'flog': await maker.dispFlog(args) } break case 'aave': switch (args._[2]) { case 'deposit': await aave.dispAaveDeposit(args.block, args.token, args.amount, args.from, args.gaslimit, args.gasprice, args.nonce) break case 'withdraw': await aave.dispAaveWithdraw(args) break case 'collateral': await aave.dispAaveCollateral(args.block, args.token, args.disable, args.from, args.gaslimit, args.gasprice, args.nonce) break case 'borrow': await aave.dispAaveBorrow(args.amount, args.token, args.fixed, args.from, args.gasLimit, args.gasPrice, args.nonce) break case 'payback': await aave.dispAavePayback(args) break case 'swaprate': await aave.dispAaveSwapRate(args) break case 'rebalance': await aave.dispAaveRebalance(args) break case 'liquidate': await aave.dispAaveLiquidate(args) break case 'info': await aave.dispAaveInfo(args) break case 'estimate': await aave.dispAaveEstimate(args) } break case 'curve': switch (args._[2]) { case 'info': await curve.dispCurveInfo(args, baseDir) break } break } break case 'ledger': switch (args._[1]) { case 'addresses': await addresses.dispLedgerAddresses(args) break } break case 'radix': await radix.dispRadixCommand(args) break } process.exit(0) } catch (e) { log.error(e.stack) process.exit(1) } } // TODO: implement background order creation async function startProc (args) { const connect = promisify(pm2.connect) const start = promisify(pm2.start) // eslint-disable-next-line const stop = promisify(pm2.stop) const list = promisify(pm2.list) // eslint-disable-next-line const readFile = promisify(fs.readFile) // eslint-disable-next-line const writeFile = promisify(fs.writeFile) await connect() var procName = path.basename(__filename) + '-' + args._[0] const listPm2 = await list() while (listPm2.findIndex(desc => desc.name === procName) !== -1) { procName += 'I' } const proc = await start({ script: procName, options: { name: procName, args: ['--pm2'].concat(process.argv), }, }) pm2.disconnect() return proc } // TODO: implement background order creation // eslint-disable-next-line async function startFork (exchange, args) { process.env.pm_id } module.exports = { argParse, startFork, startProc, }