Last active
January 14, 2018 05:05
-
-
Save hm0429/a96455b48d51c8eceee11b76a9a04043 to your computer and use it in GitHub Desktop.
かんたんな暗号通貨(トークン)のサンプル
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
// ERC20 準拠のオリジナル暗号通貨(トークン)を作成します。 | |
// ERC20 Standard - https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md | |
// コンパイラのバージョンを指定します。 | |
pragma solidity ^0.4.19; | |
// Github から OpenZeppelin の StandardToken.sol をインポートします。 | |
// OpenZeppelin は、Ethereum スマートコントラクトの開発を簡単にしてくれる素敵なライブラリです。 | |
import "https://github.com/OpenZeppelin/zeppelin-solidity/contracts/token/StandardToken.sol"; | |
// コントラクトを宣言します。 | |
// StandardToken を継承しています。 | |
contract EasyToken is StandardToken { | |
// 状態変数を宣言します。 | |
string public name; // トークンの名称 | |
string public symbol; // トークンのシンボル | |
uint8 public decimals = 18; // トークンの小数点以下の桁数 | |
uint256 public cap; // トークンの最大発行量 | |
uint256 public rate; // トークンと Ether のレート | |
address internal wallet; // 新規トークン発行時に Ether を受け取るアドレス | |
// コンストラクタです。 | |
function EasyToken( | |
uint256 initialSupply, // トークンの初期発行量 | |
uint256 tokenCap, // トークンの最大発行量 | |
string tokenName, // トークンの名称 | |
string tokenSymbol, // トークンのシンボル | |
uint256 tokenRate, // トークンと Ether のレート | |
address bankWallet // 新規トークン発行時に Ether を受け取るアドレス | |
) public { | |
totalSupply = initialSupply * 10 ** uint256(decimals); | |
cap = tokenCap * 10 ** uint256(decimals); | |
// 初期発行分のトークンは、コントラクトをデプロイしたアカウントに渡します。 | |
balances[msg.sender] = totalSupply; | |
name = tokenName; | |
symbol = tokenSymbol; | |
rate = tokenRate; | |
wallet = bankWallet; | |
Transfer(address(0), msg.sender, totalSupply); | |
} | |
// 送付されたEtherの量に応じて、トークンを新たに発行するメソッドです。 | |
function issueToken(address beneficiary) payable public { | |
require(beneficiary != 0x0); | |
uint256 tokenAmount = msg.value.mul(rate); | |
if (totalSupply.add(tokenAmount) > cap) { | |
// totalSupply に tokenAmount を加えた値が、cap を超えていた場合、 | |
// このトランザクションで行われた処理がなかったことになります。 | |
// 送付された Ether も返金されます。(gasは消費します。) | |
revert(); | |
} else { | |
totalSupply = totalSupply.add(tokenAmount); | |
balances[beneficiary] = balances[beneficiary].add(tokenAmount); | |
Transfer(address(0), beneficiary, tokenAmount); | |
wallet.transfer(msg.value); | |
} | |
} | |
// fallback 関数です。 | |
// Ether がこのコントラクトに送付されたときに実行されます。 | |
function () payable public { | |
issueToken(msg.sender); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment