Wrapper Module

Overview

The satay::wrapper module manages the conversion of the legacy coin standard to-and-fro the fungible assets standard within the Satay protocol. It handles the creation, initialization, and management of wrapped coins by providing functions to wrap and unwrap them.

Structs

Wrapper

The Wrapper struct stores a SignerCapability for managing wrapped coins and a SmartTable that maps wrapped coin metadata to their coin type names.

struct Wrapper has key {
    signer_cap: SignerCapability,
    wrapped_coins: SmartTable<Object<Metadata>, String>
}

Constants

  • WRAPPER_SEED: vector<u8> = b"000000000Wrapper" – The seed used to create the wrapper resource account.

  • EWRAPPER_ALREADY_INITIALIZED: u64 = 0 – Error indicating that the wrapper has already been initialized.

  • EWRAPPER_NOT_INITIALIZED: u64 = 1 – Error indicating that the wrapper has not been initialized.

  • EMETADATA_ALREADY_EXISTS: u64 = 2 – Error indicating that the metadata for the wrapped asset already exists.

Public Functions

  1. Module Initialization

    • init_module(_: &signer): Initializes the wrapper module by creating a new wrapper account. It generates a new signer and a Wrapper resource, storing wrapped coins in a SmartTable.

  2. Asset Initialization and Management

    • initialize_wrapped_asset<CoinType>(): Object<Metadata> acquires Wrapper: Initializes a new wrapped asset for the given CoinType. It creates metadata for the wrapped asset and stores it in the wrapper's SmartTable. If the metadata already exists, it raises EMETADATA_ALREADY_EXISTS.

    • get_wrapped_asset_metadata<CoinType>(): Object<Metadata>: Retrieves the metadata for a wrapped asset of the given CoinType.

    • get_wrapped_coin_type(metadata: Object<Metadata>): String acquires Wrapper: Retrieves the coin type name of the given wrapped asset from the wrapper's SmartTable.

    • is_wrapped_asset(metadata: Object<Metadata>): bool acquires Wrapper: Checks whether the given metadata represents a wrapped asset.

  3. Address Management

    • get_wrapper_address(): address: Computes and returns the address of the wrapper resource.

    • compute_wrap_address<CoinType>(): address: Computes the address of the wrapped asset for the given CoinType.

  4. Asset Wrapping and Unwrapping

    • wrap<CoinType>(coin: Coin<CoinType>): FungibleAsset acquires Wrapper: Wraps the given CoinType into a fungible asset. It initializes the wrapped asset if it doesn't exist, deposits the coin into the wrapper, and mints the corresponding fungible asset.

    • unwrap<CoinType>(asset: FungibleAsset): Coin<CoinType> acquires Wrapper: Unwraps a fungible asset into the corresponding CoinType. It burns the fungible asset and withdraws the equivalent amount of the original coin.

Helper Functions

  • borrow_wrapper(): Returns a reference to the Wrapper resource.

  • borrow_wrapper_mut(): Returns a mutable reference to the Wrapper resource.

View Functions

  • has_wrapped_asset<CoinType>(): bool: Checks if the wrapped asset for the given CoinType exists.

  • get_wrapped_asset_metadata<CoinType>(): Object<Metadata>: Retrieves the metadata for the wrapped asset corresponding to CoinType.

Last updated