添加数字货币钱包
- chrome安装MetaMask, 如下图:
- 创建MetaMask账户密码, 然后登录
- 打开metaMask, 在顶部选择测试网络, 然后点击购买,如图:
- 在新的页面中选择测试水管->获取ETH
- 在网页中点击 Request 1 eth from faucet,从而获取一个eth
2. 编辑智能合约
-
打开网站 https://remix.ethereum.org/ -
如上图,点击创建新文件,并编辑智能合约代码(最后附代码) -
智能合约编辑成功之后就可以开始部署了, 点击左边栏第二个按钮,并点击Compile {你的文件名}.sol, 如下图: -
编译成功后,点击左侧第三个按钮,进行部署, 如下图, 选择Injected web3, 选完之后回自动连接你metamask,点击metamask授权操作,获取你的metamask钱包地址 -
点击Deploy右侧按钮,填写需要发布的币种信息,其中: INITIALSUPPLY: 发行量 TOKENNAME: 币种名称 TOKENSYMBOL: 对应SYMBOl -
点击下面的transact->MetaMask确认 即可 -
完成之后, 链上会产生一个区块信息, 如下图, 复制其中的hash信息, 然后去https://ropsten.etherscan.io/查看合约地址信息 其中上图选中的部分就是新生成的合约地址
MetaMask添加新生成的代币
- 打开MetaMask, 添加代币->输入刚刚生成的合约地址 ,如下图:
- 完成之后,钱包账户就会多了刚刚添加的代币,如图:
- 好了,到这里就全部完成了,下面是用来测试的智能合约代码
pragma solidity ^0.4.16;
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }
contract TokenERC20 {
string public name;
string public symbol;
uint8 public decimals = 18; // 18 是建议的默认值
uint256 public totalSupply;
mapping (address => uint256) public balanceOf; //
mapping (address => mapping (address => uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 value);
event Burn(address indexed from, uint256 value);
function TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) public {
totalSupply = initialSupply * 10 ** uint256(decimals);
balanceOf[msg.sender] = totalSupply;
name = tokenName;
symbol = tokenSymbol;
}
function _transfer(address _from, address _to, uint _value) internal {
require(_to != 0x0);
require(balanceOf[_from] >= _value);
require(balanceOf[_to] + _value > balanceOf[_to]);
uint previousBalances = balanceOf[_from] + balanceOf[_to];
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
Transfer(_from, _to, _value);
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}
function transfer(address _to, uint256 _value) public {
_transfer(msg.sender, _to, _value);
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(_value <= allowance[_from][msg.sender]); // Check allowance
allowance[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public
returns (bool success) {
allowance[msg.sender][_spender] = _value;
return true;
}
function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
function burn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] -= _value;
totalSupply -= _value;
Burn(msg.sender, _value);
return true;
}
function burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value);
require(_value <= allowance[_from][msg.sender]);
balanceOf[_from] -= _value;
allowance[_from][msg.sender] -= _value;
totalSupply -= _value;
Burn(_from, _value);
return true;
}
}
|