🛠 Refactor to use commands

This commit is contained in:
Dennis Reimann
2020-11-10 11:17:07 +01:00
parent d1b2052494
commit 0e571e8965
7 changed files with 89 additions and 107 deletions

31
commands/stack.js Normal file
View File

@@ -0,0 +1,31 @@
module.exports = async (kraken, validate, getEnv) => {
const [fiat, amount] = getEnv('KRAKEN_API_FIAT', 'KRAKEN_BUY_AMOUNT')
// https://www.kraken.com/features/api
const crypto = 'XBT'
const pair = `X${crypto}Z${fiat}`
// Fetch and display information
const { result: { [`Z${fiat}`]: fiatBalance, [`X${crypto}`]: cryptoBalance } } = await kraken.api('Balance')
const { result: { [pair]: { a: [a], b: [b] } } } = await kraken.api('Ticker', { pair })
const ask = parseFloat(a)
const bid = parseFloat(b)
const price = bid
// Calculate volume and adjust precision
const volume = (amount / price).toFixed(8)
console.log('💰 Balance:', fiatBalance, fiat, '/', cryptoBalance, crypto, '\n')
console.log('📈 Ask:', ask, fiat)
console.log('📉 Bid:', bid, fiat, '\n')
// Place order
const details = { pair, type: 'buy', ordertype: 'limit', price, volume }
if (validate) details.validate = true
const { result: { descr: { order }, txid } } = await kraken.api('AddOrder', details)
console.log('💸 Order:', order)
if (txid) console.log('📎 Transaction ID:', txid.join(', '))
}

25
commands/withdraw.js Normal file
View File

@@ -0,0 +1,25 @@
module.exports = async (kraken, validate, getEnv) => {
const [maxFee, key] = getEnv('KRAKEN_MAX_REL_FEE', 'KRAKEN_WITHDRAW_KEY')
// https://api.kraken.com/0/private/WithdrawInfo
const asset = 'XBT'
const withdrawdetails = { asset, key, amount: 0 }
// Get withdrawal information
const { result: { limit, fee } } = await kraken.api('WithdrawInfo', withdrawdetails)
const relFee = 1/parseFloat(limit)*parseFloat(fee)
console.log(`💡 Relative fee of withdrawal amount: ${(relFee*100).toFixed(2)}%`)
// Place withdrawal when fee is low enough (relatively)
if (relFee < maxFee/100) {
console.log(`💰 Withdraw ${limit} ${asset} now.`)
const withdraw = { asset, key, amount: limit }
if (!validate) {
const { result: { refid } } = await kraken.api('Withdraw', withdraw)
if (refid) console.log(`📎 Withdrawal reference ID: ${refid}`)
}
} else {
console.log(`❌ Fee is too high - max rel. fee: ${parseFloat(maxFee).toFixed(2)}%`)
}
}