Atomic Transaction (Grouped Transaction Issue)

  1. Hello, I am working on an atomic transaction using the javascript sdk and it seems I can only make two grouped transactions, It doesn’t work when it is more than 2 transactions grouped together. Am I doing something wrong?

It does not work with 3 or more grouped transactions like below:

// Example: working with transaction groups

const algosdk = require('..');

const baseServer = 'https://testnet-algorand.api.purestake.io/ps2';

const port = '';

const token = {
  'X-API-key': '4lVOI7xBz51ScAknbV25oal9j66xGWnl7dCAr5mI',
};

async function main() {
  // initialize an algod client
  const client = new algosdk.Algodv2(token, baseServer, port);

  // retrieve a sender and receiver
  const receiver1 = algosdk.mnemonicToSecretKey(
    'key'
  );
  const receiver2 = algosdk.mnemonicToSecretKey(
    'key'
  );
  const sender = algosdk.mnemonicToSecretKey(
    'key'
  );

  // get suggested parameters
  const suggestedParams = await client.getTransactionParams().do();
  // console.log(suggestedParams.genesisHash);
  // create the transactions
  const amount = 1000000;
  console.log(sender);
  const txn1 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
    from: sender.addr,
    to: receiver1.addr,
    amount,
    fee: 100,
    suggestedParams,
  });
  const txn2 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
    from: sender.addr,
    to: receiver2.addr,
    amount,
    fee: 100,
    suggestedParams,
  });
  const txn3 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
    from: sender.addr,
    to: receiver2.addr,
    amount,
    fee: 100,
    suggestedParams,
  });
  const txn4 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
    from: sender.addr,
    to: receiver2.addr,
    amount,
    fee: 100,
    suggestedParams,
  });

  // assign group id to transactions
  algosdk.assignGroupID([txn1, txn2, txn3, txn4]);

  // sign transactions
  const stxn1 = txn1.signTxn(sender.sk);
  const stxn2 = txn2.signTxn(sender.sk);
  const stxn3 = txn3.signTxn(sender.sk);
  const stxn4 = txn4.signTxn(sender.sk);

  // send transactions (note that the accounts need to be funded for this to work)
  console.log('Sending transactions...');
  const sendTx = await client
    .sendRawTransaction([stxn1, stxn2, stxn3, stxn4])
    .do();
  console.log('Transaction ID');
  console.log(sendTx.txId);
}

main().catch(console.error);

But this works with just two grouped transactions:

// Example: working with transaction groups

const algosdk = require('..');

const baseServer = 'https://testnet-algorand.api.purestake.io/ps2';

const port = '';

const token = {
  'X-API-key': '4lVOI7xBz51ScAknbV25oal9j66xGWnl7dCAr5mI',
};

async function main() {
  // initialize an algod client
  const client = new algosdk.Algodv2(token, baseServer, port);

  // retrieve a sender and receiver
  const receiver1 = algosdk.mnemonicToSecretKey(
    'key'
  );
  const receiver2 = algosdk.mnemonicToSecretKey(
    'key'
  );
  const sender = algosdk.mnemonicToSecretKey(
    'key'
  );

  // get suggested parameters
  const suggestedParams = await client.getTransactionParams().do();
  // console.log(suggestedParams.genesisHash);
  // create the transactions
  const amount = 1000000;
  const txn1 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
    from: sender.addr,
    to: receiver1.addr,
    amount,
    fee: 100,
    suggestedParams,
  });
  const txn2 = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
    from: sender.addr,
    to: receiver2.addr,
    amount,
    fee: 100,
    suggestedParams,
  });

  // assign group id to transactions
  algosdk.assignGroupID([txn1, txn2]);

  // sign transactions
  const stxn1 = txn1.signTxn(sender.sk);
  const stxn2 = txn2.signTxn(sender.sk);

  // send transactions (note that the accounts need to be funded for this to work)
  console.log('Sending transactions...');
  const sendTx = await client.sendRawTransaction([stxn1, stxn2]).do();
  console.log('Transaction ID');
  console.log(sendTx.txId);
}

main().catch(console.error);

Can you show the errors you get?

There should be no difference between two and four transactions.
One issue may be that you don’t have enough Algos.

Also, why are you specifying fee: 100? It is best to let the system auto-calculate the fee, or if you want to fail in case of fee increase, to force it at a flat fee. But fee: 100 will achieve neither of those.

The error says: “transaction already in ledger”

The issue is that txn2, txn3, and txn4 are the exact same transactions. This is not allowed.

You need to change them: either use different amounts, different senders, or different notes.

1 Like

Thank you! It worked with your suggested changes.