import { mnemonicToPrivateKey, sign } from "@ton/crypto";
import { WalletContractV4 } from "@ton/ton";
// Replace with your wallet's mnemonic phrase
const walletPhrase = "YOUR MNEMONIC HERE";
const ADAMIK_API_KEY = "your-adamik-api-key"; // get it from https://dashboard.adamik.io
const recipientAddress = "recipient address";
async function main() {
// Generate a key pair from the mnemonic phrase
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();
// Prepare the transaction request
const requestBody = {
transaction: {
data: {
chainId: "ton", // TON blockchain
mode: "transfer",
sender: address,
recipient: recipientAddress,
amount: Math.floor(Math.random() * (10000 - 2 + 1) + 2).toString(), // Random amount for testing
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"),
},
},
},
};
// Encode the transaction with Adamik API
const response = 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 json = await response.json();
console.log(`Encoded Transaction: ${json.transaction.encoded}`);
// Sign the encoded transaction
const signature = sign(
Buffer.from(json.transaction.encoded, "hex"),
keyPair.secretKey
).toString("hex");
console.log(`Signature: ${signature}`);
// Prepare to broadcast the signed transaction
const broadcastRequestBody = {
transaction: {
data: json.transaction.data,
encoded: json.transaction.encoded,
signature: signature,
},
};
// Broadcast the transaction using Adamik API
const broadcastResponse = await fetch(
"https://api.adamik.io/api/ton/transaction/broadcast",
{
method: "POST",
headers: {
Authorization: ADAMIK_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(broadcastRequestBody),
}
);
const tonResponse = await broadcastResponse.json();
console.log("Transaction Result:", JSON.stringify(tonResponse, null, 2));
}
main();