Skip to content

Instantly share code, notes, and snippets.

@amnn
Last active November 18, 2022 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amnn/8602af1da93a30359efa07236d20572a to your computer and use it in GitHub Desktop.
Save amnn/8602af1da93a30359efa07236d20572a to your computer and use it in GitHub Desktop.
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