This guide demonstrates how to create, sign, and broadcast a transaction using the Adamik API along with a common Ethereum library, ethers.js. This tutorial is perfect for developers who want to integrate Adamik’s features into their Ethereum workflows, ensuring secure and efficient transaction management.

Prerequisites

  • Node.js and npm installed
  • Basic understanding of Ethereum and transaction workflows
  • Familiarity with the ethers.js library

Step-by-Step Guide

1. Setting Up the Environment

Ensure you have the ethers library installed in your project:

npm install ethers

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.

import { Wallet } from "ethers";

// Replace with your wallet's mnemonic phrase
const walletPhrase = "your mnemonic phrase 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 or leave empty for self-transfer
const recipientAddress = "0xeD35957be660cf9A2C768fDB52eC4D9D19eEB80F";

async function main() {
  try {
    console.log("Creating wallet...");
    const wallet = Wallet.fromPhrase(walletPhrase);

    console.log("Wallet created");
    console.log("Sender address:", wallet.address);

    const requestBody = {
      transaction: {
        data: {
          chainId: "sepolia", // Target Ethereum testnet chain
          mode: "transfer",
          senderAddress: wallet.address,
          recipientAddress: recipientAddress || wallet.address, // Self-send if no recipient
          amount: "10000", // Transaction amount
          useMaxAmount: false,
          fees: "0",
          gas: "0",
          memo: "",
          format: "json",
          validatorAddress: "",
          params: {
            pubKey: wallet.publicKey, // Public key of the wallet
          },
        },
      },
    };

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

    const encodedData = await responseEncode.json();
    console.log(
      "Encoded transaction data:",
      JSON.stringify(encodedData, null, 2)
    );

    // Sign the encoded transaction
    const tx = encodedData.transaction.encoded;
    console.log("Signing transaction...");
    const signature = await wallet.signTransaction(tx);
    console.log("Transaction signed");

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

    console.log("Broadcasting transaction...");

    // Broadcast the transaction using Adamik API
    const responseBroadcast = await fetch(
      "https://api.adamik.io/api/sepolia/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 Result:", JSON.stringify(responseData, null, 2));
    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 mnemonic phrase, 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 translates them into a raw transaction to be signed.
  • Signing and Broadcasting: The transaction is then signed with the wallet’s private key and finally broadcast to the Ethereum network using the Adamik API.

4. Troubleshooting

  • Invalid API Key: Ensure your Adamik API key is correct.
  • Network Issues: Verify network availability and Adamik API status.
  • Security Note: Never hardcode sensitive information like mnemonic phrases in your production environment.

5. Get in Touch

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

This guide demonstrates how to create, sign, and broadcast a transaction using the Adamik API along with a common Ethereum library, ethers.js. This tutorial is perfect for developers who want to integrate Adamik’s features into their Ethereum workflows, ensuring secure and efficient transaction management.

Prerequisites

  • Node.js and npm installed
  • Basic understanding of Ethereum and transaction workflows
  • Familiarity with the ethers.js library

Step-by-Step Guide

1. Setting Up the Environment

Ensure you have the ethers library installed in your project:

npm install ethers

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.

import { Wallet } from "ethers";

// Replace with your wallet's mnemonic phrase
const walletPhrase = "your mnemonic phrase 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 or leave empty for self-transfer
const recipientAddress = "0xeD35957be660cf9A2C768fDB52eC4D9D19eEB80F";

async function main() {
  try {
    console.log("Creating wallet...");
    const wallet = Wallet.fromPhrase(walletPhrase);

    console.log("Wallet created");
    console.log("Sender address:", wallet.address);

    const requestBody = {
      transaction: {
        data: {
          chainId: "sepolia", // Target Ethereum testnet chain
          mode: "transfer",
          senderAddress: wallet.address,
          recipientAddress: recipientAddress || wallet.address, // Self-send if no recipient
          amount: "10000", // Transaction amount
          useMaxAmount: false,
          fees: "0",
          gas: "0",
          memo: "",
          format: "json",
          validatorAddress: "",
          params: {
            pubKey: wallet.publicKey, // Public key of the wallet
          },
        },
      },
    };

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

    const encodedData = await responseEncode.json();
    console.log(
      "Encoded transaction data:",
      JSON.stringify(encodedData, null, 2)
    );

    // Sign the encoded transaction
    const tx = encodedData.transaction.encoded;
    console.log("Signing transaction...");
    const signature = await wallet.signTransaction(tx);
    console.log("Transaction signed");

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

    console.log("Broadcasting transaction...");

    // Broadcast the transaction using Adamik API
    const responseBroadcast = await fetch(
      "https://api.adamik.io/api/sepolia/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 Result:", JSON.stringify(responseData, null, 2));
    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 mnemonic phrase, 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 translates them into a raw transaction to be signed.
  • Signing and Broadcasting: The transaction is then signed with the wallet’s private key and finally broadcast to the Ethereum network using the Adamik API.

4. Troubleshooting

  • Invalid API Key: Ensure your Adamik API key is correct.
  • Network Issues: Verify network availability and Adamik API status.
  • Security Note: Never hardcode sensitive information like mnemonic phrases in your production environment.

5. Get in Touch

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