When i run my project in terminal like this node index.js
I get a reference error: config not defined
/home/ether/Documents/nodesendeth/index.js:6 const {asset, base, spread, allocation} = config; ^
ReferenceError: config is not defined
at tick (/home/ether/Documents/nodesendeth/index.js:6:47)
at run (/home/ether/Documents/nodesendeth/index.js:49:3)
at Object.<anonymous> (/home/ether/Documents/nodesendeth/index.js:52:1)
at Module._compile (internal/modules/cjs/loader.js:1151:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
in my code i have assigned the config constant like this:
require('dotenv').config;
const ccxt = require('ccxt');
const axios = require('axios');
const tick = async => {
const {asset, base, spread, allocation} = config;
const market = `${asset}/${base}`;
const orders = binanceClient.fetchOpenOrders(market);
orders.forEach(async order => {
await binanceClient.cancelOrder(order.id);
});
const results = Promise.all([
axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd'),
axios.get('https://api.coingecko.com/api/v3/simple/price?ids=tether&vs_currencies=usd')
]);
const marketPrice = results[0].data.bitcoin.usd / results[1].data.tether.usd;
const sellPrice = marketPrice * (1 + spread);
const buyPrice = marketPrice * (1 - spread);
const balances = binanceClient.fetchBalance();
const assetBalance = balances.free[asset];
const baseBalance = balances.free[base];
const sellVolume = assetBalance * allocation;
const buyVolume = (baseBalance * allocation) / marketPrice;
binanceClient.createLimitSellOrder(market, sellVolume, sellPrice);
binanceClient.createLimitBuyOrder(market, sellVolume, buyPrice);
console.log(`
New tick for ${market}...
Created limit sell order for ${sellVolume}@${sellPrice}
Create limit buy order for ${buyVolume}@${buyPrice}`)
}
const run = () => {
const config = {
asset: 'BTC',
base: 'USDT',
allocation: 0.1,
spread: 0.2,
tickInterval: 2000
};
const binanceClient = new ccxt.binance({
apiKey: process.env.API_ENV,
secret: process.env.API_SECRET,
});
tick(config, binanceClient);
setInterval(tick, config.tickInterval, config, binanceClient);
}
run()
I also got await errors saying that i need to run them in an sync function which I do. i have removed the await keyword now but the app should run with the await kewyword because those functions are async.
Why am i getting those errors when i have a config variable aswell as an async function?
I assume that something with my async function doesnt work because it neither recognises the config constant nor the async/await calls
const {asset, base, spread, allocation} = config;What do you thinkconfigis in this line?{asset, base, spread, allocation}come from?