- Truffle Quick Start Guide
- Nikhil Bhaskar
- 204字
- 2021-06-25 20:47:34
Writing our first smart contract
To start writing our first smart contract, we must do the following:
- Head over to https://github.com/PacktPublishing/Truffle-Quick-Start-Guide and clone it. That's where the fully working projects for each chapter are. You can git clone the entire repository to keep as a reference when working through this chapter.
- To ensure we follow proper formatting and camel-case naming conventions, replace Migrations.sol with the contents of https://github.com/PacktPublishing/Truffle-Quick-Start-Guide/blob/master/chapter1/contracts/Migrations.sol.
- Unfortunately, the original Migrations.sol file is poorly formatted and uses snake-case variable naming, so this step is necessary
- Create a new file under /contracts, called TaskMaster.sol.
- This is where your logic for instantiating your to-do list contract and rewarding a doer will live.
Start by creating the constructor, and define an array to hold the current TodoCoin balances of everyone in the contract:
pragma solidity ^0.4.17;
contract TaskMaster {
mapping (address => uint) public balances; // balances of everyone
function TaskMaster() public {
balances[msg.sender] = 10000;
}
}
Notice how we hardcode a value of 10000 to the initiator of this contract. Let's go with this for now. We can think of this to mean that the initiator of the TaskMaster contract starts off with a balance of 10000 TodoCoins.