get-transact API

- Get user's deposit and withdraw history (private request: ie, requires signing)
- Update floTokenAPI
This commit is contained in:
sairajzero 2022-05-25 03:55:55 +05:30
parent 56e571606c
commit 166778e7f8
5 changed files with 240 additions and 180 deletions

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
(function (EXPORTS) { //floExchangeAPI v1.1.2 (function(EXPORTS) { //floExchangeAPI v1.1.2
const exchangeAPI = EXPORTS; const exchangeAPI = EXPORTS;
/*Kademlia DHT K-bucket implementation as a binary tree.*/ /*Kademlia DHT K-bucket implementation as a binary tree.*/
@ -46,7 +46,7 @@
this.arbiter = options.arbiter || this.arbiter this.arbiter = options.arbiter || this.arbiter
this.metadata = Object.assign({}, options.metadata) this.metadata = Object.assign({}, options.metadata)
this.createNode = function () { this.createNode = function() {
return { return {
contacts: [], contacts: [],
dontSplit: false, dontSplit: false,
@ -55,7 +55,7 @@
} }
} }
this.ensureInt8 = function (name, val) { this.ensureInt8 = function(name, val) {
if (!(val instanceof Uint8Array)) { if (!(val instanceof Uint8Array)) {
throw new TypeError(name + ' is not a Uint8Array') throw new TypeError(name + ' is not a Uint8Array')
} }
@ -66,7 +66,7 @@
* @param {Uint8Array} array2 * @param {Uint8Array} array2
* @return {Boolean} * @return {Boolean}
*/ */
this.arrayEquals = function (array1, array2) { this.arrayEquals = function(array1, array2) {
if (array1 === array2) { if (array1 === array2) {
return true return true
} }
@ -94,7 +94,7 @@
* @param {Object} candidate Contact being added to the k-bucket. * @param {Object} candidate Contact being added to the k-bucket.
* @return {Object} Contact to updated the k-bucket with. * @return {Object} Contact to updated the k-bucket with.
*/ */
this.arbiter = function (incumbent, candidate) { this.arbiter = function(incumbent, candidate) {
return incumbent.vectorClock > candidate.vectorClock ? incumbent : candidate return incumbent.vectorClock > candidate.vectorClock ? incumbent : candidate
} }
@ -107,7 +107,7 @@
* @return {Number} Integer The XOR distance between firstId * @return {Number} Integer The XOR distance between firstId
* and secondId. * and secondId.
*/ */
this.distance = function (firstId, secondId) { this.distance = function(firstId, secondId) {
let distance = 0 let distance = 0
let i = 0 let i = 0
const min = Math.min(firstId.length, secondId.length) const min = Math.min(firstId.length, secondId.length)
@ -124,7 +124,7 @@
* *
* @param {Object} contact the contact object to add * @param {Object} contact the contact object to add
*/ */
this.add = function (contact) { this.add = function(contact) {
this.ensureInt8('contact.id', (contact || {}).id) this.ensureInt8('contact.id', (contact || {}).id)
let bitIndex = 0 let bitIndex = 0
@ -172,7 +172,7 @@
* closest contacts to return * closest contacts to return
* @return {Array} Array Maximum of n closest contacts to the node id * @return {Array} Array Maximum of n closest contacts to the node id
*/ */
this.closest = function (id, n = Infinity) { this.closest = function(id, n = Infinity) {
this.ensureInt8('id', id) this.ensureInt8('id', id)
if ((!Number.isInteger(n) && n !== Infinity) || n <= 0) { if ((!Number.isInteger(n) && n !== Infinity) || n <= 0) {
@ -204,7 +204,7 @@
* *
* @return {Number} The number of contacts held in the tree * @return {Number} The number of contacts held in the tree
*/ */
this.count = function () { this.count = function() {
// return this.toArray().length // return this.toArray().length
let count = 0 let count = 0
for (const nodes = [this.root]; nodes.length > 0;) { for (const nodes = [this.root]; nodes.length > 0;) {
@ -225,7 +225,7 @@
* to check in the id Uint8Array. * to check in the id Uint8Array.
* @return {Object} left leaf if id at bitIndex is 0, right leaf otherwise. * @return {Object} left leaf if id at bitIndex is 0, right leaf otherwise.
*/ */
this._determineNode = function (node, id, bitIndex) { this._determineNode = function(node, id, bitIndex) {
// *NOTE* remember that id is a Uint8Array and has granularity of // *NOTE* remember that id is a Uint8Array and has granularity of
// bytes (8 bits), whereas the bitIndex is the bit index (not byte) // bytes (8 bits), whereas the bitIndex is the bit index (not byte)
@ -267,7 +267,7 @@
* @param {Uint8Array} id The ID of the contact to fetch. * @param {Uint8Array} id The ID of the contact to fetch.
* @return {Object|Null} The contact if available, otherwise null * @return {Object|Null} The contact if available, otherwise null
*/ */
this.get = function (id) { this.get = function(id) {
this.ensureInt8('id', id) this.ensureInt8('id', id)
let bitIndex = 0 let bitIndex = 0
@ -291,7 +291,7 @@
* @return {Number} Integer Index of contact with provided id if it * @return {Number} Integer Index of contact with provided id if it
* exists, -1 otherwise. * exists, -1 otherwise.
*/ */
this._indexOf = function (node, id) { this._indexOf = function(node, id) {
for (let i = 0; i < node.contacts.length; ++i) { for (let i = 0; i < node.contacts.length; ++i) {
if (this.arrayEquals(node.contacts[i].id, id)) return i if (this.arrayEquals(node.contacts[i].id, id)) return i
} }
@ -305,7 +305,7 @@
* @param {Uint8Array} id The ID of the contact to remove. * @param {Uint8Array} id The ID of the contact to remove.
* @return {Object} The k-bucket itself. * @return {Object} The k-bucket itself.
*/ */
this.remove = function (id) { this.remove = function(id) {
this.ensureInt8('the id as parameter 1', id) this.ensureInt8('the id as parameter 1', id)
let bitIndex = 0 let bitIndex = 0
@ -332,7 +332,7 @@
* @param {Number} bitIndex the bitIndex to which byte to check in the * @param {Number} bitIndex the bitIndex to which byte to check in the
* Uint8Array for navigating the binary tree * Uint8Array for navigating the binary tree
*/ */
this._split = function (node, bitIndex) { this._split = function(node, bitIndex) {
node.left = this.createNode() node.left = this.createNode()
node.right = this.createNode() node.right = this.createNode()
@ -360,7 +360,7 @@
* *
* @return {Array} All of the contacts in the tree, as an array * @return {Array} All of the contacts in the tree, as an array
*/ */
this.toArray = function () { this.toArray = function() {
let result = [] let result = []
for (const nodes = [this.root]; nodes.length > 0;) { for (const nodes = [this.root]; nodes.length > 0;) {
const node = nodes.pop() const node = nodes.pop()
@ -386,7 +386,7 @@
* calculation) * calculation)
* @param {Object} contact The contact object to update. * @param {Object} contact The contact object to update.
*/ */
this._update = function (node, index, contact) { this._update = function(node, index, contact) {
// sanity check // sanity check
if (!this.arrayEquals(node.contacts[index].id, contact.id)) { if (!this.arrayEquals(node.contacts[index].id, contact.id)) {
throw new Error('wrong index for _update') throw new Error('wrong index for _update')
@ -405,7 +405,7 @@
} }
const K_Bucket = exchangeAPI.K_Bucket = function K_Bucket(masterID, backupList) { const K_Bucket = exchangeAPI.K_Bucket = function K_Bucket(masterID, backupList) {
const decodeID = function (floID) { const decodeID = function(floID) {
let k = bitjs.Base58.decode(floID); let k = bitjs.Base58.decode(floID);
k.shift(); k.shift();
k.splice(-4, 4); k.splice(-4, 4);
@ -431,7 +431,7 @@
get: () => Array.from(orderedList) get: () => Array.from(orderedList)
}); });
self.closestNode = function (id, N = 1) { self.closestNode = function(id, N = 1) {
let decodedId = decodeID(id); let decodedId = decodeID(id);
let n = N || orderedList.length; let n = N || orderedList.length;
let cNodes = _KB.closest(decodedId, n) let cNodes = _KB.closest(decodedId, n)
@ -444,7 +444,7 @@
self.isPrev = (source, target) => orderedList.indexOf(target) === orderedList.indexOf(source) - 1; self.isPrev = (source, target) => orderedList.indexOf(target) === orderedList.indexOf(source) - 1;
self.isNext = (source, target) => orderedList.indexOf(target) === orderedList.indexOf(source) + 1; self.isNext = (source, target) => orderedList.indexOf(target) === orderedList.indexOf(source) + 1;
self.prevNode = function (id, N = 1) { self.prevNode = function(id, N = 1) {
let n = N || orderedList.length; let n = N || orderedList.length;
if (!orderedList.includes(id)) if (!orderedList.includes(id))
throw Error(`${id} is not in KB list`); throw Error(`${id} is not in KB list`);
@ -452,7 +452,7 @@
return (N == 1 ? pNodes[0] : pNodes); return (N == 1 ? pNodes[0] : pNodes);
}; };
self.nextNode = function (id, N = 1) { self.nextNode = function(id, N = 1) {
let n = N || orderedList.length; let n = N || orderedList.length;
if (!orderedList.includes(id)) if (!orderedList.includes(id))
throw Error(`${id} is not in KB list`); throw Error(`${id} is not in KB list`);
@ -471,14 +471,14 @@
return reject(ExchangeError(ExchangeError.NODES_OFFLINE_CODE, 'No Node online! Refresh the page or try again later', errorCode.NODES_OFFLINE)); return reject(ExchangeError(ExchangeError.NODES_OFFLINE_CODE, 'No Node online! Refresh the page or try again later', errorCode.NODES_OFFLINE));
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 => {
console.warn(nodeList[curPos], 'is offline'); console.warn(nodeList[curPos], 'is offline');
//try next node //try next node
fetch_api.curPos = curPos + 1; fetch_api.curPos = curPos + 1;
fetch_api(api, options) fetch_api(api, options)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error)) .catch(error => reject(error))
}); });
}) })
} }
@ -519,7 +519,7 @@
INTERNAL_ERROR: '500' INTERNAL_ERROR: '500'
}; };
const parseErrorCode = exchangeAPI.parseErrorCode = function (message) { const parseErrorCode = exchangeAPI.parseErrorCode = function(message) {
let code = message.match(/^E\d{3}:/g); let code = message.match(/^E\d{3}:/g);
if (!code || !code.length) if (!code || !code.length)
return null; return null;
@ -546,20 +546,20 @@
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!response.ok) if (!response.ok)
response.text() response.text()
.then(result => reject(ExchangeError(response.status, result))) .then(result => reject(ExchangeError(response.status, result)))
.catch(error => reject(ExchangeError(response.status, 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(ExchangeError(ExchangeError.BAD_RESPONSE_CODE, 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(ExchangeError(ExchangeError.BAD_RESPONSE_CODE, error))); .catch(error => reject(ExchangeError(ExchangeError.BAD_RESPONSE_CODE, error)));
}); });
} }
exchangeAPI.getAccount = function (floID, proxySecret) { exchangeAPI.getAccount = function(floID, proxySecret) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let request = { let request = {
floID: floID, floID: floID,
@ -574,19 +574,19 @@
console.debug(request); console.debug(request);
fetch_api('/account', { fetch_api('/account', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result) }).then(result => responseParse(result)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)); .catch(error => reject(error));
}); });
} }
exchangeAPI.getBuyList = function (asset = null) { exchangeAPI.getBuyList = function(asset = null) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch_api('/list-buyorders' + (asset ? "?asset=" + asset : "")) fetch_api('/list-buyorders' + (asset ? "?asset=" + asset : ""))
.then(result => responseParse(result) .then(result => responseParse(result)
@ -596,7 +596,7 @@
}); });
} }
exchangeAPI.getSellList = function (asset = null) { exchangeAPI.getSellList = function(asset = null) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch_api('/list-sellorders' + (asset ? "?asset=" + asset : "")) fetch_api('/list-sellorders' + (asset ? "?asset=" + asset : ""))
.then(result => responseParse(result) .then(result => responseParse(result)
@ -606,7 +606,7 @@
}); });
} }
exchangeAPI.getTradeList = function (asset = null) { exchangeAPI.getTradeList = function(asset = null) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch_api('/list-trades' + (asset ? "?asset=" + asset : "")) fetch_api('/list-trades' + (asset ? "?asset=" + asset : ""))
.then(result => responseParse(result) .then(result => responseParse(result)
@ -616,7 +616,7 @@
}); });
} }
exchangeAPI.getRates = function (asset = null) { exchangeAPI.getRates = function(asset = null) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch_api('/get-rates' + (asset ? "?asset=" + asset : "")) fetch_api('/get-rates' + (asset ? "?asset=" + asset : ""))
.then(result => responseParse(result) .then(result => responseParse(result)
@ -626,7 +626,7 @@
}); });
} }
exchangeAPI.getRateHistory = function (asset, duration = null) { exchangeAPI.getRateHistory = function(asset, duration = null) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch_api('/rate-history?asset=' + asset + (duration ? '&duration=' + duration : "")) fetch_api('/rate-history?asset=' + asset + (duration ? '&duration=' + duration : ""))
.then(result => responseParse(result) .then(result => responseParse(result)
@ -636,7 +636,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(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Need atleast one argument", errorCode.MISSING_PARAMETER)); return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Need atleast one argument", errorCode.MISSING_PARAMETER));
@ -651,7 +651,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(ExchangeError(ExchangeError.BAD_REQUEST_CODE, 'txid required', errorCode.MISSING_PARAMETER)); return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, 'txid required', errorCode.MISSING_PARAMETER));
@ -670,7 +670,7 @@
return floCrypto.signData(req_str, signKey); return floCrypto.signData(req_str, signKey);
} }
exchangeAPI.getLoginCode = function () { exchangeAPI.getLoginCode = function() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetch_api('/get-login-code') fetch_api('/get-login-code')
.then(result => responseParse(result) .then(result => responseParse(result)
@ -713,7 +713,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(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Login Code missing", errorCode.MISSING_PARAMETER)); return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Login Code missing", errorCode.MISSING_PARAMETER));
@ -736,19 +736,19 @@
console.debug(request); console.debug(request);
fetch_api("/login", { fetch_api("/login", {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)); .catch(error => reject(error));
}) })
} }
exchangeAPI.logout = function (floID, proxySecret) { exchangeAPI.logout = function(floID, proxySecret) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let request = { let request = {
floID: floID, floID: floID,
@ -763,19 +763,19 @@
console.debug(request); console.debug(request);
fetch_api("/logout", { fetch_api("/logout", {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
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(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid quantity (${quantity})`, errorCode.INVALID_NUMBER)); return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid quantity (${quantity})`, errorCode.INVALID_NUMBER));
@ -800,20 +800,20 @@
console.debug(request); console.debug(request);
fetch_api('/buy', { fetch_api('/buy', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
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(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid quantity (${quantity})`, errorCode.INVALID_NUMBER)); return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid quantity (${quantity})`, errorCode.INVALID_NUMBER));
@ -838,20 +838,20 @@
console.debug(request); console.debug(request);
fetch_api('/sell', { fetch_api('/sell', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
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(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid type (${type}): type should be sell (or) buy`, errorCode.INVALID_TYPE)); return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid type (${type}): type should be sell (or) buy`, errorCode.INVALID_TYPE));
@ -872,20 +872,20 @@
console.debug(request); console.debug(request);
fetch_api('/cancel', { fetch_api('/cancel', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
//receiver should be object eg {floID1: amount1, floID2: amount2 ...} //receiver should be object eg {floID1: amount1, floID2: amount2 ...}
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(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Invalid receiver: parameter is not an object", errorCode.INVALID_FLO_ID)); return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Invalid receiver: parameter is not an object", errorCode.INVALID_FLO_ID));
@ -918,19 +918,19 @@
console.debug(request); console.debug(request);
fetch_api('/transfer-token', { fetch_api('/transfer-token', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
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(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid quantity (${quantity})`, errorCode.INVALID_NUMBER)); return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, `Invalid quantity (${quantity})`, errorCode.INVALID_NUMBER));
@ -952,20 +952,20 @@
console.debug(request); console.debug(request);
fetch_api('/deposit-flo', { fetch_api('/deposit-flo', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}) })
} }
exchangeAPI.withdrawFLO = function (quantity, floID, proxySecret) { exchangeAPI.withdrawFLO = function(quantity, floID, proxySecret) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let request = { let request = {
floID: floID, floID: floID,
@ -982,19 +982,19 @@
console.debug(request); console.debug(request);
fetch_api('/withdraw-flo', { fetch_api('/withdraw-flo', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
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(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Invalid Private Key", errorCode.INVALID_PRIVATE_KEY)); return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Invalid Private Key", errorCode.INVALID_PRIVATE_KEY));
@ -1014,20 +1014,20 @@
console.debug(request); console.debug(request);
fetch_api('/deposit-token', { fetch_api('/deposit-token', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}) })
} }
exchangeAPI.withdrawToken = function (token, quantity, floID, proxySecret) { exchangeAPI.withdrawToken = function(token, quantity, floID, proxySecret) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let request = { let request = {
floID: floID, floID: floID,
@ -1046,19 +1046,46 @@
console.debug(request); console.debug(request);
fetch_api('/withdraw-token', { fetch_api('/withdraw-token', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
exchangeAPI.addUserTag = function (tag_user, tag, floID, proxySecret) { exchangeAPI.getUserTransacts = function(floID, proxySecret) {
return new Promise((resolve, reject) => {
let request = {
floID: floID,
timestamp: Date.now()
};
if (floCrypto.getFloID(proxySecret) === floID) //Direct signing (without proxy)
request.pubKey = floCrypto.getPubKeyHex(proxySecret);
request.sign = signRequest({
type: "get_transact",
timestamp: request.timestamp
}, proxySecret);
console.debug(request);
fetch_api('/get-transact', {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(request)
}).then(result => responseParse(result)
.then(result => resolve(result))
.catch(error => reject(error)))
.catch(error => reject(error))
})
}
exchangeAPI.addUserTag = function(tag_user, tag, floID, proxySecret) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let request = { let request = {
floID: floID, floID: floID,
@ -1077,19 +1104,19 @@
console.debug(request); console.debug(request);
fetch_api('/add-tag', { fetch_api('/add-tag', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
exchangeAPI.removeUserTag = function (tag_user, tag, floID, proxySecret) { exchangeAPI.removeUserTag = function(tag_user, tag, floID, proxySecret) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let request = { let request = {
floID: floID, floID: floID,
@ -1108,19 +1135,19 @@
console.debug(request); console.debug(request);
fetch_api('/remove-tag', { fetch_api('/remove-tag', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
exchangeAPI.addDistributor = function (distributor, asset, floID, proxySecret) { exchangeAPI.addDistributor = function(distributor, asset, floID, proxySecret) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let request = { let request = {
floID: floID, floID: floID,
@ -1139,19 +1166,19 @@
console.debug(request); console.debug(request);
fetch_api('/add-distributor', { fetch_api('/add-distributor', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
exchangeAPI.removeDistributor = function (distributor, asset, floID, proxySecret) { exchangeAPI.removeDistributor = function(distributor, asset, floID, proxySecret) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let request = { let request = {
floID: floID, floID: floID,
@ -1170,14 +1197,14 @@
console.debug(request); console.debug(request);
fetch_api('/remove-distributor', { fetch_api('/remove-distributor', {
method: "POST", method: "POST",
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(request) body: JSON.stringify(request)
}).then(result => responseParse(result, false) }).then(result => responseParse(result, false)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error))) .catch(error => reject(error)))
.catch(error => reject(error)) .catch(error => reject(error))
}) })
} }
@ -1242,7 +1269,7 @@
}) })
} }
exchangeAPI.clearAllLocalData = function () { exchangeAPI.clearAllLocalData = function() {
localStorage.removeItem('exchange-nodes'); localStorage.removeItem('exchange-nodes');
localStorage.removeItem('exchange-assets'); localStorage.removeItem('exchange-assets');
localStorage.removeItem('exchange-tags'); localStorage.removeItem('exchange-tags');

View File

@ -1,4 +1,4 @@
(function(EXPORTS) { //floTokenAPI v1.0.3a (function(EXPORTS) { //floTokenAPI v1.0.3b
/* Token Operator to send/receive tokens via blockchain using API calls*/ /* Token Operator to send/receive tokens via blockchain using API calls*/
'use strict'; 'use strict';
const tokenAPI = EXPORTS; const tokenAPI = EXPORTS;
@ -20,6 +20,13 @@
if (floGlobals.currency) tokenAPI.currency = floGlobals.currency; if (floGlobals.currency) tokenAPI.currency = floGlobals.currency;
Object.defineProperties(floGlobals, {
currency: {
get: () => DEFAULT.currency,
set: currency => DEFAULT.currency = currency
}
});
const fetch_api = tokenAPI.fetch = function(apicall) { const fetch_api = tokenAPI.fetch = function(apicall) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log(DEFAULT.apiURL + apicall); console.log(DEFAULT.apiURL + apicall);

View File

@ -83,6 +83,7 @@ module.exports = function App(secret, DB) {
app.post('/withdraw-flo', Request.WithdrawFLO); app.post('/withdraw-flo', Request.WithdrawFLO);
app.post('/deposit-token', Request.DepositToken); app.post('/deposit-token', Request.DepositToken);
app.post('/withdraw-token', Request.WithdrawToken); app.post('/withdraw-token', Request.WithdrawToken);
app.post('/get-transact', Request.GetUserTransacts);
//Manage user tags (Access to trusted IDs only) //Manage user tags (Access to trusted IDs only)
app.post('/add-tag', Request.AddUserTag); app.post('/add-tag', Request.AddUserTag);

View File

@ -254,6 +254,18 @@ function getAccountDetails(floID) {
}); });
} }
function getUserTransacts(floID) {
return new Promise((resolve, reject) => {
DB.query("(SELECT 'deposit' as type, txid, floID, token, amount, status FROM InputToken WHERE floID=?)" +
"UNION (SELECT 'deposit' as type, txid, floID, 'FLO' as token, amount, status FROM InputFLO WHERE floID=?)" +
"UNION (SELECT 'withdraw' as type, txid, floID, token, amount, status FROM OutputToken WHERE floID=?)" +
"UNION (SELECT 'withdraw' as type, txid, floID, 'FLO' as token, amount, status FROM OutputFLO WHERE floID=?)",
[floID, floID, floID, floID])
.then(result => resolve(result))
.catch(error => reject(error))
})
}
function getTransactionDetails(txid) { function getTransactionDetails(txid) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let tableName, type; let tableName, type;
@ -712,6 +724,7 @@ function blockchainReCheck() {
retryWithdrawalToken(); retryWithdrawalToken();
confirmWithdrawalFLO(); confirmWithdrawalFLO();
confirmWithdrawalToken(); confirmWithdrawalToken();
console.debug("Last Block :", lastSyncBlockHeight);
} }
}).catch(error => console.error(error)); }).catch(error => console.error(error));
} }
@ -731,6 +744,7 @@ module.exports = {
getRateHistory, getRateHistory,
getBalance, getBalance,
getAccountDetails, getAccountDetails,
getUserTransacts,
getTransactionDetails, getTransactionDetails,
transferToken, transferToken,
depositFLO, depositFLO,

View File

@ -265,6 +265,16 @@ function WithdrawToken(req, res) {
); );
} }
function GetUserTransacts(req, res) {
let data = req.body;
processRequest(res, "User Transacts", {
type: "get_transact",
timestamp: data.timestamp
}, data.sign, data.floID, data.pubKey,
() => market.getUserTransacts(data.floID)
);
}
function AddUserTag(req, res) { function AddUserTag(req, res) {
let data = req.body; let data = req.body;
if (!trustedIDs.includes(data.floID)) if (!trustedIDs.includes(data.floID))
@ -509,6 +519,7 @@ module.exports = {
WithdrawFLO, WithdrawFLO,
DepositToken, DepositToken,
WithdrawToken, WithdrawToken,
GetUserTransacts,
AddUserTag, AddUserTag,
RemoveUserTag, RemoveUserTag,
AddDistributor, AddDistributor,