Skip to content

Commit

Permalink
[sui framework] Adds mint_and_transfer and burn_ entrypoints, changes…
Browse files Browse the repository at this point in the history
… argument order in mint() (MystenLabs#2655)
  • Loading branch information
damirka authored Jun 23, 2022
1 parent b4e5791 commit bfa5384
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ Contents: sui::coin::Coin<sui::sui::SUI> {id: sui::id::VersionedID {id: sui::id:

task 7 'view-object'. lines 20-20:
Owner: Account Address ( B )
Contents: sui::coin::Coin<sui::sui::SUI> {id: sui::id::VersionedID {id: sui::id::UniqueID {id: sui::id::ID {bytes: fake(107)}}, version: 1u64}, balance: sui::balance::Balance<sui::sui::SUI> {value: 99593u64}}
Contents: sui::coin::Coin<sui::sui::SUI> {id: sui::id::VersionedID {id: sui::id::UniqueID {id: sui::id::ID {bytes: fake(107)}}, version: 1u64}, balance: sui::balance::Balance<sui::sui::SUI> {value: 99592u64}}
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
27 changes: 21 additions & 6 deletions crates/sui-framework/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,32 @@ 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,
): 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>): u64 {
let Coin { id, balance } = c;
let value = balance::destroy<T>(balance);
id::delete(id);
cap.total_supply = cap.total_supply - value
cap.total_supply = cap.total_supply - value;
value
}

/// Return the total number of `T`'s in circulation
Expand All @@ -170,9 +171,23 @@ module sui::coin {

// === Entrypoints ===

/// Mint `amount` of `Coin` and send it to `recipient`. Invokes `mint()`.
public entry fun mint_and_transfer<T>(
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 @@ -21,7 +21,7 @@ module examples::trusted_coin {
}

public entry fun mint(treasury_cap: &mut TreasuryCap<EXAMPLE>, amount: u64, ctx: &mut TxContext) {
let coin = coin::mint<EXAMPLE>(amount, treasury_cap, ctx);
let coin = coin::mint<EXAMPLE>(treasury_cap, amount, ctx);
coin::transfer(coin, tx_context::sender(ctx));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module examples::trusted_coin {
}

public entry fun mint(treasury_cap: &mut TreasuryCap<EXAMPLE>, amount: u64, ctx: &mut TxContext) {
let coin = coin::mint<EXAMPLE>(amount, treasury_cap, ctx);
let coin = coin::mint<EXAMPLE>(treasury_cap, amount, ctx);
coin::transfer(coin, tx_context::sender(ctx));
}

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
2 changes: 1 addition & 1 deletion sui_programmability/examples/games/sources/sea_hero.move
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ module games::sea_hero {

let monster = SeaMonster {
id: tx_context::new_id(ctx),
reward: coin::mint_balance(reward_amount, &mut admin.treasury_cap)
reward: coin::mint_balance(&mut admin.treasury_cap, reward_amount)
};
admin.monsters_created = admin.monsters_created + 1;
transfer::transfer(monster, recipient);
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

0 comments on commit bfa5384

Please sign in to comment.