// Setup the Aptos client configurationconstconfig=newAptosConfig({ network:Network.TESTNET }); // Connect to Aptos Testnetconstaptos=newAptos(config); // Create an instance of the Aptos SDK
3. Wallet setup:
// Initialize the account using a private keyconstaccount=Account.fromPrivateKey({ privateKey:newEd25519PrivateKey('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 payloadconstbaseURL='https://perps-tradeapi.kanalabs.io/limitOrder';constparams= { marketId:66,// Specify the market ID for the trade amount:50000,// amount to deposit your trading account };// Fetch the payload for depositconstres=awaitaxios.get(baseURL, { params, headers: {'api_key':process.env.API_KEY, }, });constpayload=res.data.data; // Extract the data payload
5. Building transaction
// Build a transaction payload using the Aptos SDKconsttransactionPayload=awaitaptos.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 transactionconstcommittedTxn=awaitaptos.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 statusconstresponse=awaitaptos.waitForTransaction({ transactionHash:committedTxn.hash,// Hash of the submitted transaction });// Log the success status of the transactionconsole.log("response",response.success);
8. Call main function:
}// Run the main function and handle any errorsmain().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 requestsimport axios from"axios";
2. Main Function Setup:
// Main function to fetch market info from the APIasyncfunctionmain():Promise<void> {
3. API Endpoint Setup:
// The base URL for the API endpointconstbaseURL='https://perps-tradeapi.kanalabs.io/getMarketInfo';
4. Parameters Setup:
// Parameters to be sent with the API request (marketId in this case)constparams= { 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 keyconstres=awaitaxios.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 responseconstgetMarketInfo=res.data;// Logging the market info data to the consoleconsole.log("getMarketInfo: ", getMarketInfo);
7. Error Handling:
} catch (error) {// Handling errors if the API request failsconsole.error('An error occurred:', error); }
8. Calling the Main Function:
// Calling the main function and handling potential errorsmain().catch(error => {console.error('An error occurred:', error);});