代币中的decimal精度代表了什么

精度的意义在于允许发送小数的代币。举例,一个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 语言都可以

相关推荐
穗余16 小时前
hermes agent出现Empty response原因和解决方案
人工智能·web3·区块链
IvorySQL18 小时前
PostgreSQL 18.4、17.10、16.14、15.18、14.23 版本正式发布
数据库·postgresql·区块链
穗余19 小时前
2026 AI x Web3 School共学营笔记-Day7
人工智能·web3·区块链
Shota Kishi19 小时前
用 Solana Block Analyzer 以 slot 为单位观察区块生成节奏:区块时间、验证者与 True TPS
rpc·架构·区块链
明豆20 小时前
以太坊智能合约生产实战 — 安全 · Gas 优化 · 链上监控
安全·区块链·智能合约
多年小白20 小时前
科创50暴涨+5.88%创历史新高
人工智能·ai·金融·区块链·能源
Richown1 天前
密码学入门:区块链中的密码学原理
区块链·react
Richown2 天前
Web3钱包:钱包集成与签名验证
区块链·react
Richown2 天前
跨链桥接:多链资产转移实现
区块链·react