Skip to content

Commit e62e87b

Browse files
committed
update searchTransaction on /v1/status
1 parent 06afd27 commit e62e87b

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

db/models/searchTransaction.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,25 @@ SearchTransaction.prototype.read = function(txHash) {
1111
};
1212

1313
SearchTransaction.prototype.create = function(entity) {
14-
return null;
14+
return pool.query('INSERT INTO searchtransaction (txHash, network, createTimestamp, fromAddress, status)' +
15+
' VALUES ($1, $2, now(), $3, $4)',
16+
[entity.txHash, 'testnet', entity.fromAddress, entity.status]).then(function(result) {
17+
return result.rows[0].id;
18+
}).catch(function(err) {
19+
console.log(err.message, err.stack);
20+
return null;
21+
});
1522
};
1623

1724
SearchTransaction.prototype.update = function(entity) {
18-
return null;
25+
return pool.query('UPDATE searchtransaction SET status = $1, WHERE txHash = $2',
26+
[entity.status, entity.txHash]).then(function(result) {
27+
console.log(result);
28+
return true;
29+
}).catch(function(err) {
30+
console.log(err.message, err.stack);
31+
return false;
32+
});
1933
};
2034

2135
SearchTransaction.prototype.delete = function(id) {

routes/api.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
// TODO 這邊還要補齊 /v1/status,與測試程式
22
var express = require('express');
33
var transactionData = require('../db/models/transactionData.js');
4+
var searchTransaction = require('../db/models/searchTransaction.js');
45
var producer = require('../kafka/producer/producer.js');
6+
var Web3 = require('web3');
7+
var web3 = new Web3();
58
var router = express.Router();
69

710
var kafkaTopic = "InsertQueue";
11+
web3.setProvider(new web3.providers.HttpProvider(process.env.ENODE_BASE || 'http://localhost:8545'));
12+
813
/**
914
* 把基本服務的 API 都放在這邊
1015
*/
@@ -46,14 +51,19 @@ router.get('/v1/status', function(req, res, next) {
4651
transactionData.read(req.query.txHash).then(function(result){
4752
console.log(result);
4853
// FIXME 這邊要看狀態把資料丟出去
54+
var transaction = {txHash: req.query.txHash, fromAddress: web3.eth.coinbase};
4955
if (result.rowCount > 0) {
56+
transaction[status] = result.rows[0].status;
57+
searchTransaction.create(transaction);
5058
res.json({'data': {
5159
'txHash': result.rows[0].txHash,
5260
'status': result.rows[0].status,
5361
'txTimestamp': result.rows[0].txTimestamp,
5462
'tx': result.rows[0].transactionHash}
5563
});
5664
} else {
65+
transaction[status] = 'ERROR';
66+
searchTransaction.create(transaction);
5767
res.json({'error': {'message': 'invalid txHash'}});
5868
}
5969
}).catch(function (err) {

routes/ethereum.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ var web3 = new Web3();
55
var router = express.Router();
66

77
web3.setProvider(new web3.providers.HttpProvider(process.env.ENODE_BASE || 'http://localhost:8545'));
8-
var coinbase = web3.eth.coinbase;
98

109
/**
1110
* GET ethereum coinbase
1211
*/
1312
router.get('/coinbase', function(req, res, next) {
14-
res.json({'data' :{'coinbase': coinbase}});
13+
res.json({'data' :{'coinbase': web3.eth.coinbase}});
1514
});
1615

1716
/**
1817
* GET ethereum account balance
1918
*/
2019
router.get('/balance', function(req, res, next) {
20+
var coinbase = web3.eth.coinbase;
21+
if (req.query.address != null && req.query.address != '' && req.query.address != undefined) {
22+
coinbase = req.query.address;
23+
}
2124
var balance = web3.fromWei(web3.eth.getBalance(coinbase), "ether").toString(10) + ' ether';
2225
res.json({'data' :{'coinbase': coinbase, 'balance': balance}});
2326
});
@@ -60,6 +63,18 @@ router.get('/transaction', function(req, res, next) {
6063
res.json({'data' :{"txHash": txHash}});
6164
});
6265

66+
/**
67+
* GET transactionReceipt
68+
* https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgettransactionreceipt
69+
*/
70+
router.get('/transactionReceipt', function(req, res, next) {
71+
var transactionHash = req.query.transactionHash;
72+
if (transactionHash == null || transactionHash == '' || transactionHash == undefined) {
73+
res.json({'error': {'code': 102, 'message': 'transactionHash is null'}});
74+
}
75+
res.json({'data': web3.eth.getTransactionReceipt(transactionHash)});
76+
});
77+
6378
/**
6479
* GET estimate gas
6580
* https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethestimategas

0 commit comments

Comments
 (0)