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