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 committed Feb 12, 2024
1 parent 3a1cbe6 commit 40035e4
Show file tree
Hide file tree
Showing 21 changed files with 11,049 additions and 10,964 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.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ license = ''

[dev-addresses]
[dependencies.LibraFramework]
local = '/home/zoz/libra-framework/framework/libra-framework'
local = '/root/libra-framework/framework/libra-framework'

[dev-dependencies]
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d4283554ee5f8332c6e944587209aa92c4ec2c34c83b79aabaef1d5db6fe583a
2a6c36efe858195b4774fb33a50fbdca73aa2c5ac5c43e9d573e853d98a0b384
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Upgrade proposal for package `MoveStdlib`

// Framework commit hash: bf5bd0e012f2cc45593b24ff02faf612c470b704
// Framework commit hash: a08e5536f77a9a9f4b1e0344a965bbb68e428dc5
// Builder commit hash: db1137ba1f8e7301e325021f71f740063daaf76e

// Next step script hash: d53bc5c8ff4e0fea75d59dab320d605a92c6fc42d03ba050a753ebe32446ff53
// Next step script hash: 4b328fdd686a7929427b0100f471d93545f40997d72856524542dc20dec2b14f

// source digest: CD5C8655F0340314CC68657DF89A58E257A0A88218E2B07A278A20B843E7A09E
script {
Expand All @@ -17,7 +17,7 @@ script {
let framework_signer = diem_governance::resolve_multi_step_proposal(
proposal_id,
@0000000000000000000000000000000000000000000000000000000000000001,
vector[213u8,59u8,197u8,200u8,255u8,78u8,15u8,234u8,117u8,213u8,157u8,171u8,50u8,13u8,96u8,90u8,146u8,198u8,252u8,66u8,208u8,59u8,160u8,80u8,167u8,83u8,235u8,227u8,36u8,70u8,255u8,83u8,],
vector[75u8,50u8,143u8,221u8,104u8,106u8,121u8,41u8,66u8,123u8,1u8,0u8,244u8,113u8,217u8,53u8,69u8,244u8,9u8,151u8,215u8,40u8,86u8,82u8,69u8,66u8,220u8,32u8,222u8,194u8,177u8,79u8,],
);
let code = vector::empty();
let code_chunk0 =
Expand Down Expand Up @@ -1545,6 +1545,6 @@ script {
13u8,0u8,0u8,0u8,0u8,0u8,0u8,
];
code::publish_package_txn(&framework_signer, metadata_chunk1, code);
version::upgrade_set_git(&framework_signer, x"bf5bd0e012f2cc45593b24ff02faf612c470b704")
version::upgrade_set_git(&framework_signer, x"a08e5536f77a9a9f4b1e0344a965bbb68e428dc5")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ license = ''

[dev-addresses]
[dependencies.LibraFramework]
local = '/home/zoz/libra-framework/framework/libra-framework'
local = '/root/libra-framework/framework/libra-framework'

[dev-dependencies]
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d53bc5c8ff4e0fea75d59dab320d605a92c6fc42d03ba050a753ebe32446ff53
4b328fdd686a7929427b0100f471d93545f40997d72856524542dc20dec2b14f
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Upgrade proposal for package `VendorStdlib`

// Framework commit hash: bf5bd0e012f2cc45593b24ff02faf612c470b704
// Framework commit hash: a08e5536f77a9a9f4b1e0344a965bbb68e428dc5
// Builder commit hash: db1137ba1f8e7301e325021f71f740063daaf76e

// Next step script hash: 4fb21e4efa7652c08912aa00414cf38078c5d344956bcd05d54e2dfe2ed8e606
// Next step script hash: f4521b01f82714fff409a13a29a48c5286fc09f25d217f4e11368b5eb83322ca

// source digest: 5E12DDD8987B153D75378183FB77218A1FAB6038899EB9121ECD4BE94EC1D598
script {
Expand All @@ -17,7 +17,7 @@ script {
let framework_signer = diem_governance::resolve_multi_step_proposal(
proposal_id,
@0000000000000000000000000000000000000000000000000000000000000001,
vector[79u8,178u8,30u8,78u8,250u8,118u8,82u8,192u8,137u8,18u8,170u8,0u8,65u8,76u8,243u8,128u8,120u8,197u8,211u8,68u8,149u8,107u8,205u8,5u8,213u8,78u8,45u8,254u8,46u8,216u8,230u8,6u8,],
vector[244u8,82u8,27u8,1u8,248u8,39u8,20u8,255u8,244u8,9u8,161u8,58u8,41u8,164u8,140u8,82u8,134u8,252u8,9u8,242u8,93u8,33u8,127u8,78u8,17u8,54u8,139u8,94u8,184u8,51u8,34u8,202u8,],
);
let code = vector::empty();
let code_chunk0 =
Expand Down Expand Up @@ -7332,6 +7332,6 @@ script {
vector::append(&mut metadata_chunk1, metadata_chunk2);
vector::append(&mut metadata_chunk1, metadata_chunk3);
code::publish_package_txn(&framework_signer, metadata_chunk1, code);
version::upgrade_set_git(&framework_signer, x"bf5bd0e012f2cc45593b24ff02faf612c470b704")
version::upgrade_set_git(&framework_signer, x"a08e5536f77a9a9f4b1e0344a965bbb68e428dc5")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ license = ''

[dev-addresses]
[dependencies.LibraFramework]
local = '/home/zoz/libra-framework/framework/libra-framework'
local = '/root/libra-framework/framework/libra-framework'

[dev-dependencies]
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4fb21e4efa7652c08912aa00414cf38078c5d344956bcd05d54e2dfe2ed8e606
f4521b01f82714fff409a13a29a48c5286fc09f25d217f4e11368b5eb83322ca
Loading

0 comments on commit 40035e4

Please sign in to comment.