-
Notifications
You must be signed in to change notification settings - Fork 54
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
Update fa_coin and your_fungible_asset #600
Open
jmintuitive
wants to merge
4
commits into
aptos-labs:main
Choose a base branch
from
jmintuitive:update-fa-coin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ import { | |
InputViewFunctionData, | ||
Network, | ||
NetworkToNetworkName, | ||
MoveValue, | ||
} from "@aptos-labs/ts-sdk"; | ||
import { compilePackage, getPackageBytesToPublish } from "./utils"; | ||
/** | ||
|
@@ -80,6 +81,34 @@ async function burnCoin(admin: Account, fromAddress: AccountAddress, amount: Any | |
return pendingTxn.hash; | ||
} | ||
|
||
/** | ||
* Fetches the name of the fungible asset created by the given account. | ||
* This function interacts with the `get_name` view function in the Move module. | ||
* | ||
* @param aptos - An instance of the Aptos client. | ||
* @param admin - The account that deployed the fungible asset module. | ||
* @returns The name of the token as a string. | ||
*/ | ||
async function fetchTokenName(aptos: Aptos, admin: Account): Promise<string> { | ||
try { | ||
const payload: InputViewFunctionData = { | ||
function: `${admin.accountAddress}::fa_coin::get_name`, | ||
functionArguments: [], | ||
}; | ||
|
||
const result: MoveValue[] = await aptos.view({ payload }); | ||
|
||
if (!Array.isArray(result) || result.length === 0 || typeof result[0] !== "string") { | ||
throw new Error("Invalid response format. Ensure the module is deployed correctly."); | ||
} | ||
|
||
return result[0]; | ||
} catch (error) { | ||
console.error("Error fetching token name:", error); | ||
throw error; | ||
} | ||
} | ||
|
||
/** Admin freezes the primary fungible store of the specified account */ | ||
async function freeze(admin: Account, targetAddress: AccountAddress): Promise<string> { | ||
const transaction = await aptos.transaction.build.simple({ | ||
|
@@ -145,10 +174,15 @@ async function main() { | |
console.log(`Bob: ${bob.accountAddress.toString()}`); | ||
console.log(`Charlie: ${charlie.accountAddress.toString()}`); | ||
|
||
await aptos.fundAccount({ accountAddress: alice.accountAddress, amount: 100_000_000 }); | ||
await aptos.fundAccount({ | ||
accountAddress: alice.accountAddress, | ||
amount: 100_000_000, | ||
options: { waitForIndexer: false }, | ||
}); | ||
await aptos.fundAccount({ | ||
accountAddress: bob.accountAddress, | ||
amount: 100_000_000, | ||
options: { waitForIndexer: false }, | ||
}); | ||
|
||
console.log("\n=== Compiling FACoin package locally ==="); | ||
|
@@ -175,28 +209,29 @@ async function main() { | |
console.log("metadata address:", metadataAddress); | ||
|
||
console.log("All the balances in this example refer to balance in primary fungible stores of each account."); | ||
console.log(`Alice's initial FACoin balance: ${await getFaBalance(alice, metadataAddress)}.`); | ||
console.log(`Bob's initial FACoin balance: ${await getFaBalance(bob, metadataAddress)}.`); | ||
console.log(`Alice's initial balance: ${await getFaBalance(alice, metadataAddress)}.`); | ||
console.log(`Bob's initial balance: ${await getFaBalance(bob, metadataAddress)}.`); | ||
console.log(`Charlie's initial balance: ${await getFaBalance(charlie, metadataAddress)}.`); | ||
const tokenName = `"${await fetchTokenName(aptos, alice)}"`; // Adding quotes so when it's printed later it's clear it's a name. | ||
|
||
console.log("Alice mints Charlie 100 coins."); | ||
const mintCoinTransactionHash = await mintCoin(alice, charlie, 100); | ||
|
||
await aptos.waitForTransaction({ transactionHash: mintCoinTransactionHash }); | ||
console.log( | ||
`Charlie's updated FACoin primary fungible store balance: ${await getFaBalance(charlie, metadataAddress)}.`, | ||
`Charlie's updated ${tokenName} primary fungible store balance: ${await getFaBalance(charlie, metadataAddress)}.`, | ||
); | ||
|
||
console.log("Alice freezes Bob's account."); | ||
const freezeTransactionHash = await freeze(alice, bob.accountAddress); | ||
await aptos.waitForTransaction({ transactionHash: freezeTransactionHash }); | ||
|
||
console.log( | ||
"Alice as the admin forcefully transfers the newly minted coins of Charlie to Bob ignoring that Bob's account is frozen.", | ||
`Alice as the admin forcefully transfers the newly minted coins of Charlie to Bob ignoring that Bob's account is frozen.`, | ||
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. please replace with |
||
); | ||
const transferCoinTransactionHash = await transferCoin(alice, charlie.accountAddress, bob.accountAddress, 100); | ||
await aptos.waitForTransaction({ transactionHash: transferCoinTransactionHash }); | ||
console.log(`Bob's updated FACoin balance: ${await getFaBalance(bob, metadataAddress)}.`); | ||
console.log(`Bob's updated ${tokenName} balance: ${await getFaBalance(bob, metadataAddress)}.`); | ||
|
||
console.log("Alice unfreezes Bob's account."); | ||
const unfreezeTransactionHash = await unfreeze(alice, bob.accountAddress); | ||
|
@@ -205,7 +240,7 @@ async function main() { | |
console.log("Alice burns 50 coins from Bob."); | ||
const burnCoinTransactionHash = await burnCoin(alice, bob.accountAddress, 50); | ||
await aptos.waitForTransaction({ transactionHash: burnCoinTransactionHash }); | ||
console.log(`Bob's updated FACoin balance: ${await getFaBalance(bob, metadataAddress)}.`); | ||
console.log(`Bob's updated ${tokenName} balance: ${await getFaBalance(bob, metadataAddress)}.`); | ||
|
||
/// Normal fungible asset transfer between primary stores | ||
console.log("Bob transfers 10 coins to Alice as the owner."); | ||
|
@@ -220,8 +255,8 @@ async function main() { | |
transaction: transferFungibleAssetRawTransaction, | ||
}); | ||
await aptos.waitForTransaction({ transactionHash: transferFungibleAssetTransaction.hash }); | ||
console.log(`Alice's updated FACoin balance: ${await getFaBalance(alice, metadataAddress)}.`); | ||
console.log(`Bob's updated FACoin balance: ${await getFaBalance(bob, metadataAddress)}.`); | ||
console.log(`Alice's updated ${tokenName} balance: ${await getFaBalance(alice, metadataAddress)}.`); | ||
console.log(`Bob's updated ${tokenName} balance: ${await getFaBalance(bob, metadataAddress)}.`); | ||
console.log("done."); | ||
} | ||
|
||
|
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.
These examples use a "global" aptos object, so it doesn't have to be passed here