We've updated our Terms of Service. By continuing to use our services, you agree to the updated Terms.

Aptos

Aptos

#Installation

Magic interacts with the Aptos blockchain via Magic's extension NPM package @magic-ext/aptos (beta version 0.5.0). Additionally, the Aptos extension lets you interact with the blockchain using methods from Aptos' Typescript SDK and/or @aptos-labs/wallet-adapter-core.

note

This extension requires @magic-ext/aptos version ^0.5.0 , and magic-sdk version ^17.1.6 (latest preferred)

To get started, install the following dependencies for your project:

NPM
Yarn
01npm install @aptos-labs/wallet-adapter-core @magic-ext/aptos magic-sdk

#Initialization

The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key.

You can then use the SDK object to initialize an AptosWallet object that meets the Aptos Wallet standard. You can use any supported login method supported by Magic SDK in the "connect" function.

Javascript
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});
11
12const magicAptosWallet = new MagicAptosWallet(magic, {
13  connect: async () => {
14    await magic.auth.loginWithMagicLink({ email });
15    const accountInfo = await magic.aptos.getAccountInfo();    
16    return accountInfo;
17  }
18});
warning

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

#Common Methods

#Send 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 Authentication tab to get started. You can check if a user is logged in with the following code:

Javascript
01const isLoggedIn = await magic.user.isLoggedIn();

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

Javascript
01const { address } = await magic.aptos.getAccountInfo();

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

Alternatively, you can use MagicAptosWallet's account() method:

Javascript
01const { address } = await magicAptosAccount.account();

#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

Javascript
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);
04await faucetClient.fundAccount(address, 100_000_000);

#Sign and Send Transaction

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

If you are using MagicAptosWallet, you can skip the rest of this section and build your application using the Aptos Wallet Standard. You can also find example apps by Aptos here (MagicAptosWallet is not included in this useWallet, however, due to the flexibility Magic SDK provides - and the complexity of setting it up does not neatly fit into this example's React hook).

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.

Javascript
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)
16const transaction = await client.submitTransaction(signedTransaction)
17const result = await client.waitForTransactionWithResult(transaction.hash, {
18  checkSuccess: true
19})

#Get Balance

Javascript
01const address = await magic.aptos.getAccountInfo();
02const client = new AptosClient(DEVNET_NODE_URL);
03const coinClient = new CoinClient(client);
04
05const balance = await coinClient.checkBalance(address);
06console.log(balance)

#Resources