-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish the token related API for alpha (#88)
* Add getTokenCurrentOwnerData and getTokenActivities queries * get ownedTokens fetch * add mintToken * update doc * update * add example * update * Update src/api/token.ts Co-authored-by: Maayan <[email protected]> --------- Co-authored-by: Maayan <[email protected]>
- Loading branch information
1 parent
a82e996
commit 102ef95
Showing
11 changed files
with
641 additions
and
19 deletions.
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,106 @@ | ||
/** | ||
* This example shows how to use the Aptos client to mint a NFT. | ||
*/ | ||
|
||
import { Account, Aptos, AptosConfig, Network } from "aptos"; | ||
|
||
const ALICE_INITIAL_BALANCE = 100_000_000; | ||
|
||
/** | ||
* Prints the balance of an account | ||
* @param aptos | ||
* @param name | ||
* @param address | ||
* @returns {Promise<*>} | ||
* | ||
*/ | ||
const accountTokens = async (aptos: Aptos, name: string, accountAddress: string) => { | ||
let tokens = await aptos.getOwnedTokens({ ownerAddress: accountAddress }); | ||
|
||
if (tokens.length === 0) { | ||
console.log(`\n${name} has no tokens.\n`); | ||
return; | ||
} | ||
|
||
console.log(`\n${name}'s tokens:`); | ||
for (let index = 0; index < tokens.length; index++) { | ||
const token = tokens[index]; | ||
console.log( | ||
`*${token.current_token_data.token_name}* in the *${token.current_token_data.current_collection.collection_name}* collection`, | ||
); | ||
} | ||
}; | ||
|
||
const example = async () => { | ||
console.log( | ||
"This example will create and fund an account (Alice), then the account will create a collection and a token in that collection.", | ||
); | ||
|
||
// Setup the client | ||
const config = new AptosConfig({ network: Network.DEVNET }); | ||
const aptos = new Aptos(config); | ||
|
||
// Create the account | ||
let alice = Account.generate(); | ||
|
||
console.log("=== Addresses ===\n"); | ||
console.log(`Alice's address is: ${alice.accountAddress.toString()}`); | ||
|
||
// Fund the accounts | ||
console.log("\n=== Funding accounts ===\n"); | ||
|
||
const aliceFundTxn = await aptos.faucet.fundAccount({ | ||
accountAddress: alice.accountAddress.toUint8Array(), | ||
amount: ALICE_INITIAL_BALANCE, | ||
}); | ||
console.log("Alice's fund transaction: ", aliceFundTxn); | ||
|
||
const collectionName = "Example Collection"; | ||
const collectionDescription = "Example description."; | ||
const collectionURI = "aptos.dev"; | ||
|
||
// Create the collection | ||
let transaction = await aptos.createCollectionTransaction({ | ||
creator: alice, | ||
description: collectionDescription, | ||
name: collectionName, | ||
uri: collectionURI, | ||
}); | ||
|
||
console.log("\n=== Create the collection ===\n"); | ||
let committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction }); | ||
|
||
await aptos.waitForTransaction({ transactionHash: committedTxn.hash }); | ||
console.log(`Committed transaction: ${committedTxn.hash}`); | ||
|
||
console.log(`Created collection:`); | ||
let exampleCollection = await aptos.getCollectionData({ | ||
collectionName, | ||
creatorAddress: alice.accountAddress.toString(), | ||
}); | ||
console.log(exampleCollection); | ||
|
||
await accountTokens(aptos, "Alice", alice.accountAddress.toString()); | ||
|
||
const tokenName = "Example Token"; | ||
const tokenDescription = "Example token description."; | ||
const tokenURI = "aptos.dev/token"; | ||
|
||
// Mint the token | ||
transaction = await aptos.mintTokenTransaction({ | ||
creator: alice, | ||
collection: collectionName, | ||
description: tokenDescription, | ||
name: tokenName, | ||
uri: tokenURI, | ||
}); | ||
|
||
console.log("\n=== Mint the token ===\n"); | ||
committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction }); | ||
await aptos.waitForTransaction({ transactionHash: committedTxn.hash }); | ||
console.log(`Committed transaction: ${committedTxn.hash}`); | ||
|
||
await accountTokens(aptos, "Alice", alice.accountAddress.toString()); | ||
}; | ||
|
||
example(); |
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
17 changes: 17 additions & 0 deletions
17
src/internal/queries/TokenActivitiesFieldsFragment.graphql
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,17 @@ | ||
fragment TokenActivitiesFields on token_activities_v2 { | ||
after_value | ||
before_value | ||
entry_function_id_str | ||
event_account_address | ||
event_index | ||
from_address | ||
is_fungible_v2 | ||
property_version_v1 | ||
to_address | ||
token_amount | ||
token_data_id | ||
token_standard | ||
transaction_timestamp | ||
transaction_version | ||
type | ||
} |
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,11 @@ | ||
#import "./TokenActivitiesFieldsFragment"; | ||
query getTokenActivity( | ||
$where_condition: token_activities_v2_bool_exp! | ||
$offset: Int | ||
$limit: Int | ||
$order_by: [token_activities_v2_order_by!] | ||
) { | ||
token_activities_v2(where: $where_condition, order_by: $order_by, offset: $offset, limit: $limit) { | ||
...TokenActivitiesFields | ||
} | ||
} |
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,11 @@ | ||
#import "./CurrentTokenOwnershipFieldsFragment"; | ||
query getCurrentTokenOwnership( | ||
$where_condition: current_token_ownerships_v2_bool_exp! | ||
$offset: Int | ||
$limit: Int | ||
$order_by: [current_token_ownerships_v2_order_by!] | ||
) { | ||
current_token_ownerships_v2(where: $where_condition, offset: $offset, limit: $limit, order_by: $order_by) { | ||
...CurrentTokenOwnershipFields | ||
} | ||
} |
Oops, something went wrong.