Teaching material for Basic Ethereum for Developers Workshop
The featured content must be used only for educational purposes. This is a prototype for studying and learning about the development of Dapps (decentralized applications) and should not be used in production.
Mercado Pokémon is a demo of creating coins and Pokémons in Ethereum platform, as well as the trading between different accounts.
Also is presented the concept of a non-infrastructure environment, which uses blockchain as a data repository and acessing dapps directly by a standalone html.
- Geth **
- Truffle (2.1.2)**
- TestRPC (problem with gas for pokemon contracts)
- Solidity Browser
- Ethereum wallet
-
Create the directory structure
mkdir -p private_net/keystore
-
Copy the private key (password: abcd) to the keystore
cp <truffle-mercado-pokemon>/support/UTC--2015-08-09T04-06-35.465758600Z--6a5b342ec71def8aac337b82969d9ddd811023c9 private_net/keystore
-
Start the blockchain with a new genesis
geth --datadir ./private_net init <truffle-mercado-pokemon>/support/private_genesis.json
-
Start the node
geth --fast --cache 512 -ipcpath ~/Library/Ethereum/geth.ipc --networkid 1234 --datadir ./private_net --unlock 0 --rpc --rpccorsdomain="*" --rpcaddr "0.0.0.0"
-
Unlock the account[0] with the password 'abcd'
-
On a new terminal, attach to the geth console
geth attach miner.start(1)
-
Open file
accounts-for-testing.txt
-
Load
contracts/pokecoin.sol
in Ethereum Wallet (contracts > Deploy new contract) and use the demo account addresses from Player1 and Player2 to launch the contract. (2000000 gas) -
Load
contracts/pokecentral.sol
in Ethereum Wallet and use the demo account addresses from Player1 and Player2 to launch the contract. (6000000 gas) -
Write the
Pokecoin
andPokeCentral
addresses on a notepad. -
Load
contracts/pokemarket.sol
no Ethereum Wallet and usePokeCoinAddress
andPokeCentralAddress
to launch the contract. (6000000 gas) -
Write the
Pokemarket
address on a notepad. -
You must update
PokeMarketAddress
in the contractsPokeCoin
ePokeCentral
to enable access. -
Update the variables
PokeCoinAddress
,PokeCentralAddress
ePokeMarketAddress
injavascripts/mercadopokemon.js
-
Load
app/player1.html
andapp/player2.html
in separate windows on Chrome browser.
-
Go to the truffle-mercado-pokemon dir and open the Truffle console on a new terminal
truffle console
-
Run the migrate to load contracts to blockchain
migrate --reset
-
Update vars PokeCoinAddress, PokeCentralAddress and PokeMarketAddress in mercadopokemon.js
Open player1.html and player2.html each one in a new window in Chrome
-
Transfer pokecoins to the players 1 and 2 using the console:
pokecoin.transfer(account1Demo, 5000, {from:web3.eth.accounts[0],gas:2000000}); pokecoin.transfer(account2Demo, 5000, {from:web3.eth.accounts[0],gas:2000000});
5.1 Verify is the transfers were suceeded
pokecoin.balanceOf(account1Demo) // 5000 pokecoin.balanceOf(account2Demo) // 5000
-
Create the next four valid pokemons:
pokecentral.newPokemon(3,500,40, {from:web3.eth.accounts[0],gas:2000000}); pokecentral.newPokemon(1,535,70, {from:web3.eth.accounts[0],gas:2000000}); pokecentral.newPokemon(4,546,80, {from:web3.eth.accounts[0],gas:2000000}); pokecentral.newPokemon(2,557,90, {from:web3.eth.accounts[0],gas:2000000});
2.1 Verify pokemon total
pokecentral.totalPokemonSupply() // 4
2.2 Verify the pokemon owner (wait until mining)
pokecentral.pokemons(0); pokecentral.pokemons(1); pokecentral.pokemons(2); pokecentral.pokemons(3); pokecentral.pokemons(4);
-
Transfer the pokemons to player1 and player2
pokecentral.transferPokemon(web3.eth.accounts[0], account1Demo, 1,{from:web3.eth.accounts[0],gas:2000000}); pokecentral.transferPokemon(web3.eth.accounts[0], account1Demo, 4,{from:web3.eth.accounts[0],gas:2000000}); pokecentral.transferPokemon(web3.eth.accounts[0], account2Demo, 2,{from:web3.eth.accounts[0],gas:2000000}); pokecentral.transferPokemon(web3.eth.accounts[0], account2Demo, 3,{from:web3.eth.accounts[0],gas:2000000});
3.1 Verify the pokemon owner, as in 2.2
-
Update the PokeMarketAddress in dapps PokeCoin e PokeCentral
pokecoin.updatePokeMarketAddress(pokemarket.address, {from:web3.eth.accounts[0],gas:2000000}); pokecentral.updatePokeMarketAddress(pokemarket.address, {from:web3.eth.accounts[0],gas:2000000});
4.1 Verify the addresses
pokecoin.pokeMarketAddress(); pokecentral.pokeMarketAddress();
-
Put the pokemon 1 from account1Demo for selling by 2000 pkc:
pokemarket.newSale(account1Demo, 1, 2000, {from:web3.eth.accounts[0],gas:2000000});
-
Verify the number of active sells:
pokemarket.totalActiveSales();
-
Verify the active sell 1 data:
pokemarket.pokeSales(0);
-
Verify if the pokemon 1 is active for selling:
pokemarket.pokeSelling(1);
-
Buy the pokemon 1, with the player 2
pokemarket.buyPokemon(account2Demo, 1, {from:web3.eth.accounts[0],gas:2000000});
-
Verify if the pokemon owner was changed to account2Demo address
pokecentral.pokemons(1);
-
Verify the pokecoins total for each player
pokecoin.balanceOf(account1Demo); pokecoin.balanceOf(account2Demo);
```
Once the contracts were loaded, we created the PokeCoins and Pokemons, and distributed them between Player1 and Player2 accounts. Through the html files you can put Pokemons for sale and make the purchase.
It is necessary that the account [0] is unlocked and has funds. To do this, type: personal.unlockAccount(eth.accounts[0]) on the console.
If pokecoins are transferred to an account, you can trade them directly in the secondary market, but that wallet must have funds for paying the rsk gas transaction.