Sign and Broadcast Transaction

To sign and broadcast a transaction to the TON network, follow the steps below specified for the type of pool you participate in.

Nominator Pool

  1. Prepare unsigned transaction in Base64 encrypted format.

  2. Sign the transaction using the following code. In the example below, the TonWeb signing library is used. You can also sign the transaction via custom wallets or via QR code integration.

const TonWeb = require('tonweb');
// Use testnet or mainnet URL
const tonweb = new TonWeb(new TonWeb.HttpProvider('<https://testnet.toncenter.com/api/v2/jsonRPC>'));
const userPrivateKey = new Uint8Array([/* your private key here */]);
// Init wallet with TonWeb
const WalletClass = TonWeb.wallet.all.v3R2;
const wallet = new WalletClass(tonweb.provider, {
    publicKey: TonWeb.utils.bytesToBase64(userPrivateKey),
    wc: 0 // Use 0 for mainnet, -1 for testnet
});
// Define the nominator pool address
const poolAddress = 'EQA...';

// The transaction received in step 1
const unsignedTx = '...'; 

async function signTransaction() {
	const signedTransaction = await unsignedTx.sign(userPrivateKey);
	console.log('Signed transaction:', signedTransaction);
}

signTransaction().catch

  1. Broadcast the transaction to the TON network by sending a POST request to /api/v1/ton/{network}/transaction/broadcast.

Example request (for testnet network):

curl --request POST \
     --url https://api.p2p.org/api/v1/ton/testnet/transaction/broadcast \
     --header 'accept: application/json' \
     --header 'authorization: Bearer <token>' \
     --header 'content-type: application/json' \
     --data '
     {
       "signedTransaction": "0x450284002c6eca5cdaa3e87d7f8e06d10015bf0508b52d301c8991af113d5cf49a53553f01befdb7fa39c5a995a8d58676a0513d082be"
     }'
  • signedTransactionsigned transaction in the hexadecimal format.

Example response:


Single Nominator Pool

Use the example of code for the TonWeb library below to first retrieve the unsigned serialized transaction and then sign and broadcast it.

// Import the required libraries for TON interaction (using TonWeb as an example)
const TonWeb = require('tonweb');

// Initialize TonWeb instance (you can use testnet or mainnet URL)
const tonweb = new TonWeb(new TonWeb.HttpProvider('<https://testnet.toncenter.com/api/v2/jsonRPC>')); // Example: testnet

// Define the user's wallet private key (replace with your actual private key)
const userPrivateKey = new Uint8Array([/* your private key here */]);

// Initialize wallet with TonWeb (use your wallet type and the key pair)
const WalletClass = TonWeb.wallet.all.v3R2; // Example wallet
const wallet = new WalletClass(tonweb.provider, {
    publicKey: TonWeb.utils.bytesToBase64(userPrivateKey),
    wc: 0 // Use 0 for mainnet, -1 for testnet
});

// Define the pool address (Single Nominator Pool address)
const poolAddress = 'EQA...'; // Replace with the actual pool address

// Define the amount you wish to stake (in nanoTONs)
const stakeAmount = TonWeb.utils.toNano(10000); // Example: 10,000 TONs

// Create the transaction body (without memo as it's not required for Single Nominator Pool)
async function createUnsignedTransaction() {
    const seqno = await wallet.methods.seqno().call(); // Get the sequence number from the wallet

    // Prepare the transaction details
    const unsignedTransaction = wallet.methods.transfer({
        secretKey: userPrivateKey, // Signing happens later
        toAddress: poolAddress, // Pool contract address
        amount: stakeAmount, // Stake amount
        seqno: seqno, // Sequence number from the wallet
        sendMode: 3, // Default mode for staking
    });

    return unsignedTransaction;
}

// Sign the transaction and send it
async function signAndSendTransaction() {
    const unsignedTx = await createUnsignedTransaction();

    // Sign the transaction using the wallet private key
    const signedTransaction = await unsignedTx.sign(userPrivateKey);

    // Broadcast the signed transaction to the TON network (through API)
    const broadcastResponse = await tonweb.provider.sendBoc(signedTransaction.toBoc());

    console.log('Transaction broadcast result:', broadcastResponse);
}

// Run the function to sign and broadcast the staking transaction
signAndSendTransaction().catch(console.error);

What's Next?