Skip to content

Commit

Permalink
Merge pull request #48 from Adamant-im/dev
Browse files Browse the repository at this point in the history
v2.4.4
  • Loading branch information
dev-adamant-im authored Apr 14, 2022
2 parents 4be7acc + cc82d9c commit 77ff83c
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 21 deletions.
16 changes: 11 additions & 5 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
**/

"node_ADM": [
"https://bid.adamant.im",
"http://localhost:36666",
"https://endless.adamant.im",
"https://clown.adamant.im",
"https://bid.adamant.im",
"https://unusual.adamant.im",
"https://debate.adamant.im",
"http://23.226.231.225:36666",
Expand Down Expand Up @@ -103,11 +103,17 @@
/** If to show limit info in /help command **/
"daily_limit_show": true,

/** Maximum price to buy specific coin in USD. Remove the line or set to 0 to disable **/
"max_buy_price_usd_ADM": 0.03,
/** Maximum price to buy specific coin in USD. Set 0 to disable **/
"max_buy_price_usd_ADM": 0,

/** Minimum price to sell specific coin in USD. Set to 0 to disable **/
"min_sell_price_usd_ADM": 0,

/** Fixed price to buy specific coin in USD. Set to 0 to disable and use market rate. **/
"fixed_buy_price_usd_ADM": 0,

/** Minimum price to sell specific coin in USD. Remove the line or set to 0 to disable **/
"min_sell_price_usd_ADM": 0.02,
/** Fixed price to sell specific coin in USD. Set to 0 to disable and use market rate. **/
"fixed_sell_price_usd_ADM": 0,

/** How many confirmations to consider transaction as accepted **/
"min_confirmations": 2,
Expand Down
28 changes: 23 additions & 5 deletions helpers/cryptos/exchanger.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,36 @@ module.exports = {

/**
* Returns rate for from/to
* For fixed rates: Assume the bot buys 'from' currency and sell 'to' currency
* @param {String} from Like 'ADM'
* @param {String} to Like 'ETH'
* @return {Number} or NaN or undefined
*/
getRate(from, to) {
try {
let price = this.currencies[from + '/' + to] || 1 / this.currencies[to + '/' + from];
if (!price) {
// We don't have direct or reverse rate, calculate it from /USD rates
const priceFrom = this.currencies[from + '/USD'];
const priceTo = this.currencies[to + '/USD'];
let price; let priceFrom; let priceTo;
if (config['fixed_buy_price_usd_' + from] || config['fixed_sell_price_usd_' + to]) {
// Calculate at a fixed rate
priceFrom = from === 'USD' ? 1 : this.currencies[from + '/USD'];
if (config['fixed_buy_price_usd_' + from]) {
priceFrom = config['fixed_buy_price_usd_' + from];
log.warn(`Used fixed ${from} rate of ${priceFrom} USD instead of market ${this.currencies[from + '/USD']} USD to convert ${from} to ${to} (buying ${from}).`);
}
priceTo = to === 'USD' ? 1 : this.currencies[to + '/USD'];
if (config['fixed_sell_price_usd_' + to]) {
priceTo = config['fixed_sell_price_usd_' + to];
log.warn(`Used fixed ${to} rate of ${priceTo} USD instead of market ${this.currencies[to + '/USD']} USD to convert ${from} to ${to} (selling ${to}).`);
}
price = priceFrom / priceTo;
} else {
// Use market rate
price = this.currencies[from + '/' + to] || 1 / this.currencies[to + '/' + from];
if (!price) {
// We don't have direct or reverse rate, calculate it from /USD rates
const priceFrom = this.currencies[from + '/USD'];
const priceTo = this.currencies[to + '/USD'];
price = priceFrom / priceTo;
}
}
return price;
} catch (e) {
Expand Down
33 changes: 29 additions & 4 deletions modules/commandTxs.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ module.exports = async (commandMsg, tx, itx) => {
function help({}, {}, commandFix) {

const specialFees = [];
const specialLimits = [];
let oneSpecialFeeCoin = '';
let oneSpecialFeeRate = '';

const fixedFees = [];
const specialDailyLimits = [];

let feesString = '';

// Special fees in %
config.known_crypto.forEach((coin) => {
if (config['exchange_fee_' + coin] !== config.exchange_fee) {
specialFees.push(`*${coin}*: *${config['exchange_fee_' + coin]}%*`);
Expand All @@ -54,6 +58,23 @@ function help({}, {}, commandFix) {
};
});

// Fixed fees in USD
config.known_crypto.forEach((coin) => {
if (config['fixed_buy_price_usd_' + coin] ||config['fixed_sell_price_usd_' + coin]) {
let fixedFeeString = `*${coin}*: `;
let isBuyPrice = false;
if (config['fixed_buy_price_usd_' + coin]) {
fixedFeeString += `buying at ${config['fixed_buy_price_usd_' + coin]} USD`;
isBuyPrice = true;
}
if (config['fixed_sell_price_usd_' + coin]) {
if (isBuyPrice) fixedFeeString += ', ';
fixedFeeString += `selling at ${config['fixed_sell_price_usd_' + coin]} USD`;
}
fixedFees.push(fixedFeeString);
};
});

if (specialFees.length === 1) {
feesString = `I take *${config.exchange_fee}%* fee, plus you pay blockchain Tx fees. Due to the rates fluctuation, I take ${oneSpecialFeeRate} fee, if you send me ${oneSpecialFeeCoin}`;
} else if (specialFees.length) {
Expand All @@ -62,6 +83,10 @@ function help({}, {}, commandFix) {
feesString = `I take *${config.exchange_fee}%* fee, plus you pay blockchain Tx fees`;
}

if (fixedFees.length) {
feesString += `. Fixed rates not including fees — ${fixedFees.join(', ')}`;
}

const minValueString = config.min_value_usd ? ` I accept minimal exchange of *${config.min_value_usd}* USD equivalent.` : '';

let result = `I am **online** and ready for a deal. `;
Expand All @@ -73,11 +98,11 @@ function help({}, {}, commandFix) {
config.known_crypto.forEach((coin) => {
const coinDailyLimit = config['daily_limit_usd_' + coin];
if (coinDailyLimit !== config.daily_limit_usd) {
specialLimits.push(`${coin}: ${coinDailyLimit ? coinDailyLimit + ' USD' : 'no limit' }`);
specialDailyLimits.push(`${coin}: ${coinDailyLimit ? coinDailyLimit + ' USD' : 'no limit' }`);
};
});
if (specialLimits.length) {
result += ` (buying ${specialLimits.join(', ')}).`;
if (specialDailyLimits.length) {
result += ` (buying ${specialDailyLimits.join(', ')}).`;
} else {
result += '.';
}
Expand Down
8 changes: 5 additions & 3 deletions modules/configReader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const jsonminify = require('jsonminify');
const fs = require('fs');
const keys = require('adamant-api/helpers/keys');
const keys = require('adamant-api/src/helpers/keys');
const isDev = process.argv.includes('dev');
let config = {};

Expand Down Expand Up @@ -108,10 +108,12 @@ try {
config.notifyName = `${config.bot_name} (${config.address})`;
config.version = require('../package.json').version;

['min_confirmations', 'exchange_fee', 'daily_limit_usd', 'max_buy_price_usd', 'min_sell_price_usd'].forEach((param) => {
['min_confirmations', 'exchange_fee', 'daily_limit_usd',
'max_buy_price_usd', 'min_sell_price_usd',
'fixed_buy_price_usd', 'fixed_sell_price_usd'].forEach((param) => {
config.known_crypto.forEach((coin) => {
const field = param + '_' + coin;
if (fields[param]) { // 'max_buy_price', 'min_sell_price' don't have default values
if (fields[param]) { // some params have default values
if (!config[field] && config[field] !== 0) {
config[field] = config[param] || fields[param].default;
}
Expand Down
24 changes: 22 additions & 2 deletions modules/exchangeTxs.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,28 @@ module.exports = async (itx, tx, payToUpdate) => {
} else { // Transaction is fine
pay.isBasicChecksPassed = true;
notifyType = 'log';
msgNotify = `${config.notifyName} notifies about incoming transaction to exchange _${inAmountMessage}_ _${inCurrency}_ for *${pay.outAmount}* *${outCurrency}* at _${pay.exchangePrice}_ _${outCurrency}_ / _${inCurrency}_. Tx hash: _${inTxid}_. ${admTxDescription}.`;
msgSendBack = `I’ve got a request to exchange _${inAmountMessage}_ _${inCurrency}_ for **${pay.outAmount}** **${outCurrency}** at _${pay.exchangePrice}_ _${outCurrency}_ / _${inCurrency}_. Now I’ll validate your transfer${exchangerUtils.isFastPayments(inCurrency) ? ' and' : ' and wait for _' + min_confirmations + '_ block confirmations, then'} make an exchange. It can take a time, please be patient.`;

// Building a nice informative messages for admin and for user
let conversionStringNotify = `_${inAmountMessage}_ _${inCurrency}_ (got from user) for **${pay.outAmount}** **${outCurrency}** (to be send to user) at _${pay.exchangePrice}_ _${outCurrency}_ / _${inCurrency}_`;
const conversionStringSendBack = `_${inAmountMessage}_ _${inCurrency}_ for **${pay.outAmount}** **${outCurrency}** at _${pay.exchangePrice}_ _${outCurrency}_ / _${inCurrency}_`;
const inCurrencyRateInUsd = pay.exchangePrice * exchangerUtils.getRate(outCurrency, 'USD');
let decimals = inCurrencyRateInUsd < 0.02 ? 4 : 2;
conversionStringNotify += ` (buying ${inCurrency} at ${inCurrencyRateInUsd.toFixed(decimals)} USD`;
if (outCurrency !== 'BTC') {
const inCurrencyRateInBtc = pay.exchangePrice * exchangerUtils.getRate(outCurrency, 'BTC');
conversionStringNotify += `, ${inCurrencyRateInBtc.toFixed(8)} BTC`;
}
const outCurrencyRateInUsd = exchangerUtils.getRate(inCurrency, 'USD') / pay.exchangePrice;
decimals = outCurrencyRateInUsd < 0.02 ? 4 : 2;
conversionStringNotify += `, selling ${outCurrency} at ${outCurrencyRateInUsd.toFixed(decimals)} USD`;
if (inCurrency !== 'BTC') {
const outCurrencyRateInBtc = exchangerUtils.getRate(inCurrency, 'BTC') / pay.exchangePrice;
conversionStringNotify += `, ${outCurrencyRateInBtc.toFixed(8)} BTC`;
}

conversionStringNotify += `)`;
msgNotify = `${config.notifyName} notifies about incoming transaction to exchange ${conversionStringNotify}. Tx hash: _${inTxid}_. ${admTxDescription}.`;
msgSendBack = `I’ve got your request to exchange ${conversionStringSendBack}. Now I’ll validate the transaction${exchangerUtils.isFastPayments(inCurrency) ? ' and' : ' and wait for _' + min_confirmations + '_ block confirmations, then'} make an exchange. It can take a time, please be patient.`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adamant-exchangebot",
"version": "2.4.0",
"version": "2.4.4",
"description": "ADAMANT anonymous Exchange bot",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -35,7 +35,7 @@
"lsk",
"lisk"
],
"author": "Aleksei Lebedev, RomanS <[email protected]> (https://adamant.im)",
"author": "ADAMANT Foundation <[email protected]> (https://adamant.im)",
"license": "GPL-3.0",
"dependencies": {
"@liskhq/lisk-cryptography": "^3.2.0",
Expand Down

0 comments on commit 77ff83c

Please sign in to comment.