-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor moveos_stdlib::move_module and add example of issue coin without code #1107
Merged
+766
−297
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc61774
add module_template
pause125 fb8c857
add native functions for replaceing u256 and u8 constant
pause125 50581a0
refactor native move_module functions
pause125 84fdb8a
add example and tests
pause125 9626f3a
update example
pause125 f7868d6
fmt
pause125 fcc6b17
fix
pause125 18e6844
fix
pause125 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "module_template" | ||
version = "0.0.1" | ||
|
||
[dependencies] | ||
#MoveStdlib = { git = "https://github.com/rooch-network/rooch.git", subdir = "moveos/moveos-stdlib/move-stdlib", rev = "main" } | ||
#MoveosStdlib = { git = "https://github.com/rooch-network/rooch.git", subdir = "moveos/moveos-stdlib/moveos-stdlib", rev = "main" } | ||
#RoochFramework = { git = "https://github.com/rooch-network/rooch.git", subdir = "crates/rooch-framework", rev = "main" } | ||
|
||
MoveStdlib = { local = "../../moveos/moveos-stdlib/move-stdlib" } | ||
MoveosStdlib = { local = "../../moveos/moveos-stdlib/moveos-stdlib" } | ||
RoochFramework = { local = "../../crates/rooch-framework" } | ||
|
||
[addresses] | ||
rooch_examples = "_" | ||
std = "0x1" | ||
moveos_std = "0x2" | ||
rooch_framework = "0x3" | ||
|
||
[dev-addresses] | ||
rooch_examples = "0x42" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) RoochNetwork | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
module rooch_examples::coin_factory { | ||
use std::string::{Self, String}; | ||
use std::vector; | ||
use moveos_std::signer; | ||
use moveos_std::table::{Self, Table}; | ||
use moveos_std::account_storage; | ||
use moveos_std::context::Context; | ||
use moveos_std::move_module; | ||
|
||
const TEMPLATE_MODULE_ADDRESS: address = @0xdeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadead; | ||
const TEMPLATE_MODULE_IDENTIFIER: vector<u8> = b"coin_module_identifier_placeholder"; | ||
const TEMPLATE_COIN_STRUCT_IDENTIFIER_PLACEHOLDER: vector<u8> = b"COIN_STRUCT_IDENTIFIER_PLACEHOLDER"; | ||
const TEMPLATE_COIN_NAME_PLACEHOLDER: vector<u8> = b"COIN_NAME_PLACEHOLDER"; | ||
const TEMPLATE_COIN_SYMBOL_PLACEHOLDER: vector<u8> = b"COIN_SYMBOL_PLACEHOLDER"; | ||
const TEMPLATE_COIN_SUPPLY_PLACEHOLDER: u256 = 123_321_123_456u256; | ||
const TEMPLATE_COIN_DECIMALS: u8 = 222u8; | ||
|
||
struct TemplateStore has key, store { | ||
templates: Table<String, vector<u8>>, | ||
} | ||
|
||
fun init(ctx: &mut Context) { | ||
let module_signer = signer::module_signer<TemplateStore>(); | ||
let templates = table::new<String, vector<u8>>(ctx); | ||
account_storage::global_move_to(ctx, &module_signer, TemplateStore { templates }); | ||
} | ||
|
||
public entry fun register_template(ctx: &mut Context) { | ||
let name = string::utf8(b"fixed_supply_coin"); | ||
let template_bytes = x"a11ceb0b060000000b010012021220033250048201140596017e079402e50208f904800106f9057b0af4060e0c82077d0dff070200000101020202030204020503060307030800090c00000a0800030b0000040f0c0100010810080007110001080101170700000c000100000d020100051204050002130708010804140a08010808150c0d010806160e01010c0118101100071912010108051a01130100071b140d0108081c0215010c081616010108021d1701010803060409050b060b080b090b0a0b0b0b0c0b0d0602070802060c000107080202050b0501080001060c010501080102070802050107090001080401070b03010900010800020708040f010b0501090003070802050b05010900030b050108000b030108040c010a02010806040708020806080602010c020708020f010b03010804020708040b0501090003070802060c090022636f696e5f6d6f64756c655f6964656e7469666965725f706c616365686f6c64657206737472696e670f6163636f756e745f73746f7261676507636f6e74657874066f626a656374067369676e6572126163636f756e745f636f696e5f73746f726504636f696e0a636f696e5f73746f726522434f494e5f5354525543545f4944454e5449464945525f504c414345484f4c44455208547265617375727907436f6e746578740666617563657404696e69740b64756d6d795f6669656c64064f626a65637409436f696e53746f726504436f696e0a616464726573735f6f6611676c6f62616c5f626f72726f775f6d75740a626f72726f775f6d7574087769746864726177076465706f73697406537472696e6704757466380f72656769737465725f657874656e640d6d6f64756c655f7369676e65720b6d696e745f657874656e64116372656174655f636f696e5f73746f72650e676c6f62616c5f6d6f76655f746fdeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadead0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030201de0f20800283b61c0000000000000000000000000000000000000000000000000000000520deadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadead0a021615434f494e5f4e414d455f504c414345484f4c4445520a021817434f494e5f53594d424f4c5f504c414345484f4c4445520002010e01010201080b030108040001040003100b0111020c020a00070238000f0038014a102700000000000000000000000000000000000000000000000000000000000038020c030b000b020b03380302010000000f1a0a0007031107070411070700380438050c030a00070138060c010a0038070c020d0238010b0138080b000e030b021201380902010000"; | ||
|
||
let template_store = account_storage::global_borrow_mut<TemplateStore>(ctx, @rooch_examples); | ||
table::add(&mut template_store.templates, name, template_bytes); | ||
} | ||
|
||
public entry fun issue_fixed_supply_coin(ctx: &mut Context, account: &signer, | ||
module_name: String, coin_name: String, | ||
coin_symbol: String, total_supply: u256, decimals: u8 | ||
) { | ||
let template_store = account_storage::global_borrow_mut<TemplateStore>(ctx, @rooch_examples); | ||
let template_bytes = *table::borrow(&template_store.templates, string::utf8(b"fixed_supply_coin")); | ||
let template_module = move_module::new(template_bytes); | ||
|
||
let sender = signer::address_of(account); | ||
let modules = vector::singleton(template_module); | ||
let modules = move_module::binding_module_address(modules, TEMPLATE_MODULE_ADDRESS, sender); | ||
let modules = move_module::replace_module_identiner( | ||
modules, | ||
vector::singleton(string::utf8(TEMPLATE_MODULE_IDENTIFIER)), | ||
vector::singleton(module_name) | ||
); | ||
let modules = move_module::replace_struct_identifier( | ||
modules, | ||
vector::singleton(string::utf8(TEMPLATE_COIN_STRUCT_IDENTIFIER_PLACEHOLDER)), | ||
vector::singleton(coin_symbol) | ||
); | ||
|
||
let old_strings = vector::singleton(string::utf8(TEMPLATE_COIN_NAME_PLACEHOLDER)); | ||
vector::push_back(&mut old_strings, string::utf8(TEMPLATE_COIN_SYMBOL_PLACEHOLDER)); | ||
let new_strings = vector::singleton(coin_name); | ||
vector::push_back(&mut new_strings, coin_symbol); | ||
let modules = move_module::replace_constant_string( | ||
modules, | ||
old_strings, | ||
new_strings | ||
); | ||
|
||
let new_supply = vector::singleton(total_supply); | ||
let old_supply = vector::singleton(TEMPLATE_COIN_SUPPLY_PLACEHOLDER); | ||
let modules = move_module::replace_constant_u256(modules, old_supply, new_supply); | ||
|
||
let new_decimal = vector::singleton(decimals); | ||
let old_decimal = vector::singleton(TEMPLATE_COIN_DECIMALS); | ||
let modules = move_module::replace_constant_u8(modules, old_decimal, new_decimal); | ||
|
||
// publish modules | ||
account_storage::publish_modules(ctx, account, modules); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
examples/module_template/template/fixed_supply_coin_template.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) RoochNetwork | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
module 0xdeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadead::coin_module_identifier_placeholder { | ||
|
||
use std::string; | ||
use moveos_std::signer; | ||
use moveos_std::account_storage; | ||
use moveos_std::context::Context; | ||
use moveos_std::object::{Self, Object}; | ||
use rooch_framework::coin; | ||
use rooch_framework::coin_store::{Self, CoinStore}; | ||
use rooch_framework::account_coin_store; | ||
|
||
|
||
struct COIN_STRUCT_IDENTIFIER_PLACEHOLDER has key, store {} | ||
|
||
struct Treasury has key { | ||
coin_store: Object<CoinStore> | ||
} | ||
|
||
const TOTAL_SUPPLY: u256 = 123_321_123_456u256; | ||
const DECIMALS: u8 = 222u8; | ||
|
||
|
||
fun init(ctx: &mut Context) { | ||
coin::register_extend<COIN_STRUCT_IDENTIFIER_PLACEHOLDER>( | ||
ctx, | ||
string::utf8(b"COIN_NAME_PLACEHOLDER"), | ||
string::utf8(b"COIN_SYMBOL_PLACEHOLDER"), | ||
DECIMALS, | ||
); | ||
let coins_signer = signer::module_signer<COIN_STRUCT_IDENTIFIER_PLACEHOLDER>(); | ||
// Mint the total supply of coins, and store it to the treasury | ||
let coin = coin::mint_extend<COIN_STRUCT_IDENTIFIER_PLACEHOLDER>(ctx, TOTAL_SUPPLY); | ||
let coin_store_ref = coin_store::create_coin_store<COIN_STRUCT_IDENTIFIER_PLACEHOLDER>(ctx); | ||
coin_store::deposit(object::borrow_mut(&mut coin_store_ref), coin); | ||
account_storage::global_move_to(ctx, &coins_signer, Treasury { coin_store: coin_store_ref }); | ||
} | ||
|
||
/// Provide a faucet to give out coins to users | ||
/// In a real world scenario, the coins should be given out in the application business logic. | ||
public entry fun faucet(ctx: &mut Context, account: &signer) { | ||
let account_addr = signer::address_of(account); | ||
let treasury = account_storage::global_borrow_mut<Treasury>(ctx, @0xdeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadeadead); | ||
let coin = coin_store::withdraw<COIN_STRUCT_IDENTIFIER_PLACEHOLDER>(object::borrow_mut(&mut treasury.coin_store), 10000); | ||
account_coin_store::deposit(ctx, account_addr, coin); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
293 changes: 177 additions & 116 deletions
293
moveos/moveos-stdlib/moveos-stdlib/sources/move_module.move
Large diffs are not rendered by default.
Oops, something went wrong.
461 changes: 283 additions & 178 deletions
461
moveos/moveos-stdlib/src/natives/moveos_stdlib/move_module.rs
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put in the
init()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an example, I think it doesn't matter. My first thought is to make the name and temple_bytes as function parameters. But the template bytes is too long, which putted here maybe more clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example will auto-deploy to the devnet, and we can integrate it into the Dashboard.
#1109