Skip to content

Commit 0c9b9b0

Browse files
WIP - adding updated metrics and blockchains
1 parent cbb771b commit 0c9b9b0

File tree

20 files changed

+2484
-4834
lines changed

20 files changed

+2484
-4834
lines changed

ava.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default {
22
files: ['test/**.test.js'],
33
ignoredByWatcher: ['dist/**/*'],
44
cache: true,
5-
concurrency: 5,
5+
concurrency: 2,
66
require: ['@babel/register'],
77
babel: true,
88
inherit: true

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web3data-js",
3-
"version": "0.7.0",
3+
"version": "0.7.1",
44
"description": "A javascript wrapper for accessing amberdata's public API.",
55
"main": "index.js",
66
"browser": "dist/web3data.min.js",

src/block.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,13 @@ class Block {
137137
}
138138

139139
getMetrics(filterOptions) {
140+
const subendpoint =
141+
filterOptions && (filterOptions.startDate || filterOptions.endDate)
142+
? 'historical'
143+
: 'latest'
140144
return get(this.web3data, {
141-
endpoint: ENDPOINT,
142-
subendpoint: 'metrics/latest',
145+
endpoint: `${ENDPOINT}/metrics`,
146+
subendpoint,
143147
filterOptions
144148
}).then(onFulfilled, onError)
145149
}

src/blockchain.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const {BLOCKCHAINS_ENDPOINT: ENDPOINT} = require('./constants')
2+
3+
const {get, onFulfilled, onError} = require('./utils')
4+
5+
class Blockchain {
6+
constructor(web3data) {
7+
this.web3data = web3data
8+
}
9+
10+
getMetrics(filterOptions) {
11+
const subendpoint =
12+
filterOptions && (filterOptions.startDate || filterOptions.endDate)
13+
? 'historical'
14+
: 'latest'
15+
return get(this.web3data, {
16+
endpoint: `${ENDPOINT}/metrics`,
17+
subendpoint,
18+
filterOptions
19+
}).then(onFulfilled, onError)
20+
}
21+
}
22+
23+
module.exports = Blockchain

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports.ADDRESSES_ENDPOINT = '/addresses'
2020
module.exports.TOKENS_ENDPOINT = '/tokens'
2121
module.exports.TRANSACTIONS_ENDPOINT = '/transactions'
2222
module.exports.BLOCKS_ENDPOINT = '/blocks'
23+
module.exports.BLOCKCHAINS_ENDPOINT = '/blockchains'
2324
module.exports.CONTRACTS_ENDPOINT = '/contracts'
2425
module.exports.SIGNATURES_ENDPOINT = '/signatures'
2526
module.exports.MARKET_ENDPOINT = '/market'

src/web3data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Token = require('./token')
1212
const Contract = require('./contract')
1313
const Transaction = require('./transaction')
1414
const Block = require('./block')
15+
const Blockchain = require('./blockchain')
1516
const Signature = require('./signature')
1617
const Market = require('./market')
1718
const Eth = require('./eth')
@@ -71,6 +72,7 @@ class Web3DataFactory {
7172
this.contract = new Contract(this)
7273
this.transaction = new Transaction(this)
7374
this.block = new Block(this)
75+
this.blockchain = new Blockchain(this)
7476
this.signature = new Signature(this)
7577
this.market = new Market(this)
7678
}

test/address.test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test, {only} from "ava"
2-
import { ADDRESS, DATES } from "./constants";
2+
import { ADDRESS, TOKEN_ADDRESS, DATES } from "./constants";
33
import {ERROR_MESSAGE_ADDRESS_NO_ADDRESS as NO_ADDRESS} from "../src/constants";
44
import {
55
setUpPolly,
@@ -64,7 +64,7 @@ test('Successfully gets address information', async t => {
6464
t.true(info.hasProp('balance'))
6565
})
6666
test('Successfully gets address information + pricing data', async t => {
67-
const info = await t.context.web3data.address.getInformation(ADDRESS, {includePrice: true, currency: 'usd'})
67+
const info = await t.context.web3data.address.getInformation(TOKEN_ADDRESS, {includePrice: true, currency: 'usd'})
6868
t.true(info.hasProp('balance'))
6969
t.true(info.hasProp('price'))
7070
t.true(info.price.balance.hasProp('currency'))
@@ -178,7 +178,7 @@ test('Successfully gets address transactions - no filters', async t => {
178178
})
179179

180180
test('Successfully gets address transactions - with filters', async t => {
181-
const [transaction] = await t.context.web3data.address.getTransactions(ADDRESS, {includePrice: true})
181+
const [transaction] = await t.context.web3data.address.getTransactions(TOKEN_ADDRESS, {includePrice: true})
182182
t.true(transaction.hasProp('price'))
183183
})
184184

@@ -195,7 +195,7 @@ test('Successfully gets address pending transactions - no filters', async t => {
195195
})
196196

197197
test('Successfully gets address pending transactions - with filters', async t => {
198-
const [pendingTxn] = await t.context.web3data.address.getPendingTransactions(ADDRESS, {includePrice: true})
198+
const [pendingTxn] = await t.context.web3data.address.getPendingTransactions(TOKEN_ADDRESS, {includePrice: true})
199199
t.true(pendingTxn.hasProp('price'))
200200
})
201201

@@ -218,7 +218,7 @@ test('Successfully gets address tokens - no filters', async t => {
218218
})
219219

220220
test('Successfully gets address tokens - with filters', async t => {
221-
const [token] = await t.context.web3data.address.getTokens(ADDRESS, {includePrice: true})
221+
const [token] = await t.context.web3data.address.getTokens(TOKEN_ADDRESS, {includePrice: true})
222222
t.false(token.hasProp('price'))
223223
})
224224

@@ -289,7 +289,7 @@ test('Successfully gets latest address balance (no filters)', async t => {
289289
})
290290

291291
test('Successfully gets latest address balance (with filters)', async t => {
292-
const balance = await t.context.web3data.address.getBalance(ADDRESS, {includePrice: true, currency: 'btc'})
292+
const balance = await t.context.web3data.address.getBalance(TOKEN_ADDRESS, {includePrice: true, currency: 'btc'})
293293
t.true(balance.hasProp('price'))
294294
/* Test that balance is numerical */
295295
t.is(balance.price.balance.currency, 'btc')
@@ -302,7 +302,7 @@ test('Successfully gets historical address balance (no filters)', async t => {
302302
})
303303

304304
test('Successfully gets historical address balance (with filters)', async t => {
305-
const histBalance = await t.context.web3data.address.getBalance(ADDRESS, {startDate: DATES["2019-10-14"], endDate: DATES["2019-10-15"], includePrice: true})
305+
const histBalance = await t.context.web3data.address.getBalance(TOKEN_ADDRESS, {startDate: DATES["2019-10-14"], endDate: DATES["2019-10-15"], includePrice: true})
306306
t.true(histBalance.hasProp('data'))
307307
t.true(Array.isArray(histBalance.data))
308308
t.true(histBalance.metadata.columns.includes('price'))
@@ -324,10 +324,10 @@ test('Successfully gets latest address balance + token balances for multiple add
324324
})
325325

326326
test('Successfully gets latest address balance + token balances + pricing data for multiple addresses ', async t => {
327-
const balances = await t.context.web3data.address.getBalance([ADDRESS, '0xce9af648a831ddf0cd6d05e3fe5787b3c7987246'], {includeTokens: true, includePrice: true})
328-
t.true(balances.hasProp(ADDRESS))
329-
t.true(balances[ADDRESS].hasProp('price'))
330-
t.true(balances[ADDRESS].hasProp('tokens'))
327+
const balances = await t.context.web3data.address.getBalance([TOKEN_ADDRESS, '0xce9af648a831ddf0cd6d05e3fe5787b3c7987246'], {includeTokens: true, includePrice: true})
328+
t.true(balances.hasProp(TOKEN_ADDRESS))
329+
t.true(balances[TOKEN_ADDRESS].hasProp('price'))
330+
t.true(balances[TOKEN_ADDRESS].hasProp('tokens'))
331331

332332
/* Test that balance is numerical */
333333
t.regex(balances[ADDRESS].balance, /[0-9]/)

test/block.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import test from 'ava'
22
import {ERROR_MESSAGE_BLOCK_NO_ID as NO_BLOCK_ID} from '../src/constants'
3+
import { DATES } from './constants'
34

45
import {setUpPolly, getNewWeb3DataInstance} from './utils'
56

@@ -195,7 +196,13 @@ test('Successfully gets block functions - with filters', async t => {
195196
})
196197

197198
/** ********* Test getMetrics() ***********/
198-
test('Successfully gets block metrics', async t => {
199-
const metrics = await t.context.web3data.block.getMetrics(8805274)
199+
test.only('Successfully gets block current metrics', async t => {
200+
const metrics = await t.context.web3data.block.getMetrics()
200201
t.true(metrics.hasProp('issuanceTotal'))
201202
})
203+
204+
test.only('Successfully gets block historical metrics', async t => {
205+
const metrics = await t.context.web3data.block.getMetrics({startDate: 1583625600000, endDate: 1583712000000})
206+
t.true(metrics.hasProp('records'))
207+
t.true(metrics.records[0].hasProp('issuanceTotal'))
208+
})

test/blockchain.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import test from 'ava'
2+
import {setUpPolly, getNewWeb3DataInstance} from './utils'
3+
4+
/**********************************
5+
* -------- Tests Setup ---------- *
6+
**********************************/
7+
test.before(t => {
8+
t.context.polly = setUpPolly('blockchain')
9+
})
10+
11+
test.after(async t => {
12+
await t.context.polly.stop()
13+
})
14+
15+
test.beforeEach(t => {
16+
t.context.web3data = getNewWeb3DataInstance()
17+
})
18+
19+
/** ********* Test getMetrics() ***********/
20+
test.only('Successfully gets blockchain current metrics', async t => {
21+
const metrics = await t.context.web3data.blockchain.getMetrics()
22+
t.true(metrics.hasProp('sizeTotal'))
23+
})
24+
25+
// NOTE: This feature is not yet available
26+
// test.only('Successfully gets blockchain historical metrics', async t => {
27+
// const metrics = await t.context.web3data.blockchain.getMetrics({startDate: 1583625600000, endDate: 1583712000000})
28+
// t.true(metrics.hasProp('records'))
29+
// t.true(metrics.records[0].hasProp('sizeTotal'))
30+
// })

0 commit comments

Comments
 (0)