Skip to content

Commit

Permalink
[framework] Closed Loop Token (MystenLabs#14702)
Browse files Browse the repository at this point in the history
## Description 

Adds the `sui::token` module to the framework.

## Test Plan 

Adds a number of tests to test out the new token module along with examples.

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [x] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes

- Adds a closed loop `token` to the framework

---------

Co-authored-by: Tim Zakian <[email protected]>
Co-authored-by: Ashok Menon <[email protected]>
  • Loading branch information
3 people authored Nov 14, 2023
1 parent 137c92a commit 4751868
Show file tree
Hide file tree
Showing 32 changed files with 1,887 additions and 118 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 1 addition & 10 deletions crates/sui-framework-snapshot/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,7 @@
]
},
"31": {
"git_revision": "c87e139af9-dirty",
"package_ids": [
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000002",
"0x0000000000000000000000000000000000000000000000000000000000000003",
"0x000000000000000000000000000000000000000000000000000000000000dee9"
]
},
"32": {
"git_revision": "e4c5b06356",
"git_revision": "ac208a8cb1",
"package_ids": [
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000002",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ module sui::balance {
let Balance { value } = self;
value
}

#[test_only]
/// Create a `Supply` of any coin for testing purposes.
public fun create_supply_for_testing<T>(): Supply<T> {
Supply { value: 0 }
}
}

#[test_only]
Expand Down
31 changes: 16 additions & 15 deletions crates/sui-framework/packages/sui-framework/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -364,33 +364,23 @@ module sui::coin {

// === Get coin metadata fields for on-chain consumption ===

public fun get_decimals<T>(
metadata: &CoinMetadata<T>
): u8 {
public fun get_decimals<T>(metadata: &CoinMetadata<T>): u8 {
metadata.decimals
}

public fun get_name<T>(
metadata: &CoinMetadata<T>
): string::String {
public fun get_name<T>(metadata: &CoinMetadata<T>): string::String {
metadata.name
}

public fun get_symbol<T>(
metadata: &CoinMetadata<T>
): ascii::String {
public fun get_symbol<T>(metadata: &CoinMetadata<T>): ascii::String {
metadata.symbol
}

public fun get_description<T>(
metadata: &CoinMetadata<T>
): string::String {
public fun get_description<T>(metadata: &CoinMetadata<T>): string::String {
metadata.description
}

public fun get_icon_url<T>(
metadata: &CoinMetadata<T>
): Option<Url> {
public fun get_icon_url<T>(metadata: &CoinMetadata<T>): Option<Url> {
metadata.icon_url
}

Expand All @@ -410,6 +400,17 @@ module sui::coin {
balance::destroy_for_testing(balance)
}

#[test_only]
/// Create a `TreasuryCap` for any `Coin` for testing purposes.
public fun create_treasury_cap_for_testing<T>(
ctx: &mut TxContext
): TreasuryCap<T> {
TreasuryCap {
id: object::new(ctx),
total_supply: balance::create_supply_for_testing()
}
}

// === Deprecated code ===

// oops, wanted treasury: &TreasuryCap<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ module sui::transfer_policy {
/// making the discoverability and tracking the supported types easier.
struct TransferPolicyCreated<phantom T> has copy, drop { id: ID }

/// Event that is emitted when a publisher destroys a `TransferPolicyCap`.
/// Allows for tracking supported policies.
struct TransferPolicyDestroyed<phantom T> has copy, drop { id: ID }

/// Key to store "Rule" configuration for a specific `TransferPolicy`.
struct RuleKey<phantom T: drop> has copy, store, drop {}

Expand Down Expand Up @@ -166,11 +170,12 @@ module sui::transfer_policy {
): Coin<SUI> {
assert!(object::id(&self) == cap.policy_id, ENotOwner);

let TransferPolicyCap { id: cap_id, policy_id: _ } = cap;
let TransferPolicyCap { id: cap_id, policy_id } = cap;
let TransferPolicy { id, rules: _, balance } = self;

object::delete(id);
object::delete(cap_id);
event::emit(TransferPolicyDestroyed<T> { id: policy_id });
coin::from_balance(balance, ctx)
}

Expand Down
Loading

0 comments on commit 4751868

Please sign in to comment.