-
-
Save DerayGa/3b7246875610f78914bc19934b5c62f3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| pragma solidity ^0.4.0; | |
| contract BuyNccuCoin { | |
| address public minter; | |
| mapping (address => uint) public balances; | |
| event Sent(address from, address to, uint amount); | |
| function BuyNccuCoin() { | |
| minter = msg.sender; | |
| } | |
| function mint(address receiver, uint amount) returns (bool) { | |
| if (msg.sender != minter) return false; | |
| balances[receiver] += amount; | |
| return true; | |
| } | |
| function _send(address receiver, uint amount) returns (bool) { | |
| if (balances[msg.sender] < amount) return false; | |
| balances[msg.sender] -= amount; | |
| balances[receiver] += amount; | |
| Sent(msg.sender, receiver, amount); | |
| return true; | |
| } | |
| function check() constant returns (uint) { | |
| return balances[msg.sender]; | |
| } | |
| function buy() payable returns (bool) { | |
| if (msg.value <= 0) return false; | |
| //msg.value的單位一定是wei,不論傳什麼進來,都會轉成wei | |
| balances[msg.sender] += (msg.value / 1 ether); | |
| //msg.value所帶的數值,在function被執行之後,會自動被存入balance底下 | |
| //不需要手動輸入 this.send(msg.value) | |
| //contract自殺的時候,要指定收貨幣的人,若不指定,錢就再也沒辦法拿回來了 | |
| return true; | |
| } | |
| function sell(uint howMuchNccuCoin) returns (bool) { | |
| //這邊要做單位換算,可以直接乘除 1 ether 這樣的寫法 | |
| balances[msg.sender] -= howMuchNccuCoin; | |
| if (balances[msg.sender] < 0) { | |
| //出錯就呼叫throw 會rollback整個程式當做沒有執行過 | |
| throw; | |
| } | |
| //從合約送錢給msg.sender(賣貨幣的人) | |
| if (!msg.sender.send(howMuchNccuCoin * 1 ether)) { | |
| throw; | |
| } | |
| return true; | |
| } | |
| function thisContractBalance() constant returns (uint) { | |
| return this.balance / 1 ether; | |
| } | |
| function myEtherBalance() constant returns (uint) { | |
| return msg.sender.balance / 1 ether; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment