Documentation Index
Fetch the complete documentation index at: https://docs.adamik.io/llms.txt
Use this file to discover all available pages before exploring further.
This guide demonstrates how to create, sign, and broadcast a transaction using the Adamik API along with a TON signer. This tutorial is perfect for developers who want to leverage Adamikto power their TON workflow, ensuring secure and efficient transaction management.
Prerequisites
- Node.js and npm installed
- Basic understanding of TON blockchain and transaction workflows
- Familiarity with the TON libraries (
@ton/crypto, @ton/ton)
Step-by-Step Guide
1. Setting Up the Environment
Ensure you have the required TON libraries installed in your project:
npm install @ton/crypto @ton/ton node-fetch
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 { mnemonicToPrivateKey, sign } from "@ton/crypto";
import { WalletContractV4 } from "@ton/ton";
// 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 recipient address or leave empty for self-transfer
const recipientAddress = "";
async function main() {
try {
console.log("Creating wallet...");
const keyPair = await mnemonicToPrivateKey(walletPhrase.split(" "));
// Define the workchain and create a wallet contract
let workchain = 0;
let wallet = WalletContractV4.create({
workchain,
publicKey: keyPair.publicKey,
});
const address = wallet.address.toString();
console.log("Wallet created: ", address);
// Prepare the transaction request
const requestBody = {
transaction: {
data: {
chainId: "ton", // TON blockchain
mode: "transfer",
senderAddress: address,
recipientAddress: recipientAddress || address,
amount: "10000",
useMaxAmount: false,
fees: "0", // Set fees to 0 for now
gas: "0", // Gas is not required on TON
memo: "", // Optionally, add a memo
format: "hex",
validatorAddress: "",
params: {
pubKey: keyPair.publicKey.toString("hex"),
},
},
},
};
console.log("Encoding transaction...");
// Encode the transaction with Adamik API
const responseEncode = await fetch(
"https://api.adamik.io/api/ton/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...");
// Sign the encoded transaction
const signature = sign(
Buffer.from(encodedData.transaction.encoded, "hex"),
keyPair.secretKey
).toString("hex");
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/ton/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
- Key Pair Generation: The script generates a key pair from a mnemonic phrase using the
@ton/crypto library, which is helpful for managing private keys and signing transactions on TON. Please note Adamik API doesn’t have the custody of the assets.
- Transaction Encoding: The transaction details, such as sender, recipient, and amount, are passed to the Adamik API, which encodes the transaction for signing.
- Signing and Broadcasting: The encoded transaction is signed using the private key, and the signed transaction is then broadcast to the TON blockchain via the Adamik API.
4. Troubleshooting
- Invalid API Key: Ensure your Adamik API key is correct and up to date.
- Network Issues: Verify network availability and Adamik API status.
- Security Note: Never hardcode sensitive information such as mnemonic phrases or private keys in your production environment.
5. Get in Touch
Connect with us through our Discord Server or directly through GitHub.