-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: dded code examples for Contract (#982).
- Loading branch information
Showing
2 changed files
with
272 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Do not use this; it is only for an example in the docs | ||
|
||
contract MyToken { | ||
event Transfer(address indexed from, address indexed to, uint amount); | ||
|
||
mapping (address => uint256) _balances; | ||
|
||
constructor(uint256 totalSupply) { | ||
emit Transfer(address(0), msg.sender, totalSupply); | ||
_balances[msg.sender] = totalSupply; | ||
} | ||
|
||
// Read-Only Functions | ||
function balanceOf(address owner) public view returns (uint256) { | ||
return _balances[owner]; | ||
} | ||
|
||
function decimals() public pure returns (uint8) { | ||
return 18; | ||
} | ||
|
||
function symbol() public pure returns (string memory) { | ||
return "MyToken"; | ||
} | ||
|
||
// Authenticated Functions | ||
function transfer(address to, uint amount) public returns (bool) { | ||
require(_balances[msg.sender] >= amount, "insufficient token balance"); | ||
_balances[msg.sender] -= amount; | ||
_balances[to] += amount; | ||
emit Transfer(msg.sender, to, amount); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters