Anqa Documentation
  • Welcome to Anqa Aggregator
  • Getting Started
    • User Guides
      • Connect your Wallet
      • Swap
      • Gamified Anqa
      • Supported Aptos DEXs
  • Anqa Overview
    • Aggregator Protocol
      • Overview
      • API
      • Typescript SDK
      • FAQ
    • Anqa - Gamified Defi Protocol
  • Security and License
    • Audits
    • Terms of Use
    • Privacy Policy
Powered by GitBook
On this page
  1. Anqa Overview
  2. Aggregator Protocol

Typescript SDK

API

Example of using Anqa SDK to build a Swap Transaction on Aptos

1. Importing Required Modules

First, import the necessary modules and dependencies for the Aptos blockchain transactions and swap data retrieval:

import { getSwapData } from '@anqa-ag/ts-sdk';
import { Aptos, AptosConfig, Network, APTOS_COIN, Ed25519PublicKey, Ed25519PrivateKey, Account } from '@aptos-labs/ts-sdk';
import invariant from 'tiny-invariant';

2. Setting Up Constants

Next, define the constants that will be used throughout the script. These include wallet addresses, keys, and asset identifiers:

export const TEST_WALLET_ADDRESS = ''; // change me
export const TEST_PRIVATE_KEY = ''; // change me
export const TEST_PUBLIC_KEY = ''; // change me
export const Z_USDC = '0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDC';
export const SLIPPAGE_BPS = 50;

3. Configuring Aptos

Configure the Aptos blockchain network settings:

const aptosConfig = new AptosConfig({ network: Network.MAINNET });
export const aptos = new Aptos(aptosConfig);

4. Retrieving Swap Data

Use the getSwapData function to fetch the necessary data for the swap transaction:

const swapData = await getSwapData(APTOS_COIN, Z_USDC, '1000000', SLIPPAGE_BPS);

5. Building the Transaction

Build a simple transaction with the swap data retrieved:

const transaction = await aptos.transaction.build.simple({
  sender: TEST_WALLET_ADDRESS,
  data: {
    function: swapData.function,
    functionArguments: swapData.functionArguments,
    typeArguments: swapData.typeArguments,
  },
});

6. Simulating the Transaction

Simulate the transaction to ensure it will succeed:

const simulateResponse = await aptos.transaction.simulate.simple({
  signerPublicKey: new Ed25519PublicKey(TEST_PUBLIC_KEY),
  transaction,
});

invariant(simulateResponse.length === 1, `unexpected error, simulateResponse = ${JSON.stringify(simulateResponse)}`);
invariant(simulateResponse[0].success, 'simulate failed');

7. Signing and Submitting the Transaction

Sign and submit the transaction using the private key:

const pendingTxResponse = await aptos.transaction.signAndSubmitTransaction({
  signer: Account.fromPrivateKey({ privateKey: new Ed25519PrivateKey(TEST_PRIVATE_KEY) }),
  transaction,
});

8. Waiting for Transaction Confirmation

Wait for the transaction to be confirmed on the blockchain:

const committedTxResponse = await aptos.transaction.waitForTransaction({ transactionHash: pendingTxResponse.hash });
invariant(committedTxResponse.success, 'transaction failed');

9. Logging the Transaction Result

Finally, log the success message with the transaction details:

console.log(`Success. https://aptoscan.com/transaction/${committedTxResponse.version}`);

Each section of this guide is designed to walk you through a specific stage in the transaction process on the Aptos blockchain, from setup and build to simulation, signing, submission, and confirmation. This structured approach helps clarify the purpose and function of each step in the script.

PreviousAPINextFAQ

Last updated 11 months ago