Skip to content

Commit

Permalink
[asset-mapping] change implements for asset mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
welbon committed Jan 11, 2025
1 parent b25deba commit d9797b5
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 211 deletions.
119 changes: 57 additions & 62 deletions vm/framework/starcoin-framework/doc/asset_mapping.md

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion vm/framework/starcoin-framework/doc/coin.md
Original file line number Diff line number Diff line change
Expand Up @@ -1591,9 +1591,15 @@ Conversion from coin to fungible asset
<pre><code><b>public</b> <b>fun</b> <a href="coin.md#0x1_coin_coin_to_fungible_asset">coin_to_fungible_asset</a>&lt;CoinType&gt;(
<a href="coin.md#0x1_coin">coin</a>: <a href="coin.md#0x1_coin_Coin">Coin</a>&lt;CoinType&gt;
): FungibleAsset <b>acquires</b> <a href="coin.md#0x1_coin_CoinConversionMap">CoinConversionMap</a>, <a href="coin.md#0x1_coin_CoinInfo">CoinInfo</a> {
<a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&<a href="../../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"<a href="coin.md#0x1_coin_coin_to_fungible_asset">coin::coin_to_fungible_asset</a> | entered"));

<b>let</b> metadata = <a href="coin.md#0x1_coin_ensure_paired_metadata">ensure_paired_metadata</a>&lt;CoinType&gt;();
<b>let</b> amount = <a href="coin.md#0x1_coin_burn_internal">burn_internal</a>(<a href="coin.md#0x1_coin">coin</a>);
<a href="fungible_asset.md#0x1_fungible_asset_mint_internal">fungible_asset::mint_internal</a>(metadata, amount)

<b>let</b> ret = <a href="fungible_asset.md#0x1_fungible_asset_mint_internal">fungible_asset::mint_internal</a>(metadata, amount);

<a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&<a href="../../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"<a href="coin.md#0x1_coin_coin_to_fungible_asset">coin::coin_to_fungible_asset</a> | exited"));
ret
}
</code></pre>

Expand Down
4 changes: 2 additions & 2 deletions vm/framework/starcoin-framework/doc/fungible_asset.md
Original file line number Diff line number Diff line change
Expand Up @@ -3576,11 +3576,11 @@ Decrease the supply of a fungible asset by burning.


<pre><code>inline <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_borrow_store_resource">borrow_store_resource</a>&lt;T: key&gt;(store: &Object&lt;T&gt;): &<a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">FungibleStore</a> <b>acquires</b> <a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">FungibleStore</a> {
<a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&<a href="../../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"<a href="fungible_asset.md#0x1_fungible_asset_borrow_store_resource">fungible_asset::borrow_store_resource</a> | entered"));
// <a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&<a href="../../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"<a href="fungible_asset.md#0x1_fungible_asset_borrow_store_resource">fungible_asset::borrow_store_resource</a> | entered"));
<b>let</b> store_addr = <a href="object.md#0x1_object_object_address">object::object_address</a>(store);
<a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&store_addr);
<b>assert</b>!(<b>exists</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">FungibleStore</a>&gt;(store_addr), <a href="../../move-stdlib/doc/error.md#0x1_error_not_found">error::not_found</a>(<a href="fungible_asset.md#0x1_fungible_asset_EFUNGIBLE_STORE_EXISTENCE">EFUNGIBLE_STORE_EXISTENCE</a>));
<a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&<a href="../../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"<a href="fungible_asset.md#0x1_fungible_asset_borrow_store_resource">fungible_asset::borrow_store_resource</a> | exited"));
// <a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&<a href="../../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"<a href="fungible_asset.md#0x1_fungible_asset_borrow_store_resource">fungible_asset::borrow_store_resource</a> | exited"));
<b>borrow_global</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">FungibleStore</a>&gt;(store_addr)
}
</code></pre>
Expand Down
2 changes: 0 additions & 2 deletions vm/framework/starcoin-framework/doc/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -1097,11 +1097,9 @@ by knowing the user generated seed used to create them. Named objects cannot be


<pre><code><b>public</b> <b>fun</b> <a href="object.md#0x1_object_create_named_object">create_named_object</a>(creator: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, seed: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="object.md#0x1_object_ConstructorRef">ConstructorRef</a> {
// <a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&<a href="../../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"<a href="object.md#0x1_object_create_named_object">object::create_named_object</a> | entered"));
<b>let</b> creator_address = <a href="../../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(creator);
<b>let</b> obj_addr = <a href="object.md#0x1_object_create_object_address">create_object_address</a>(&creator_address, seed);
<b>let</b> ret = <a href="object.md#0x1_object_create_object_internal">create_object_internal</a>(creator_address, obj_addr, <b>false</b>);
// <a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&<a href="../../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"<a href="object.md#0x1_object_create_named_object">object::create_named_object</a> | exited"));
ret
}
</code></pre>
Expand Down
1 change: 1 addition & 0 deletions vm/framework/starcoin-framework/doc/stc_genesis.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ Overall governance allocation strategy:
b"0x1::STC::STC",
asset_mapping_coin
);
// fungible_asset::put_test_store_genesis(core_resource_account);

// Initialize <a href="treasury.md#0x1_treasury">treasury</a>
<b>let</b> treasury_withdraw_cap = <a href="treasury.md#0x1_treasury_initialize">treasury::initialize</a>(starcoin_framework, total_supply_stc);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
processed 7 tasks
processed 6 tasks

task 5 'run'. lines 57-67:
task 5 'run'. lines 108-120:
{
"gas_used": 5320541,
"status": "Executed"
}

task 6 'run'. lines 69-93:
{
"gas_used": 4349075,
"status": "Executed"
"gas_used": 936902,
"status": {
"ExecutionFailure": {
"location": {
"Module": {
"address": "0x00000000000000000000000000000001",
"name": "asset_mapping"
}
},
"function": 0,
"code_offset": 54
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,100 +8,113 @@

//# faucet --addr core_resources

//
// //# publish
// module bob::fake_money {
// use std::signer;
// use std::string;
//
// use starcoin_framework::coin;
//
// struct FakeMoney has key {}
//
// struct FakeMoneyCapabilities has key {
// burn_cap: coin::BurnCapability<FakeMoney>,
// freeze_cap: coin::FreezeCapability<FakeMoney>,
// mint_cap: coin::MintCapability<FakeMoney>,
// }
//
// public fun init(account: &signer, decimal: u8) {
// let (
// burn_cap,
// freeze_cap,
// mint_cap
// ) = coin::initialize<FakeMoney>(
// account,
// string::utf8(b"FakeMoney"),
// string::utf8(b"FakeMoney"),
// decimal,
// true,
// );
// coin::register<FakeMoney>(account);
// move_to(account, FakeMoneyCapabilities {
// burn_cap,
// freeze_cap,
// mint_cap,
// })
// }
//
// public fun mint(account: &signer, amount: u64): coin::Coin<FakeMoney> acquires FakeMoneyCapabilities {
// let cap = borrow_global<FakeMoneyCapabilities>(signer::address_of(account));
// coin::mint(amount, &cap.mint_cap)
// }
//
// public fun burn(coin: coin::Coin<FakeMoney>) acquires FakeMoneyCapabilities {
// let cap = borrow_global<FakeMoneyCapabilities>(@bob);
// coin::burn(coin, &cap.burn_cap)
// }
// }
// // check: EXECUTED
//
// //# run --signers bob
// script {
// use bob::fake_money::{Self, FakeMoney};
// use starcoin_framework::asset_mapping;
//
// fun test_create_fake_money_store(account: &signer) {
// fake_money::init(account, 9);
// let fake_money_coin = fake_money::mint(account, 100000000000);
// asset_mapping::create_store_from_coin<FakeMoney>(account, b"bob::fake_money::FakeMoney", fake_money_coin);
// }
// }
//
// //# run --signers Genesis
// script {
// use bob::fake_money::{FakeMoney};
// use starcoin_framework::coin;
// use starcoin_framework::asset_mapping;
//
// fun test_create_fake_money_store(account: &signer) {
// asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 50000000000);
// assert!(coin::balance<FakeMoney>(@bob) == 50000000000, 10001);
// }
// }
// // check: EXECUTED
//
// //# run --signers core_resources
// script {
// use starcoin_framework::coin;
// use starcoin_framework::asset_mapping;
// use bob::fake_money::{FakeMoney};
//
// fun test_create_fake_money_store(account: &signer) {
// asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 50000000000);
// assert!(coin::balance<FakeMoney>(@bob) == 100000000000, 10002);
// }
// }
// // check: EXECUTED
//
// //# run --signers core_resources
// script {
// use starcoin_framework::asset_mapping;
//
// fun test_create_fake_money_store(account: &signer) {
// asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 50000000000);
// }
// }
// // check: ABORT: fungible_asset.move 65540

//# publish
module bob::fake_money {
use std::signer;
use std::string;

use starcoin_framework::coin;

struct FakeMoney has key {}

struct FakeMoneyCapabilities has key {
burn_cap: coin::BurnCapability<FakeMoney>,
freeze_cap: coin::FreezeCapability<FakeMoney>,
mint_cap: coin::MintCapability<FakeMoney>,
}

public fun init(account: &signer, decimal: u8) {
let (
burn_cap,
freeze_cap,
mint_cap
) = coin::initialize<FakeMoney>(
account,
string::utf8(b"FakeMoney"),
string::utf8(b"FakeMoney"),
decimal,
true,
);
coin::register<FakeMoney>(account);
move_to(account, FakeMoneyCapabilities {
burn_cap,
freeze_cap,
mint_cap,
})
}

public fun mint(account: &signer, amount: u64): coin::Coin<FakeMoney> acquires FakeMoneyCapabilities {
let cap = borrow_global<FakeMoneyCapabilities>(signer::address_of(account));
coin::mint(amount, &cap.mint_cap)
}

public fun burn(coin: coin::Coin<FakeMoney>) acquires FakeMoneyCapabilities {
let cap = borrow_global<FakeMoneyCapabilities>(@bob);
coin::burn(coin, &cap.burn_cap)
}
}
// check: EXECUTED

//# run --signers bob
script {
use bob::fake_money::{Self, FakeMoney};
use starcoin_framework::asset_mapping;

fun test_create_fake_money_store(account: &signer) {
fake_money::init(account, 9);
let fake_money_coin = fake_money::mint(account, 100000000000);
asset_mapping::create_store_from_coin<FakeMoney>(account, b"bob::fake_money::FakeMoney", fake_money_coin);
}
}

//# run --signers Genesis
script {
use bob::fake_money::{FakeMoney};
use starcoin_framework::coin;
use starcoin_framework::asset_mapping;

fun test_create_fake_money_store(account: &signer) {
asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 100000000000);
assert!(coin::balance<FakeMoney>(@bob) == 100000000000, 10001);
}
}

//# run --signers core_resources
script {
use bob::fake_money::{FakeMoney};
use starcoin_framework::starcoin_coin::STC;
use starcoin_framework::coin;
use starcoin_framework::asset_mapping;

fun test_create_fake_money_store(account: &signer) {
asset_mapping::assign_to_account(account, @bob, b"bob::fake_money::FakeMoney", 100000000000);
assert!(coin::balance<FakeMoney>(@bob) == 100000000000, 10001);
fun test_asset_mapping_assign_to_account_with_proof(account: &signer) {
assert!(coin::balance<STC>(@alice) == 0, 10001);
asset_mapping::assign_to_account(account, @alice, b"0x1::STC::STC", 1000000000);
assert!(coin::balance<STC>(@alice) == 1000000000, 10002);
}
}

// //# run --signers Genesis
// script {
// use starcoin_framework::starcoin_coin::STC;
// use starcoin_framework::coin;
// use starcoin_framework::asset_mapping;
//
// fun test_asset_mapping_assign_to_account_with_proof(framework: signer) {
// assert!(coin::balance<STC>(@alice) == 0, 10001);
// asset_mapping::assign_to_account(&framework, @alice, b"0x1::STC::STC", 100);
// assert!(coin::balance<STC>(@alice) == 100, 10002);
// }
// }
// // check: EXECUTED
// check: EXECUTED
Loading

0 comments on commit d9797b5

Please sign in to comment.