Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cardano-api

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cardano-api - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

58

lib/cardano.js
const request = require('request')
const { apis, endpoints } = require('./config')
const { api, endpoints } = require('./config')
const noPathAllowed = 'no path is allowed with this method'
const noPathProvided = 'please pass a txn, epoch or hash into this method'
const noPathAllowed = 'no address, txid, epoch or hash query allowed'
const noPathProvided = 'please provide an address, txid, epoch or hash'
const paths = ['epoch', 'txid', 'hash', 'address']
const buildMethod = endpoint => query =>
new Promise((resolve, reject) => {
let stopper, nameTrack
let params, path = ''
let path = '', params = ''
let stopper, keyTrack
if(query) {
if(!endpoint.path && query.path) {
return reject(noPathAllowed)
}
path = endpoint.path && query.path ? `/${query.path}` : ''
delete query.path
if(endpoint.path && !path) return reject(noPathProvided)
if(!endpoint.path && query.path) return reject(noPathAllowed)
params = Object.keys(query).reduce((str, name) => {
params = Object.keys(query).reduce((str, key) => {
// check to see if the params is in the list
if(!endpoint.params.includes(name)) {
if(!endpoint.params.includes(key)) {
stopper = true
nameTrack = name
keyTrack = key
}
str += `${name}=${query[name]}&`
if(paths.includes(key)) {
path = `/${query[key]}`
return str
}
str += `${key}=${query[key]}&`
return str
}, '')
params = params ? `?${params.replace(/\&$/, '')}` : ''
params = params ? `?${params.replace(/\&$/, '')}` : ''
if(stopper) return reject(`${nameTrack} is not a valid query to this endpoint`)
if(stopper) return reject(`${keyTrack} is not a valid query to this endpoint`)
}
request(`${apis[0]}/${endpoint.url}${path}${params}`, (err, res, body) => {
if(endpoint.path && !path) return reject(noPathProvided)
const url = `${api}${endpoint.url}${path}${params}`
request(url, (err, res, body) => {
if(err) reject(err)
if(res.statusCode === 404 || res.statusCode === 400) reject(res.statusMessage)
if(body) resolve(JSON.parse(body))
if(res.statusCode === 404 || res.statusCode === 400) reject(res.statusMessage)
if(err) reject(err)
})
})
const cardano = endpoints.reduce((obj, endpoint) => {
exports.cardano = endpoints.reduce((obj, endpoint) => {
obj[endpoint.method] = buildMethod(endpoint)
return obj
}, {})
exports.cardano = cardano
}, {})

@@ -7,3 +7,3 @@ module.exports = {

path: true,
params: []
params: ['address']
},

@@ -14,3 +14,3 @@ {

path: true,
params: []
params: ['hash']
},

@@ -24,2 +24,8 @@ {

{
method: 'blockTransaction',
url: 'api/blocks/txs',
path: true,
params: ['hash', 'limit', 'offset']
},
{
method: 'pagesTotal',

@@ -34,3 +40,3 @@ url: 'api/blocks/pages/total',

path: true,
params: ['slot']
params: ['epoch', 'slot']
},

@@ -59,3 +65,3 @@ {

path: true,
params: []
params: ['txid']
},

@@ -69,7 +75,3 @@ {

],
apis: [
'https://cardanoexplorer.com',
'https://explorer.iohkdev.io',
'http://cardano-explorer.cardano-mainnet.iohk.io'
]
api: 'https://cardanoexplorer.com/'
}
{
"name": "cardano-api",
"version": "0.0.2",
"version": "0.0.3",
"description": "Node promise wrapper for the Cardano Explorer API",

@@ -9,2 +9,6 @@ "main": "lib/cardano.js",

},
"repository": {
"type": "git",
"url": "git+https://github.com/funador/cardano-api.git"
},
"keywords": [

@@ -15,2 +19,3 @@ "cardano"

"license": "MIT",
"bugs": "https://github.com/funador/cardano-api/issues",
"dependencies": {

@@ -17,0 +22,0 @@ "request": "^2.83.0"

# cardano-api
Promise wrapper for the [Cardano API](https://cardanodocs.com/technical/explorer/api/).
### This is not quite finished :/

@@ -10,3 +9,3 @@ ```js

const query = {
path: 'Ae2tdPwU...fQwGpm' // the address to query
address: 'Ae2tdP...fQwGpm' // the address to check
}

@@ -31,13 +30,10 @@

# Supported Endpoints
# Endpoints
#### cardano.address() - [/api/addresses/summary/{address}](https://cardanodocs.com/technical/explorer/api/#path--api-addresses-summary--address-)
```js
// Get summary information about an address
const query = {
path: 'Ae2tdPwU...fQwGpm' // Required, wallet address to check
address: 'Ae2tdP...fQwGpm' // Required, wallet address to check
}

@@ -51,5 +47,5 @@

```js
//
// Get block's summary information
const query = {
path: 'Ae2tdPwU...fQwGpm' // Required, block hash to check
hash: '52659d...195a70' // Required, block hash to check
}

@@ -61,6 +57,20 @@

#### cardano.blockTransaction() - [/api/blocks/txs/{hash}](https://cardanodocs.com/technical/explorer/api/#path--api-blocks-txs--hash-)
```js
// Get brief information about transaction based on blocks
// For info on specific transactions use cardano.transaction()
const query = {
hash: '52659d...195a70', // Required, block hash to check
limit: 10, // Optional
offset: 2 // Optional
}
cardano.blockTransaction(query)
.then(data => console.log('block', data))
```
#### cardano.pagesTotal() - [/api/blocks/pages/total](https://cardanodocs.com/technical/explorer/api/#path--api-blocks-pages-total)
Get the list of total pages
```js
// Get the list of total pages
const query = {

@@ -77,2 +87,3 @@ pageSize: 3 // Optional

```js
const query = {

@@ -110,4 +121,5 @@ page: 1, // Optional

```js
// Search the blocks by epoch and slot
const query = {
path: 3, // Required, epoch to check
epoch: 3, // Required, epoch to check
slot: 24 // Optional

@@ -123,4 +135,5 @@ }

```js
// Get summary information about a transaction
const query = {
path: 'Ae2tdPwU...fQwGpm' // Required, transaction to check
txid: 'f3d468...3ac99b' // Required, transaction to check
}

@@ -135,2 +148,3 @@

```js
// Get information about the 20 latest transactions
cardano.transactionLast()

@@ -140,7 +154,13 @@ .then(data => console.log('transactionLast', data))

### Issues
- A couple of the endpoints are represented with breaking changes in the official documentation, assume that more could change in the future
Something not working? Please [open an issue](https://github.com/funador/cardano-api/issues)
### test
### Test
``` npm test```
``` npm test```
### Cardano tips
```DdzFFzCqrht8iQ2utWYssBnfGvSqkGfM7fxHXZWoB57ormT17td1CY4Eye7bADF6HpeGC57vwV5ZPzmVjiZRQEkAD9Rc4P8LDF7FfYne```
##### Thank You

@@ -21,3 +21,3 @@ const assert = require('assert')

const query = {
path: 'DdzFFzCqrhsoTFwZkQRfYMGMvKwmej3wEbGevViniuuDPTwJoGfLYpWo2zXD8G7cz5ti3m2sz2vsheD4vskNCYVuedsPApxRdDZWJYrm'
address: 'DdzFFzCqrhsoTFwZkQRfYMGMvKwmej3wEbGevViniuuDPTwJoGfLYpWo2zXD8G7cz5ti3m2sz2vsheD4vskNCYVuedsPApxRdDZWJYrm'
}

@@ -39,3 +39,3 @@

const query = {
path: '52659df3f8ef7997e0dc8c04b041a956512429b95f4a28275cb53c9f6a195a70',
hash: '52659df3f8ef7997e0dc8c04b041a956512429b95f4a28275cb53c9f6a195a70',
}

@@ -71,2 +71,20 @@

describe('cardano.blockTransaction()', () => {
it('should return information about transactions', () => {
const query = {
hash: '52659df3f8ef7997e0dc8c04b041a956512429b95f4a28275cb53c9f6a195a70',
limit: 10,
offset: 10
}
return cardano.blockTransaction(query)
.then(data => {
assert.equal('Right', Object.keys(data)[0])
})
.catch(err => {
console.log('ERR', err)
})
})
})
describe('cardano.pagesTotal()', () => {

@@ -91,3 +109,3 @@ it('should return the total amount of blocks', () => {

const query = {
path: 3
epoch: 3
}

@@ -159,3 +177,3 @@

const query = {
path: 'f3d4686499fd54dcbadc1d501047aadb52b28704fb8126f10f4123d69e3ac99b'
txid: 'f3d4686499fd54dcbadc1d501047aadb52b28704fb8126f10f4123d69e3ac99b'
}

@@ -162,0 +180,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc