First, you need to create a directory for your app. Name the directory altcoin. Inside the altcoin directory, run this command to initialize your project:
truffle init
Once completed, you'll have a project structure with the following items:
- contracts: The directory where truffle expects to find Solidity contracts.
- migrations: The directory to place files that contain contract deployment code.
- test: The location of test files to test your smart contracts.
- truffle.js: The main truffle configuration file.
By default, truffle init gives you a set of example contracts (MetaCoin and ConvertLib), which act like a simple altcoin built on top of ethereum.
Here is the source code of the MetaCoin smart contract just for reference:
pragma Solidity ^0.4.4;
import "./ConvertLib.sol";
contract MetaCoin {
mapping (address => uint) balances;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
function MetaCoin() {
balances[tx.origin] = 10000;
}
function sendCoin(address receiver, uint amount) returns(bool sufficient) {
if (balances[msg.sender] < amount) return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
Transfer(msg.sender, receiver, amount);
return true;
}
function getBalanceInEth(address addr) returns(uint){
return ConvertLib.convert(getBalance(addr),2);
}
function getBalance(address addr) returns(uint) {
return balances[addr];
}
}
MetaCoin assigns 10 k metacoins to the account address that deployed the contract. 10 k is the total amount of bitcoins that exists. Now this user can send these metacoins to anyone using the sendCoin() function. You can find the balance of your account using getBalance()anytime. Assuming that one metacoin is equal to two ethers, you can get the balance in ether using getBalanceInEth().
The ConvertLib library is used to calculate the value of metacoins in ether. For this purpose, it provides the convert() method.