diff --git a/master_solidity/boilerplates/learning/token.sol b/master_solidity/boilerplates/learning/token.sol new file mode 100644 index 0000000..ff4e272 --- /dev/null +++ b/master_solidity/boilerplates/learning/token.sol @@ -0,0 +1,58 @@ +pragma solidity ^0.5.10; + +contract Token { + // An `address` is comparable to an email address - it's used to identify an account on Ethereum. + // Addresses can represent a smart contract or an external (user) accounts. + // Learn more: https://solidity.readthedocs.io/en/v0.5.10/types.html#address + address public owner; + + // A `mapping` is essentially a hash table data structure. + // This `mapping` assigns an unsigned integer (the token balance) to an address (the token holder). + // Learn more: https://solidity.readthedocs.io/en/v0.5.10/types.html#mapping-types + mapping (address => uint) public balances; + + // Events allow for logging of activity on the blockchain. + // Ethereum clients can listen for events in order to react to contract state changes. + // Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#events + event Transfer(address from, address to, uint amount); + + // Initializes the contract's data, setting the `owner` + // to the address of the contract creator. + constructor() public { + // All smart contracts rely on external transactions to trigger its functions. + // `msg` is a global variable that includes relevant data on the given transaction, + // such as the address of the sender and the ETH value included in the transaction. + // Learn more: https://solidity.readthedocs.io/en/v0.5.10/units-and-global-variables.html#block-and-transaction-properties + owner = msg.sender; + } + + // Creates an amount of new tokens and sends them to an address. + function mint(address receiver, uint amount) public { + // `require` is a control structure used to enforce certain conditions. + // If a `require` statement evaluates to `false`, an exception is triggered, + // which reverts all changes made to the state during the current call. + // Learn more: https://solidity.readthedocs.io/en/v0.5.10/control-structures.html#error-handling-assert-require-revert-and-exceptions + + // Only the contract owner can call this function + require(msg.sender == owner, "You are not the owner."); + + // Enforces a maximum amount of tokens + require(amount < 1e60, "Maximum issuance exceeded"); + + // Increases the balance of `receiver` by `amount` + balances[receiver] += amount; + } + + // Sends an amount of existing tokens from any caller to an address. + function transfer(address receiver, uint amount) public { + // The sender must have enough tokens to send + require(amount <= balances[msg.sender], "Insufficient balance."); + + // Adjusts token balances of the two addresses + balances[msg.sender] -= amount; + balances[receiver] += amount; + + // Emits the event defined earlier + emit Transfer(msg.sender, receiver, amount); + } +}