Skip to content

Commit b5cb256

Browse files
committed
updated readme with new changes for setting up config/connections.json for node connection by network. Added endpoints to get block and transaction information as well as syncing
1 parent a381a0f commit b5cb256

File tree

10 files changed

+282
-64
lines changed

10 files changed

+282
-64
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ typings/
6060
# contracts after truffle compile
6161
#token-contract/build
6262

63+
# Config for adding network connections to nodes
64+
config/connections.json
65+
!config/.gitkeep
66+
6367
# network wallets as json files with keystore and password each
6468
network-wallets/*
6569
!network-wallets/.gitkeep

README.md

Lines changed: 153 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,64 @@
11
# Threshodl Token
22

3+
## Table of Contents
4+
5+
- [Quick Start](#quick-start)
6+
- Getting Started
7+
- [Run Private Test Node](#run-private-test-node)
8+
- [Config](#config)
9+
- [Environment Variables](#environment-variablese)
10+
- [Truffle Token Contract](#truffle-token-contract)
11+
- [Development local node](#development-local-node)
12+
- [Testnet node](#testnet-node)
13+
- [Run truffle commands](#run-truffle-commands)
14+
- [Server](#server)
15+
- [Adding Networks](#adding-networks)
316
- [Notes](#notes)
4-
17+
- [Scripts](#scripts)
18+
- [Bash](#bash)
19+
- [Helpers](#helpers)
20+
- [Keystore](#keystore)
21+
- [Error Debugging](#error-debugging)
22+
- [Sample Contracts](#sample-contracts)
23+
- [Version Control]($version-control)
524
- [Endpoint Notes](#endpoint-notes)
6-
25+
- [Get Block Info](#get-block-info)
26+
- [Get Transaction Info](#get-transaction-info)
27+
- [Send Transaction](#send-transaction)
728
- [List of All API Endpoints](#endpoints)
829

30+
## Quick Start
31+
32+
Install [Ganache](http://truffleframework.com/ganache/) to have a local blockchain and start it. You will see 10 address and a mnemonic phrase. Copy `RPC Server` URL, e.g `HTTP://127.0.0.1:7545`
33+
34+
```
35+
brew cask install ganache
36+
```
37+
38+
Create `.env` in root
39+
40+
```
41+
NODE_URL=http://127.0.0.1:7545
42+
```
43+
44+
Clone repo and start server
45+
46+
```
47+
git clone git@github.com:ar-to/ethereum-api.git
48+
cd ethereum-api
49+
npm install
50+
npm start
51+
```
52+
in new terminal
53+
```
54+
curl http://localhost:3000/api/eth/syncing
55+
{"nodeSynced":true}
56+
```
957

1058
## Getting Started
1159

12-
Please read notes for important information before developing.
60+
Please read [notes](#notes) for important information before developing.
61+
1362

1463
### Run Private Test Node
1564

@@ -20,44 +69,36 @@ brew cask install ganache
2069
```
2170
Open application and it will automatically create 10 addresses. Save the mnemonic phrase if you want to use it again. It will be set the url to `http://127.0.0.1:7545`
2271

23-
### Environment Variables
24-
25-
Create `.env` file in the root directory and add the following:
72+
### Config
73+
Add connections to `config/connections.json`, and set the name of the network you want to use for the API using `"connectApi": "network-name",`
2674

2775
```
28-
#
29-
# Required by API
30-
# to connect to correct node
31-
# add NODE_URL values for different networks but have only one uncommented for use
32-
# token contract address required for instantiating contract to use its functions
33-
# owner address required when /api/balance
34-
#
35-
36-
# Ganache GUI local Private node
37-
#NODE_URL=http://127.0.0.1:7545
38-
#TOKEN_CONTRACT_ADDRESS=0x4c59b696552863429d25917b52263532af6e6078
39-
#OWNER_ACCOUNT=0x451E62137891156215d9D58BeDdc6dE3f30218e7
40-
41-
# Ropsten
42-
NODE_URL=http://127.0.0.1:8545
43-
TOKEN_CONTRACT_ADDRESS=0x3e672122bfd3d6548ee1cc4f1fa111174e8465fb
44-
OWNER_ACCOUNT=0x83634a8eaadc34b860b4553e0daf1fac1cb43b1e
76+
{
77+
"networks": {
78+
"connectApi": "ganache",
79+
"ganache":{
80+
"url": "http://127.0.0.1:7545",
81+
"token": {
82+
"ownerAddress": "0x451E62137891156215d9D58BeDdc6dE3f30218e7",
83+
"tokenContractAddress": "0x4c59b696552863429d25917b52263532af6e6078",
84+
"migrateContractAddress": "0xcf068555df7eab0a9bad97829aa1a187bbffbdba"
85+
}
86+
},
87+
"ropsten":{},
88+
"mainnet":{}
89+
}
90+
}
91+
```
4592

46-
#
47-
# Reference
48-
# add variables below for potential use
49-
# migration address is here for reference and used by truffle
50-
#
93+
Copy the same parameters from ganache above to other networks.
5194

52-
GANACHE_MIGRATE_ADDRESS=0xcf068555df7eab0a9bad97829aa1a187bbffbdba
53-
ROPSTEN_MIGRATE_ADDRESS=0xa8ebf36b0a34acf98395bc5163103efc37621052
54-
```
95+
### Environment Variables
5596

56-
Currently, the best way to change the network you want to have the API connect web3 to, is to change the `NODE_URL`. So you can have multiple values but only have one uncommented. The example below will make the second url enabled.
97+
You can change the port for the server and overwrite the node url from the connections.json by adding it in a `.env` file at the root directory.
5798

5899
```
59-
#NODE_URL=http://127.0.0.1:7545
60-
NODE_URL=http://127.0.0.1:8545
100+
PORT=4000
101+
NODE_URL=http://127.0.0.1:7545
61102
```
62103

63104
**Remember to restart your server after changing environment variables. If running nodemon run `npm run nodemon` again**
@@ -81,7 +122,9 @@ First add the same OWNER_ACCOUNT address from the environment variable into the
81122
}
82123
```
83124

84-
#### Testnet node (e.g. Ropsten, etc)
125+
#### Testnet node
126+
127+
(e.g. Ropsten, etc)
85128

86129
See section on adding new networks
87130

@@ -204,7 +247,10 @@ bash bin/truffle-migrate-ropsten
204247

205248
Scripts are added for convenience to allow commands to be performs from the root and for testing.
206249

207-
- rpc-call - a curl command via an rpc call to communicated to a node. Remember to change the url:port to connect to the correct node and address when using the `eth_getBalance` method.
250+
- rpc-call - a curl command via an rpc call to communicated to a node. Remember to change the url:port to connect to the correct node and address when using the `eth_getBalance` method or pass it as an argument:
251+
```
252+
sh bin/rpc-call http://127.0.0.1:7545
253+
```
208254

209255
- bash bin/truffle-compile
210256

@@ -261,13 +307,25 @@ gas cost estimation = 0.0449887 ether
261307

262308
Read about what is a [keystore](https://medium.com/@julien.m./what-is-an-ethereum-keystore-file-86c8c5917b97) file and how to make one. Above keystore taken from [here](https://github.com/hashcat/hashcat/issues/1228) If using the geth client for creating your node, you can run `geth account new`, enter your password and check the `keystore/` directory for the keystore file. This directory is next to the chaindata where you are storing the blockchain data.
263309

310+
## Error Debugging
311+
312+
Setting up can be somewhat complicated if not tedious. Maybe in future releases there will be much more efficient way to setup but for now fixing and understanding error is best to guarantee everything is running correctly. Below are a few warnings or error you may see and possible ways to solve them. More added as they are found. Keep in mind error do not necessarily mean bugs, but it does not mean a bug is not present. If an error turns out to be a bug file an issue and your solution. Thanks.
313+
314+
- `Invalid JSON RPC response: ""` - This may show as a response from testing the api endpoint the first time you are connecting to a node. It is a failure to connect by web3 provider. Solution:
315+
1. restart your server
316+
2. Check you have the correct node url inside the `.env` file
317+
3. Make an RPC call to the node using the `bin/rpc-call` script to test for connection. See [scripts section](#scripts) for details
318+
4. wait until the connection is successful and try the endpoint you got the error again
319+
320+
321+
264322
## Sample Contracts
265323

266324
Sample constracts are used for reference and can be found in `token-contract/samples`.
267325

268326
- advancedToken.sol - sample token code that has been fixed to meet solidity v0.4.23. This can be tested by adding it into Mist app. Deploy using TokenERC20 token name.
269327

270-
## VC
328+
## Version Control
271329
Question about storing artifact files (e.g. Token.json) after truffle compiles contracts with abi and contract addresses for different networks. Community in gitter (chat app) recommended to add to VC but optional not to.
272330

273331
- [stackexchange](https://ethereum.stackexchange.com/questions/19486/storing-a-truffle-contract-interface-in-version-control) - recommendation to store outside
@@ -277,14 +335,28 @@ Question about storing artifact files (e.g. Token.json) after truffle compiles c
277335

278336
transferOwnership & kill functions both catch an 'invalid address'. The response it 200 with `false` boolean indicating request failed.
279337

338+
`api/eth/tx-from-block/hashStringOrNumber` endpoint seems to fail when connected to Ropsten Testnet but not with ganache local node
339+
340+
280341
## Endpoint Notes
281342

282-
### Send Transaction info
343+
The ethereum endpoint for this API uses the [web3js](http://web3js.readthedocs.io/en/1.0/) so parameters for a web3 method is normally supported by the endpoint unless otherwise specified.
344+
345+
### Get Block Info
346+
347+
The following endpoints can be used to get information about blocks and even transactions.
348+
349+
- `/api/eth/tx:txHash` : get transaction details by the transaction hash
350+
- `api/eth/tx-from-block/hashStringOrNumber` : hashStringOrNumber can be same as in the [web3 docs](http://web3js.readthedocs.io/en/1.0/web3-eth.html#gettransactionfromblock), e.g. `/latest` or `0xa343d89bb0447c8a22c8ce73bf35504d9363e234b2a1a8229d40b69ce3439fc5`. See [bug](#bugs-to-fix) for problem when using it in Ropsten
351+
352+
- `api/eth/block/2?showTx=true&useString=latest` : 1 is the blocknumber, the query `showTx` will show the transaction object in the output when set to true, and the query `useString` will overwrite the block number and use one of the strings used in the [getBlock api](http://web3js.readthedocs.io/en/1.0/web3-eth.html#getblock) such as `latest` to get the most recent block.
353+
354+
### Get Transaction info
283355

284356

285357
You can get information about the transaction you want to send before sending it. Information like gas, gas price, amount in wei and ether, etc.
286358

287-
send the transaction object to the `api/eth/send-tx-info` endpoint
359+
send a POST request with transaction object below to the `api/eth/send-tx-info` endpoint
288360
```
289361
{
290362
"from": "0x451E62137891156215d9D58BeDdc6dE3f30218e7",
@@ -460,6 +532,12 @@ output
460532
"GET"
461533
]
462534
},
535+
{
536+
"path": "/api/eth/syncing",
537+
"methods": [
538+
"GET"
539+
]
540+
},
463541
{
464542
"path": "/api/eth/balance/:address",
465543
"methods": [
@@ -472,6 +550,24 @@ output
472550
"GET"
473551
]
474552
},
553+
{
554+
"path": "/api/eth/block/:blockNumber",
555+
"methods": [
556+
"GET"
557+
]
558+
},
559+
{
560+
"path": "/api/eth/tx/:transactionHash",
561+
"methods": [
562+
"GET"
563+
]
564+
},
565+
{
566+
"path": "/api/eth/tx-from-block/:hashStringOrNumber",
567+
"methods": [
568+
"GET"
569+
]
570+
},
475571
{
476572
"path": "/api/eth/create-account",
477573
"methods": [
@@ -484,6 +580,24 @@ output
484580
"GET"
485581
]
486582
},
583+
{
584+
"path": "/api/eth/send-tx-info",
585+
"methods": [
586+
"POST"
587+
]
588+
},
589+
{
590+
"path": "/api/eth/send-tx",
591+
"methods": [
592+
"POST"
593+
]
594+
},
595+
{
596+
"path": "/api/eth/wallet",
597+
"methods": [
598+
"GET"
599+
]
600+
},
487601
{
488602
"path": "/api-endpoints",
489603
"methods": [

bin/rpc-call

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#!/usr/bin/env bash
22

3-
curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}' http://127.0.0.1:7545
3+
#
4+
# ARG1 is the url of node to connect to
5+
# e.g. sh bin/rpc-call http://127.0.0.1:7545
6+
#
7+
ARG1=$1
8+
echo $ARG1
9+
10+
curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}' $ARG1
11+
# curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}' $ARG1
412

513
#
614
# balance returns a hexadecimal

config/.gitkeep

Whitespace-only changes.

server/app/controllers/api-ethereum.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ module.exports = {
66
getTest: function (req, res, next) {
77
res.send("Ethereum API")
88
},
9+
isSyncing: function (req, res, next) {
10+
ethereum.isSyncing().then((value) => {
11+
return (value.error != null ? res.status(404).send(value).end() : res.send(value));
12+
});
13+
},
914
getBalance: function (req, res, next) {
1015
if (req.params) {
1116
let address = "";
@@ -27,10 +32,22 @@ module.exports = {
2732
});
2833
},
2934
getBlock: function (req, res, next) {
30-
ethereum.getBlock(req.params.blockNumber).then((value) => {
35+
let showTx = req.query.showTx != null ? req.query.showTx : false;
36+
let useString = req.query.useString != null ? req.query.useString : null;
37+
ethereum.getBlock(req.params.blockNumber, showTx, useString).then((value) => {
3138
return res.send(value)
3239
})
3340
},
41+
getTransaction: function (req, res, next) {
42+
ethereum.getTransaction(req.params.transactionHash).then((value) => {
43+
return res.send(value)
44+
});
45+
},
46+
getTransactionFromBlock: function (req, res, next) {
47+
ethereum.getTransactionFromBlock(req.params.hashStringOrNumber).then((value) => {
48+
return res.send(value)
49+
});
50+
},
3451
createAccount: function (req, res, next) {
3552
ethereum.createAccount().then((value) => {
3653
return res.send(value)

server/app/controllers/api-token.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
});
2020
},
2121
getOwnerAddressBalance: async function (req, res, next) {
22-
let owner = process.env.OWNER_ACCOUNT;
22+
let owner = token.network.ownerAddress;
2323
await token.getBalance(owner).then((value) => {
2424
return res.send(value)
2525
});

0 commit comments

Comments
 (0)