Skip to content

Commit

Permalink
[feature] show indexed coin value (#171)
Browse files Browse the repository at this point in the history
Co-authored-by: 0o-de-lally <[email protected]>
  • Loading branch information
0xzoz and 0o-de-lally authored Feb 12, 2024
1 parent 3a1cbe6 commit a08e553
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
11 changes: 11 additions & 0 deletions framework/libra-framework/sources/modified_source/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module diem_framework::coin {
use diem_framework::system_addresses;

use diem_std::type_info;
use diem_std::math128;
// use diem_std::debug::print;

friend ol_framework::libra_coin;
Expand Down Expand Up @@ -563,6 +564,16 @@ module diem_framework::coin {
coin.value
}

/// Returns an indexed value based on the current supply, compared to the
/// final supply
public fun index_value<CoinType>(coin: &Coin<CoinType>, index_supply: u128):
u128 acquires CoinInfo {
let units = (value(coin) as u128);
let supply_now_opt = supply<CoinType>();
let supply_now = option::borrow(&supply_now_opt);
math128::mul_div(units, *supply_now, index_supply)
}

/// Withdraw specifed `amount` of coin `CoinType` from the signing account.
public(friend) fun withdraw<CoinType>(
account: &signer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ module ol_framework::libra_coin {
coin::destroy_freeze_cap(freeze_cap);
(burn_cap, mint_cap)
}

// This is particularly useful if the aggregator_factory is already initialized via another call path.
#[test_only]
public fun initialize_for_test_without_aggregator_factory(diem_framework: &signer): (BurnCapability<LibraCoin>, MintCapability<LibraCoin>) {
Expand Down
27 changes: 23 additions & 4 deletions framework/libra-framework/sources/ol_sources/ol_account.move
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ module ol_framework::ol_account {
/// why is VM trying to use this?
const ENOT_FOR_VM: u64 = 9;





struct BurnTracker has key {
prev_supply: u64,
prev_balance: u64,
Expand Down Expand Up @@ -334,6 +330,29 @@ module ol_framework::ol_account {
slow_wallet::balance(addr)
}

#[view]
/// returns the INDEXED value of the coins.
// Note: there is a similar function in coin.move to get the indexed
// value of a single coin.
public fun real_balance(addr: address): (u64, u64) {
let final = libra_coin::get_final_supply();
let current = libra_coin::supply();
let (unlocked, total) = slow_wallet::balance(addr);

let unlocked_indexed = math64::mul_div(unlocked, final, current);
let total_indexed = math64::mul_div(total, final, current);

(unlocked_indexed, total_indexed)
// Going to another place
// Another universe, another realm
// Sleeping, dreaming, what is real?
// Leap and the net will appear
// Going to another place
// Another universe, another realm
// Sleeping, dreaming, what is real?
// Leap and the net will appear
}

#[view]
/// Returns a human readable version of the balance with (integer, decimal_part)
public fun balance_human(owner: address): (u64, u64) {
Expand Down
35 changes: 33 additions & 2 deletions framework/libra-framework/sources/ol_sources/tests/burn.test.move
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module ol_framework::test_burn {
// use diem_std::debug::print;

#[test(root = @ol_framework, alice = @0x1000a)]

fun burn_reduces_supply(root: &signer, alice: &signer) {
mock::genesis_n_vals(root, 1);
mock::ol_initialize_coin_and_fund_vals(root, 10000, true);
Expand All @@ -30,9 +29,41 @@ module ol_framework::test_burn {
burn::burn_and_track(c);
let supply = libra_coin::supply();
assert!(supply == (supply_pre - alice_burn), 7357001);

}

// burn changes indexed real_balance
#[test(root = @ol_framework, alice = @0x1000a)]
fun burn_changes_real_balance(root: &signer, alice: &signer) {
mock::genesis_n_vals(root, 1);
// the mint to alice will double the supply
mock::ol_initialize_coin_and_fund_vals(root, 100000000, true);

let supply_pre = libra_coin::supply();
// need to adjust this since the validator init increased the supply above
// the final
libra_coin::test_set_final_supply(root, supply_pre);
let final = libra_coin::get_final_supply();
assert!(supply_pre == final, 7357000);
// no change should happen before any burns
let (unlocked, total) = ol_account::balance(@0x1000a);
let (real_unlocked, real_total) = ol_account::real_balance(@0x1000a);
assert!(real_unlocked == unlocked, 7357001);
assert!(real_total == total, 7357002);

// burn half of alices coins, which is 25% of the supply
let alice_burn = 50000000;


let c = ol_account::withdraw(alice, alice_burn);
burn::burn_and_track(c);
let supply = libra_coin::supply();
assert!(supply == (supply_pre - alice_burn), 7357003);

let (unlocked, total) = ol_account::balance(@0x1000a);
let (real_unlocked, real_total) = ol_account::real_balance(@0x1000a);
assert!(real_unlocked > unlocked, 7357004);
assert!(real_total > total, 7357005);
}


#[test(root = @ol_framework, alice = @0x1000a, bob = @0x1000d, eve = @0x1000e)]
Expand Down
Binary file modified framework/releases/head.mrb
Binary file not shown.

0 comments on commit a08e553

Please sign in to comment.