-
Notifications
You must be signed in to change notification settings - Fork 44
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
Add Swap example #112
Merged
Merged
Add Swap example #112
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "facoin" | ||
version = "1.0.0" | ||
authors = [] | ||
|
||
[addresses] | ||
FACoin = "_" | ||
|
||
[dependencies.AptosFramework] | ||
git = "https://github.com/aptos-labs/aptos-core.git" | ||
rev = "mainnet" | ||
subdir = "aptos-move/framework/aptos-framework" |
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,158 @@ | ||
/// A 2-in-1 module that combines managed_fungible_asset and coin_example into one module that when deployed, the | ||
/// deployer will be creating a new managed fungible asset with the hardcoded supply config, name, symbol, and decimals. | ||
/// The address of the asset can be obtained via get_metadata(). As a simple version, it only deals with primary stores. | ||
module FACoin::cat { | ||
0xmigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use aptos_framework::fungible_asset::{Self, MintRef, TransferRef, BurnRef, Metadata, FungibleAsset}; | ||
use aptos_framework::object::{Self, Object}; | ||
use aptos_framework::primary_fungible_store; | ||
use std::error; | ||
use std::signer; | ||
use std::string::utf8; | ||
use std::option; | ||
|
||
/// Only fungible asset metadata owner can make changes. | ||
const ENOT_OWNER: u64 = 1; | ||
|
||
const ASSET_SYMBOL: vector<u8> = b"CAT"; | ||
|
||
#[resource_group_member(group = aptos_framework::object::ObjectGroup)] | ||
/// Hold refs to control the minting, transfer and burning of fungible assets. | ||
struct ManagedFungibleAsset has key { | ||
mint_ref: MintRef, | ||
transfer_ref: TransferRef, | ||
burn_ref: BurnRef, | ||
} | ||
|
||
/// Initialize metadata object and store the refs. | ||
// :!:>initialize | ||
fun init_module(admin: &signer) { | ||
let constructor_ref = &object::create_named_object(admin, ASSET_SYMBOL); | ||
primary_fungible_store::create_primary_store_enabled_fungible_asset( | ||
constructor_ref, | ||
option::none(), | ||
utf8(b"CAT Coin"), /* name */ | ||
utf8(ASSET_SYMBOL), /* symbol */ | ||
8, /* decimals */ | ||
utf8(b"http://example.com/favicon.ico"), /* icon */ | ||
utf8(b"http://example.com"), /* project */ | ||
); | ||
|
||
// Create mint/burn/transfer refs to allow creator to manage the fungible asset. | ||
let mint_ref = fungible_asset::generate_mint_ref(constructor_ref); | ||
let burn_ref = fungible_asset::generate_burn_ref(constructor_ref); | ||
let transfer_ref = fungible_asset::generate_transfer_ref(constructor_ref); | ||
let metadata_object_signer = object::generate_signer(constructor_ref); | ||
move_to( | ||
&metadata_object_signer, | ||
ManagedFungibleAsset { mint_ref, transfer_ref, burn_ref } | ||
)// <:!:initialize | ||
} | ||
|
||
#[view] | ||
/// Return the address of the managed fungible asset that's created when this module is deployed. | ||
public fun get_metadata(): Object<Metadata> { | ||
let asset_address = object::create_object_address(&@FACoin, ASSET_SYMBOL); | ||
object::address_to_object<Metadata>(asset_address) | ||
} | ||
|
||
// :!:>mint | ||
/// Mint as the owner of metadata object and deposit to a specific account. | ||
public entry fun mint(admin: &signer, to: address, amount: u64) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let managed_fungible_asset = authorized_borrow_refs(admin, asset); | ||
let to_wallet = primary_fungible_store::ensure_primary_store_exists(to, asset); | ||
let fa = fungible_asset::mint(&managed_fungible_asset.mint_ref, amount); | ||
fungible_asset::deposit_with_ref(&managed_fungible_asset.transfer_ref, to_wallet, fa); | ||
}// <:!:mint_to | ||
|
||
/// Transfer as the owner of metadata object ignoring `frozen` field. | ||
public entry fun transfer(admin: &signer, from: address, to: address, amount: u64) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let from_wallet = primary_fungible_store::primary_store(from, asset); | ||
let to_wallet = primary_fungible_store::ensure_primary_store_exists(to, asset); | ||
fungible_asset::transfer_with_ref(transfer_ref, from_wallet, to_wallet, amount); | ||
} | ||
|
||
/// Burn fungible assets as the owner of metadata object. | ||
public entry fun burn(admin: &signer, from: address, amount: u64) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let burn_ref = &authorized_borrow_refs(admin, asset).burn_ref; | ||
let from_wallet = primary_fungible_store::primary_store(from, asset); | ||
fungible_asset::burn_from(burn_ref, from_wallet, amount); | ||
} | ||
|
||
/// Freeze an account so it cannot transfer or receive fungible assets. | ||
public entry fun freeze_account(admin: &signer, account: address) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let wallet = primary_fungible_store::ensure_primary_store_exists(account, asset); | ||
fungible_asset::set_frozen_flag(transfer_ref, wallet, true); | ||
} | ||
|
||
/// Unfreeze an account so it can transfer or receive fungible assets. | ||
public entry fun unfreeze_account(admin: &signer, account: address) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let wallet = primary_fungible_store::ensure_primary_store_exists(account, asset); | ||
fungible_asset::set_frozen_flag(transfer_ref, wallet, false); | ||
} | ||
|
||
/// Withdraw as the owner of metadata object ignoring `frozen` field. | ||
public fun withdraw(admin: &signer, amount: u64, from: address): FungibleAsset acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let from_wallet = primary_fungible_store::primary_store(from, asset); | ||
fungible_asset::withdraw_with_ref(transfer_ref, from_wallet, amount) | ||
} | ||
|
||
/// Deposit as the owner of metadata object ignoring `frozen` field. | ||
public fun deposit(admin: &signer, to: address, fa: FungibleAsset) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let to_wallet = primary_fungible_store::ensure_primary_store_exists(to, asset); | ||
fungible_asset::deposit_with_ref(transfer_ref, to_wallet, fa); | ||
} | ||
|
||
/// Borrow the immutable reference of the refs of `metadata`. | ||
/// This validates that the signer is the metadata object's owner. | ||
inline fun authorized_borrow_refs( | ||
owner: &signer, | ||
asset: Object<Metadata>, | ||
): &ManagedFungibleAsset acquires ManagedFungibleAsset { | ||
assert!(object::is_owner(asset, signer::address_of(owner)), error::permission_denied(ENOT_OWNER)); | ||
borrow_global<ManagedFungibleAsset>(object::object_address(&asset)) | ||
} | ||
|
||
#[test(creator = @FACoin)] | ||
fun test_basic_flow( | ||
creator: &signer, | ||
) acquires ManagedFungibleAsset { | ||
init_module(creator); | ||
let creator_address = signer::address_of(creator); | ||
let aaron_address = @0xface; | ||
|
||
mint(creator, creator_address, 100); | ||
let asset = get_metadata(); | ||
assert!(primary_fungible_store::balance(creator_address, asset) == 100, 4); | ||
freeze_account(creator, creator_address); | ||
assert!(primary_fungible_store::is_frozen(creator_address, asset), 5); | ||
transfer(creator, creator_address, aaron_address, 10); | ||
assert!(primary_fungible_store::balance(aaron_address, asset) == 10, 6); | ||
|
||
unfreeze_account(creator, creator_address); | ||
assert!(!primary_fungible_store::is_frozen(creator_address, asset), 7); | ||
burn(creator, creator_address, 90); | ||
} | ||
|
||
#[test(creator = @FACoin, aaron = @0xface)] | ||
#[expected_failure(abort_code = 0x50001, location = Self)] | ||
fun test_permission_denied( | ||
creator: &signer, | ||
aaron: &signer | ||
) acquires ManagedFungibleAsset { | ||
init_module(creator); | ||
let creator_address = signer::address_of(creator); | ||
mint(aaron, creator_address, 100); | ||
} | ||
} |
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,158 @@ | ||
/// A 2-in-1 module that combines managed_fungible_asset and coin_example into one module that when deployed, the | ||
/// deployer will be creating a new managed fungible asset with the hardcoded supply config, name, symbol, and decimals. | ||
/// The address of the asset can be obtained via get_metadata(). As a simple version, it only deals with primary stores. | ||
module FACoin::dog { | ||
use aptos_framework::fungible_asset::{Self, MintRef, TransferRef, BurnRef, Metadata, FungibleAsset}; | ||
use aptos_framework::object::{Self, Object}; | ||
use aptos_framework::primary_fungible_store; | ||
use std::error; | ||
use std::signer; | ||
use std::string::utf8; | ||
use std::option; | ||
|
||
/// Only fungible asset metadata owner can make changes. | ||
const ENOT_OWNER: u64 = 1; | ||
|
||
const ASSET_SYMBOL: vector<u8> = b"DOG"; | ||
|
||
#[resource_group_member(group = aptos_framework::object::ObjectGroup)] | ||
/// Hold refs to control the minting, transfer and burning of fungible assets. | ||
struct ManagedFungibleAsset has key { | ||
mint_ref: MintRef, | ||
transfer_ref: TransferRef, | ||
burn_ref: BurnRef, | ||
} | ||
|
||
/// Initialize metadata object and store the refs. | ||
// :!:>initialize | ||
fun init_module(admin: &signer) { | ||
let constructor_ref = &object::create_named_object(admin, ASSET_SYMBOL); | ||
primary_fungible_store::create_primary_store_enabled_fungible_asset( | ||
constructor_ref, | ||
option::none(), | ||
utf8(b"DOG Coin"), /* name */ | ||
utf8(ASSET_SYMBOL), /* symbol */ | ||
8, /* decimals */ | ||
utf8(b"http://example.com/favicon.ico"), /* icon */ | ||
utf8(b"http://example.com"), /* project */ | ||
); | ||
|
||
// Create mint/burn/transfer refs to allow creator to manage the fungible asset. | ||
let mint_ref = fungible_asset::generate_mint_ref(constructor_ref); | ||
let burn_ref = fungible_asset::generate_burn_ref(constructor_ref); | ||
let transfer_ref = fungible_asset::generate_transfer_ref(constructor_ref); | ||
let metadata_object_signer = object::generate_signer(constructor_ref); | ||
move_to( | ||
&metadata_object_signer, | ||
ManagedFungibleAsset { mint_ref, transfer_ref, burn_ref } | ||
)// <:!:initialize | ||
} | ||
|
||
#[view] | ||
/// Return the address of the managed fungible asset that's created when this module is deployed. | ||
public fun get_metadata(): Object<Metadata> { | ||
let asset_address = object::create_object_address(&@FACoin, ASSET_SYMBOL); | ||
object::address_to_object<Metadata>(asset_address) | ||
} | ||
|
||
// :!:>mint | ||
/// Mint as the owner of metadata object and deposit to a specific account. | ||
public entry fun mint(admin: &signer, to: address, amount: u64) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let managed_fungible_asset = authorized_borrow_refs(admin, asset); | ||
let to_wallet = primary_fungible_store::ensure_primary_store_exists(to, asset); | ||
let fa = fungible_asset::mint(&managed_fungible_asset.mint_ref, amount); | ||
fungible_asset::deposit_with_ref(&managed_fungible_asset.transfer_ref, to_wallet, fa); | ||
}// <:!:mint_to | ||
|
||
/// Transfer as the owner of metadata object ignoring `frozen` field. | ||
public entry fun transfer(admin: &signer, from: address, to: address, amount: u64) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let from_wallet = primary_fungible_store::primary_store(from, asset); | ||
let to_wallet = primary_fungible_store::ensure_primary_store_exists(to, asset); | ||
fungible_asset::transfer_with_ref(transfer_ref, from_wallet, to_wallet, amount); | ||
} | ||
|
||
/// Burn fungible assets as the owner of metadata object. | ||
public entry fun burn(admin: &signer, from: address, amount: u64) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let burn_ref = &authorized_borrow_refs(admin, asset).burn_ref; | ||
let from_wallet = primary_fungible_store::primary_store(from, asset); | ||
fungible_asset::burn_from(burn_ref, from_wallet, amount); | ||
} | ||
|
||
/// Freeze an account so it cannot transfer or receive fungible assets. | ||
public entry fun freeze_account(admin: &signer, account: address) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let wallet = primary_fungible_store::ensure_primary_store_exists(account, asset); | ||
fungible_asset::set_frozen_flag(transfer_ref, wallet, true); | ||
} | ||
|
||
/// Unfreeze an account so it can transfer or receive fungible assets. | ||
public entry fun unfreeze_account(admin: &signer, account: address) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let wallet = primary_fungible_store::ensure_primary_store_exists(account, asset); | ||
fungible_asset::set_frozen_flag(transfer_ref, wallet, false); | ||
} | ||
|
||
/// Withdraw as the owner of metadata object ignoring `frozen` field. | ||
public fun withdraw(admin: &signer, amount: u64, from: address): FungibleAsset acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let from_wallet = primary_fungible_store::primary_store(from, asset); | ||
fungible_asset::withdraw_with_ref(transfer_ref, from_wallet, amount) | ||
} | ||
|
||
/// Deposit as the owner of metadata object ignoring `frozen` field. | ||
public fun deposit(admin: &signer, to: address, fa: FungibleAsset) acquires ManagedFungibleAsset { | ||
let asset = get_metadata(); | ||
let transfer_ref = &authorized_borrow_refs(admin, asset).transfer_ref; | ||
let to_wallet = primary_fungible_store::ensure_primary_store_exists(to, asset); | ||
fungible_asset::deposit_with_ref(transfer_ref, to_wallet, fa); | ||
} | ||
|
||
/// Borrow the immutable reference of the refs of `metadata`. | ||
/// This validates that the signer is the metadata object's owner. | ||
inline fun authorized_borrow_refs( | ||
owner: &signer, | ||
asset: Object<Metadata>, | ||
): &ManagedFungibleAsset acquires ManagedFungibleAsset { | ||
assert!(object::is_owner(asset, signer::address_of(owner)), error::permission_denied(ENOT_OWNER)); | ||
borrow_global<ManagedFungibleAsset>(object::object_address(&asset)) | ||
} | ||
|
||
#[test(creator = @FACoin)] | ||
fun test_basic_flow( | ||
creator: &signer, | ||
) acquires ManagedFungibleAsset { | ||
init_module(creator); | ||
let creator_address = signer::address_of(creator); | ||
let aaron_address = @0xface; | ||
|
||
mint(creator, creator_address, 100); | ||
let asset = get_metadata(); | ||
assert!(primary_fungible_store::balance(creator_address, asset) == 100, 4); | ||
freeze_account(creator, creator_address); | ||
assert!(primary_fungible_store::is_frozen(creator_address, asset), 5); | ||
transfer(creator, creator_address, aaron_address, 10); | ||
assert!(primary_fungible_store::balance(aaron_address, asset) == 10, 6); | ||
|
||
unfreeze_account(creator, creator_address); | ||
assert!(!primary_fungible_store::is_frozen(creator_address, asset), 7); | ||
burn(creator, creator_address, 90); | ||
} | ||
|
||
#[test(creator = @FACoin, aaron = @0xface)] | ||
#[expected_failure(abort_code = 0x50001, location = Self)] | ||
fun test_permission_denied( | ||
creator: &signer, | ||
aaron: &signer | ||
) acquires ManagedFungibleAsset { | ||
init_module(creator); | ||
let creator_address = signer::address_of(creator); | ||
mint(aaron, creator_address, 100); | ||
} | ||
} |
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
Oops, something went wrong.
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.
There's a decent amount of duplication between this and dog module. I think one of @lightmark's examples can help?
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.
Hmm, so I am not sure if creating two identical FA and doing swap between them works. Any idea? @movekevin
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.
That shouldn't matter :)
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.
Oh interesting! Though I feel like the user might get confused, and cause some confusion.
@movekevin Shall we keep it as two separate module?