Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sui framework] Adds mint_and_transfer and burn_ entrypoints, changes argument order in mint() #2655

Merged
merged 7 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/sui-core/src/unit_tests/data/hero/sources/hero.move
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ module examples::hero {
{
let treasury_cap = test_scenario::take_owned<TreasuryCap<EXAMPLE>>(scenario);
let ctx = test_scenario::ctx(scenario);
let coins = coin::mint(500, &mut treasury_cap, ctx);
let coins = coin::mint(&mut treasury_cap, 500, ctx);
coin::transfer(coins, copy player);
test_scenario::return_owned(scenario, treasury_cap);
};
Expand Down
24 changes: 19 additions & 5 deletions crates/sui-framework/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,27 @@ module sui::coin {
/// Create a coin worth `value`. and increase the total supply
/// in `cap` accordingly.
public fun mint<T>(
value: u64, cap: &mut TreasuryCap<T>, ctx: &mut TxContext,
cap: &mut TreasuryCap<T>, value: u64, ctx: &mut TxContext,
damirka marked this conversation as resolved.
Show resolved Hide resolved
): Coin<T> {
Coin {
id: tx_context::new_id(ctx),
balance: mint_balance(value, cap)
balance: mint_balance(cap, value)
}
}

/// Mint some amount of T as a `Balance` and increase the total
/// supply in `cap` accordingly.
/// Aborts if `value` + `cap.total_supply` >= U64_MAX
public fun mint_balance<T>(
value: u64, cap: &mut TreasuryCap<T>
cap: &mut TreasuryCap<T>, value: u64
): Balance<T> {
cap.total_supply = cap.total_supply + value;
balance::create_with_value(value)
}

/// Destroy the coin `c` and decrease the total supply in `cap`
/// accordingly.
public fun burn<T>(c: Coin<T>, cap: &mut TreasuryCap<T>) {
public fun burn<T>(cap: &mut TreasuryCap<T>, c: Coin<T>) {
let Coin { id, balance } = c;
let value = balance::destroy<T>(balance);
id::delete(id);
Expand All @@ -170,9 +170,23 @@ module sui::coin {

// === Entrypoints ===

/// Mint `amount` of `Coin` and send it to `recipient`. Invokes `mint()`.
public entry fun mint_<T>(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree with the requirement of having entry functions for these flows (as it will help new Coins to be created in just a few lines of code), but we need to agree on the naming convention. Is the _ the pattern we're looking for as best practice here or mint_to_sender etc? @tnowacki

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _ convention was from when you actually needed a separate function for visibility reasons.

This feels a bit different though since the functionality is actually different. I would advise something like mint_and_transfer

Copy link
Contributor Author

@damirka damirka Jun 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tnowacki what do you think about burn_ in that context?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one where it drops the u64? Feels okay.

I would ask yourself, What would the and_<verb> version be? If it is un-interesting, just an _ is fine. If its something like and_transfer_to_someone, you probably want a different name or suffix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the way!

c: &mut TreasuryCap<T>, amount: u64, recipient: address, ctx: &mut TxContext
) {
transfer::transfer(mint(c, amount, ctx), recipient)
}

/// Burn a Coin and reduce the total_supply. Invokes `burn()`.
public entry fun burn_<T>(c: &mut TreasuryCap<T>, coin: Coin<T>) {
burn(c, coin)
}

/// Send `amount` units of `c` to `recipient
/// Aborts with `EVALUE` if `amount` is greater than or equal to `amount`
public entry fun split_and_transfer<T>(c: &mut Coin<T>, amount: u64, recipient: address, ctx: &mut TxContext) {
public entry fun split_and_transfer<T>(
c: &mut Coin<T>, amount: u64, recipient: address, ctx: &mut TxContext
) {
transfer::transfer(withdraw(&mut c.balance, amount, ctx), recipient)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/sui-framework/sources/governance/genesis.move
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module sui::genesis {
ctx: &mut TxContext,
) {
let treasury_cap = sui::new(ctx);
let storage_fund = coin::mint_balance(INIT_STORAGE_FUND, &mut treasury_cap);
let storage_fund = coin::mint_balance(&mut treasury_cap, INIT_STORAGE_FUND);
let validators = vector::empty();
let count = vector::length(&validator_pubkeys);
assert!(
Expand All @@ -54,7 +54,7 @@ module sui::genesis {
pubkey,
name,
net_address,
coin::mint_balance(stake, &mut treasury_cap),
coin::mint_balance(&mut treasury_cap, stake),
));
i = i + 1;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ module fungible_tokens::basket {

coin::deposit(&mut reserve.sui, sui);
coin::deposit(&mut reserve.managed, managed);
coin::mint(num_sui, &mut reserve.treasury_cap, ctx)
coin::mint(&mut reserve.treasury_cap, num_sui, ctx)
}

/// Burn BASKET coins and return the underlying reserve assets
public fun burn(
reserve: &mut Reserve, basket: Coin<BASKET>, ctx: &mut TxContext
): (Coin<SUI>, Coin<MANAGED>) {
let num_basket = coin::value(&basket);
coin::burn(basket, &mut reserve.treasury_cap);
coin::burn(&mut reserve.treasury_cap, basket);
let sui = coin::withdraw(&mut reserve.sui, num_basket, ctx);
let managed = coin::withdraw(&mut reserve.managed, num_basket, ctx);
(sui, managed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ module fungible_tokens::managed {

/// Manager can mint new coins
public fun mint(treasury_cap: &mut TreasuryCap<MANAGED>, amount: u64, ctx: &mut TxContext): Coin<MANAGED> {
coin::mint<MANAGED>(amount, treasury_cap, ctx)
coin::mint<MANAGED>(treasury_cap, amount, ctx)
}

/// Manager can burn coins
public entry fun burn(treasury_cap: &mut TreasuryCap<MANAGED>, coin: Coin<MANAGED>) {
coin::burn(coin, treasury_cap)
coin::burn(treasury_cap, coin)
}

/// Manager can transfer the treasury capability to a new manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ module abc::abc {
///
/// TODO: Make TreasuryCap a part of Balance module instead of Coin.
public entry fun burn(treasury: &mut TreasuryCap<ABC>, owned: &mut RCoin<ABC>, value: u64, ctx: &mut TxContext) {
coin::burn(coin::withdraw(borrow_mut(owned), value, ctx), treasury);
coin::burn(treasury, coin::withdraw(borrow_mut(owned), value, ctx));
}

/// Ban some address and forbid making any transactions from or to this address.
Expand Down