Skip to content

Commit

Permalink
Add FIAT exchange for addresses and tx, tx improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
yograterol committed Mar 3, 2019
1 parent dc5adb7 commit c4502a0
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 35 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"patch": true,
"patchBlocks": 100,
"bulkSize": 100,
"CMC_API_KEY": "<PUT CMC API KEY HERE>",
"settings": {
"symbol": "CLO",
"name": "Callisto Network",
Expand Down Expand Up @@ -46,7 +47,7 @@
"0x458ddc6a7e924554756f95715a53bf948560ee38": "clo.digipools.org",
"0x3c6b9edb1f8ec6c85436b7cb384eb607489c732f": "vvpool.com",
"0x2a1efdf9f09869a82e5e6b0f3736aabcb5381206": "uleypool.com",
"0xf30a30315d5214e490458d0511595e42b3d917d0": "clopool.maxhash.org"
"0xf30a30315d5214e490458d0511595e42b3d917d0": "maxhash.org"
}
}
}
10 changes: 10 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ var BlockStat = new Schema(
"uncleCount": Number
});

var Market = new Schema({
"symbol": String,
"timestamp": Number,
"quoteBTC": Number,
"quoteUSD": Number
})

// create indices
Transaction.index({timestamp:-1});
Transaction.index({blockNumber:-1});
Expand All @@ -86,17 +93,20 @@ Transaction.index({creates:1, blockNumber:-1});
Account.index({balance:-1});
Account.index({balance:-1, blockNumber:-1});
Block.index({miner:1});
Market.index({timestamp: -1})

mongoose.model('BlockStat', BlockStat);
mongoose.model('Block', Block);
mongoose.model('Account', Account);
mongoose.model('Contract', Contract);
mongoose.model('Transaction', Transaction);
mongoose.model('Market', Market);
module.exports.BlockStat = mongoose.model('BlockStat');
module.exports.Block = mongoose.model('Block');
module.exports.Contract = mongoose.model('Contract');
module.exports.Transaction = mongoose.model('Transaction');
module.exports.Account = mongoose.model('Account');
module.exports.Market = mongoose.model('Market');

mongoose.connect(process.env.MONGO_URI || 'mongodb://localhost/blockDB');

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"express": "^4.16.0",
"mongoose": "^4.10.8",
"morgan": "^1.9.0",
"node-fetch": "^2.3.0",
"rlp": "^2.0.0",
"serve-favicon": "~2.4.5",
"socket.io": "^2.0.4",
Expand Down
6 changes: 5 additions & 1 deletion public/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ img.logo-default {
color: #7f90a4;
}
.eth-stat-title {
font-size: 25px;
font-size: 20px;
text-align: center;
}
.eth-stat-subtitle {
font-size: 14px;
text-align: center;
}
.eth-stat-text {
Expand Down
13 changes: 5 additions & 8 deletions public/views/address.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
</div>
<div class="portlet-body">
<div class="row">
<div class="col-md-6 center">
<div class="col-md-12 center">
<div >
<span class="eth-stat-title">
{{ addr.balance | number: 10 }} </span><br>
<span class="eth-stat-title">{{ addr.balance | number: 4 }} {{ settings.symbol }}</span><br>
<span class="eth-stat-text">
{{ settings.symbol }} Balance <i class="fa fa-question-circle" data-toggle="tooltip" data-placement="right" title="Note: Balance may not reflect transaction data if you have transactions resulting from Contract Internal Transactions. We are working on adding that functionality."></i>
</span><br>
<div ng-if="settings.useEthFiat" class="margin-top-20">
<span class="fade">{{ addr.ethfiat | number: 10 }}</span> <span class="eth-stat-text fade">ETH-F balance
</span> <a href="http://etherscan.io/address/{{addrHash}}" target="_blank"><i class="fa fa-external-link"></i></a>
</div>
</span><br><br>
<span class="fade">{{ addr.balanceUSD | currency : "$" : 4 }}</span> <span class="eth-stat-text fade">USD balance
</span>
<div class="margin-top-20">
{{ addr.mined }} <span class="eth-stat-text">Mined</span>
</div>
Expand Down
2 changes: 1 addition & 1 deletion public/views/simple-summary-stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h3>
<small>Network Hashrate</small>
<h3>
<div class="icon">
<i class="fa fa-tachometer"></i>
<i class="fa fa-tachometer-alt"></i>
</div>
<span data-counter="counterup" data-value="stats.hashrate" class="font-green-title">{{stats.hashrate | totalDifficulty}}/s</span>
</h3>
Expand Down
37 changes: 22 additions & 15 deletions public/views/tx.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,36 @@
<div class="portlet-body">

<table class="col-md-6 table table-hover">
<tr ><td width="25%">Hash</td><td>{{tx.hash}}</td></tr>
<tr ><td width="25%">Status</td><td>
<tr ><td width="25%">Transaction Hash:</td><td>{{tx.hash}}</td></tr>
<tr ><td width="25%">Status:</td><td>
<div class="tx-sucess" ng-if="tx.status == 1">Success</div>
<div class="tx-failed" ng-if="tx.status == 0">Failed</div>
</td></tr>
<tr ><td width="25%">Block Number</td>
<td><a href="/block/{{tx.blockNumber}}">{{tx.blockNumber}}</a></td>
<tr ><td width="25%">Block Number:</td>
<td><a href="/block/{{tx.blockNumber}}">{{tx.blockNumber}}</a>
<i class="fas fa-check-circle"></i>
<ng-pluralize count="tx.confirmations"
when="{'0': 'No block confirmation',
'one': '1 block confirmation',
'other': '{} block confirmations'}">
</ng-pluralize></td>
</tr>
<tr ><td width="25%">Time</td>
<td>{{tx.datetime | date:'yyyy-MM-dd HH:mm:ss Z' }} ({{tx.timestamp | timeDuration}} ago)</td>
<tr ><td width="25%">TimeStamp:</td>
<td><i class="far fa-clock"></i> {{tx.datetime | date:'yyyy-MM-dd HH:mm:ss Z' }} ({{tx.timestamp | timeDuration}} ago)</td>
</tr>
<tr ><td width="25%">From</td>
<tr ><td width="25%">From:</td>
<td><a href="/addr/{{tx.from}}">{{tx.from}}</a></td></tr>
<tr ng-show="tx.to"><td width="25%">To</td>
<td><span ng-show="tx.isTrace">Contract </span><a href="/addr/{{tx.to}}">{{tx.to}}</a></td></tr>
<tr ng-show="tx.creates"><td width="25%">Creates</td>
<td><span ng-show="tx.isTrace">Contract:</span><a href="/addr/{{tx.to}}">{{tx.to}}</a></td></tr>
<tr ng-show="tx.creates"><td width="25%">Creates:</td>
<td><a href="/addr/{{tx.creates}}">{{tx.creates}}</a></td></tr>
<tr ><td width="25%">Value</td><td>{{tx.value}} {{ settings.symbol }}</td></tr>
<tr ><td width="25%">Gas Provided</td><td>{{tx.gas | number}}</td></tr>
<tr ><td width="25%">Gas Used</td><td>{{tx.gasUsed | number}}</td></tr>
<tr ><td width="25%">Gas Price</td><td>{{tx.gasPrice | number}} wei</td></tr>
<tr ><td width="25%">Nonce</td><td>{{tx.nonce}}</td></tr>
<tr ><td width="25%">Input Data</td><td><pre class="code">{{tx.input}}</pre></td></tr>
<tr ><td width="25%">Value:</td><td><span class="font-green-title"><i class="fab fa-ethereum"></i> {{tx.value | number: 4}} {{ settings.symbol }}</span> => {{ tx.valueUSD | currency : "USD $" : 4 }}</td></tr>
<tr ><td width="25%">Transaction fee:</td><td>{{tx.transactionFee | number:6}} {{ settings.symbol }} => {{ tx.transactionFeeUSD | currency : "USD $" : 10 }}</td></tr>
<tr ><td width="25%">Gas Provided:</td><td>{{tx.gas | number}}</td></tr>
<tr ><td width="25%">Gas Used:</td><td>{{tx.gasUsed | number}}</td></tr>
<tr ><td width="25%">Gas Price:</td><td>{{tx.gasPrice | number:9}} {{ settings.symbol }} ({{tx.gasPriceGwei | number}} Gwei)</td></tr>
<tr ><td width="25%">Nonce:</td><td>{{tx.nonce}}</td></tr>
<tr ><td width="25%">Input Data:</td><td><pre class="code">{{tx.input}}</pre></td></tr>

</table>

Expand Down
34 changes: 25 additions & 9 deletions routes/web3relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ var asyncL = require('async');
var abiDecoder = require('abi-decoder');

require( '../db.js' );
var mongoose = require( 'mongoose' );
var Block = mongoose.model( 'Block' );
var Contract = mongoose.model( 'Contract' );
var Transaction = mongoose.model( 'Transaction' );
const mongoose = require( 'mongoose' );
const Block = mongoose.model( 'Block' );
const Contract = mongoose.model( 'Contract' );
const Transaction = mongoose.model( 'Transaction' );
const Market = mongoose.model( 'Market' );

var getLatestBlocks = require('./index').getLatestBlocks;
var filterBlocks = require('./filters').filterBlocks;
Expand Down Expand Up @@ -88,7 +89,7 @@ exports.data = async (req, res) => {
if ("tx" in req.body) {
var txHash = req.body.tx.toLowerCase();

Transaction.findOne({hash: txHash}).lean(true).exec(function(err, doc) {
Transaction.findOne({hash: txHash}).lean(true).exec(async(err, doc) => {
if (err || !doc) {
web3.eth.getTransaction(txHash, function(err, tx) {
if(err || !tx) {
Expand Down Expand Up @@ -116,15 +117,27 @@ exports.data = async (req, res) => {
if (!err && block)
ttx.timestamp = block.timestamp;
ttx.isTrace = (ttx.input != "0x");
res.write(JSON.stringify(ttx));
res.end();
transactionResponse = ttx;
});
}
});
} else {
res.write(JSON.stringify(doc));
res.end();
transactionResponse = doc;
}

const latestPrice = await Market.findOne().sort({timestamp: -1})

const latestBlock = await web3.eth.getBlockNumber();

transactionResponse.confirmations = latestBlock - transactionResponse.blockNumber;
transactionResponse.gasPriceGwei = web3.utils.fromWei(transactionResponse.gasPrice, 'Gwei');
transactionResponse.gasPrice = web3.utils.fromWei(transactionResponse.gasPrice, 'ether');
transactionResponse.transactionFee = transactionResponse.gasPrice * transactionResponse.gasUsed;
transactionResponse.transactionFeeUSD = transactionResponse.transactionFee * latestPrice.quoteUSD;
transactionResponse.valueUSD = transactionResponse.value * latestPrice.quoteUSD;

res.write(JSON.stringify(transactionResponse));
res.end();
});

} else if ("tx_trace" in req.body) {
Expand Down Expand Up @@ -469,6 +482,9 @@ exports.data = async (req, res) => {
// }
}

const latestPrice = await Market.findOne().sort({timestamp: -1})
addrData["balanceUSD"] = addrData.balance * latestPrice.quoteUSD;

res.write(JSON.stringify(addrData));
res.end();
} else if ("block" in req.body) {
Expand Down
64 changes: 64 additions & 0 deletions tools/price.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const _ = require('lodash');
const mongoose = require( 'mongoose' );
const fetch = require("node-fetch");
const https = require('https');
const Market = require( '../db.js' ).Market;

// 10 minutes
const quoteInterval = 10 * 60 * 1000;

const getQuote = async () => {
const options = {
timeout: 10000
}
const URL = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${config.settings.symbol}&CMC_PRO_API_KEY=${config.CMC_API_KEY}`;

try {
let requestUSD = await fetch(URL);
let requestBTC = await fetch(URL + '&convert=BTC');
let quoteUSD = await requestUSD.json();
let quoteBTC = await requestBTC.json();
console.log(quoteUSD)

quoteObject = {
symbol: config.settings.symbol,
timestamp: Math.round(new Date(quoteUSD.status.timestamp).getTime() / 1000),
quoteBTC: quoteBTC.data.CLO.quote.BTC.price,
quoteUSD: quoteUSD.data.CLO.quote.USD.price,
}

new Market(quoteObject).save( ( err, market, count ) => {
console.log(market)
if ( typeof err !== 'undefined' && err ) {
process.exit(9);
} else {
console.log('DB successfully written for market quote.');
}
});
} catch (error) {
console.log(error);
}
}


var config = { nodeAddr: 'localhost', gethPort: 8545, bulkSize: 100 };
try {
var local = require('../config.json');
_.extend(config, local);
console.log('config.json found.');
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
var local = require('../config.example.json');
_.extend(config, local);
console.log('No config file found. Using default configuration... (config.example.json)');
} else {
throw error;
process.exit(1);
}
}

getQuote()

setInterval(() => {
getQuote()
}, quoteInterval);
1 change: 1 addition & 0 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<!-- BEGIN GLOBAL MANDATORY STYLES -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css" />
<link href="/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link href="/plugins/simple-line-icons/simple-line-icons.min.css" rel="stylesheet" type="text/css" />
<link href="/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css" />
Expand Down

0 comments on commit c4502a0

Please sign in to comment.