Conversion rates are fixed at time of request
- Fix the conversion rate when request is made. - FROM_FIXED_VALUES, FROM_MIN_VALUE, FROM_MAX_VALUE constants use currency values instead of coin value (to keep uniformity)
This commit is contained in:
parent
93933cc5c1
commit
5d93259098
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const keys = require('./keys');
|
const keys = require('./keys');
|
||||||
const blockchain = require('./blockchain');
|
const blockchain = require('./blockchain');
|
||||||
const conversion_rates = require('./services/conversion').getRate;
|
//const conversion_rates = require('./services/conversion').getRate;
|
||||||
/*
|
/*
|
||||||
const bond_util = require('./services/bonds').util;
|
const bond_util = require('./services/bonds').util;
|
||||||
const fund_util = require('./services/bobs-fund').util;
|
const fund_util = require('./services/bobs-fund').util;
|
||||||
@ -230,15 +230,13 @@ function verifyConvert() {
|
|||||||
txQueries.push([to_refund_sql, [pCode.ASSET_TYPE_COIN, "BTC", pCode.STATUS_PENDING, req_timeout, pCode.CONVERT_MODE_PUT]]);
|
txQueries.push([to_refund_sql, [pCode.ASSET_TYPE_COIN, "BTC", pCode.STATUS_PENDING, req_timeout, pCode.CONVERT_MODE_PUT]]);
|
||||||
txQueries.push(["UPDATE DirectConvert SET r_status=? WHERE r_status=? AND locktime<?", [pCode.STATUS_REJECTED, pCode.STATUS_PENDING, req_timeout]]);
|
txQueries.push(["UPDATE DirectConvert SET r_status=? WHERE r_status=? AND locktime<?", [pCode.STATUS_REJECTED, pCode.STATUS_PENDING, req_timeout]]);
|
||||||
DB.transaction(txQueries).then(result => {
|
DB.transaction(txQueries).then(result => {
|
||||||
DB.query("SELECT id, floID, mode, in_txid, amount, quantity FROM DirectConvert WHERE r_status=? AND coin=?", [pCode.STATUS_PENDING, "BTC"]).then(results => {
|
DB.query("SELECT id, floID, mode, in_txid, amount, quantity, rate FROM DirectConvert WHERE r_status=? AND coin=?", [pCode.STATUS_PENDING, "BTC"]).then(results => {
|
||||||
results.forEach(r => {
|
results.forEach(r => {
|
||||||
if (r.mode == pCode.CONVERT_MODE_GET) {
|
if (r.mode == pCode.CONVERT_MODE_GET) {
|
||||||
verifyTx.token(r.floID, r.in_txid, keys.sink_groups.CONVERT, true).then(({ amount }) => {
|
verifyTx.token(r.floID, r.in_txid, keys.sink_groups.CONVERT, true).then(({ amount }) => {
|
||||||
if (r.amount !== amount)
|
if (r.amount !== amount)
|
||||||
throw ([true, "Transaction amount mismatched in blockchain"]);
|
throw ([true, "Transaction amount mismatched in blockchain"]);
|
||||||
conversion_rates.BTC_USD().then(rate => {
|
blockchain.convertToCoin.init(r.floID, "BTC", r.amount, r.rate, r.id)
|
||||||
blockchain.convertToCoin.init(r.floID, "BTC", amount, rate, r.id)
|
|
||||||
}).catch(error => console.error(error))
|
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
if (error[0])
|
if (error[0])
|
||||||
@ -249,9 +247,7 @@ function verifyConvert() {
|
|||||||
verifyTx.BTC(r.floID, r.in_txid, keys.sink_groups.CONVERT).then(quantity => {
|
verifyTx.BTC(r.floID, r.in_txid, keys.sink_groups.CONVERT).then(quantity => {
|
||||||
if (r.quantity !== quantity)
|
if (r.quantity !== quantity)
|
||||||
throw ([true, "Transaction quantity mismatched in blockchain"]);
|
throw ([true, "Transaction quantity mismatched in blockchain"]);
|
||||||
conversion_rates.BTC_USD().then(rate => {
|
blockchain.convertFromCoin.init(r.floID, r.quantity, r.rate, r.id)
|
||||||
blockchain.convertFromCoin.init(r.floID, quantity, rate, r.id)
|
|
||||||
}).catch(error => console.error(error))
|
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
if (error[0])
|
if (error[0])
|
||||||
|
|||||||
@ -116,7 +116,7 @@ function getConvertValues() {
|
|||||||
}
|
}
|
||||||
} else result[pCode.CONVERT_MODE_GET] = null;
|
} else result[pCode.CONVERT_MODE_GET] = null;
|
||||||
if (avail.cash > 0) {
|
if (avail.cash > 0) {
|
||||||
let cash_availability = global.toStandardDecimal(avail.cash / avail.rate); //convert to coin value
|
let cash_availability = global.toStandardDecimal(avail.cash); //availability in currency value
|
||||||
if (Array.isArray(FROM_FIXED_VALUES) && FROM_FIXED_VALUES.length)
|
if (Array.isArray(FROM_FIXED_VALUES) && FROM_FIXED_VALUES.length)
|
||||||
result[pCode.CONVERT_MODE_PUT] = FROM_FIXED_VALUES.filter(a => a < cash_availability);
|
result[pCode.CONVERT_MODE_PUT] = FROM_FIXED_VALUES.filter(a => a < cash_availability);
|
||||||
else if (!FROM_MIN_VALUE || FROM_MIN_VALUE <= cash_availability) {
|
else if (!FROM_MIN_VALUE || FROM_MIN_VALUE <= cash_availability) {
|
||||||
@ -135,26 +135,28 @@ function convertToCoin(floID, txid, coin, amount) {
|
|||||||
return reject(INVALID(eCode.INVALID_TOKEN_NAME, `Invalid coin (${coin})`));
|
return reject(INVALID(eCode.INVALID_TOKEN_NAME, `Invalid coin (${coin})`));
|
||||||
else if (typeof amount !== "number" || amount <= 0)
|
else if (typeof amount !== "number" || amount <= 0)
|
||||||
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid amount (${amount})`));
|
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid amount (${amount})`));
|
||||||
else if (Array.isArray(TO_FIXED_VALUES) && TO_FIXED_VALUES.length) {
|
BTC_USD().then(rate => {
|
||||||
if (!TO_FIXED_VALUES.includes(amount))
|
if (Array.isArray(TO_FIXED_VALUES) && TO_FIXED_VALUES.length) {
|
||||||
|
if (!TO_FIXED_VALUES.includes(amount))
|
||||||
|
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid amount (${amount})`));
|
||||||
|
} else if (TO_MIN_VALUE && TO_MIN_VALUE > amount || TO_MAX_VALUE && TO_MAX_VALUE < amount)
|
||||||
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid amount (${amount})`));
|
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid amount (${amount})`));
|
||||||
} else if (TO_MIN_VALUE && TO_MIN_VALUE > amount || TO_MAX_VALUE && TO_MAX_VALUE < amount)
|
DB.query("SELECT r_status FROM DirectConvert WHERE in_txid=? AND floID=? AND mode=?", [txid, floID, pCode.CONVERT_MODE_GET]).then(result => {
|
||||||
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid amount (${amount})`));
|
if (result.length)
|
||||||
DB.query("SELECT r_status FROM DirectConvert WHERE in_txid=? AND floID=? AND mode=?", [txid, floID, pCode.CONVERT_MODE_GET]).then(result => {
|
return reject(INVALID(eCode.DUPLICATE_ENTRY, "Transaction already in process"));
|
||||||
if (result.length)
|
checkPoolBalance(coin, amount, pCode.CONVERT_MODE_GET).then(result => {
|
||||||
return reject(INVALID(eCode.DUPLICATE_ENTRY, "Transaction already in process"));
|
DB.query("INSERT INTO DirectConvert(floID, in_txid, mode, coin, amount, rate, r_status) VALUES (?)", [[floID, txid, pCode.CONVERT_MODE_GET, coin, amount, rate, pCode.STATUS_PENDING]])
|
||||||
checkPoolBalance(coin, amount, pCode.CONVERT_MODE_GET).then(result => {
|
.then(result => resolve("Conversion request in process"))
|
||||||
DB.query("INSERT INTO DirectConvert(floID, in_txid, mode, coin, amount, r_status) VALUES (?)", [[floID, txid, pCode.CONVERT_MODE_GET, coin, amount, pCode.STATUS_PENDING]])
|
.catch(error => reject(error));
|
||||||
.then(result => resolve("Conversion request in process"))
|
}).catch(error => {
|
||||||
.catch(error => reject(error));
|
if (error instanceof INVALID && error.ecode === eCode.INSUFFICIENT_FUND)
|
||||||
}).catch(error => {
|
DB.query("INSERT INTO DirectConvert(floID, in_txid, mode, coin, amount, r_status) VALUES (?)", [[floID, txid, pCode.CONVERT_MODE_GET, coin, amount, pCode.STATUS_REJECTED]]).then(result => {
|
||||||
if (error instanceof INVALID && error.ecode === eCode.INSUFFICIENT_FUND)
|
DB.query("INSERT INTO RefundConvert(floID, in_txid, asset_type, asset, r_status) VALUES (?)", [[floID, txid, pCode.ASSET_TYPE_TOKEN, floGlobals.currency, pCode.STATUS_PENDING]])
|
||||||
DB.query("INSERT INTO DirectConvert(floID, in_txid, mode, coin, amount, r_status) VALUES (?)", [[floID, txid, pCode.CONVERT_MODE_GET, coin, amount, pCode.STATUS_REJECTED]]).then(result => {
|
.then(_ => null).catch(error => console.error(error));
|
||||||
DB.query("INSERT INTO RefundConvert(floID, in_txid, asset_type, asset, r_status) VALUES (?)", [[floID, txid, pCode.ASSET_TYPE_TOKEN, floGlobals.currency, pCode.STATUS_PENDING]])
|
}).catch(error => console.error(error))
|
||||||
.then(_ => null).catch(error => console.error(error));
|
reject(error);
|
||||||
}).catch(error => console.error(error))
|
})
|
||||||
reject(error);
|
}).catch(error => reject(error))
|
||||||
})
|
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -165,29 +167,32 @@ function convertFromCoin(floID, txid, tx_hex, coin, quantity) {
|
|||||||
return reject(INVALID(eCode.INVALID_TOKEN_NAME, `Invalid coin (${coin})`));
|
return reject(INVALID(eCode.INVALID_TOKEN_NAME, `Invalid coin (${coin})`));
|
||||||
else if (typeof quantity !== "number" || quantity <= 0)
|
else if (typeof quantity !== "number" || quantity <= 0)
|
||||||
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid quantity (${quantity})`));
|
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid quantity (${quantity})`));
|
||||||
else if (Array.isArray(FROM_FIXED_VALUES) && FROM_FIXED_VALUES.length) {
|
BTC_USD().then(rate => {
|
||||||
if (!FROM_FIXED_VALUES.includes(quantity))
|
let currency_amount = global.toStandardDecimal(quantity * rate);
|
||||||
|
if (Array.isArray(FROM_FIXED_VALUES) && FROM_FIXED_VALUES.length) {
|
||||||
|
if (!FROM_FIXED_VALUES.includes(currency_amount))
|
||||||
|
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid quantity (${quantity})`));
|
||||||
|
} else if (FROM_MIN_VALUE && FROM_MIN_VALUE > currency_amount || FROM_MAX_VALUE && FROM_MAX_VALUE < currency_amount)
|
||||||
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid quantity (${quantity})`));
|
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid quantity (${quantity})`));
|
||||||
} else if (FROM_MIN_VALUE && FROM_MIN_VALUE > quantity || FROM_MAX_VALUE && FROM_MAX_VALUE < quantity)
|
else if (btcOperator.transactionID(tx_hex) !== txid)
|
||||||
return reject(INVALID(eCode.INVALID_NUMBER, `Invalid quantity (${quantity})`));
|
return reject(INVALID(eCode.INVALID_TX_ID, `txid ${txid} doesnt match the tx-hex`));
|
||||||
else if (btcOperator.transactionID(tx_hex) !== txid)
|
DB.query("SELECT r_status FROM DirectConvert WHERE in_txid=? AND floID=? AND mode=?", [txid, floID, pCode.CONVERT_MODE_PUT]).then(result => {
|
||||||
return reject(INVALID(eCode.INVALID_TX_ID, `txid ${txid} doesnt match the tx-hex`));
|
if (result.length)
|
||||||
DB.query("SELECT r_status FROM DirectConvert WHERE in_txid=? AND floID=? AND mode=?", [txid, floID, pCode.CONVERT_MODE_PUT]).then(result => {
|
return reject(INVALID(eCode.DUPLICATE_ENTRY, "Transaction already in process"));
|
||||||
if (result.length)
|
checkPoolBalance(coin, quantity, pCode.CONVERT_MODE_PUT).then(result => {
|
||||||
return reject(INVALID(eCode.DUPLICATE_ENTRY, "Transaction already in process"));
|
btcOperator.broadcastTx(tx_hex).then(b_txid => {
|
||||||
checkPoolBalance(coin, quantity, pCode.CONVERT_MODE_PUT).then(result => {
|
if (b_txid !== txid)
|
||||||
btcOperator.broadcastTx(tx_hex).then(b_txid => {
|
console.warn("broadcast TX-ID is not same as calculated TX-ID");
|
||||||
if (b_txid !== txid)
|
DB.query("INSERT INTO DirectConvert(floID, in_txid, mode, coin, quantity, rate, r_status) VALUES (?)", [[floID, b_txid, pCode.CONVERT_MODE_PUT, coin, quantity, rate, pCode.STATUS_PENDING]])
|
||||||
console.warn("broadcast TX-ID is not same as calculated TX-ID");
|
.then(result => resolve("Conversion request in process"))
|
||||||
DB.query("INSERT INTO DirectConvert(floID, in_txid, mode, coin, quantity, r_status) VALUES (?)", [[floID, b_txid, pCode.CONVERT_MODE_PUT, coin, quantity, pCode.STATUS_PENDING]])
|
.catch(error => reject(error));
|
||||||
.then(result => resolve("Conversion request in process"))
|
}).catch(error => {
|
||||||
.catch(error => reject(error));
|
if (error === null)
|
||||||
}).catch(error => {
|
reject(INVALID(eCode.INVALID_TX_ID, `Invalid transaction hex`));
|
||||||
if (error === null)
|
else
|
||||||
reject(INVALID(eCode.INVALID_TX_ID, `Invalid transaction hex`));
|
reject(error);
|
||||||
else
|
})
|
||||||
reject(error);
|
}).catch(error => reject(error))
|
||||||
})
|
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user