First Contract - skanakakorn/eth-nonode GitHub Wiki
Your first contract
Writing your first contract
pragma solidity ^0.4.10;
contract MessageStorage {
string storedData;
event Set(
address indexed _from,
string s
);
function set(string s) {
storedData = s;
Set(msg.sender, s);
}
function get() constant returns (string retVal) {
return storedData;
}
}
This is stored in a file called contract_1st.sol.
Compiling your first contract.
You can compile contract via various means. For now, use Remix IDE. https://remix.ethereum.org/#optimize=false&version=soljson-v0.4.10+commit.f0d539ae.js
Copy the contract code to the window of compiler.
Notice the contract address ? You’ll need it. Save it somewhere.
Verify that contract has been created. On test net, goto https://ropsten.etherscan.io/
Lab 1
- Change message using set method.
- Check if your message is correct using get method.
- Use this contract address with injected web3 0x165bba1094f37203ab46b0107f9050ac84b5cf30. Use get method. What's the string do you get?
- Why is it different from the string you set in other contract?
Interacting with smart contract via mycrypto
Use this link https://mycrypto.com. Access your wallet via MetaMask
Exercise
- What is ABI ?
- Why does a contract have an address ?
- When does a contract generate transaction ?
- What is your transaction ID or number ?
- How do you figure out which block has your transaction ?
Programming Exercise
- Write a Contract called "Math1" that has function multiply. The function would look like this
function multiply(int a, int b) constant returns(int retVal) {
return a * b;
}
- Write a contract called "Math2". Add a function called
setBase(int baseVal)
. This would set multiplication base. Add another function called mulbase(int param). This would multiply param with baseVal and return int. I.E, setBase(3) follow by mulbase(7) would return 21.