TAGS

rsk-plant-box

CREATED

UPDATED

STARS

INSTALL

...

REPOSITORY
GitHub Download
Please wait: Fetching readme...

b'# RSK Truffle Plant Box\n\nTruffle box is configured to create a complete dApp using Truffle framework on [RSK Blockchain](https://developers.rsk.co/rsk/), including a user interface to interact with the smart contract.\n\n[RSK](https://www.rsk.co/) is an open source platform for Ethereum compatible smart contracts based on the Bitcoin network.\n\nIt was inspired by [Truffle pet shop box](https://www.trufflesuite.com/boxes/pet-shop). \nThanks, Truffle team :)\n\n## Requirements\n\nThere are a few technical requirements before we start. \nTo use `Truffle boxes`, you need to have installed in your computer:\n\n- Git\n- a POSIX compliant shell\n- cURL\n- Node.js and NPM\n- a code editor\n\nIf you don\'t have any of them installed, go to the tutorial [Truffle boxes prerequisites](https://developers.rsk.co/tutorials/truffle-boxes/truffle-boxes-prerequisites/) which have all the instructions to setup these requirements.\n\n**Truffle framework**\n\nOnce you have those requirements installed, you only need one command to install `Truffle`.\nIt is better to do it globally:\n\n```shell\nnpm install -g truffle\n```\n\n## Installing the Truffle box\n\n1. Create a new folder. \nFor example, create the folder `rsk-plant`.\nNavigate to the folder in the terminal.\n\n```shell\nmkdir rsk-plant\ncd rsk-plant\n```\n\n2. Run the unbox command. \nThis also takes care of installing the necessary dependencies and it can take some time.\n\n```shell\ntruffle unbox rsksmart/rsk-plant-box\n```\n\nThis is the result using Windows OS:\n\n![truffle unbox](/images/image-01.png)\n\n## PlantShop.sol\n\nTake a look at the smart contract `PlantShop.sol`. You can check it out in folder `contracts`.\n\n![PlantShop.sol](/images/image-02.png)\n\nThis smart contract has:\n\n* A variable `buyers` to store an array with 16 posisions to store addresses\n* A function `getBuyers` to return the list of addresses stored at variable `buyers`\n* A function `buy` to update an address at variable `buyers`, in the number of position sent as parameter\n\n## Development console\n\nTruffle has an interactive console that also spawns a development blockchain. This is very useful for compiling, deploying and testing locally.\n\n3. Run the development console. This command is successful if you see a list of 10 accounts, a mnemonic and the command prompt is now `truffle(develop)>`\n\n```shell\ntruffle develop\n```\n\nYou will now be in the truffle develop console with seeded accounts and their associated private keys listed.\n\n```txt\nC:\\RSK\\rsk-plant>truffle develop\n\nTruffle Develop started at http://127.0.0.1:8545/\n\nAccounts:\n(0) 0x1056f747cf4bc7710e178b2aeed4eb8c8506c728\n(1) 0x45a71c00382c2898b5d6fae69a6f7bfe6edab80c\n(2) 0x1596384706dc9ac4cca7f50279a4abe591d6c3fe\n(3) 0x9576d0a496b645baa64f22aceb2328e7468d4113\n(4) 0xd431572eef7d77584d944c1809398a155e89f830\n(5) 0x92c111839718fe0800fadccc67068b40b8524a0f\n(6) 0x6da22b5a027146619bfe6704957f7f36ff029c48\n(7) 0x2c3a82d8c3993f8c80dcaf91025437bd057df867\n(8) 0xc43ae7a44f7deb759177b7093f06512a0a9ff5d7\n(9) 0xe61bf00cd7dce248449cfe58f23a4ef7d542bc0b\n\nPrivate Keys:\n(0) f32f32839fe27ad906b63eafb326f26fed95c231e3c5e33c7cdd08f62db63167\n(1) ebef990088f27f6ef13b5e52a77d5dcc5a76862a701908c586d01b6fe93562b3\n(2) 598ccae5e4436fedeb0e798c0d254789c55a63401ebfc3ae8ddde29634ddfcde\n(3) 09934b80f391e0024b8cb00cd73790fdf64c4d0509e144766414fee317cd3f4e\n(4) ac745b84b6574b5738d364b43e0d471c9d5107504acc709c90f6f091b78c751b\n(5) 449654cde095f2349113ef12a93e139b4302bc95adb3619d08adf53dde9b8847\n(6) c217f12a89c352fc70b5f1bd5742314b4fb1bb1e35cb779fdb3c2390106355db\n(7) 1d4c74dfa4e99e161130c18cc63938bb120a128cefbf1b9188efc678bf5722cb\n(8) 0f44e0becf2e090db498a1b747d2a758fcc81fb0241f350d61117a9c6b1fa82e\n(9) 85218c5eec657470dafeb09e6f7101f91d21bfe822fbeeecfc9275f798662a63\n\nMnemonic: virtual valve razor retreat either turn possible student grief engage attract fiber\n\n\xe2\x9a\xa0\xef\xb8\x8f Important \xe2\x9a\xa0\xef\xb8\x8f : This mnemonic was created for you by Truffle. It is not secure.\nEnsure you do not use it on production blockchains, or else you risk losing funds.\n\ntruffle(develop)>\n```\n\n> Inside the development console we don\'t preface commands with `truffle`.\n\n4. Compile the smart contract. \n\n> To make sure you\'re in the development console, the command prompt must be `truffle(develop)>`\n\n```shell\ncompile\n```\n\nThe `compile output` should be similar to:\n\n![truffle compile](/images/image-03.png)\n\n5. Deploy (migrate) the smart contract. \n\n```shell\nmigrate\n```\n\nAnd the `migrate output` should be similar to:\n\n![truffle migrate](/images/image-04.png)\n\n6. Testing the smart contract.\n\nThis Truffle box also comes with the file `TestPlantShop.js` which include some examples for testing the smart contract. \nYou can check it out in the `test` folder.\n\nRun this command in the development console:\n\n```shell\ntest\n```\n\nThe `test output` should be similar to:\n\n![truffle test](/images/image-05.png)\n\n### Exit Truffle console\n\nIn the Truffle console, enter this command to exit the terminal:\n\n```shell\n.exit\n```\n\n## Using RSK networks\n\nThis Truffle box is already configured to connect to three RSK networks: \n\n1. regtest (local node)\n2. testnet\n3. mainnet\n\nTestnet will be used here. \n\nWe need to do some tasks:\n\n- Setup an account and get R-BTC\n- Update RSK network gas price\n- Connect to an RSK network\n- Deploy in the network of your choose\n\n\n### Setup an account & get R-BTC\n\n1. Create a wallet\n\nThe easy way to setup an account is using a web3 wallet injected in the browser.\nSome options are:\n- [Metamask](https://metamask.io/)\n- [Nifty](https://www.poa.network/for-users/nifty-wallet)\n\nSelect the RSK Network in the web wallet.\n- Nifty: select in the dropdown list\n- Metamask: go to [RSK Testnet](https://developers.rsk.co/wallet/use/metamask/) to configure it in `Custom RPC`\n\n![wallets](/images/image-06.png)\n\nYou can learn more about [account based RSK addresses](https://developers.rsk.co/rsk/architecture/account-based/ "Account based RSK addresses - RSK Developers Portal").\n\nTake a look `truffle-config.js` file to realize that we are using `HDWalletProvider` with RSK Networks derivations path:\n- RSK Testnet dpath: `m/44\xe2\x80\x99/37310\xe2\x80\x99/0\xe2\x80\x99/0`\n- RSK Mainnet dpath: `m/44\xe2\x80\x99/137\xe2\x80\x99/0\xe2\x80\x99/0`\n\nFor more information check [RSKIP57](https://github.com/rsksmart/RSKIPs/blob/master/IPs/RSKIP57.md).\n\n2. Update `.secret` file\n\nAfter create your wallet, update your mnemonic in the file `.secret`, located in the folder project, and save it.\n\n3. Get some R-BTCs:\n- For the RSK Testnet, get tR-BTC from [our faucet](https://faucet.testnet.rsk.co/).\n\n### Setup the gas price\n\n**Gas** is the internal pricing for running a transaction or contract. When you send tokens, interact with a contract, send R-BTC, or do anything else on the blockchain, you must pay for that computation. That payment is calculated as gas. In RSK, this is paid in **R-BTC**.\nThe **minimumGasPrice** is written in the block header by miners and establishes the minimum gas price that a transaction should have in order to be included in that block.\n\nTo update the Testnet **minimumGasPrice** in our project run this query using cURL:\n\n```shell\ncurl https://public-node.testnet.rsk.co/ -X POST -H "Content-Type: application/json" --data \'{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest",false],"id":1}\' > .minimum-gas-price-testnet.json\n```\n\nThis query saved the details of latest block to file .minimum-gas-price-testnet.json \n\nIn the `truffle-config.js`, we are reading the parameter `minimumGasPrice` from this json file.\n\nFor more information about the **Gas** and **minimumGasPrice** please go to the [gas page](https://developers.rsk.co/rsk/rbtc/gas/ "Gas - RSK Developers Portal").\n\n### Connect to RSK Testnet\n\nRun the development console for any RSK network.\n\n```shell\ntruffle console --network testnet\n```\n\n### Test the connection to RSK network\n\nRun this commands in the Truffle console:\n\n**Block number**\n\nShows the last block number.\n\n```javascript\n(await web3.eth.getBlockNumber()).toString()\n```\n**Network ID**\n\nTo get the network ID, run this command:\n\n```javascript\n(await web3.eth.net.getId()).toString()\n```\n\nList of network IDs:\n- mainnet: 30\n- testnet: 31\n- regtest (local node): 33\n\nCheck it out the last steps in this image:\n\n![connect to rsk network](/images/image-07.png)\n\nYou can verify that I get the last block twice, and the block number inscreased, so we conclude that the connection is ok.\n\nExit the Truffle console:\n\n```shell\n.exit\n```\n\n### Migrate the smart contract \n\nWe will do it running the below commands directly in the terminal, without using the Truffle console, to show you this alternative.\n\nOn any of the networks, run this commands in a terminal (not in Truffle console).\nTo use Testnet or Mainnet, you need to specify this using the parameter `-- network`:\n\n```shell\ntruffle migrate --network testnet\n```\n\nThe migrate process in a real blockchain takes more time, because Truffle creates some transactions which need to be mined on the blockchain.\n\n## The dApp\n\nIncluded with the plant-shop Truffle Box was the code for the app\'s front-end. That code exists within the `src` directory.\n\n> Make sure you have selected the RSK testnet in the wallet.\n\n### Running the dev server\n\nNow we\'re ready to use our dapp!\n\nStart the local web server:\n\n```shell\nnpm run dev\n```\n\nThe dev server will launch and automatically open a new browser tab containing your dapp.\n\nIt is running at [http://localhost:3000](http://localhost:3000)\n\n![rsk plant garden](/images/image-11.png)\n\n### Buying plants\n\nIn our garden store, don\'t worry about the prices, the plants are free!\n\nClick the `Get` button on the plant of your choice.\n\nYou\'ll be automatically prompted to approve the transaction by the web wallet. \nClick submit / confirm to approve the transaction.\n\nAfter the transaction is confirmed, you\'ll see the button next to the choosed plant change to show the first characters of the wallet that got the plant and become disabled, just as we specified, because the plant has now been acquired.\n\nCongratulations\xf0\x9f\x91\x8f\xf0\x9f\x91\x8f\xf0\x9f\x91\x8f! You built and ran a complete dApp on RSK network!\n\n### Do you have questions?\n\nAsk in the [RSK chat](https://gitter.im/rsksmart/getting-started).\n\n'