赞
踩
精度的意义在于允许发送小数的代币。举例,一个CAT代币合约的精度为6。那么 你拥有1个CAT就意味着合约中的balance = 1 * 10^6 , 转账 0.1CAT出去的话,就需要输入 0.1*10^6 = 10^5。 也就时在涉及代币时,查询到的余额、转账的代币数量 都和 代币合约的精度挂钩
ERC20 合约默认的精度为18,其余精度需要自己override重写
示例合约:
合约实现简单的mint 功能,但是精度=18,也就是在mint时,数量应该为:amount * 10^18
// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.19; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract QWER is ERC20 { address public owner; constructor(string memory name, string memory symbol) payable ERC20(name,symbol) { owner = msg.sender; } function mint( address receiver, uint256 amount ) public { _mint(receiver, amount * (10 **18)); } }
mint 1 个代币到特定账户:https://sepolia.etherscan.io/tx/0xa7c847f5295e40cd47443a9de6d7b3e0fe62125fff11fd3a8ae0dba898dc0b51
合约中显示的余额:
钱包中显示的余额,钱包会自动进行精度的除法:
有偿写各种逻辑合约,solidity move 语言都可以
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。