Skip to main content
This guide demonstrates how to create, sign, and broadcast a transaction using the Adamik API along with the Stellar SDK for JavaScript. This tutorial is perfect for developers who want to integrate Adamik’s features into their Stellar workflows, ensuring secure and efficient transaction management.

Prerequisites

  • Node.js and npm installed
  • Basic understanding of Stellar and transaction workflows
  • Familiarity with the Stellar SDK

Step-by-Step Guide

1. Setting Up the Environment

Ensure you have the Stellar SDK installed in your project:
npm install @stellar/stellar-sdk

2. Writing the Script

Below is the script that handles the full transaction flow, from creating a wallet to broadcasting the signed transaction. You can find the complete code example in the Adamik GitHub repository.
const { Keypair } = require("@stellar/stellar-sdk");

// Replace with your wallet's secret key
const secretKey = "your-stellar-secret-key-here";
// Replace with your Adamik API key from https://dashboard.adamik.io
const ADAMIK_API_KEY = "your-adamik-api-key";
// Replace with the recipient's address
const recipientAddress = "GCKFBEIYV2U22IO2BJ4KVJOIP7XPWQGQFKKWXR6DOSJBV7STMAQSMTMA";

async function main() {
  try {
    // Create wallet from secret key
    const sourceKeypair = Keypair.fromSecret(secretKey);
    const sourcePublicKey = sourceKeypair.publicKey();

    // Prepare the transaction request for Adamik API
    const requestBody = {
      transaction: {
        data: {
          chainId: "stellar", // Target Stellar mainnet (use "stellar-testnet" for testnet)
          mode: "transfer",
          senderAddress: sourcePublicKey,
          recipientAddress: recipientAddress || sourcePublicKey, // Self-send if no recipient
          amount: "10000000", // Transaction amount in stroops (1 XLM = 10^7 stroops)
          useMaxAmount: false,
          fees: "0",
          gas: "0",
          memo: "Adamik Transfer",
          format: "json",
          validatorAddress: "",
        },
      },
    };

    // Encode the transaction with Adamik API
    const responseEncode = await fetch(
      "https://api.adamik.io/api/stellar/transaction/encode",
      {
        method: "POST",
        headers: {
          Authorization: ADAMIK_API_KEY,
          "Content-Type": "application/json",
        },
        body: JSON.stringify(requestBody),
      }
    );

    const encodedData = await responseEncode.json();
    
    if (!responseEncode.ok) {
      console.error("Encode failed:", encodedData);
      return;
    }

    // Sign the transaction hash
    const hashToSign = encodedData.transaction.encoded[0].hash.value;
    const hashBuffer = Buffer.from(hashToSign, 'hex');
    const signature = sourceKeypair.sign(hashBuffer);
    const signatureHex = signature.toString('hex');

    // Prepare to broadcast the signed transaction
    const sendTransactionBody = {
      transaction: {
        data: encodedData.transaction.data,
        encoded: encodedData.transaction.encoded,
        signature: signatureHex,
      },
    };

    // Broadcast the transaction using Adamik API
    const responseBroadcast = await fetch(
      "https://api.adamik.io/api/stellar/transaction/broadcast",
      {
        method: "POST",
        headers: {
          Authorization: ADAMIK_API_KEY,
          "Content-Type": "application/json",
        },
        body: JSON.stringify(sendTransactionBody),
      }
    );

    const responseData = await responseBroadcast.json();
    console.log("Transaction hash:", responseData.hash);
  } catch (error) {
    console.error("Error:", error);
  }
}

main();

3. Explanation

  • Wallet Creation: The script begins by creating a wallet using a Stellar secret key, which is crucial for managing private keys. Please note Adamik API doesn’t have the custody of the assets.
  • Transaction Encoding: The transaction details are passed to the Adamik API, which returns an encoded transaction with its hash. Note that amounts are in stroops (1 XLM = 10,000,000 stroops).
  • Signing: The transaction hash is signed directly using the Stellar Keypair’s sign method, producing a hex signature.
  • Broadcasting: The signed transaction is broadcast to the Stellar network using the Adamik API with the signature attached.

4. Troubleshooting

  • Invalid API Key: Ensure your Adamik API key is correct.
  • Network Issues: Verify network availability and Adamik API status.
  • Insufficient Balance: Ensure your account has enough XLM to cover the transaction amount plus fees.
  • Security Note: Never hardcode sensitive information like secret keys in your production environment.

5. Get in Touch

Connect with us through our Discord Server or directly through GitHub.
I