Skip to content

Commit

Permalink
[asset-mapping] add fake money for asset mapping assign
Browse files Browse the repository at this point in the history
  • Loading branch information
welbon committed Jan 11, 2025
1 parent bf6dbad commit b25deba
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 50 deletions.
36 changes: 15 additions & 21 deletions vm/framework/starcoin-framework/doc/asset_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,8 @@ Requirements:

<a href="fungible_asset.md#0x1_fungible_asset_deposit">fungible_asset::deposit</a>(fungible_store, <a href="fungible_asset.md#0x1_fungible_asset">fungible_asset</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="asset_mapping.md#0x1_asset_mapping_create_store_from_coin">asset_mapping::create_store_from_coin</a> | withdraw test begin"));
// {
// <b>let</b> test_fa = <a href="fungible_asset.md#0x1_fungible_asset_withdraw">fungible_asset::withdraw</a>(
// &<a href="object.md#0x1_object_generate_signer_for_extending">object::generate_signer_for_extending</a>(&extend_ref),
// fungible_store,
// 10000
// );
// <b>assert</b>!(<a href="fungible_asset.md#0x1_fungible_asset_amount">fungible_asset::amount</a>(&test_fa) == 10000, 10000);
// <a href="fungible_asset.md#0x1_fungible_asset_deposit">fungible_asset::deposit</a>(fungible_store, test_fa);
// };
// <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="asset_mapping.md#0x1_asset_mapping_create_store_from_coin">asset_mapping::create_store_from_coin</a> | withdraw test end"));

// Add token mapping <a href="coin.md#0x1_coin">coin</a> type
<b>let</b> asset_coin_type =
<b>borrow_global_mut</b>&lt;<a href="asset_mapping.md#0x1_asset_mapping_AssetMappingPool">AssetMappingPool</a>&gt;(<a href="system_addresses.md#0x1_system_addresses_get_starcoin_framework">system_addresses::get_starcoin_framework</a>());
<b>let</b> asset_coin_type = <b>borrow_global_mut</b>&lt;<a href="asset_mapping.md#0x1_asset_mapping_AssetMappingPool">AssetMappingPool</a>&gt;(<a href="system_addresses.md#0x1_system_addresses_get_starcoin_framework">system_addresses::get_starcoin_framework</a>());
<a href="../../starcoin-stdlib/doc/smart_table.md#0x1_smart_table_add">smart_table::add</a>(
&<b>mut</b> asset_coin_type.token_mapping,
<a href="../../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(old_token_str),
Expand Down Expand Up @@ -308,7 +295,7 @@ Creates a store for a specific token type
- extend_ref: Extension reference for the store


<pre><code><b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_create_store_for_coin_type">create_store_for_coin_type</a>&lt;T&gt;(framework: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>): (<a href="object.md#0x1_object_Object">object::Object</a>&lt;<a href="fungible_asset.md#0x1_fungible_asset_Metadata">fungible_asset::Metadata</a>&gt;, <a href="object.md#0x1_object_Object">object::Object</a>&lt;<a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">fungible_asset::FungibleStore</a>&gt;, <a href="object.md#0x1_object_ExtendRef">object::ExtendRef</a>)
<pre><code><b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_create_store_for_coin_type">create_store_for_coin_type</a>&lt;T&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>): (<a href="object.md#0x1_object_Object">object::Object</a>&lt;<a href="fungible_asset.md#0x1_fungible_asset_Metadata">fungible_asset::Metadata</a>&gt;, <a href="object.md#0x1_object_Object">object::Object</a>&lt;<a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">fungible_asset::FungibleStore</a>&gt;, <a href="object.md#0x1_object_ExtendRef">object::ExtendRef</a>)
</code></pre>


Expand All @@ -317,11 +304,11 @@ Creates a store for a specific token type
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_create_store_for_coin_type">create_store_for_coin_type</a>&lt;T&gt;(framework: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>): (Object&lt;Metadata&gt;, Object&lt;FungibleStore&gt;, ExtendRef) {
<pre><code><b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_create_store_for_coin_type">create_store_for_coin_type</a>&lt;T&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>): (Object&lt;Metadata&gt;, Object&lt;FungibleStore&gt;, ExtendRef) {
<a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&std::string::utf8(b"asset_mapping::create_store_for_type | entered"));

<b>let</b> metadata = <a href="coin.md#0x1_coin_ensure_paired_metadata">coin::ensure_paired_metadata</a>&lt;T&gt;();
<b>let</b> construct_ref = <a href="object.md#0x1_object_create_object_from_account">object::create_object_from_account</a>(framework);
<b>let</b> construct_ref = <a href="object.md#0x1_object_create_object_from_account">object::create_object_from_account</a>(<a href="account.md#0x1_account">account</a>);

<b>let</b> store = <a href="fungible_asset.md#0x1_fungible_asset_create_store">fungible_asset::create_store</a>(&construct_ref, metadata);

Expand Down Expand Up @@ -424,7 +411,7 @@ Requirements:
- Sufficient balance must exist


<pre><code><b>public</b> <b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_assign_to_account">assign_to_account</a>(token_issuer: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, receiver: <b>address</b>, old_token_str: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, amount: u64)
<pre><code><b>public</b> <b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_assign_to_account">assign_to_account</a>(system_account: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, receiver: <b>address</b>, old_token_str: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, amount: u64)
</code></pre>


Expand All @@ -434,15 +421,22 @@ Requirements:


<pre><code><b>public</b> <b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_assign_to_account">assign_to_account</a>(
token_issuer: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
system_account: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
receiver: <b>address</b>,
old_token_str: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
amount: u64
) <b>acquires</b> <a href="asset_mapping.md#0x1_asset_mapping_AssetMappingPool">AssetMappingPool</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="asset_mapping.md#0x1_asset_mapping_assign_to_account">asset_mapping::assign_to_account</a> | entered"));

<b>let</b> account_addr = <a href="../../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(system_account);
<b>assert</b>!(
<a href="system_addresses.md#0x1_system_addresses_is_starcoin_framework_address">system_addresses::is_starcoin_framework_address</a>(account_addr) ||
<a href="system_addresses.md#0x1_system_addresses_is_core_resource_address">system_addresses::is_core_resource_address</a>(account_addr),
<a href="asset_mapping.md#0x1_asset_mapping_EINVALID_SIGNER">EINVALID_SIGNER</a>
);

<b>assert</b>!(
<b>exists</b>&lt;<a href="asset_mapping.md#0x1_asset_mapping_AssetMappingPool">AssetMappingPool</a>&gt;(<a href="../../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(token_issuer)),
<b>exists</b>&lt;<a href="asset_mapping.md#0x1_asset_mapping_AssetMappingPool">AssetMappingPool</a>&gt;(<a href="system_addresses.md#0x1_system_addresses_get_starcoin_framework">system_addresses::get_starcoin_framework</a>()),
<a href="../../move-stdlib/doc/error.md#0x1_error_invalid_state">error::invalid_state</a>(<a href="asset_mapping.md#0x1_asset_mapping_EINVALID_ASSET_MAPPING_POOL">EINVALID_ASSET_MAPPING_POOL</a>)
);

Expand All @@ -457,7 +451,7 @@ Requirements:
<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="asset_mapping.md#0x1_asset_mapping_assign_to_account">asset_mapping::assign_to_account</a> | metadata"));
<a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&<a href="fungible_asset.md#0x1_fungible_asset_is_frozen">fungible_asset::is_frozen</a>(mapping_store.fungible_store));

<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="asset_mapping.md#0x1_asset_mapping_assign_to_account">asset_mapping::assign_to_account</a> | <a href="fungible_asset.md#0x1_fungible_asset_withdraw">fungible_asset::withdraw</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="asset_mapping.md#0x1_asset_mapping_assign_to_account">asset_mapping::assign_to_account</a> | <a href="fungible_asset.md#0x1_fungible_asset_withdraw">fungible_asset::withdraw</a>"));
<b>let</b> mapping_fa = <a href="fungible_asset.md#0x1_fungible_asset_withdraw">fungible_asset::withdraw</a>(
&<a href="object.md#0x1_object_generate_signer_for_extending">object::generate_signer_for_extending</a>(&mapping_store.extend_ref),
mapping_store.fungible_store,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
processed 4 tasks
processed 7 tasks

task 3 'run'. lines 7-19:
task 5 'run'. lines 57-67:
{
"gas_used": 2260938,
"status": {
"MoveAbort": {
"location": {
"Module": {
"address": "0x00000000000000000000000000000001",
"name": "fungible_asset"
}
},
"abort_code": "393239"
}
}
"gas_used": 5320541,
"status": "Executed"
}

task 6 'run'. lines 69-93:
{
"gas_used": 4349075,
"status": "Executed"
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,107 @@
//# init -n dev

//# faucet --addr alice --amount 100
//# faucet --addr alice --amount 0

//# faucet --addr Genesis
//# faucet --addr bob --amount 10000000000000000

//# faucet --addr Genesis --amount 10000000000000000

//# 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 starcoin_framework::starcoin_coin::STC;
use bob::fake_money::{FakeMoney};
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) == 100, 10001);
asset_mapping::assign_to_account(&framework, @alice, b"0x1::STC::STC", 100);
assert!(coin::balance<STC>(@alice) == 200, 10002);
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);
}
}
// check: EXECUTED

//# run --signers core_resources
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 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
20 changes: 13 additions & 7 deletions vm/framework/starcoin-framework/sources/asset_mapping.move
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ module starcoin_framework::asset_mapping {
fungible_asset::deposit(fungible_store, fungible_asset);

// Add token mapping coin type
let asset_coin_type =
borrow_global_mut<AssetMappingPool>(system_addresses::get_starcoin_framework());
let asset_coin_type = borrow_global_mut<AssetMappingPool>(system_addresses::get_starcoin_framework());
smart_table::add(
&mut asset_coin_type.token_mapping,
string::utf8(old_token_str),
Expand All @@ -127,11 +126,11 @@ module starcoin_framework::asset_mapping {
/// - metadata: Token metadata object
/// - store: Created fungible store
/// - extend_ref: Extension reference for the store
fun create_store_for_coin_type<T>(framework: &signer): (Object<Metadata>, Object<FungibleStore>, ExtendRef) {
fun create_store_for_coin_type<T>(account: &signer): (Object<Metadata>, Object<FungibleStore>, ExtendRef) {
debug::print(&std::string::utf8(b"asset_mapping::create_store_for_type | entered"));

let metadata = coin::ensure_paired_metadata<T>();
let construct_ref = object::create_object_from_account(framework);
let construct_ref = object::create_object_from_account(account);

let store = fungible_asset::create_store(&construct_ref, metadata);

Expand Down Expand Up @@ -184,15 +183,22 @@ module starcoin_framework::asset_mapping {
/// - Valid proof must be provided
/// - Sufficient balance must exist
public fun assign_to_account(
token_issuer: &signer,
system_account: &signer,
receiver: address,
old_token_str: vector<u8>,
amount: u64
) acquires AssetMappingPool {
debug::print(&string::utf8(b"asset_mapping::assign_to_account | entered"));

let account_addr = signer::address_of(system_account);
assert!(
system_addresses::is_starcoin_framework_address(account_addr) ||
system_addresses::is_core_resource_address(account_addr),
EINVALID_SIGNER
);

assert!(
exists<AssetMappingPool>(signer::address_of(token_issuer)),
exists<AssetMappingPool>(system_addresses::get_starcoin_framework()),
error::invalid_state(EINVALID_ASSET_MAPPING_POOL)
);

Expand All @@ -207,7 +213,7 @@ module starcoin_framework::asset_mapping {
debug::print(&string::utf8(b"asset_mapping::assign_to_account | metadata"));
debug::print(&fungible_asset::is_frozen(mapping_store.fungible_store));

debug::print(&string::utf8(b"asset_mapping::assign_to_account | fungible_asset::withdraw:"));
debug::print(&string::utf8(b"asset_mapping::assign_to_account | fungible_asset::withdraw"));
let mapping_fa = fungible_asset::withdraw(
&object::generate_signer_for_extending(&mapping_store.extend_ref),
mapping_store.fungible_store,
Expand Down

0 comments on commit b25deba

Please sign in to comment.