Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Insight API] Integration (WiP) #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ db/testnet/blocks

README.html
public

/nbproject/
104 changes: 52 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# Insight API Dash
# Insight API PIVX

A Dash blockchain REST and web socket API service for [Bitcore Node Dash](https://github.com/dashpay/bitcore-node-dash).
A PIVX blockchain REST and web socket API service for [Bitcore Node PIVX](https://github.com/PIVX-Project/bitcore-node-pivx).

This is a backend-only service. If you're looking for the web frontend application, take a look at https://github.com/dashpay/insight-ui-dash.
This is a backend-only service. If you're looking for the web frontend application, take a look at https://github.com/PIVX-Project/insight-ui-pivx.

## Getting Started

```bashl
npm install -g bitcore-node-dash@latest
bitcore-node-dash create mynode
npm install -g bitcore-node-pivx@latest
bitcore-node-pivx create mynode
cd mynode
bitcore-node-dash install insight-api-dash
bitcore-node-dash start
bitcore-node-pivx install insight-api-pivx
bitcore-node-pivx start
```

The API endpoints will be available by default at: `http://localhost:3001/insight-api-dash/`
The API endpoints will be available by default at: `http://localhost:3001/insight-api-pivx/`

## Prerequisites

- [Bitcore Node Dash 3.x](https://github.com/dashpay/bitcore-node-dash)
- [Bitcore Node PIVX 3.x](https://github.com/PIVX-Project/bitcore-node-pivx)

**Note:** You can use an existing Dash data directory, however `txindex`, `addressindex`, `timestampindex` and `spentindex` needs to be set to true in `dash.conf`, as well as a few other additional fields.
**Note:** You can use an existing PIVX data directory, however `txindex`, `addressindex`, `timestampindex` and `spentindex` needs to be set to true in `pivx.conf`, as well as a few other additional fields.

## Notes on Upgrading from v0.3

Expand Down Expand Up @@ -125,15 +125,15 @@ Or disabled entirely with:

### Block
```
/insight-api-dash/block/[:hash]
/insight-api-dash/block/00000000a967199a2fad0877433c93df785a8d8ce062e5f9b451cd1397bdbf62
/insight-api-pivx/block/[:hash]
/insight-api-pivx/block/00000000a967199a2fad0877433c93df785a8d8ce062e5f9b451cd1397bdbf62
```

### Block Index
Get block hash by height
```
/insight-api-dash/block-index/[:height]
/insight-api-dash/block-index/0
/insight-api-pivx/block-index/[:height]
/insight-api-pivx/block-index/0
```
This would return:
```
Expand All @@ -146,8 +146,8 @@ which is the hash of the Genesis block (0 height)

### Raw Block
```
/insight-api-dash/rawblock/[:blockHash]
/insight-api-dash/rawblock/[:blockHeight]
/insight-api-pivx/rawblock/[:blockHash]
/insight-api-pivx/rawblock/[:blockHeight]
```

This would return:
Expand All @@ -161,7 +161,7 @@ This would return:

Get block summaries by date:
```
/insight-api-dash/blocks?limit=3&blockDate=2016-04-22
/insight-api-pivx/blocks?limit=3&blockDate=2016-04-22
```

Example response:
Expand Down Expand Up @@ -195,31 +195,31 @@ Example response:

### Transaction
```
/insight-api-dash/tx/[:txid]
/insight-api-dash/tx/525de308971eabd941b139f46c7198b5af9479325c2395db7f2fb5ae8562556c
/insight-api-dash/rawtx/[:rawid]
/insight-api-dash/rawtx/525de308971eabd941b139f46c7198b5af9479325c2395db7f2fb5ae8562556c
/insight-api-pivx/tx/[:txid]
/insight-api-pivx/tx/525de308971eabd941b139f46c7198b5af9479325c2395db7f2fb5ae8562556c
/insight-api-pivx/rawtx/[:rawid]
/insight-api-pivx/rawtx/525de308971eabd941b139f46c7198b5af9479325c2395db7f2fb5ae8562556c
```

### Address
```
/insight-api-dash/addr/[:addr][?noTxList=1][&from=&to=]
/insight-api-dash/addr/mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5?noTxList=1
/insight-api-dash/addr/mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5?from=1000&to=2000
/insight-api-pivx/addr/[:addr][?noTxList=1][&from=&to=]
/insight-api-pivx/addr/mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5?noTxList=1
/insight-api-pivx/addr/mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5?from=1000&to=2000
```

### Address Properties
```
/insight-api-dash/addr/[:addr]/balance
/insight-api-dash/addr/[:addr]/totalReceived
/insight-api-dash/addr/[:addr]/totalSent
/insight-api-dash/addr/[:addr]/unconfirmedBalance
/insight-api-pivx/addr/[:addr]/balance
/insight-api-pivx/addr/[:addr]/totalReceived
/insight-api-pivx/addr/[:addr]/totalSent
/insight-api-pivx/addr/[:addr]/unconfirmedBalance
```
The response contains the value in Satoshis.

### Unspent Outputs
```
/insight-api-dash/addr/[:addr]/utxo
/insight-api-pivx/addr/[:addr]/utxo
```
Sample return:
```
Expand Down Expand Up @@ -250,21 +250,21 @@ Sample return:
### Unspent Outputs for Multiple Addresses
GET method:
```
/insight-api-dash/addrs/[:addrs]/utxo
/insight-api-dash/addrs/2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f/utxo
/insight-api-pivx/addrs/[:addrs]/utxo
/insight-api-pivx/addrs/2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f/utxo
```

POST method:
```
/insight-api-dash/addrs/utxo
/insight-api-pivx/addrs/utxo
```

POST params:
```
addrs: 2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f
```

### InstantSend Transactions
### SwiftTX Transactions
If a Transaction Lock has been observed by Insight API a 'txlock' value of true will be included in the Transaction Object.

Sample output:
Expand All @@ -290,25 +290,25 @@ Sample output:

### Transactions by Block
```
/insight-api-dash/txs/?block=HASH
/insight-api-dash/txs/?block=00000000fa6cf7367e50ad14eb0ca4737131f256fc4c5841fd3c3f140140e6b6
/insight-api-pivx/txs/?block=HASH
/insight-api-pivx/txs/?block=00000000fa6cf7367e50ad14eb0ca4737131f256fc4c5841fd3c3f140140e6b6
```
### Transactions by Address
```
/insight-api-dash/txs/?address=ADDR
/insight-api-dash/txs/?address=mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL
/insight-api-pivx/txs/?address=ADDR
/insight-api-pivx/txs/?address=mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL
```

### Transactions for Multiple Addresses
GET method:
```
/insight-api-dash/addrs/[:addrs]/txs[?from=&to=]
/insight-api-dash/addrs/2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f/txs?from=0&to=20
/insight-api-pivx/addrs/[:addrs]/txs[?from=&to=]
/insight-api-pivx/addrs/2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f/txs?from=0&to=20
```

POST method:
```
/insight-api-dash/addrs/txs
/insight-api-pivx/addrs/txs
```

POST params:
Expand Down Expand Up @@ -355,7 +355,7 @@ Note: if pagination params are not specified, the result is an array of transact
### Transaction Broadcasting
POST method:
```
/insight-api-dash/tx/send
/insight-api-pivx/tx/send
```
POST params:
```
Expand All @@ -382,7 +382,7 @@ POST response:
### Budget Proposal List
GET method:
```
/insight-api-dash/gobject/list/proposal
/insight-api-pivx/gobject/list/proposal
```

Sample output:
Expand All @@ -395,7 +395,7 @@ Sample output:
payment_amount: 5,
start_epoch: 1482105600,
type: 1,
url: 'https://www.dash.org'
url: 'https://pivx.org/'
},
AbsoluteYesCount: 40,
YesCount: 40,
Expand All @@ -406,8 +406,8 @@ Sample output:
### Budget Proposal Detail
GET method:
```
/insight-api-dash/gobject/get/[:hash]
/insight-api-dash/gobject/get/b6af3e70c686f660541a77bc035df2e5e46841020699ce3ec8fad786f7d1aa35
/insight-api-pivx/gobject/get/[:hash]
/insight-api-pivx/gobject/get/b6af3e70c686f660541a77bc035df2e5e46841020699ce3ec8fad786f7d1aa35
```

Sample output:
Expand All @@ -422,7 +422,7 @@ Sample output:
payment_amount: 5,
start_epoch: 1482105600,
type: 1,
url: 'https://www.dash.org'
url: 'https://pivx.org/'
},
CreationTime: 1482223714,
FundingResult: {
Expand Down Expand Up @@ -454,17 +454,17 @@ Sample output:

### Historic Blockchain Data Sync Status
```
/insight-api-dash/sync
/insight-api-pivx/sync
```

### Live Network P2P Data Sync Status
```
/insight-api-dash/peer
/insight-api-pivx/peer
```

### Status of the Bitcoin Network
```
/insight-api-dash/status?q=xxx
/insight-api-pivx/status?q=xxx
```

Where "xxx" can be:
Expand All @@ -477,7 +477,7 @@ Where "xxx" can be:

### Utility Methods
```
/insight-api-dash/utils/estimatefee[?nbBlocks=2]
/insight-api-pivx/utils/estimatefee[?nbBlocks=2]
```


Expand All @@ -497,7 +497,7 @@ Sample output:
}
```

`txlock`: InstantSend transaction received from network, this event is published alongside the 'tx' event when a transaction lock event occurs. Data will be a app/models/Transaction object.
`txlock`: SwiftTX transaction received from network, this event is published alongside the 'tx' event when a transaction lock event occurs. Data will be a app/models/Transaction object.
Sample output:
```
{
Expand Down Expand Up @@ -555,7 +555,7 @@ html
})
socket.on(eventToListenTo, function(data) {
if (data.txlock) {
console.log("New InstantSend transaction received: " + data.txid)
console.log("New SwiftTX transaction received: " + data.txid)
} else {
console.log("New transaction received: " + data.txid)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/addresses.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var bitcore = require('bitcore-lib-dash');
var bitcore = require('bitcore-lib-pivx');
var async = require('async');
var TxController = require('./transactions');
var Common = require('./common');
Expand Down
86 changes: 40 additions & 46 deletions lib/blocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var async = require('async');
var bitcore = require('bitcore-lib-dash');
var bitcore = require('bitcore-lib-pivx');
var _ = bitcore.deps._;
var pools = require('../pools.json');
var BN = bitcore.crypto.BN;
Expand Down Expand Up @@ -74,7 +74,7 @@ BlockController.prototype.block = function(req, res, next) {
return self.common.handleErrors(err, res);
}
var blockResult = self.transformBlock(block, info);
self.getBlockReward(blockResult.previousblockhash, function(err,reward) {
self.getBlockReward(self.node.services.bitcoind.height, function(err,reward) {
if (err) {
return self.common.handleErrors(err, res);
}
Expand Down Expand Up @@ -357,52 +357,46 @@ BlockController.prototype.getPreviousBlock = function(prevHash, cb) {
});
};

BlockController.prototype.getBlockReward = function(prevHash, cb) {
var self = this;
var nSubsidyHalvingInterval = 210240;
var nBudgetPaymentsStartBlock = 100000;
var nSubsidyBase;
BlockController.prototype.getBlockReward = function(nHeight) {
var nSubsidy = 0;
var COIN = 100000000;
var nLastPOWBlock = 259200;

// block reward is based on the previous block diff / height
self.getPreviousBlock(prevHash, function(err, info) {
if(err) cb(err, null);

var dDiff = info.difficulty;
var nPrevHeight = info.height;

if (nPrevHeight < 5465) {
// Early ages...
// 1111/((x+1)^2)
nSubsidyBase = (1111.0 / (Math.pow((dDiff+1.0),2.0)));
if(nSubsidyBase > 500) nSubsidyBase = 500;
else if(nSubsidyBase < 1) nSubsidyBase = 1;
} else if (nPrevHeight < 17000 || (dDiff <= 75 && nPrevHeight < 24000)) {
// CPU mining era
// 11111/(((x+51)/6)^2)
nSubsidyBase = (11111.0 / (Math.pow((dDiff+51.0)/6.0,2.0)));
if(nSubsidyBase > 500) nSubsidyBase = 500;
else if(nSubsidyBase < 25) nSubsidyBase = 25;
} else {
// GPU/ASIC mining era
// 2222222/(((x+2600)/9)^2)
nSubsidyBase = (2222222.0 / (Math.pow((dDiff+2600.0)/9.0,2.0)));
if(nSubsidyBase > 25) nSubsidyBase = 25;
else if(nSubsidyBase < 5) nSubsidyBase = 5;
}

var nSubsidy = nSubsidyBase;

// yearly decline of production by ~7.1% per year, projected ~18M coins max by year 2050+.
for (var i = nSubsidyHalvingInterval; i <= nPrevHeight; i += nSubsidyHalvingInterval) {
nSubsidy -= nSubsidy/14;
}

// Hard fork to reduce the block reward by 10 extra percent (allowing budget/superblocks)
var nSuperblockPart = (nPrevHeight > nBudgetPaymentsStartBlock) ? nSubsidy/10 : 0;
var reward = nSubsidy - nSuperblockPart;

cb(null, parseFloat(reward.toString(10)).toFixed(8));
});
if (nHeight === 0) {
nSubsidy = 60001 * COIN;
} else if (nHeight < 86400 && nHeight > 0) {
nSubsidy = 250 * COIN;
} else if (nHeight < 151200 && nHeight >= 86400) {
nSubsidy = 225 * COIN;
} else if (nHeight <= nLastPOWBlock && nHeight >= 151200) {
nSubsidy = 45 * COIN;
} else if (nHeight <= 302399 && nHeight > nLastPOWBlock) {
nSubsidy = 45 * COIN;
} else if (nHeight <= 345599 && nHeight >= 302400) {
nSubsidy = 40.5 * COIN;
} else if (nHeight <= 388799 && nHeight >= 345600) {
nSubsidy = 36 * COIN;
} else if (nHeight <= 431999 && nHeight >= 388800) {
nSubsidy = 31.5 * COIN;
} else if (nHeight <= 475199 && nHeight >= 432000) {
nSubsidy = 27 * COIN;
} else if (nHeight <= 518399 && nHeight >= 475200) {
nSubsidy = 22.5 * COIN;
} else if (nHeight <= 561599 && nHeight >= 518400) {
nSubsidy = 18 * COIN;
} else if (nHeight <= 604799 && nHeight >= 561600) {
nSubsidy = 13.5 * COIN;
} else if (nHeight <= 647999 && nHeight >= 604800) {
nSubsidy = 9 * COIN;
} else if (nHeight >= 648000) {
nSubsidy = 4.5 * COIN;
} else {
nSubsidy = 0 * COIN;
}

var parsedFloat = parseFloat(nSubsidy.toString(10));
return parsedFloat.toFixed(8);
};

module.exports = BlockController;
Loading