floExchangeAPI v1.1.1
- All rejected errors are now an instance of ExchangeError
This commit is contained in:
parent
74b643fc08
commit
38cc9659ce
@ -20,7 +20,6 @@
|
||||
<script src="scripts/floTokenAPI.js"></script>
|
||||
<script src="scripts/floExchangeAPI.js"></script>
|
||||
<script src="https://unpkg.com/lightweight-charts/dist/lightweight-charts.standalone.production.js"></script>
|
||||
<script>console.debug(floExchangeAPI);</script>
|
||||
</head>
|
||||
|
||||
<body class="hide">
|
||||
@ -1370,7 +1369,7 @@
|
||||
})
|
||||
}
|
||||
catch (err) {
|
||||
notify(err.data, 'error')
|
||||
notify(err.message, 'error')
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
@ -1391,7 +1390,7 @@
|
||||
getRef('market_orders_list').append(marketTransactionList)
|
||||
}
|
||||
catch (err) {
|
||||
notify(err.data, 'error')
|
||||
notify(err.message, 'error')
|
||||
}
|
||||
}
|
||||
getRef('market_orders_list').append(frag)
|
||||
@ -1512,10 +1511,10 @@
|
||||
}
|
||||
catch (err) {
|
||||
getRef('trade_button_wrapper').append(getRef('failure_template').content.cloneNode(true))
|
||||
if (err.data === 'Insufficient rupee') {
|
||||
if (err.message === 'Insufficient rupee') {
|
||||
notify(`Insufficient balance to ${tradeType} ${asset}. Please deposit rupee to wallet first.`, 'error', { pinned: true })
|
||||
} else {
|
||||
notify(err.data, 'error')
|
||||
notify(err.message, 'error')
|
||||
}
|
||||
}
|
||||
finally {
|
||||
@ -1694,10 +1693,10 @@
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
if ((err.data || err) === 'Insufficient rupe balance') {
|
||||
showWalletResult('error', `Failed`, err.data || err, 'open-upi')
|
||||
if ((err.message || err) === 'Insufficient rupe balance') {
|
||||
showWalletResult('error', `Failed`, err.message || err, 'open-upi')
|
||||
} else {
|
||||
showWalletResult('error', `Failed`, err.data || err)
|
||||
showWalletResult('error', `Failed`, err.message || err)
|
||||
}
|
||||
}
|
||||
finally {
|
||||
@ -1875,7 +1874,7 @@
|
||||
}, 200);
|
||||
}
|
||||
})
|
||||
.catch(err => notify(err.data, 'error'))
|
||||
.catch(err => notify(err.message, 'error'))
|
||||
}
|
||||
})
|
||||
} else if (e.target.closest('.more-info')) {
|
||||
@ -1893,7 +1892,7 @@
|
||||
hideMyOrdersOptions()
|
||||
}
|
||||
catch (err) {
|
||||
notify(err.data, 'error')
|
||||
notify(err.message, 'error')
|
||||
}
|
||||
finally {
|
||||
refresh()
|
||||
@ -2083,7 +2082,7 @@
|
||||
if (pagesData.params.hasOwnProperty('asset' && pagesData.params.asset !== ''))
|
||||
getRef('get_price').value = parseFloat(parseFloat(rates[pagesData.params.asset]).toFixed(4))
|
||||
|
||||
}).catch(error => console.error(error))
|
||||
}).catch(error => console.error(error.message))
|
||||
}
|
||||
|
||||
function refresh(init = false) {
|
||||
@ -2165,12 +2164,13 @@
|
||||
proxy.secret.then(_ => null).catch(_ => null);
|
||||
showPage(window.location.hash);
|
||||
}).catch(error => {
|
||||
if (error.data === 'Invalid request signature! Re-login if this error occurs frequently') {
|
||||
console.debug(error)
|
||||
if (error.message === 'Invalid request signature! Re-login if this error occurs frequently') {
|
||||
proxy.clear()
|
||||
location.reload()
|
||||
}
|
||||
else
|
||||
notify(error.data, 'error')
|
||||
notify(error.message, 'error')
|
||||
})
|
||||
.finally(() => {
|
||||
hideProcess('login_button_wrapper')
|
||||
@ -2207,7 +2207,7 @@
|
||||
getRef('sign_in_hash').value = null;
|
||||
account();
|
||||
}).catch(error => {
|
||||
notify(error.data, 'error')
|
||||
notify(error.message, 'error')
|
||||
hideProcess('login_button_wrapper')
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
(function(EXPORTS) {
|
||||
(function(EXPORTS) { //floExchangeAPI v1.1.1
|
||||
const exchangeAPI = EXPORTS;
|
||||
|
||||
/*Kademlia DHT K-bucket implementation as a binary tree.*/
|
||||
@ -462,14 +462,13 @@
|
||||
|
||||
}
|
||||
|
||||
const INVALID_SERVER_MSG = "INCORRECT_SERVER_ERROR";
|
||||
var nodeList, nodeURL, nodeKBucket; //Container for (backup) node list
|
||||
|
||||
function fetch_api(api, options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let curPos = fetch_api.curPos || 0;
|
||||
if (curPos >= nodeList.length)
|
||||
return resolve('No Nodes online');
|
||||
return reject(ExchangeError(ExchangeError.NODES_OFFLINE_CODE, 'No Node online! Try again later'));
|
||||
let url = "https://" + nodeURL[nodeList[curPos]];
|
||||
(options ? fetch(url + api, options) : fetch(url + api))
|
||||
.then(result => resolve(result)).catch(error => {
|
||||
@ -483,30 +482,35 @@
|
||||
})
|
||||
}
|
||||
|
||||
function ResponseError(status, data) {
|
||||
if (data === INVALID_SERVER_MSG)
|
||||
function ExchangeError(status, message) {
|
||||
if (message === ExchangeError.INVALID_SERVER_MSG)
|
||||
location.reload();
|
||||
else if (this instanceof ResponseError) {
|
||||
this.data = data;
|
||||
else if (this instanceof ExchangeError) {
|
||||
this.message = message;
|
||||
this.status = status;
|
||||
} else
|
||||
return new ResponseError(status, data);
|
||||
return new ExchangeError(status, message);
|
||||
}
|
||||
|
||||
ExchangeError.INVALID_SERVER_MSG = "INCORRECT_SERVER_ERROR";
|
||||
ExchangeError.BAD_REQUEST_CODE = 400;
|
||||
ExchangeError.BAD_RESPONSE_CODE = 500;
|
||||
ExchangeError.NODES_OFFLINE_CODE = 404;
|
||||
|
||||
function responseParse(response, json_ = true) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!response.ok)
|
||||
response.text()
|
||||
.then(result => reject(ResponseError(response.status, result)))
|
||||
.catch(error => reject(error));
|
||||
.then(result => reject(ExchangeError(response.status, result)))
|
||||
.catch(error => reject(ExchangeError(response.status, error)));
|
||||
else if (json_)
|
||||
response.json()
|
||||
.then(result => resolve(result))
|
||||
.catch(error => reject(error));
|
||||
.catch(error => reject(ExchangeError(ExchangeError.BAD_RESPONSE_CODE, error)));
|
||||
else
|
||||
response.text()
|
||||
.then(result => resolve(result))
|
||||
.catch(error => reject(error));
|
||||
.catch(error => reject(ExchangeError(ExchangeError.BAD_RESPONSE_CODE, error)));
|
||||
});
|
||||
}
|
||||
|
||||
@ -590,7 +594,7 @@
|
||||
exchangeAPI.getBalance = function(floID = null, token = null) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!floID && !token)
|
||||
return reject("Need atleast one argument")
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Need atleast one argument"));
|
||||
let queryStr = (floID ? "floID=" + floID : "") +
|
||||
(floID && token ? "&" : "") +
|
||||
(token ? "token=" + token : "");
|
||||
@ -605,7 +609,7 @@
|
||||
exchangeAPI.getTx = function(txid) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!txid)
|
||||
return reject('txid required');
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, 'txid required'));
|
||||
fetch_api('/get-transaction?txid=' + txid)
|
||||
.then(result => responseParse(result)
|
||||
.then(result => resolve(result))
|
||||
@ -635,7 +639,7 @@
|
||||
exchangeAPI.signUp = function (privKey, code, hash) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!code || !hash)
|
||||
return reject("Login Code missing")
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Login Code missing"));
|
||||
let request = {
|
||||
pubKey: floCrypto.getPubKeyHex(privKey),
|
||||
floID: floCrypto.getFloID(privKey),
|
||||
@ -667,7 +671,7 @@
|
||||
exchangeAPI.login = function(privKey, proxyKey, code, hash) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!code || !hash)
|
||||
return reject("Login Code missing")
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Login Code missing"));
|
||||
let request = {
|
||||
proxyKey: proxyKey,
|
||||
floID: floCrypto.getFloID(privKey),
|
||||
@ -677,7 +681,7 @@
|
||||
hash: hash
|
||||
};
|
||||
if (!privKey || !request.floID)
|
||||
return reject("Invalid Private key");
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Invalid Private key"));
|
||||
request.sign = signRequest({
|
||||
type: "login",
|
||||
random: code,
|
||||
@ -729,9 +733,9 @@
|
||||
exchangeAPI.buy = function(asset, quantity, max_price, floID, proxySecret) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (typeof quantity !== "number" || quantity <= 0)
|
||||
return reject(`Invalid quantity (${quantity})`);
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid quantity (${quantity})`));
|
||||
else if (typeof max_price !== "number" || max_price <= 0)
|
||||
return reject(`Invalid max_price (${max_price})`);
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid max_price (${max_price})`));
|
||||
let request = {
|
||||
floID: floID,
|
||||
asset: asset,
|
||||
@ -767,9 +771,9 @@
|
||||
exchangeAPI.sell = function(asset, quantity, min_price, floID, proxySecret) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (typeof quantity !== "number" || quantity <= 0)
|
||||
return reject(`Invalid quantity (${quantity})`);
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid quantity (${quantity})`));
|
||||
else if (typeof min_price !== "number" || min_price <= 0)
|
||||
return reject(`Invalid min_price (${min_price})`);
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid min_price (${min_price})`));
|
||||
let request = {
|
||||
floID: floID,
|
||||
asset: asset,
|
||||
@ -805,7 +809,7 @@
|
||||
exchangeAPI.cancelOrder = function(type, id, floID, proxySecret) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (type !== "buy" && type !== "sell")
|
||||
return reject(`Invalid type (${type}): type should be sell (or) buy`);
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid type (${type}): type should be sell (or) buy`));
|
||||
let request = {
|
||||
floID: floID,
|
||||
orderType: type,
|
||||
@ -839,7 +843,7 @@
|
||||
exchangeAPI.transferToken = function(receiver, token, floID, proxySecret) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (typeof receiver !== 'object' || receiver === null)
|
||||
return reject("Invalid receiver: parameter is not an object");
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Invalid receiver: parameter is not an object"));
|
||||
let invalidIDs = [],
|
||||
invalidAmt = [];
|
||||
for (let f in receiver) {
|
||||
@ -849,9 +853,9 @@
|
||||
invalidAmt.push(receiver[f])
|
||||
}
|
||||
if (invalidIDs.length)
|
||||
return reject(INVALID(`Invalid receiver (${invalidIDs})`));
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid receiver (${invalidIDs})`));
|
||||
else if (invalidAmt.length)
|
||||
return reject(`Invalid amount (${invalidAmt})`);
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid amount (${invalidAmt})`));
|
||||
let request = {
|
||||
floID: floID,
|
||||
token: token,
|
||||
@ -884,7 +888,7 @@
|
||||
exchangeAPI.depositFLO = function(quantity, floID, sinkID, privKey, proxySecret = null) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (typeof quantity !== "number" || quantity <= floGlobals.fee)
|
||||
return reject(`Invalid quantity (${quantity})`);
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid quantity (${quantity})`));
|
||||
floBlockchainAPI.sendTx(floID, sinkID, quantity, privKey, '(deposit in market)').then(txid => {
|
||||
let request = {
|
||||
floID: floID,
|
||||
@ -946,7 +950,7 @@
|
||||
exchangeAPI.depositToken = function(token, quantity, floID, sinkID, privKey, proxySecret = null) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!floCrypto.verifyPrivKey(privKey, floID))
|
||||
return reject("Invalid Private Key");
|
||||
return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Invalid Private Key"));
|
||||
floTokenAPI.sendToken(privKey, quantity, sinkID, '(deposit in market)', token).then(txid => {
|
||||
let request = {
|
||||
floID: floID,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user