Example setup functions

We will see how to implement the run functions : (Example deposit function)

1. Imports:

import { AptosConfig, Aptos, Network, Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk";
import axios from "axios";

async function main(): Promise<void> {

2. Aptos client setup:

    // Setup the Aptos client configuration
    const config = new AptosConfig({ network: Network.TESTNET }); // Connect to Aptos Testnet
    const aptos = new Aptos(config); // Create an instance of the Aptos SDK

3. Wallet setup:

    // Initialize the account using a private key
    const account = Account.fromPrivateKey({
        privateKey: new Ed25519PrivateKey('your_private_key'), // Ensure the private key is valid
    });

4. Get transaction payload: (required function parameters for order deposit function)

    // API endpoint for fetching limit order payload
    const baseURL = 'https://perps-tradeapi.kanalabs.io/limitOrder';
    const params = {
        marketId: 66, // Specify the market ID for the trade
        amount: 50000, // amount to deposit your trading account
    };
    // Fetch the payload for deposit
    const res = await axios.get(baseURL, {
        params, 
        headers: {
            'api_key': process.env.API_KEY,
        },
    });
    const payload = res.data.data; // Extract the data payload

5. Building transaction

    // Build a transaction payload using the Aptos SDK
    const transactionPayload = await aptos.transaction.build.simple({
        sender: account.accountAddress, // The address of the account sending the transaction
        data: payload, // Payload fetched from the API
    });

6. Sign and Submit Transaction

    // Sign and submit the transaction
    const committedTxn = await aptos.transaction.signAndSubmitTransaction({
        transaction: transactionPayload, // Built transaction payload
        signer: account, // Account used for signing
    });

7. Wait for transaction to complete

    // Wait for the transaction to be confirmed and retrieve its status
    const response = await aptos.waitForTransaction({
        transactionHash: committedTxn.hash, // Hash of the submitted transaction
    });
    // Log the success status of the transaction
    console.log("response", response.success);

8. Call main function:

}
// Run the main function and handle any errors
main().catch(error => {
    console.error('An error occurred:', error);
});

We will see how to implement the view functions : (Example get market info function)

1. Imports:

// Import the axios library for making HTTP requests
import axios from "axios";

2. Main Function Setup:

// Main function to fetch market info from the API
async function main(): Promise<void> {

3. API Endpoint Setup:

// The base URL for the API endpoint
const baseURL = 'https://perps-tradeapi.kanalabs.io/getMarketInfo';

4. Parameters Setup:

// Parameters to be sent with the API request (marketId in this case)
const params = {
      marketId: 'your_market_id' // Replace with the actual marketId
  };

5. API Request:

try {
    // Sending a GET request to the API with the parameters and the API key
    const res = await axios.get(baseURL, {
        params, // Query parameters to be included in the request URL
        headers: {
            'api_key': process.env.API_KEY, // API key passed in the headers from environment variables
        },
    });

6. Extracting and Logging Data:

// Extracting the data returned by the API response
const getMarketInfo = res.data;
// Logging the market info data to the console
console.log("getMarketInfo: ", getMarketInfo);

7. Error Handling:

} catch (error) {
        // Handling errors if the API request fails
        console.error('An error occurred:', error);
    }

8. Calling the Main Function:

// Calling the main function and handling potential errors
main().catch(error => {
    console.error('An error occurred:', error);
});

Last updated