Browse Source

add npm module in progress

pull/19/head
Esteban Santiesteban 1 month ago
parent
commit
4e172ddc81
5 changed files with 270 additions and 0 deletions
  1. 150
    0
      code/index.js
  2. 42
    0
      code/tests.js
  3. 50
    0
      code/utilities/convert.js
  4. 3
    0
      package-lock.json
  5. 25
    0
      package.json

+ 150
- 0
code/index.js View File

@@ -0,0 +1,150 @@
const Axios = require('axios')
const Convert = require('./utilities/convert')

const Nano = {

url: 'https://api.nano.to',

/**
* Big Number Conversion API
**/
toRaw: Convert.toRaw,
fromRaw: Convert.fromRaw,

/**
* Live Price (CoinMarketCap)
**/
async price(currency, config) {

var data

currency = currency || 'USD'

let symbol = config && config.symbol ? config.symbol.toUpperCase() : 'NANO'

if (typeof config == 'object' && (config.apiKey || config.key)) {

let headers = { headers: { 'X-CMC_PRO_API_KEY': config.apiKey || config.key } }
var price = await server.http.get(`https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${symbol}${ currency !== 'USD' ? '&convert=' + currency : '' }`, headers)
data = { symbol: symbol.toUpperCase(), currency: currency.toUpperCase(), price: price.data.data[symbol].quote[currency].price, timestamp: price.data.status.timestamp }

} else {
var price = await Axios.get(`https://api.nano.to/price?symbol=${symbol}&currency=${currency}`)
data = price.data

}

return config && config.timestamp ? data : data.price

},

/**
* General information about address, including balance.
**/
account: async (address) => {
if (!address) return new Error("First parameter, NANO Address is missing.")
var account = await Axios.get(`${Nano.url}/account/${address}`)
return account.data
},

address: this.account,

/**
* Get pending payments
**/
pending: async (address) => {
if (!address) return new Error("First parameter, NANO Address is missing.")
var history = await Axios.get(`${Nano.url}/pending/${address}`)
return history.data
},


/**
* Check for a specific payment
**/
payment: async (address, amount) => {
return await this.findBlockByAmount(address, amount)
},

/**
* Get payment history
**/
history: async (address) => {
if (!address) return new Error("First parameter, NANO Address is missing.")
var history = await Axios.get(`${Nano.url}/history/${address}`)
return history.data
},

/**
* Checkout POST API
**/
checkout: async (body) => {
if (!body) return new Error("First parameter, Checkout body is missing.")
var checkout = await Axios.post(`https://nano.to`, body)
return checkout.data
},

// aliases
// order: this.checkout,
// invoice: this.checkout,
// purchase: this.checkout,
// donation: this.checkout,

/**
* Short Names :)
**/
name: async (name) => {
if (!name) return new Error("First parameter, Nano.to Name is missing.")
var name = await Axios.get(`${Nano.url}/name/${name}`)
return name.data
},

/**
* QrCode API
* TODO
**/
// qrCode: async (address, icon, amount) => {
// // psuedo code
// if (!address) return new Error("First parameter, Nano.to address is missing.")
// var qrCode = await Axios.get(`https://api.nano.to/qrcode/${address}`)
// return qrCode.data
// },

/**
* Wallet API
* TODO
**/
wallet: async (config) => {
return {
send(to, amount) {},
receive() {},
create(name, seed) {},
backup() {},
}
},

/**
* Representative API
* TODO
**/
representative: {
list(address) {},
create() {}, // ;)
},

/**
* Blockchain Helpers
**/

findBlockByAmount: async (address, amount) => {
if (!address) return new Error("First parameter, NANO Address is missing.")
var payment = await Axios.get(`${Nano.url}/payment/${address}/${amount}`)
return payment.data
},


}

module.exports = Nano

+ 42
- 0
code/tests.js View File

@@ -0,0 +1,42 @@
const Nano = require('./index.js')

const debug = true

function validate(name, value, type, output) {
if (value && (typeof value).toLowerCase() == (type || 'string').toLowerCase()) {
console.log( `${name}:`, '\x1b[32m', "Ok", '\x1b[0m')
} else {
console.log( `${name}:`, '\x1b[41m', "Error", '\x1b[0m')
}
if (debug && output) console.log(output)
}

;(async () => {

console.clear()

// Price from Nano.to, CoinMarketCap cached for 60 seconds.
validate( "Nano.price()", await Nano.price(), 'number')

// Price from CoinMarketCap
// check( "Nano.price({ config })", (await api.price({ key: '{{ API_KEY }}' })).price, 'number')
var account = await Nano.account('nano_37y6iq8m1zx9inwkkcgqh34kqsihzpjfwgp9jir8xpb9jrcwhkmoxpo61f4o')
validate( "Nano.account()", account.balance, 'string')

var pending = await Nano.pending('nano_37y6iq8m1zx9inwkkcgqh34kqsihzpjfwgp9jir8xpb9jrcwhkmoxpo61f4o')
validate( "Nano.pending()", pending, 'object')

var history = await Nano.history('nano_37y6iq8m1zx9inwkkcgqh34kqsihzpjfwgp9jir8xpb9jrcwhkmoxpo61f4o')
validate( "Nano.address()", history, 'object')

var rawAmount = '19424000000000000000000000000'
var convertFromRaw = Nano.fromRaw(rawAmount)
validate( "Nano.fromRaw()", convertFromRaw, 'string')

var convertToRaw = Nano.toRaw(convertFromRaw)
validate( "Nano.toRaw()", convertToRaw, 'string')

var findBlockByAmount = Nano.findBlockByAmount("nano_37y6iq8m1zx9inwkkcgqh34kqsihzpjfwgp9jir8xpb9jrcwhkmoxpo61f4o", history[0].amount)
validate( "Nano.findBlockByAmount()", findBlockByAmount, 'object')

})()

+ 50
- 0
code/utilities/convert.js View File

@@ -0,0 +1,50 @@
const big = require('big.js')

// Configure Big to never show exponential notation.
big.NE = -31;
big.PE = 39;

const RAW_IN_MEGA = new big('1000000000000000000000000000000');
const MEGA_IN_RAW = new big('0.000000000000000000000000000001');
const MEGA_MIN_AMOUNT = MEGA_IN_RAW;
const RAW_MAX_AMOUNT = new big('340282366920938463463374607431768211455');
const MEGA_MAX_AMOUNT = new big(RAW_MAX_AMOUNT).times(MEGA_IN_RAW);

module.exports = {

fromRaw(raw) {
let rawBig;
try {
rawBig = new big(raw);
} catch (error) {
throw new Error('The raw amount is invalid.');
}
return rawBig.mul(MEGA_IN_RAW).toString();
},
toRaw(mega) {
if (mega === undefined) return new Error("First parameter, NANO amount is missing.")

if (!parseFloat(mega)) return new Error("First parameter, NANO amount must be a Number.")

mega = parseFloat(mega)

let megaBig = big;

try {

megaBig = new big(mega);

if (megaBig.lt(0)) return new Error("First parameter, NANO amount must not be negative.")
if (megaBig.lt(MEGA_MIN_AMOUNT)) return new Error("First parameter, NANO amount is too small.")
if (megaBig.gt(MEGA_MAX_AMOUNT)) return new Error("First parameter, NANO amount is too large.")

return megaBig.times(RAW_IN_MEGA).toString();

} catch (error) {
return new Error('The mega amount is invalid.');
}
},

}

+ 3
- 0
package-lock.json View File

@@ -0,0 +1,3 @@
{
"lockfileVersion": 1
}

+ 25
- 0
package.json View File

@@ -0,0 +1,25 @@
{
"main": "code/index.js",
"name": "@formsend/nano",
"author": {
"name": "Formsend",
"email": "hello@forward.miami"
},
"bugs": {
"url": "https://github.com/formsend/nano/issues"
},
"bundleDependencies": false,
"dependencies": {
"axios": "^0.22.0",
"big.js": "^6.1.1"
},
"deprecated": false,
"description": "A super simple NANO crypto currency blockchain API.",
"homepage": "https://github.com/formsend/nano",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/formsend/nano.git"
},
"version": "0.0.1"
}

Loading…
Cancel
Save