You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Sui Move包中实现已铸造RCOIN代币的转账?

实现自定义代币RCOIN的转账功能

你的RCOIN基于Sui标准Coin框架实现,无需依赖原生transfer函数,直接利用sui::coin模块提供的工具即可实现转账。以下是两种常见的转账实现方案:

方案1:直接转移完整Coin对象

添加一个entry函数,用于将持有者手中的完整RCOIN代币对象转移给目标地址:

module fungible_tokens::rcoin {
    use sui::coin::{Self, Coin, TreasuryCap};
    use sui::transfer;
    use sui::tx_context::{Self, TxContext};

    struct RCOIN has drop {}

    /// Register the RCOIN currency to acquire its `TreasuryCap`. Because
    /// this is a module initializer, it ensures the currency only gets
    /// registered once.
    fun init(witness: RCOIN, ctx: &mut TxContext) {
        let treasury_cap = coin::create_currency<RCOIN>(witness, 2, ctx);
        transfer::share_object(treasury_cap)
    }

    public entry fun mint(
        treasury_cap: &mut TreasuryCap<RCOIN>, amount: u64, recipient: address, ctx: &mut TxContext
    ) {
        coin::mint_and_transfer(treasury_cap, amount, recipient, ctx)
    }

    // 新增:转移完整Coin对象
    public entry fun transfer(
        coin: Coin<RCOIN>, recipient: address
    ) {
        coin::transfer(coin, recipient);
    }
}

方案2:拆分指定金额转账

如果需要从现有Coin对象中拆分出指定金额转账(原持有者保留剩余代币),可以添加如下entry函数:

// 新增:拆分指定金额转账
public entry fun transfer_amount(
    coin: &mut Coin<RCOIN>, amount: u64, recipient: address, ctx: &mut TxContext
) {
    // 从原Coin中拆分出指定金额的新Coin对象
    let transferred_coin = coin::split(coin, amount, ctx);
    // 将拆分出的Coin转移给接收方
    coin::transfer(transferred_coin, recipient);
}

使用说明

  • 调用transfer函数时,需传入你持有的完整RCOIN对象和目标接收地址,执行后该Coin对象的所有权会完全转移给接收方。
  • 调用transfer_amount函数时,需传入你的RCOIN对象的可变引用、转账金额、接收地址和交易上下文,执行后仅指定金额的代币会被转移,剩余部分仍保留在原对象中。

内容的提问来源于stack exchange,提问作者Alwaysblue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 00:35:20