1
async function getTransaction(){
    $.ajax({
        url: 'http://localhost:8080/gettransaction',
        type: 'GET',
        dataType: 'json',
        success: async function(data) {
            console.log(data); // Log the data for debugging

            // Deserialize the Transaction object
            var transaction = solanaWeb3.Transaction.from(data);

            // Sign and send the transaction
            var provider = await getProvider();
            if (!provider) return; // Ensure provider is available
            let signed = await provider.signTransaction(transaction);
            var connection = new solanaWeb3.Connection(solanaWeb3.clusterApiUrl('mainnet-beta'));
            let signature = await connection.sendRawTransaction(signed.serialize());
            await connection.confirmTransaction(signature);
            console.log("Transaction signature:", signature);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.error('Error:', errorThrown); // Handle errors here
        }
    });
}
app.get('/gettransaction', async (req, res) => {
    try {
        const amountInSol = 0.01;   
        const publicKey = new solanaWeb3.PublicKey(publicKeyStr);
        var connection = new solanaWeb3.Connection(rpcNodeLink);
        var recieverWallet = new solanaWeb3.PublicKey(recieverWalletAddress);
        var transaction = new solanaWeb3.Transaction().add(
        solanaWeb3.SystemProgram.transfer({
            fromPubkey: publicKey,
            toPubkey: recieverWallet,
            lamports: solanaWeb3.LAMPORTS_PER_SOL * amountInSol //Investing 1 SOL. Remember 1 Lamport = 10^-9 SOL.
        }),
        );
        transaction.feePayer = publicKey;


        if(transaction) {
            console.log("Txn created successfully");
        }  
        res.status(200).json(transaction);
    } catch (error) {
        console.error(error);
        res.status(500).json({ error: 'Internal server error' });
    }
});

The first is the front end code

And the second is the backend code

When I try to sign the transaction or deserialize it ( still didnt figure out which ), I get a buffer$1 is not iterable error, I was not expecting it, as I cant wrap my head around why signing the transaction using the phantom wallet provider would require the buffer from the backend in the frontend, and I can't sign it in the backend because I dont have access to the provider, I also dont want to do it using a private Key. I though of reconstructing the transaction on the front end, but with no success.

I tried writing it all in the frontend and couldnt create the transaction, and had to use an api to make it in the backend and return it to the front end, but now I have issues when it comes to signing it, as i get a buffer$1 is not iterable error, I tried using a CDN to get a Buffer into my front end, but all I got were more errors.

2 Answers 2

0

It looks like you're sending the Transaction object back to your frontend, but the frontend is expecting a serialized buffer. In your backend, try serializing the transaction before sending by doing:

        res.status(200).json(transaction.serialize());
0

Once you have built and signed the transaction on your backend, you will need to send the serialized version to your frontend.

You can use the serialize() method on the transaction to format the transaction in a way that will easily send over the wire without errors (I like base64 encoding mine since it helps avoid some of the errors you might get in some JS frameworks):

return res.status(200).json({
   serializedTransaction: Buffer.from(transaction.serialize()).toString("base64")
})

Then on your frontend, after your get a successful API response, you will need to parse and deserialize the transaction:

let transaction = solanaWeb3.Transaction.from(Buffer.from("", "base64"));

And finally, you can now have the frontend handle the final transaction signing from your user. Then send the transaction to the Solana blockchain the way you already have it setup

let signed = await provider.signTransaction(transaction);

PS: Don't use var to create variables. That is effectively deprecated. The recommended way is to use let if you need to mutate the variable data and const if you do not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.