Move.

帖子

分享您的知识。

BigDev.
Aug 24, 2025
专家问答

Generic Types in Move for Reusability

Move’s generics enable reusable, type-safe code, critical for DeFi protocols handling multiple token types. Intermediate developers must apply constraints like key and store to ensure compatibility with Sui’s object model. Why are generics useful in Move for building reusable DeFi modules? Write a generic Move module for a vault that can store and withdraw any coin type. What constraints ensure the module works with Sui’s coin system?

module vault::generic_vault {
    use sui::object::{Self, UID};
    use sui::coin::{Self, Coin};
    use sui::balance::{Self, Balance};
    use sui::tx_context::{Self, TxContext};
    use sui::transfer;

    struct Vault has key {
        id: UID,
        balance: Balance,
    }

    // Create vault for any coin type
    public entry fun create_vault(ctx: &mut TxContext) {
        let vault = Vault {
            id: object::new(ctx),
            balance: balance::zero(),
        };
        transfer::share_object(vault);
    }

    // Deposit coins into vault
    public entry fun deposit(vault: &mut Vault, coin: Coin) {
        coin::put(&mut vault.balance, coin);
    }

    // Withdraw coins (simplified to sender)
    public entry fun withdraw(
        vault: &mut Vault,
        amount: u64,
        ctx: &mut TxContext
    ) {
        let coin = coin::take(&mut vault.balance, amount, ctx);
        transfer::public_transfer(coin, tx_context::sender(ctx));
    }
}
  • Move
5
3
分享
评论
.

答案

3
jakodelarin.
Aug 24 2025, 20:17

The Vault struct is generic over type T with key + store constraints, ensuring T is a valid Sui object (e.g., a coin) that can be stored and transferred. The create_vault function initializes a shared vault with an empty balance. The deposit function merges a Coin into the vault’s Balance using coin::put, while withdraw extracts a specified amount and transfers it to the sender. Generics allow this module to work with any coin type (e.g., SUI, custom tokens) without duplicating code. The key + store constraints ensure compatibility with Sui’s coin system, which requires these abilities for storage and transfer.

3
评论
.
0xF1RTYB00B5.
Sep 5 2025, 11:58

As a Move developer specializing in DeFi protocols, I'll show you how to implement a generic vault module that can handle any coin type while maintaining type safety and compatibility with Sui's object model.

Generic Implementation

module vault::generic_vault {
    use sui::object::{Self, UID};
    use sui::coin::{Self, Coin};
    use sui::balance::{Self, Balance};
    use sui::tx_context::{Self, TxContext};
    use sui::transfer;

    struct Vault has key {
        id: UID,
        balance: Balance,
    }

    public entry fun create_vault(ctx: &mut TxContext) {
        let vault = Vault {
            id: object::new(ctx),
            balance: balance::zero(),
        };
        transfer::share_object(vault);
    }

    public entry fun deposit(vault: &mut Vault, coin: Coin) {
        coin::put(&mut vault.balance, coin);
    }

    public entry fun withdraw(
        vault: &mut Vault,
        amount: u64,
        ctx: &mut TxContext
    ) {
        let coin = coin::take(&mut vault.balance, amount, ctx);
        transfer::public_transfer(coin, tx_context::sender(ctx));
    }
}

Key Constraints

  1. Object Model Compatibility
  • has key ensures vault can be stored
  • UID provides unique identification
  • Balance handles coin arithmetic
  1. Type Safety
  • Coin type ensures proper coin handling
  • Balance manages coin quantities
  • TxContext provides transaction context

Usage Example

// Create vault
fun create_vault_for_coin_type<CoinType: store>(ctx: &mut TxContext) {
    vault::create_vault(ctx);
}

// Deposit coins
fun deposit<CoinType: store>(vault: &mut Vault, coin: CoinType) {
    vault::deposit(vault, coin);
}

This implementation provides a reusable vault module that works with any coin type while maintaining type safety through Sui's constraint system. The module is particularly useful for DeFi protocols as it can handle multiple token types without code duplication.

1
评论
.

你知道答案吗?

请登录并分享。

Move is an executable bytecode language used to implement custom transactions and smart contracts.

271帖子617答案
Sui.X.Peera.

赚取你的 1000 Sui 份额

获取声誉积分,并因帮助 Sui 社区成长而获得奖励。