Skip to content

Commit

Permalink
feat: store IOTA's TreasutyCap in SystemObject
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriyr committed Jun 20, 2024
1 parent 3cb1f1b commit 9f2b264
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 30 deletions.
43 changes: 36 additions & 7 deletions crates/iota-framework/docs/iota-framework/iota.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ It has 9 decimals, and the smallest unit (10^-9) is called "micros".
- [Struct `IOTA`](#0x2_iota_IOTA)
- [Constants](#@Constants_0)
- [Function `new`](#0x2_iota_new)
- [Function `increase_supply`](#0x2_iota_increase_supply)
- [Function `transfer`](#0x2_iota_transfer)


Expand Down Expand Up @@ -109,11 +110,11 @@ The total supply of Iota denominated in Micros (10 Billion * 10^9)

## Function `new`

Register the <code><a href="../iota-framework/iota.md#0x2_iota_IOTA">IOTA</a></code> Coin to acquire its <code>Supply</code>.
Register the <code><a href="../iota-framework/iota.md#0x2_iota_IOTA">IOTA</a></code> Coin to acquire its <code>TreasuryCap</code>.
This should be called only once during genesis creation.


<pre><code><b>fun</b> <a href="../iota-framework/iota.md#0x2_iota_new">new</a>(ctx: &<b>mut</b> <a href="../iota-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="../iota-framework/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;
<pre><code><b>fun</b> <a href="../iota-framework/iota.md#0x2_iota_new">new</a>(ctx: &<b>mut</b> <a href="../iota-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="../iota-framework/coin.md#0x2_coin_TreasuryCap">coin::TreasuryCap</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;
</code></pre>


Expand All @@ -122,7 +123,7 @@ This should be called only once during genesis creation.
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="../iota-framework/iota.md#0x2_iota_new">new</a>(ctx: &<b>mut</b> TxContext): Balance&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">IOTA</a>&gt; {
<pre><code><b>fun</b> <a href="../iota-framework/iota.md#0x2_iota_new">new</a>(ctx: &<b>mut</b> TxContext): TreasuryCap&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">IOTA</a>&gt; {
<b>assert</b>!(ctx.sender() == @0x0, <a href="../iota-framework/iota.md#0x2_iota_ENotSystemAddress">ENotSystemAddress</a>);
<b>assert</b>!(ctx.epoch() == 0, <a href="../iota-framework/iota.md#0x2_iota_EAlreadyMinted">EAlreadyMinted</a>);

Expand All @@ -136,11 +137,39 @@ This should be called only once during genesis creation.
<a href="../move-stdlib/option.md#0x1_option_none">option::none</a>(),
ctx
);

<a href="../iota-framework/transfer.md#0x2_transfer_public_freeze_object">transfer::public_freeze_object</a>(metadata);
<b>let</b> <b>mut</b> supply = treasury.treasury_into_supply();
<b>let</b> total_iota = supply.increase_supply(<a href="../iota-framework/iota.md#0x2_iota_TOTAL_SUPPLY_MICROS">TOTAL_SUPPLY_MICROS</a>);
supply.destroy_supply();
total_iota

treasury
}
</code></pre>



</details>

<a name="0x2_iota_increase_supply"></a>

## Function `increase_supply`

Increase the IOTA supply.
This should be called only once during genesis creation.


<pre><code><b>fun</b> <a href="../iota-framework/iota.md#0x2_iota_increase_supply">increase_supply</a>(cap: &<b>mut</b> <a href="../iota-framework/coin.md#0x2_coin_TreasuryCap">coin::TreasuryCap</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, ctx: &<a href="../iota-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="../iota-framework/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="../iota-framework/iota.md#0x2_iota_increase_supply">increase_supply</a>(cap: &<b>mut</b> TreasuryCap&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">IOTA</a>&gt;, ctx: &TxContext): Balance&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">IOTA</a>&gt; {
<b>assert</b>!(ctx.sender() == @0x0, <a href="../iota-framework/iota.md#0x2_iota_ENotSystemAddress">ENotSystemAddress</a>);
<b>assert</b>!(ctx.epoch() == 0, <a href="../iota-framework/iota.md#0x2_iota_EAlreadyMinted">EAlreadyMinted</a>);

cap.supply_mut().<a href="../iota-framework/iota.md#0x2_iota_increase_supply">increase_supply</a>(<a href="../iota-framework/iota.md#0x2_iota_TOTAL_SUPPLY_MICROS">TOTAL_SUPPLY_MICROS</a>)
}
</code></pre>

Expand Down
4 changes: 3 additions & 1 deletion crates/iota-framework/docs/iota-system/genesis.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ It will create a singleton IotaSystemState object, which contains
all the information we need in the system.


<pre><code><b>fun</b> <a href="genesis.md#0x3_genesis_create">create</a>(iota_system_state_id: <a href="../iota-framework/object.md#0x2_object_UID">object::UID</a>, iota_supply: <a href="../iota-framework/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, genesis_chain_parameters: <a href="genesis.md#0x3_genesis_GenesisChainParameters">genesis::GenesisChainParameters</a>, genesis_validators: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="genesis.md#0x3_genesis_GenesisValidatorMetadata">genesis::GenesisValidatorMetadata</a>&gt;, token_distribution_schedule: <a href="genesis.md#0x3_genesis_TokenDistributionSchedule">genesis::TokenDistributionSchedule</a>, ctx: &<b>mut</b> <a href="../iota-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
<pre><code><b>fun</b> <a href="genesis.md#0x3_genesis_create">create</a>(iota_system_state_id: <a href="../iota-framework/object.md#0x2_object_UID">object::UID</a>, iota_treasury_cap: <a href="../iota-framework/coin.md#0x2_coin_TreasuryCap">coin::TreasuryCap</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, iota_supply: <a href="../iota-framework/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, genesis_chain_parameters: <a href="genesis.md#0x3_genesis_GenesisChainParameters">genesis::GenesisChainParameters</a>, genesis_validators: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="genesis.md#0x3_genesis_GenesisValidatorMetadata">genesis::GenesisValidatorMetadata</a>&gt;, token_distribution_schedule: <a href="genesis.md#0x3_genesis_TokenDistributionSchedule">genesis::TokenDistributionSchedule</a>, ctx: &<b>mut</b> <a href="../iota-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
</code></pre>


Expand All @@ -351,6 +351,7 @@ all the information we need in the system.

<pre><code><b>fun</b> <a href="genesis.md#0x3_genesis_create">create</a>(
iota_system_state_id: UID,
iota_treasury_cap: TreasuryCap&lt;IOTA&gt;,
<b>mut</b> iota_supply: Balance&lt;IOTA&gt;,
genesis_chain_parameters: <a href="genesis.md#0x3_genesis_GenesisChainParameters">GenesisChainParameters</a>,
genesis_validators: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="genesis.md#0x3_genesis_GenesisValidatorMetadata">GenesisValidatorMetadata</a>&gt;,
Expand Down Expand Up @@ -456,6 +457,7 @@ all the information we need in the system.

<a href="iota_system.md#0x3_iota_system_create">iota_system::create</a>(
iota_system_state_id,
iota_treasury_cap,
validators,
<a href="storage_fund.md#0x3_storage_fund">storage_fund</a>,
genesis_chain_parameters.protocol_version,
Expand Down
4 changes: 3 additions & 1 deletion crates/iota-framework/docs/iota-system/iota_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Create a new IotaSystemState object and make it shared.
This function will be called only once in genesis.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="iota_system.md#0x3_iota_system_create">create</a>(id: <a href="../iota-framework/object.md#0x2_object_UID">object::UID</a>, validators: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="validator.md#0x3_validator_Validator">validator::Validator</a>&gt;, <a href="storage_fund.md#0x3_storage_fund">storage_fund</a>: <a href="../iota-framework/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, protocol_version: u64, epoch_start_timestamp_ms: u64, parameters: <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_SystemParameters">iota_system_state_inner::SystemParameters</a>, <a href="stake_subsidy.md#0x3_stake_subsidy">stake_subsidy</a>: <a href="stake_subsidy.md#0x3_stake_subsidy_StakeSubsidy">stake_subsidy::StakeSubsidy</a>, ctx: &<b>mut</b> <a href="../iota-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="iota_system.md#0x3_iota_system_create">create</a>(id: <a href="../iota-framework/object.md#0x2_object_UID">object::UID</a>, iota_treasury_cap: <a href="../iota-framework/coin.md#0x2_coin_TreasuryCap">coin::TreasuryCap</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, validators: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="validator.md#0x3_validator_Validator">validator::Validator</a>&gt;, <a href="storage_fund.md#0x3_storage_fund">storage_fund</a>: <a href="../iota-framework/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, protocol_version: u64, epoch_start_timestamp_ms: u64, parameters: <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_SystemParameters">iota_system_state_inner::SystemParameters</a>, <a href="stake_subsidy.md#0x3_stake_subsidy">stake_subsidy</a>: <a href="stake_subsidy.md#0x3_stake_subsidy_StakeSubsidy">stake_subsidy::StakeSubsidy</a>, ctx: &<b>mut</b> <a href="../iota-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
</code></pre>


Expand All @@ -178,6 +178,7 @@ This function will be called only once in genesis.

<pre><code><b>public</b>(package) <b>fun</b> <a href="iota_system.md#0x3_iota_system_create">create</a>(
id: UID,
iota_treasury_cap: TreasuryCap&lt;IOTA&gt;,
validators: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Validator&gt;,
<a href="storage_fund.md#0x3_storage_fund">storage_fund</a>: Balance&lt;IOTA&gt;,
protocol_version: u64,
Expand All @@ -187,6 +188,7 @@ This function will be called only once in genesis.
ctx: &<b>mut</b> TxContext,
) {
<b>let</b> system_state = <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_create">iota_system_state_inner::create</a>(
iota_treasury_cap,
validators,
<a href="storage_fund.md#0x3_storage_fund">storage_fund</a>,
protocol_version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ The top-level object containing all information of the Iota system.
we know what version it is by inspecting IotaSystemStateInner as well.
</dd>
<dt>
<code>iota_treasury_cap: <a href="../iota-framework/coin.md#0x2_coin_TreasuryCap">coin::TreasuryCap</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;</code>
</dt>
<dd>
The IOTA's TreasuryCap.
</dd>
<dt>
<code>validators: <a href="validator_set.md#0x3_validator_set_ValidatorSet">validator_set::ValidatorSet</a></code>
</dt>
<dd>
Expand Down Expand Up @@ -413,6 +419,12 @@ Uses SystemParametersV2 as the parameters.
we know what version it is by inspecting IotaSystemStateInner as well.
</dd>
<dt>
<code>iota_treasury_cap: <a href="../iota-framework/coin.md#0x2_coin_TreasuryCap">coin::TreasuryCap</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;</code>
</dt>
<dd>
The IOTA's TreasuryCap.
</dd>
<dt>
<code>validators: <a href="validator_set.md#0x3_validator_set_ValidatorSet">validator_set::ValidatorSet</a></code>
</dt>
<dd>
Expand Down Expand Up @@ -741,7 +753,7 @@ Create a new IotaSystemState object and make it shared.
This function will be called only once in genesis.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_create">create</a>(validators: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="validator.md#0x3_validator_Validator">validator::Validator</a>&gt;, initial_storage_fund: <a href="../iota-framework/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, protocol_version: u64, epoch_start_timestamp_ms: u64, parameters: <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_SystemParameters">iota_system_state_inner::SystemParameters</a>, <a href="stake_subsidy.md#0x3_stake_subsidy">stake_subsidy</a>: <a href="stake_subsidy.md#0x3_stake_subsidy_StakeSubsidy">stake_subsidy::StakeSubsidy</a>, ctx: &<b>mut</b> <a href="../iota-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_IotaSystemStateInner">iota_system_state_inner::IotaSystemStateInner</a>
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_create">create</a>(iota_treasury_cap: <a href="../iota-framework/coin.md#0x2_coin_TreasuryCap">coin::TreasuryCap</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, validators: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="validator.md#0x3_validator_Validator">validator::Validator</a>&gt;, initial_storage_fund: <a href="../iota-framework/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../iota-framework/iota.md#0x2_iota_IOTA">iota::IOTA</a>&gt;, protocol_version: u64, epoch_start_timestamp_ms: u64, parameters: <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_SystemParameters">iota_system_state_inner::SystemParameters</a>, <a href="stake_subsidy.md#0x3_stake_subsidy">stake_subsidy</a>: <a href="stake_subsidy.md#0x3_stake_subsidy_StakeSubsidy">stake_subsidy::StakeSubsidy</a>, ctx: &<b>mut</b> <a href="../iota-framework/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_IotaSystemStateInner">iota_system_state_inner::IotaSystemStateInner</a>
</code></pre>


Expand All @@ -751,6 +763,7 @@ This function will be called only once in genesis.


<pre><code><b>public</b>(package) <b>fun</b> <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_create">create</a>(
iota_treasury_cap: TreasuryCap&lt;IOTA&gt;,
validators: <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Validator&gt;,
initial_storage_fund: Balance&lt;IOTA&gt;,
protocol_version: u64,
Expand All @@ -766,6 +779,7 @@ This function will be called only once in genesis.
epoch: 0,
protocol_version,
system_state_version: <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_genesis_system_state_version">genesis_system_state_version</a>(),
iota_treasury_cap,
validators,
<a href="storage_fund.md#0x3_storage_fund">storage_fund</a>: <a href="storage_fund.md#0x3_storage_fund_new">storage_fund::new</a>(initial_storage_fund),
parameters,
Expand Down Expand Up @@ -852,6 +866,7 @@ This function will be called only once in genesis.
epoch,
protocol_version,
system_state_version: _,
iota_treasury_cap,
validators,
<a href="storage_fund.md#0x3_storage_fund">storage_fund</a>,
parameters,
Expand Down Expand Up @@ -880,6 +895,7 @@ This function will be called only once in genesis.
epoch,
protocol_version,
system_state_version: 2,
iota_treasury_cap,
validators,
<a href="storage_fund.md#0x3_storage_fund">storage_fund</a>,
parameters: <a href="iota_system_state_inner.md#0x3_iota_system_state_inner_SystemParametersV2">SystemParametersV2</a> {
Expand Down
23 changes: 16 additions & 7 deletions crates/iota-framework/packages/iota-framework/sources/iota.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// It has 9 decimals, and the smallest unit (10^-9) is called "micros".
module iota::iota {
use iota::balance::Balance;
use iota::coin;
use iota::coin::{Self, TreasuryCap};

const EAlreadyMinted: u64 = 0;
/// Sender is not @0x0 the system address.
Expand All @@ -28,9 +28,9 @@ module iota::iota {
public struct IOTA has drop {}

#[allow(unused_function)]
/// Register the `IOTA` Coin to acquire its `Supply`.
/// Register the `IOTA` Coin to acquire its `TreasuryCap`.
/// This should be called only once during genesis creation.
fun new(ctx: &mut TxContext): Balance<IOTA> {
fun new(ctx: &mut TxContext): TreasuryCap<IOTA> {
assert!(ctx.sender() == @0x0, ENotSystemAddress);
assert!(ctx.epoch() == 0, EAlreadyMinted);

Expand All @@ -44,11 +44,20 @@ module iota::iota {
option::none(),
ctx
);

transfer::public_freeze_object(metadata);
let mut supply = treasury.treasury_into_supply();
let total_iota = supply.increase_supply(TOTAL_SUPPLY_MICROS);
supply.destroy_supply();
total_iota

treasury
}

#[allow(unused_function)]
/// Increase the IOTA supply.
/// This should be called only once during genesis creation.
fun increase_supply(cap: &mut TreasuryCap<IOTA>, ctx: &TxContext): Balance<IOTA> {
assert!(ctx.sender() == @0x0, ENotSystemAddress);
assert!(ctx.epoch() == 0, EAlreadyMinted);

cap.supply_mut().increase_supply(TOTAL_SUPPLY_MICROS)
}

public entry fun transfer(c: coin::Coin<IOTA>, recipient: address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module iota_system::genesis {

use iota::balance::{Self, Balance};
use iota::coin::TreasuryCap;
use iota::iota::{Self, IOTA};
use iota_system::iota_system;
use iota_system::validator::{Self, Validator};
Expand Down Expand Up @@ -79,6 +80,7 @@ module iota_system::genesis {
/// all the information we need in the system.
fun create(
iota_system_state_id: UID,
iota_treasury_cap: TreasuryCap<IOTA>,
mut iota_supply: Balance<IOTA>,
genesis_chain_parameters: GenesisChainParameters,
genesis_validators: vector<GenesisValidatorMetadata>,
Expand Down Expand Up @@ -184,6 +186,7 @@ module iota_system::genesis {

iota_system::create(
iota_system_state_id,
iota_treasury_cap,
validators,
storage_fund,
genesis_chain_parameters.protocol_version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
module iota_system::iota_system {
use iota::balance::Balance;

use iota::coin::Coin;
use iota::coin::{Coin, TreasuryCap};
use iota_system::staking_pool::StakedIota;
use iota::iota::IOTA;
use iota::table::Table;
Expand Down Expand Up @@ -78,6 +78,7 @@ module iota_system::iota_system {
/// This function will be called only once in genesis.
public(package) fun create(
id: UID,
iota_treasury_cap: TreasuryCap<IOTA>,
validators: vector<Validator>,
storage_fund: Balance<IOTA>,
protocol_version: u64,
Expand All @@ -87,6 +88,7 @@ module iota_system::iota_system {
ctx: &mut TxContext,
) {
let system_state = iota_system_state_inner::create(
iota_treasury_cap,
validators,
storage_fund,
protocol_version,
Expand Down
Loading

0 comments on commit 9f2b264

Please sign in to comment.