How to Integrate the Aptos Blockchain with Magic (beta)

How to Integrate the Aptos Blockchain with Magic (beta)

#Installation

note

Note: this extension requires @magic-ext/aptos version ^0.2.1 , and magic-sdk version ^17.1.5

Magic interacts with the Aptos blockchain via Magic's extension NPM package @magic-ext/aptos (beta version 0.2.1) . The Aptos extension also lets you interact with the blockchain using methods from Aptos' Typescript SDK.

note

You can skip straight to our kitchen sink example directly:

👉 Aptos Example

This example shows how to create accounts, create raw transactions, and send the transactions to the blockchain.

#NPM

Bash
01npm install --save @magic-ext/aptos

#Yarn

Bash
01yarn add @magic-ext/aptos

#Initializing the Extension

Initialize the Magic SDK using your developer api key, found on the Magic Dashboard .

Typescript
01import { Magic } from 'magic-sdk';
02import { AptosExtension } from '@magic-ext/aptos';
03
04const magic = new Magic('YOUR_API_KEY', {
05  extensions: [
06    new AptosExtension({
07      nodeUrl: 'https://fullnode.testnet.aptoslabs.com'
08    }),
09  ],
10});
warning

The nodeUrl you specify will determine whether you are using the mainnet or testnet for Aptos.

#Send a Transaction

#Getting the Aptos Account

The first step will be getting the logged in user's Aptos Account address. Follow any of the Login Guides on the Magic Auth Overview page to get started. You can check if a user is logged in with the following code:

Typescript
01magic.user.isLoggedIn()

Once the user is logged in, you can get their Aptos Account address by running:

Typescript
01const address = await magic.aptos.getAccount();

Note: this does not return an actual AptosAccount object, only the address. See the note below for more detail.

#Funding an Account with the Faucet

Next up for our testing, you'll need to fund the account using the Faucet. This code snippet is taken from the Aptos Tutorial

Typescript
01const DEVNET_NODE_URL = 'https://fullnode.testnet.aptoslabs.com';
02const DEVNET_FAUCET_URL = 'https://faucet.testnet.aptoslabs.com';
03const faucetClient = new FaucetClient(DEVNET_NODE_URL, DEVNET_FAUCET_URL);
04⁠await faucetClient.fundAccount(address, 100_000_000);

#Signing and Sending a Transaction

Note that the Magic Aptos extension follows the method names and conventions of Aptos' Typescript SDK.

warning

The Beta version of the Magic Aptos Extension only supports Raw transactions. Future versions will include full support for the following: ⁠ ⁠ - CoinClient ⁠ - TokenClient ⁠ - AptosAccount

In the meantime, you can find the equivalent RawTransactions in the Aptos source code for any of the object methods above. See the bottom of this page for a detailed example.

Typescript
01// Set the input variables
02const MAGIC_WALLET_ADDRESS = '0x906fd65afe31b7237cd4d7c4073d8bf76c61b6a24ec64dd26f0c16de5c2444d5'
03const SAMPLE_RAW_TRANSACTION = {
04  function: "0x1::coin::transfer",
05  type_arguments: ["0x1::aptos_coin::AptosCoin"],
06  arguments: [MAGIC_WALLET_ADDRESS, 1000]
07}
08
09⁠
10// Initialize the AptosClient and transaction
11const client = new AptosClient(DEVNET_NODE_URL);
12const rawTransaction = await client.generateTransaction(address, SAMPLE_RAW_TRANSACTION)
13
14// Sign the transaction using Magic SDK, then send
15const signedTransaction = await magic.aptos.signTransaction(rawTransaction)
16⁠const transaction = await client.submitTransaction(signedTransaction)
17const result = await client.waitForTransactionWithResult(transaction.hash, {
18  checkSuccess: true
19})

#Recreating Class Methods

As noted above, we currently don't support using the AptosAccount, CoinClient, or TokenClient classes. You can dig into the Aptos source code to find the equivalent code to create raw transactions.⁠

For example, to create a Token Collection as described in the Aptos Guide , you would run:

Typescript
01const name = "abcd";
02⁠const desc = "my collection";
03const uri = "http://abcd.com";
04⁠
05const client = new AptosClient(NODE_URL);
06⁠const tokenClient = new TokenClient(client); 
07⁠const txnHash1 = await tokenClient.createCollection(
08⁠    alice,
09⁠    name,
10    ⁠desc,
11    uri,
12⁠);

Looking in the source code , you can create an equivalent rawTransaction using the following:

Typescript
01⁠const name = "abcd";
02⁠const desc = "my collection";
03const uri = "http://abcd.com";
04
05⁠const CREATE_COLLECTION_TRANSACTION = {
06  function: "0x3::token::create_collection_script",
07  type_arguments: ["string","string", "string", uint8],
08  arguments: [name, desc, uri, null, [false, false, false]]
09}
10
11// Initialize the AptosClient and transaction
12const client = new AptosClient(DEVNET_NODE_URL);
13const builder = new TransactionBuilderRemoteABI(client, { sender: address, });
14const rawTransaction = await client.generateTransaction(address, CREATE_COLLECTION_TRANSACTION)
15
16// Sign the transaction using Magic SDK, then send
17const signedTransaction = await magic.aptos.signTransaction(rawTransaction)
18⁠const transaction = await client.submitTransaction(signedTransaction)
19const result = await client.waitForTransactionWithResult(transaction.hash, {
20  checkSuccess: true
21})

Did you find what you were looking for?

How to Integrate the Aptos Blockchain with Magic (beta)

Did you find what you were looking for?