-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[aptos-framework] Remove balance checks in DFA #16313
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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
92 changes: 92 additions & 0 deletions
92
aptos-move/framework/aptos-framework/tests/clamped_token.move
This file contains hidden or 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,92 @@ | ||
| #[test_only] | ||
| module 0xcafe::clamped_token { | ||
| // Create a token with max amount one can withdraw on each withdraw call. | ||
|
|
||
| use aptos_framework::fungible_asset::{Self, FungibleAsset, RawBalanceRef, RawSupplyRef, TransferRef}; | ||
| use aptos_framework::dispatchable_fungible_asset; | ||
| use aptos_framework::object::{ConstructorRef, Object}; | ||
| use aptos_framework::function_info; | ||
|
|
||
| use std::option; | ||
| use std::option::Option; | ||
| use std::signer; | ||
| use std::string; | ||
|
|
||
| struct BalanceStore has key { | ||
| balance_ref: RawBalanceRef, | ||
| supply_ref: RawSupplyRef, | ||
| } | ||
|
|
||
| public fun initialize(account: &signer, constructor_ref: &ConstructorRef) { | ||
| assert!(signer::address_of(account) == @0xcafe, 1); | ||
| let balance_ref = fungible_asset::generate_raw_balance_ref(constructor_ref); | ||
| let supply_ref = fungible_asset::generate_raw_supply_ref(constructor_ref); | ||
| move_to<BalanceStore>(account, BalanceStore { balance_ref, supply_ref }); | ||
|
|
||
| let balance_value = function_info::new_function_info( | ||
| account, | ||
| string::utf8(b"clamped_token"), | ||
| string::utf8(b"derived_balance"), | ||
| ); | ||
| let supply_value = function_info::new_function_info( | ||
| account, | ||
| string::utf8(b"clamped_token"), | ||
| string::utf8(b"derived_supply"), | ||
| ); | ||
|
|
||
| let withdraw = function_info::new_function_info( | ||
| account, | ||
| string::utf8(b"clamped_token"), | ||
| string::utf8(b"withdraw"), | ||
| ); | ||
|
|
||
| let deposit = function_info::new_function_info( | ||
| account, | ||
| string::utf8(b"clamped_token"), | ||
| string::utf8(b"deposit"), | ||
| ); | ||
|
|
||
| dispatchable_fungible_asset::register_dispatch_functions( | ||
| constructor_ref, | ||
| option::some(withdraw), | ||
| option::some(deposit), | ||
| option::some(balance_value) | ||
| ); | ||
| dispatchable_fungible_asset::register_derive_supply_dispatch_function( | ||
| constructor_ref, | ||
| option::some(supply_value) | ||
| ); | ||
| } | ||
|
|
||
| public fun derived_balance<T: key>(store: Object<T>): u64 acquires BalanceStore { | ||
| fungible_asset::balance_with_ref( | ||
| &borrow_global<BalanceStore>(@0xcafe).balance_ref, | ||
| store | ||
| ) | ||
| } | ||
|
|
||
| public fun derived_supply<T: key>(metadata: Object<T>): Option<u128> acquires BalanceStore { | ||
| option::some(option::extract(&mut fungible_asset::supply_with_ref( | ||
| &borrow_global<BalanceStore>(@0xcafe).supply_ref, | ||
| metadata | ||
| ))) | ||
| } | ||
|
|
||
| public fun withdraw<T: key>( | ||
| store: Object<T>, | ||
| amount: u64, | ||
| transfer_ref: &TransferRef, | ||
| ): FungibleAsset { | ||
| // Clamp the max amount of asset to withdraw: at most 10 can be withdrawn each call. | ||
| assert!(amount <= 10, 0); | ||
| fungible_asset::withdraw_with_ref(transfer_ref, store, amount) | ||
| } | ||
|
|
||
| public fun deposit<T: key>( | ||
| store: Object<T>, | ||
| fa: FungibleAsset, | ||
| transfer_ref: &TransferRef, | ||
| ) { | ||
| fungible_asset::deposit_with_ref(transfer_ref, store, fa) | ||
| } | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
aptos-move/framework/aptos-framework/tests/clamped_token_tests.move
This file contains hidden or 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,55 @@ | ||
| #[test_only] | ||
| module aptos_framework::clamped_token_tests { | ||
| use aptos_framework::fungible_asset::{Self, Metadata, TestToken}; | ||
| use aptos_framework::dispatchable_fungible_asset; | ||
| use aptos_framework::object; | ||
| use 0xcafe::clamped_token; | ||
| use std::option; | ||
|
|
||
| #[test(creator = @0xcafe)] | ||
| fun test_clamped( | ||
| creator: &signer, | ||
| ) { | ||
| let (creator_ref, token_object) = fungible_asset::create_test_token(creator); | ||
| let (mint, _, _, _) = fungible_asset::init_test_metadata(&creator_ref); | ||
| let metadata = object::convert<TestToken, Metadata>(token_object); | ||
|
|
||
| let creator_store = fungible_asset::create_test_store(creator, metadata); | ||
|
|
||
| clamped_token::initialize(creator, &creator_ref); | ||
|
|
||
| assert!(dispatchable_fungible_asset::derived_supply(metadata) == option::some(0), 2); | ||
| // Mint | ||
| let fa = fungible_asset::mint(&mint, 100); | ||
| dispatchable_fungible_asset::deposit(creator_store, fa); | ||
|
|
||
| assert!(dispatchable_fungible_asset::derived_balance(creator_store) == 100, 4); | ||
| assert!(dispatchable_fungible_asset::derived_supply(metadata) == option::some(100), 5); | ||
|
|
||
| let fa = dispatchable_fungible_asset::withdraw(creator, creator_store, 5); | ||
| dispatchable_fungible_asset::deposit(creator_store, fa); | ||
| } | ||
|
|
||
| #[test(creator = @0xcafe)] | ||
| #[expected_failure(abort_code = 0, location = 0xcafe::clamped_token)] | ||
| fun test_clamped_aborted( | ||
| creator: &signer, | ||
| ) { | ||
| let (creator_ref, token_object) = fungible_asset::create_test_token(creator); | ||
| let (mint, _, _, _) = fungible_asset::init_test_metadata(&creator_ref); | ||
| let metadata = object::convert<TestToken, Metadata>(token_object); | ||
|
|
||
| let creator_store = fungible_asset::create_test_store(creator, metadata); | ||
|
|
||
| clamped_token::initialize(creator, &creator_ref); | ||
|
|
||
| assert!(dispatchable_fungible_asset::derived_supply(metadata) == option::some(0), 2); | ||
| // Mint | ||
| let fa = fungible_asset::mint(&mint, 100); | ||
| dispatchable_fungible_asset::deposit(creator_store, fa); | ||
|
|
||
| // Failed to withdraw as it exceeds the withdraw limit. | ||
| let fa = dispatchable_fungible_asset::withdraw(creator, creator_store, 20); | ||
| dispatchable_fungible_asset::deposit(creator_store, fa); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,7 +7,6 @@ module aptos_framework::nil_op_token_tests { | |
| use std::option; | ||
|
|
||
| #[test(creator = @0xcafe)] | ||
| #[expected_failure(abort_code=0x70002, location=aptos_framework::dispatchable_fungible_asset)] | ||
| fun test_nil_op_token( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update the test name to record the new expected behavior now? |
||
| creator: &signer, | ||
| ) { | ||
|
|
@@ -26,7 +25,6 @@ module aptos_framework::nil_op_token_tests { | |
| // Deposit will cause an re-entrant call into dispatchable_fungible_asset | ||
| dispatchable_fungible_asset::deposit(creator_store, fa); | ||
|
|
||
| // Withdraw will fail because it's not drawing the basic amount. | ||
| let fa = dispatchable_fungible_asset::withdraw(creator, creator_store, 10); | ||
| dispatchable_fungible_asset::deposit(creator_store, fa); | ||
| } | ||
|
|
||
This file contains hidden or 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
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.
what does clamped mean here?
add some comments?