concordapi
Advanced tools
Comparing version 1.0.5 to 1.1.0
110
index.js
const req = require('requestify') | ||
const basePublicUrl = 'https://www.cxd.network/api/public/v1/' | ||
var cachedPrices = [] /* Temporary solution until i properly learn Promises/Callbacks! Please do a pull-request if you can fix this. <3 */ | ||
/* cachedProfiles = [] */ | ||
var lastPrice = 0, | ||
lastCoin = '', | ||
user = {discordID: '', discordUsername: '', balance: 0} | ||
exports.price = function (coin) { | ||
exports.price = async function (coin) { | ||
if (coin && coin != null && typeof coin === 'string' && coin.length >= 2) { | ||
checkPrice(coin) | ||
try { | ||
var price = await req.get(basePublicUrl + 'getprice?coin=' + coin.toLowerCase()) | ||
price = Number(price.body) | ||
if (price <= 0 || price === NaN || price === null || price === undefined) { | ||
throw 'Price returned for ' + coin + ' is not a number, you may have entered an incorrect ticker' | ||
} | ||
lastPrice = price | ||
lastCoin = coin | ||
return price | ||
} catch (err) { | ||
if (err.body) { | ||
console.log('ConcordAPI Error caught: (' + err.body + ')') | ||
} else { | ||
console.log('ConcordAPI Error caught: (' + err + ')') | ||
} | ||
if (lastCoin === coin) { | ||
return lastPrice | ||
} else { | ||
return 0 | ||
} | ||
} | ||
} else { | ||
console.warn('String variable missing, "' + typeof coin + ' - ' + coin + '" was recieved') | ||
return | ||
} | ||
if (cachedPrices.length >= 1) { | ||
for (var ii = 0; ii < cachedPrices.length; ii++) { | ||
if (cachedPrices[ii].coin === coin.toLowerCase()) { | ||
if (cachedPrices[ii].price !== undefined && cachedPrices[ii].price >= 0.00000001) { | ||
/* console.log(cachedPrices[ii].price); */ | ||
return cachedPrices[ii].price | ||
} else { | ||
console.warn('API Response could not be parsed, Concord REST API may be down, or your input of "' + coin.toUpperCase() + '" is an incorrect Ticker') | ||
} | ||
} | ||
exports.getBalance = async function (discordID) { | ||
if (discordID && discordID != null && typeof discordID === 'string' && discordID.length === 18) { | ||
try { | ||
var balance = await req.get(basePublicUrl + 'getprofile?id=' + discordID) | ||
balance = Number((Number(JSON.parse(balance.body).balance) - 0.00000001).toFixed(8)) | ||
return balance | ||
} catch (err) { | ||
if (err.body) { | ||
console.log('ConcordAPI Error caught: (' + err.body + ')') | ||
} else { | ||
console.log('ConcordAPI Error caught: (' + err + ')') | ||
} | ||
} | ||
} else if (typeof discordID !== 'string') { | ||
console.warn('String variable missing, "' + typeof discordID + ' - ' + discordID + '" was recieved') | ||
} else { | ||
return console.log('Cache has not loaded correctly, please use the connect() function to refresh cache') | ||
console.warn('Invalid parameters, please ensure discordID is 18 characters long and is the correct format: string') | ||
} | ||
} | ||
exports.connect = function () { /* This exists to help with starter-caching of CXD Service API data, as i am currently unable to correctly return data from this module to a dev's main script :( */ | ||
try { | ||
req.get(basePublicUrl + 'getprice?coin=ic').then(function (res) { | ||
var price = Number(res.body) | ||
if (price > 0 && price !== undefined) { | ||
if (cachedPrices.length === 0) { | ||
cachedPrices.push({coin: 'ic', price: price}) /* If coin isn't already cached, cache it! */ | ||
} | ||
console.log('CXDx Prices cached... Services are Ready') | ||
} | ||
}) | ||
} catch (err) { | ||
console.warn('API Request failed, error: ' + err) | ||
} | ||
} | ||
/* None-Sandboxed functions */ | ||
function checkPrice (coin) { | ||
try { | ||
req.get(basePublicUrl + 'getprice?coin=' + coin.toLowerCase()).then(function (res) { | ||
var price = Number(res.body) | ||
if (price >= 0.00000001 && price !== undefined) { | ||
if (cachedPrices.length > 0) { | ||
var i, /* eslint-disable-line */ | ||
iLength = cachedPrices.length | ||
for (i = 0; i < iLength; i++) { | ||
if (cachedPrices[i].coin === coin.toLowerCase()) { | ||
cachedPrices[i].price = price /* If coin is already cached, update the cache! */ | ||
} | ||
} | ||
} else { | ||
cachedPrices.push({coin: 'ic', price: price}) /* If coin isn't already cached, cache it! */ | ||
} | ||
exports.getUser = async function (discordID) { | ||
if (discordID && discordID != null && typeof discordID === 'string' && discordID.length === 18) { | ||
try { | ||
var data = await req.get(basePublicUrl + 'getprofile?id=' + discordID) | ||
let userData = user | ||
userData.balance = Number((Number(JSON.parse(data.body).balance) - 0.00000001).toFixed(8)) | ||
userData.discordID = discordID | ||
userData.discordUsername = JSON.parse(data.body).username | ||
return userData | ||
} catch (err) { | ||
if (err.body) { | ||
console.log('ConcordAPI Error caught: (' + err.body + ')') | ||
} else { | ||
console.warn('API Response could not be parsed, Concord REST API may be down, or your input of "' + coin.toUpperCase() + '" is an incorrect Ticker') | ||
console.log('ConcordAPI Error caught: (' + err + ')') | ||
} | ||
}) | ||
} catch (err) { | ||
console.warn('API Request failed, error: ' + err) | ||
} | ||
} else if (typeof discordID !== 'string') { | ||
console.warn('String variable missing, "' + typeof discordID + ' - ' + discordID + '" was recieved') | ||
} else { | ||
console.warn('Invalid parameters, please ensure discordID is 18 characters long and is the correct format: string') | ||
} | ||
} |
{ | ||
"name": "concordapi", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "The Official Concord REST API Package for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,6 +6,6 @@ # ConcordAPI | ||
**WARNING:** This module is in very early development, and has a few bugs which are known by me, you are free to create a Pull Request if any issues/bugs are fixed, please do not rely on this module solely. | ||
**Notice:** This module is in early development, and may have minor bugs and inconveniences, this module is under active development by the Concord Team | ||
## Getting Started | ||
# Getting Started | ||
@@ -21,18 +21,11 @@ Install the Concord-API NPM Package with: | ||
# Using the API | ||
### Setting up Concord-API | ||
## Reading Concord Exchange (CXDx) Prices | ||
The Concord API, at current time, has to have an instantiation period. This is a temporary fix to a bug which i will learn to fix soon! | ||
To get the API ready for usage, instantiate the API and Caching with: | ||
```js | ||
concord.connect() //Console should print "CXDx Prices cached... Services are Ready" | ||
``` | ||
### Reading Concord Exchange (CXDx) Prices | ||
To check a coin's price in CXD use: | ||
```js | ||
concord.price(string) | ||
concord.price(string).then(price => { | ||
console.log(price) // Logs the price of 'string' | ||
}) | ||
``` | ||
@@ -43,27 +36,47 @@ | ||
var coin = "IC" | ||
setInterval(checkPrice, 5000) | ||
function checkPrice () { | ||
console.log('1 '+coin+' is '+concord.price(coin)+' CXD') | ||
} | ||
concord.price(coin).then(price => { | ||
console.log(coin + " is priced at " + price + " CXD") // Logs the price of 'IC' | ||
}) | ||
``` | ||
Would log the price of IC (Ignition Coin) every 5 seconds, due to the way caching works here, each result is one 'API-Tick' older than the requested latest price. | ||
Would log the price of IC (Ignition Coin) live from the Concord Exchange. | ||
### Reading Concord Social Platform (CSP) Profiles | ||
## Reading Concord Social Platform (CSP) Profiles | ||
There are two options you can choose from, to read data from a user's profile. | ||
### Method One - Via the user Object | ||
The user Object contains all public data of a user's profile, the contents of the object can be seen by stringifying the result of the function: | ||
```js | ||
/* userID is the DiscordID of the user who's profile you're reading from, as a string */ | ||
concord.getUser(userID).then(user => { | ||
console.log("User's full data object: "+JSON.stringify(user)) | ||
console.log("User's balance is "+user.balance) | ||
console.log("User's Discord Username is "+user.discordUsername) | ||
console.log("User's Discord ID is "+user.discordID) | ||
}) | ||
``` | ||
Coming soon, Module bug mentioned before makes this function impossible to create. | ||
### Method Two - Individual Profile elements | ||
If you don't want to use the full object, or want to create a Lightweight function without accessing the object, you can use: | ||
```js | ||
concord.getBalance(userID).then(balance => { | ||
console.log("Balance of "+userID+" is " + balance + " CXD") | ||
}) | ||
``` | ||
## Full Examples | ||
Examples in this section can be freely copy/pasted into your code and modified to your choosing. | ||
Examples in this section can be freely copy/pasted into your code and modified to your choosing, and when unmodified, "just work" | ||
```js | ||
/* This script would run indefinitely until the console/terminal is closed manually */ | ||
var concord = require("concordapi") | ||
concord.connect() //Instantiate Concord Library and load cache. | ||
var coin = "IC" //Coin listed on CXDx to check price of, in CXD. | ||
setInterval(checkPrice, 5000) //Check coin-price every 5 seconds | ||
var coin = "IC" // A Coin listed on the CXDx to check the price of, in CXD. | ||
setInterval(checkPrice, 5000) // Check coin price every 5 seconds | ||
function checkPrice () { | ||
console.log('1 '+coin+' is '+concord.price(coin)+' CXD') // Prints the price of the CXDx Coin | ||
concord.price(coin).then(price => { | ||
console.log('1 '+coin+' is '+price+' CXD') // Prints the price of the CXDx Coin | ||
}) | ||
} | ||
``` |
7684
75
80