美文网首页
20230313Coin合约代码及相关翻译

20230313Coin合约代码及相关翻译

作者: 李荣强Justin | 来源:发表于2023-03-12 13:52 被阅读0次

//SPDX-License-Identifiler: GPL-3.0

pragma solidity ^0.8.18;

contract Coin{

/*

    定义了一个地址类型的公共状态变量叫 minter, 地址类型是一个160位的字节值,不允许任何数学运输操作,适合用于存储合约地址,

    以及存储归属于外部账户的密码对的一半的hash值    minter 的public修饰符号,等同于自动生成了一个允许外部合约访问的函数,并且返回值为address的类型

    效果等同于:

function minter() external view returns (address)  {

return minter;

}

*/

    address public minter;

    /*

    同样的,这样声明的balances 也是一个公共状态变量,它是一个更加复杂的数据结构,类似于一个hash table的数据结构,

    所有的key都默认有一个值为0的value,但是你不能获得所有的keylist ,也不能获得所有的value list,

    这个public关键词这样声明,默认会产生一个get函数,如下

    参数是一个地址类型,而返回值是一个uint类型,你可以使用这个方法获得某个地址帐户的余额function balances(address account) external view returns(uint){

return balances[account];

}

*/

    mapping(address =>uint)public balances;

    /*

    这里声明了一个事件Sent,你可以每当send方法调用的时候,他就会触发这个事件,会将from to amount 这三个数据通过

    在前端定义监听器实现的方式得到这些数据,从而实现对交易的追踪,并且并不需要花费很多gas费*/

    event Sent(address from,address to,uint amount);

    /*

    构造函数是当前合约对象被创建出来的时候执行一次,不能事后执行,因此他永久性的记录了当时创建者的地址The msg variable (together with tx and block) is a special global variable that contains properties which allow access to the blockchain.

    变量msg以及tx 和block 是一个特殊的全局变量,包含了许多可以供区块链访问的属性数据msg.sender is always the address where the current (external) function call came from.

    msg.sender 就是代表的是当前调用该函数的外部调用者的地址*/

    constructor(){

minter = msg.sender;

    }

/*

The functions that make up the contract, and that users and contracts can call are mint and send.

    函数构成了合约,这些用户和合约可以调用的函数是mint 和send

The mint function sends an amount of newly created coins to another address.

The require function call defines conditions that reverts all changes if not met.

    mint函数将新创建的代币发送给其他的地址,同时require是一个校验函数,定义了必须满足的条件,如果不满足,交易将会被回滚.

In this example, require(msg.sender == minter); ensures that only the creator of the contract can call mint.

In general, the creator can mint as many tokens as they like, but at some point, this will lead to a phenomenon called “overflow”.

    在这个案例中, require(msg.sender == minter)这个语句,确保了只有合约的创建者才可以调用mint函数. 通常情况下,创建者可以创建任意数量的代币,只要他想.

    但是在某种意义上,这也将导致溢出的现象,

Note that because of the default Checked arithmetic, the transaction would revert if the expression balances[receiver] += amount;

overflows, i.e., when balances[receiver] + amount in arbitrary precision arithmetic is larger than the maximum value of uint (2**256 - 1).

This is also true for the statement balances[receiver] += amount; in the function send.

    注意由于有默认的校验算法,这个交易将会被回滚, 如果balances[receiver] += amount;表达式被移除, 如果 balances[receiver] += amount; 这个值是一个任意精度算术

    超过了uint的最大值, 即使 require(msg.sender == minter) 这个判断在 balances[receiver] += amount send 函数中依然是对的.

*/

    function mint(address receiver,uint amount)public {

require(msg.sender == minter);

        balances[receiver] += amount;

    }

/*

Errors allow you to provide more information to the caller about why a condition or operation failed.

Errors are used together with the revert statement.

The revert statement unconditionally aborts and reverts all changes similar to the require function,

but it also allows you to provide the name of an error and additional data which will be supplied to the caller

(and eventually to the front-end application or block explorer) so that a failure can more easily be debugged or reacted upon.

    错误允许你提供更多的信息给调用方, 为什么条件或者操作失败了. 通常error是跟 revert声明是一起用的.

    revert 声明是无条件的撤回,并且回滚所有的变更,类似于require 函数

    然而它同样允许你提供错误的名字和额外的数据给调用方(并且最终反馈到前端应用或者区块浏览器中)

    因此错误会更容易的被测试和重现出来.

*/

    error InsuffientBalance(uint requested,uint avaiable);

    /*

The send function can be used by anyone (who already has some of these coins) to send coins to anyone else.

If the sender does not have enough coins to send, the if condition evaluates to true.

As a result, the revert will cause the operation to fail

while providing the sender with error details using the InsufficientBalance error.

    send 函数可以被任何持有当前币种的人调用,去发送给任何人.如果发送方没有足够的币去发,那么这个if函数的条件就是true

    因此回滚操作将导致当前操作失败,同时给调用方发送错误信息的细节,通过余额不足函数的错误来提供.

*/

    function send(address receiver,uint amount)public {

if (amount > balance[msg.sender])

revert InsuffientBalance({

requested:amount,

available: balances[msg.sender]

});

        balances[msg.send] -= amount;

        balances[receiver] += amount;

        emit Sent(msg.sender,receiver,amount);

    }

}

相关文章

网友评论

      本文标题:20230313Coin合约代码及相关翻译

      本文链接:https://www.haomeiwen.com/subject/yjafrdtx.html