Truffle Framework: npm install -g truffle@4.1.13
Ganache: Download and install the GUI Client
Truffle is the framework we'll use to help us easily manage and deploy our Solidity codes. Ganache runs a local instance of the Ethereum network for our testing purposes.
Note that since Truffle and Solidity are quite version sensitive, doing this tutorial with a different Truffle version other than might break.
When Ganache is started, you will see 10 Ethereum addresses automatically generated for you. These are the wallets that we can test against later.
By default, Ganache starts at http://127.0.0.1:7545. Take note of this address.
Let’s create a new folder for our demo project and initialize Truffle Framework inside. Open up your terminal and type:
You should see a couple of files being created by Truffle in the demo folder.
Open up the demo project in your favorite editor, create a new file called Demo.sol inside the contracts folder. Paste the following
codes into Demo.sol:
Here, we have a very simple smart contract where it has a contribute function that takes in Ether and update its balance.
In order to deploy our Demo contract to our test Ethereum network on Ganache, we need a migration script.
Inside the migrations folder, create a new file called 2_demo_migration.js. The 2_ prefix is important so that Truffle knows it’s the step 2 of all the migration steps for this project. Paste the following codes:
Now we are ready to deploy to Ganache for testing. Let’s do it! Open up your terminal and type:
Once it’s done, let’s check our Ganache. You should see a few transactions being recorded:
2 contracts are generated by our deployment due to us having 2 migrations, with 1 being the default Truffle Framework’s migration.
For our Democontract, we need to check the second transaction from the top (circled in blue). Click on it, and copy the contract
address (circled below) for our next step.
Now that the Demo contract is deployed, we want to play around with it. Truffle provides a nice console tool for this purpose. Open
up your terminal and type:
Once you are inside the console, type Demo (the name of our contract) and you should see some data dump on our contract. But that’s not very useful, let’s type these instead into your terminal:
Here, the 0x85365158Ed31cF2d8D9E8c00708981d9419eA5B6E is the contract address that you needed to copy in Step 6. We assigned this instance of the Demo contract to the variable dm so that we can manipulate the contract easily afterwards.
First you’ll need to copy one of the wallet address in Ganache. Let’s take the third wallet for this:
Now to transfer the money from our wallet to our Demo smart contract, we will need to run this command in the console:
Here we are sending 10000000000000000000 wei (or 10 ether) from our third wallet to the smart contract. You should see some data dump on screen if it’s successful.
Note: 1 ether = 1018 wei. See: https://etherconverter.online/
Next let’s check our balance, do you see 10000000000000000000?