项目作者: DataHighway-DHX

项目描述 :
Inter-Chain Bridged Token Asset Mining
高级语言: JavaScript
项目地址: git://github.com/DataHighway-DHX/mining.git
创建时间: 2020-01-29T21:15:10Z
项目社区:https://github.com/DataHighway-DHX/mining

开源协议:GNU General Public License v3.0

下载


Data Highway Inter-Chain Bridged Token Asset Mining

Table of Contents

Credits

Setup

Clone the repo

  1. git clone https://github.com/DataHighway-com/mining
  2. cd ./mining

Install Dependencies

Switch to relevant Node.js version using NVM and install dependencies

  1. nvm use v11.6.0

Note: Otherwise may get error like no matching constructor for initialization of 'v8::String::Utf8Value'

  1. npm install

Install Truffle and Test Framework with Ganache CLI (previously Ethereum TestRPC)

  1. npm install -g truffle ganache-cli

Latest beta (see https://github.com/trufflesuite/ganache-cli/releases)

  1. npm uninstall ganache-cli -g
  2. npm install ganache-cli@beta -g

Configure Blockchain

Run Ethereum Client (in separate Terminal tab)

Delete DB folder if starting fresh

  1. rm -rf ./db

Create DB folder

  1. mkdir db && mkdir db/chain_database

Start Ethereum Blockchain Protocol Node Simulation that will be served on http://localhost:8545

Note:

  • 0xce31EeD26ff009f1F5e38408571ea174c5d54f20 is Ethereum address of seed 0x209c205f333b5a65cc428589a51bd9f2621e2fc01de1b02dbf8c0f0b68e4974e
  • 0xe66628e37eFE36098c148d2a3B970074999E95C6 is Ethereum address of seed 0x0edb559026c8f779be17b4c9d8e4dfc14bead6592241de4d6612f77769327f7f
  • These keys are defined in ./helpers/constants.js
    1. ganache-cli \
    2. --account="0x209c205f333b5a65cc428589a51bd9f2621e2fc01de1b02dbf8c0f0b68e4974e, 50471238800000000000" \
    3. --account="0x0edb559026c8f779be17b4c9d8e4dfc14bead6592241de4d6612f77769327f7f, 100471238800000000000" \
    4. --unlock "0x209c205f333b5a65cc428589a51bd9f2621e2fc01de1b02dbf8c0f0b68e4974e" \
    5. --unlock "0x0edb559026c8f779be17b4c9d8e4dfc14bead6592241de4d6612f77769327f7f" \
    6. --port 8545 \
    7. --hostname localhost \
    8. --seed '0x209c205f333b5a65cc428589a51bd9f2621e2fc01de1b02dbf8c0f0b68e4974e' \
    9. --debug true \
    10. --mem true \
    11. --mnemonic 'end sleep vote expire arctic magic crack wrap toddler lizard acoustic owner' \
    12. --db './db/chain_database' \
    13. --verbose \
    14. --networkId=3 \
    15. --gasLimit=7984452 \
    16. --gasPrice=20000000000;

Compile and Migrate Contracts onto Network of choice (i.e. “development”) defined in truffle.js

Compile
  • Compile Contract Latest - truffle compile (only changes since last compile)
  • Compile Contract Full - truffle compile --compile-all (full compile)
Migrate
  • Run Migrations Latest - truffle migrate
  • Run Migrations Full (Ropsten) - truffle migrate --reset --network ropsten
  • Run Migrations Full (Development) - truffle migrate --reset --network development
  • Run Contracts from specific Migration - truffle migrate -f <number>
  • Run Migration on specific network called ‘live’ defined in truffle.js - truffle migrate --network live

Note: If you get error Could not find suitable configuration file. then you’re running the command in the wrong directory.
Note: If the above results in a time-out, then instead run the following to try and uncover any other errors:

  1. truffle develop
  2. truffle(develop)> migrate --reset

References:

Build DApp Front-end

Build Artifacts (requires Default or Custom Builder such as Webpack to be configured)

  1. npm run build

(same as truffle build)

Run DApp Node.js Server & Interact

Terminal 3 - Install & Run MongoDB

macOS

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

  1. brew tap mongodb/brew
  2. brew install mongodb-community@4.2
  3. brew services start mongodb-community@4.2

Terminal 1 - Run Server

Drop DB. Build App and Run Dev Server:

  1. npm run drop; npm run dev

Open open http://localhost:8080 in browser

Terminal 2 - Interact using cURL

  • Send request to server and receive response for authentication and authorisation to access specific API endpoints.
    • Register. JWT provided in response (i.e. {"token":"xyz"})
      1. curl -v POST http://localhost:7000/users/auth/register -d "network=ethereum-testnet-local&publicAddress=0x123&email=ltfschoen@gmail.com&password=123456&name=Luke" -H "Content-Type: application/x-www-form-urlencoded"
      2. curl -v POST http://localhost:7000/users/auth/register -d '{"network": "ethereum-testnet-local", "publicAddress": "0x123", "email":"ltfschoen@gmail.com", "password":"123456", "name":"Luke"}' -H "Content-Type: application/json"
    • Fetch the Nonce if it exists for given Public Address. Nonce provided in response (i.e. {"nonce":"123"})
      1. curl -v GET http://localhost:7000/users/show?network='ethereum-testnet-local&publicAddress=0x123'
    • Sign in using signature verification. JWT provided in response (i.e. {"token":"xyz"})
      1. curl -v POST http://localhost:7000/users/auth/login -d "network='ethereum-testnet-local'&publicAddress=0x123&signature=0x456&email=ltfschoen@gmail.com&password=123456" -H "Content-Type: application/x-www-form-urlencoded"
      2. curl -v POST http://localhost:7000/users/auth/login -d '{"network": "ethereum-testnet-local", "publicAddress": "0x123", "signature": "0x456", "email":"ltfschoen@gmail.com", "password":"123456"}' -H "Content-Type: application/json"
    • Access a restricted endpoint by providing JWT
      1. curl -v GET http://localhost:7000/users/list -H "Content-Type: application/json" -H "Authorization: Bearer <INSERT_TOKEN>"
    • Create user by providing JWT
      1. curl -v POST http://localhost:7000/users/create --data '[{"network": "ethereum-testnet-local", "publicAddress": "0x123", "signature": "0x456", "email":"test@fake.com", "name":"Test"}]' -H "Content-Type: application/json" -H "Authorization: JWT <INSERT_TOKEN>"
      2. curl -v POST http://localhost:7000/users/create -d "network='ethereum-testnet-local'&publicAddress=0x123&signature=0x456&email=test2@fake.com&name=Test2" -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: JWT <INSERT_TOKEN>"

Example 2:

  • Within the DApp transfer say 10 wei to Account No. 0x0000000000000000000000000000000000000000000000000000000000000001 that we created on Ethereum TestRPC

  • Check Account Balances from Terminal by loading External JavaScript file:

  1. truffle exec './scripts/checkAllBalances.js’

Example 2:

  1. $ truffle develop
  2. truffle(develop)> compile
  3. truffle(develop)> migrate
  4. truffle(develop)> exec ./scripts/deployMxc.js

OR truffle exec ./scripts/deployMxc.js --network development

Watch

Watch for changes to contracts, app and config files. Rebuild app upon changes.

  1. truffle watch

Reference

Test

  1. truffle test
  2. truffle test ./path/to/test/file.js

OR

  1. $ truffle develop
  2. truffle(develop)> test

Linter

Run Linter:

  1. npm run lint

Truffle Interactive Console (REPL)

Run REPL on specified network and log communication between Truffle and the RPC

  1. truffle console --network development --verbose-rpc

Try the following commands

  1. web3
  2. // Show existing MXCToken accounts
  3. web3.eth.accounts
  4. i.e.
  5. [ '0xce31EeD26ff009f1F5e38408571ea174c5d54f20',
  6. '0xe66628e37eFE36098c148d2a3B970074999E95C6' ]
  7. web3.eth.blockNumber

Remix

Refer to Remix

References

FAQ

  • Question: Why do I get the following error when running truffle test: Transaction was not mined within 50 blocks, please make sure your transaction was properly sent. Be aware that it might still be mined!?
    • Answer: Running Ganache CLI with --blockTime 3 mines 1 block every 3 seconds,
      whereas if the blockTime option is omitted then blocks are mined instantly.
  • Question: Why do I get the following error when running truffle test: sender doesn't have enough funds to send tx. The upfront cost is: 1134439500000000000 and the sender's account only has: 320739879999999999?
    • Answer: When running Ganache CLI, provide more ETH to the default accounts (i.e. --account="0x0000000000000000000000000000000000000000000000000000000000000001, 50471238800000000000" \ provides 50 ETH to that account.) or restart the Ganache CLI