diff --git a/README.md b/README.md index 87c0307..cf77f1c 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ KRAKEN_API_KEY="apiKeyFromTheKrakenSettings" KRAKEN_API_SECRET="privateKeyFromTheKrakenSettings" # used for buying +KRAKEN_ORDER_TYPE="market" # "limit" (default) or "market" KRAKEN_API_FIAT="USD" # the governmental shitcoin you are selling KRAKEN_BUY_AMOUNT=21 # fiat amount you trade for the future of money @@ -138,6 +139,7 @@ set -e export KRAKEN_API_KEY="apiKeyFromTheKrakenSettings" export KRAKEN_API_SECRET="privateKeyFromTheKrakenSettings" +export KRAKEN_ORDER_TYPE="market" export KRAKEN_API_FIAT="USD" export KRAKEN_BUY_AMOUNT=21 export KRAKEN_MAX_REL_FEE=0.5 diff --git a/commands/stack.js b/commands/stack.js index 7d223c6..727a320 100644 --- a/commands/stack.js +++ b/commands/stack.js @@ -1,5 +1,6 @@ -module.exports = async (kraken, validate, getEnv) => { +module.exports = async (kraken, validate, { getEnv, getEnvOpt }) => { const [fiat, amount] = getEnv('KRAKEN_API_FIAT', 'KRAKEN_BUY_AMOUNT') + const ordertype = getEnvOpt('KRAKEN_ORDER_TYPE', 'limit', ['limit', 'market']) // https://www.kraken.com/features/api const crypto = 'XBT' @@ -24,7 +25,7 @@ module.exports = async (kraken, validate, getEnv) => { console.log('šŸ“‰ Bid:', bid, fiat, '\n') // Place order - const details = { pair, type: 'buy', ordertype: 'limit', price, volume } + const details = { pair, type: 'buy', ordertype, price, volume } if (validate) details.validate = true const { result: { descr: { order }, txid } } = await kraken.api('AddOrder', details) diff --git a/commands/withdraw.js b/commands/withdraw.js index 154f4cc..e88c656 100644 --- a/commands/withdraw.js +++ b/commands/withdraw.js @@ -1,4 +1,4 @@ -module.exports = async (kraken, validate, getEnv) => { +module.exports = async (kraken, validate, { getEnv }) => { const [maxFee, key] = getEnv('KRAKEN_MAX_REL_FEE', 'KRAKEN_WITHDRAW_KEY') // https://api.kraken.com/0/private/WithdrawInfo diff --git a/index.js b/index.js index 7569f4f..a37667b 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,11 @@ const getEnv = (...vars) => vars.map(name => { assert(value, `Provide the ${name} environment variable.`) return value }) +const getEnvOpt = (varname, defaultValue, allowedValues) => { + const value = process.env[varname] || defaultValue + if (allowedValues) assert(allowedValues.includes(value), `The ${varname} environment variable must be one of ${allowedValues.map(v => `"${v}"`).join(", ")}.`) + return value +} const command = process.argv[2].replace('--cmd=', '') const validate = process.argv.includes('--validate') || process.env['KRAKEN_DRY_RUN_PLACE_NO_ORDER'] @@ -15,7 +20,7 @@ const validate = process.argv.includes('--validate') || process.env['KRAKEN_DRY_ const kraken = new Kraken(apiKey, secret) const cmd = require(`./commands/${command}`) - await cmd(kraken, validate, getEnv) + await cmd(kraken, validate, { getEnv, getEnvOpt }) if (validate) console.log('\n🚨 THIS WAS JUST A VALIDATION RUN!') } catch (err) {