区块链开发必备(2)——IDE,编写测试代码及调试

  • Wade
  • 更新于 2022-11-10 19:25
  • 阅读 2863

5分钟基于以太坊发一个属于自己的代币,仅需4步

入门福利:5分钟基于以太坊发一个属于自己的代币,仅需4步。 需要准备:

  1. 钱包:推荐使用MetaMask
  2. 原生代币:ETH。测试建议使用测试网:ropstenkovanrinkeby
  3. 合约代码:open source, 本文最下面也有合约代码
  4. 编译及发布工具:推荐remix

正文开始:

1. IDE:

○ web 网页客户端:remix ■ 简单的代码测试,debug都可以

image.png ○ 桌面客户端:任何你熟悉的ide:vscode,sublime,webstorm ■ 编译器:trufflehardhat ■ 以太坊本地区块链节点:ganache-cli

2. Demo: https://gitee.com/klg-blockchain/smart_contract_demo

○ 项目结构: ■

image.png ○ 简答合约验证

truffle console
Hello.deployed().then((instance)=>{app = instance})
app.setGreeting('Hello world')
app.greet()

image.png ○ 单元测试

image.png

image.png

3. 调试

○ web 网页客户端:remix image.png

○ VScode 客户端 ■ 安装solidity debugger 插件 ■ 安装dotnet sdk

image.png

solidity 学习文档

smart contract erc20 token demo:


pragma solidity ^0.4.22;

contract Token {
    // token总量,默认会为public变量生成一个getter函数接口,名称为totalSupply().
    uint256 public totalSupply;
    /// 获取账户_owner拥有token的数量
    function balanceOf(address _owner) public view returns (uint256 balance);
    //从消息发送者账户中往_to账户转数量为_value的token
    function transfer(address _to, uint256 _value) returns (bool success);
    //从账户_from中往账户_to转数量为_value的token,与approve方法配合使用
    function transferFrom(address _from, address _to, uint256 _value) returns (bool success);
    //消息发送账户设置账户_spender能从发送账户中转出数量为_value的token
    function approve(address _spender, uint256 _value) returns (bool success);
    //获取账户_spender可以从账户_owner中转出token的数量
    function allowance(address _owner, address _spender) public view returns (uint256 remaining);
    //发生转账时必须要触发的事件
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    //当函数approve(address _spender, uint256 _value)成功执行时必须触发的事件
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

contract StandardToken is Token {
    mapping (address => uint256) balances;
    mapping (address => mapping (address => uint256)) allowed;
    function transfer(address _to, uint256 _value) returns(bool success) {
        //默认totalSupply 不会超过最大值 (2^256 - 1).
        //如果随着时间的推移将会有新的token生成,则可以用下面这句避免溢出的异常
        //require(balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]);
        require(balances[msg.sender] >= _value);
        balances[msg.sender] -= _value; //从消息发送者账户中减去token数量_value
        balances[_to] += _value; //往接收账户增加token数量_value
        Transfer(msg.sender, _to, _value); //触发转币交易事件
        return true;
    }
    function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
        require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value);
        balances[_to] += _value; //接收账户增加token数量_value
        balances[_from] -= _value; //支出账户_from减去token数量_value
        allowed[_from][msg.sender] -= _value; //消息发送者可以从账户_from中转出的数量减少_value
        Transfer(_from, _to, _value); //触发转币交易事件
        return true;
    }
    function balanceOf(address _owner) public view returns (uint256 balance) {
        return balances[_owner];
    }
    function approve(address _spender, uint256 _value) returns (bool success) {
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);
        return true;
    }
    function allowance(address _owner, address _spender) public view returns (uint256 remaining) {
        return allowed[_owner][_spender]; //允许_spender从_owner中转出的token数
    }
}

contract HumanStandardToken is StandardToken {
    /* Public variables of the token */
    string public name; //名称: eg Simon Bucks
    uint256 public decimals; //最多的小数位数,How many decimals to show. ie. There could 1000 base units with 3 decimals. Meaning 0.980 SBX =980 base units. It's like comparing 1 wei to 1 ether.
    string public symbol; //token简称: eg SBX
    string public version = "V8";    //版本

    // 转换
    function formatDecimals(uint256 _value) internal returns (uint256 ) {
        return _value * 10 ** decimals;
    }

    constructor(uint256 _initialAmount, string _tokenName, uint256 _decimalUnits, string _tokenSymbol) public {
        name = _tokenName; // token名称
        decimals = _decimalUnits; // 小数位数
        symbol = _tokenSymbol; // token简称
        totalSupply = formatDecimals(_initialAmount); //设置初始总量
        balances[msg.sender] = totalSupply; //初始token数量给予消息发送者
    }
    /* Approves and then calls the receiving contract */
    function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) {
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);
        require(_spender.call(bytes4(bytes32(sha3("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData));
        return true;
    }
}
点赞 5
收藏 3
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
Wade
Wade
CTO of Footprint Analytics (https://www.footprint.network/) | data scientist | crypto investor | ex-degen