Last active
November 18, 2022 11:00
-
-
Save amnn/8602af1da93a30359efa07236d20572a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module p::example { | |
use sui::coin::{Self, Coin}; | |
use sui::dynamic_field as field; | |
use sui::object::UID; | |
use sui::tx_context::TxContext; | |
struct Manager has key { id: UID, /* ... */ } | |
struct Label<phantom COIN> has copy, drop, store {} | |
entry fun handle_coin<COIN>(m: &mut Manager, coin: Coin<COIN>, ctx: &mut TxContext) { | |
let l = Label<COIN> {}; | |
if (!field::exists_with_type<Label<COIN>, Coin<COIN>>(&m.id, l)) { | |
field::add(&mut m.id, l, coin::zero<Coin>()); | |
} | |
let manager_coin = field::borrow_mut<Label<COIN>, Coin<COIN>>(&mut m.id, l); | |
// Do what you like with manager_coin and coin. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment