Custom implementation of basic ERC20 token using OpenZeppelin library
Check if Node.js and truffle are installed by typing
node -v
and truffle version
#BACKEND#
-
Initialize truffle (development environment)
truffle init
-
Set up Truffle environment in
truffle-config.js
-
Set up the migration script in
2-deploy_migration.js
to deploy the contract -
Deploy MerxToken by running
truffle migrate
-
Run
truffle compile
to compile the token code -
Run Test 1 (
truffle test
, see Tests) -
Create MerxToken.js and run Test 2 (
truffle test
, see Tests) -
Reason for
AssertionError: error message must contain revert
: Ganache has implemented a workaround to forward the reason of a failedrequire
but other clients will fail with a regular exception.
This is good for ganache because truffle is able to show the exact cause of an require
failure. But the feature is not portable because other clients didn't implement a similar feature.
#FRONTEND#
-
Run
npm init
to create apackage.json
file -
Run
npm install
, add lite-server dependencies -
Run
npm run dev
(lite-server) -
Create
bs-config.json
file for lite-server -
Create css folder for css, js and index.html files. /minified css, js, bootstrap and jquery/
-
Import
web3
,truffle-contract
libraries and createapp.js
Web3 allows our app to communicate with blockchain. MetaMask injects into HHTPprovider that allows our browser to talk to the blockchain. -
Setup MetaMask.Choose import account->select type
private key
and paste private key from one of Ganache addresses. It will look like(://localhost:7545)
-
Install GETH, full ETH node on our machine.(for Win10) Type
geth account new
, pressenter
, create a password. Start it by typinggeth --rpc
To check the GETH version, typegeth version
-
We are using
Rinkeby
test network.To start Geth on Rinkeby, typegeth --rinkeby --rpc --rpcapi="personal,eth,network,web3, net" --ipcpath ~/AppData/Roaming/Ethereum
(for Win10) In a separate window, typegeth attach http://localhost:8545
. Once you get a greeting message ("Welcome to the Geth JS console!") , typeeth.syncing
to see the progress. -
Type
geth --rinkeby account new
. Copy the address of the created account. Request ETH from the faucet if needed. -
Run
geth attach http://localhost:8545
again and check your accounts by typingeth.accounts
. To specify the account, typeeth.accounts[0]
and to check balance, typeeth.getBalance(eth.accounts[0])
-
Add rinkeby configuration to
truffle-config.js
-
Run Ganache
-
Run
eth.getBalance(eth.accounts[0])
and typepersonal.unlockAccount(eth.accounts[0], null, 1200)
//null is the password, 1200 is amount of time to unlock the account -
Type
truffle migrate --reset --compile-all --network rinkeby
-
Deploy the contract on the Rinkeby network. Run
geth attach http://localhost:8545
. In the console, typevar admin=eth.accounts[0]
,admin
,
var tokensAvailable = 75000
,tokensAvailable
. -
Describe the token to web3 - ABI file. Copy
MerxToken.js
ABI (can minify it as well, or use Prettier JSON plugin) and paste it invar abi ="........."
-
Tell web3 the token address.
var tokenAddress =
......(copy from MerxToken.json)
Don`t confuse it with tokenSaleAddress! -
var TokenContract = web3.eth.contract(abi)
(we got web3 object) -
var tokenInstance = TokenContract.at(tokenAddress)
(we got token instance ) -
Type
tokenInstance.name()
(we should receive "MerxToken"as a result, meaning we deployed our contract succesfully) -
tokenInstance.balanceOf(admin)
// 1000000tokenInstance.balanceOf(tokenSaleAddress)
// 750000 andtokenInstance.transfer(tokenSaleAddress, tokensAvailable, {from:admin})
//asynchronous call, wait for the transaction to finish -
Type
~/AppData/Roaming/Ethereum
(for Win10), selectkeystore
to see json file associated with each account. Upload json file to MetaMask. -
Run
npm run dev
-
Create
deployfrontend.sh
anddocs
folder. Makedeployfrontend.sh
executable and deploy it. All of our file appear in the docs folder. -
In your GitHub repo, go to
settings
-->source
-->selectmaster branch/docs folder
-
Test 1.
truffle console
MerxToken.deployed().then(function(i) {token=i;})
token.address
//result '0x113317147297363C39928CAbD1dFDc986ce9030c' -
Test 2. Please see
MerxToken.js
andMerxTokenSale.js
Runtruffle test
to test.