Calculating max fee for a transfer request

I am trying to add maxFee parameter to a transfer txn using the getTransactionCost method, however I get the error -
_FuelError: Validity(InsufficientFeeAmount { expected: 17396, provided: 49 })
_FuelError: InsufficientMaxFee { max_fee_from_policies: 17396, max_fee_from_gas_price: 17511 }

I am adding fee to the request as follows:

request.addResources(
      await this.client.getResourcesToSpend(
        B256From,
        gasResults.txnCost.requiredQuantities
      )
    );

    const gasResults = await this.client.getTransactionCost(props.txnRequest);
    request.gasLimit = gasResults.txnCost.gasUsed;
    request.maxFee = gasResults.txnCost.maxFee;

Hey @singhparshant :wave:

Ccould you share the full code snippet that you’re trying to run, including the construction of the transaction request props.txnRequest?

Hi @p.s ,This is my transaction request:

const request = new ScriptTransactionRequest();

    const B256To = toB256(to as Bech32Address);
    const B256From = toB256(from as Bech32Address);

    request.addCoinOutput(
      new Address(toBech32(B256To)),
      transferAmount.toString(),
      this.client.getBaseAssetId()
    );

    const result = await this.client.getTransactionCost(request);
    const gasResults =  {
      txnCost: result,
      txnRequest: request,
    };

    request.addResources(
      await this.client.getResourcesToSpend(
        B256From,
        gasResults.txnCost.requiredQuantities
      )
    );

    request.gasLimit = gasResults.txnCost.gasUsed;
    request.maxFee = gasResults.txnCost.maxFee;

And when i try to dry run as well, i see this error:

"error": [FuelError: InsufficientMaxFee { max_fee_from_policies: 17396, max_fee_from_gas_price: 17511 }], "errorUpdateCount": 2, "errorUpdatedAt": 1721221511408, "failureCount": 4, "failureReason": [FuelError: InsufficientMaxFee { max_fee_from_policies: 17396, max_fee_from_gas_price: 17511 }],

I believe the transaction flow here should be as follows:

  • Add coin output to the request (addCoinOutput )

  • Get the resource to spend for the request (getResourcesToSpend)

  • Add the resources to the request (addResources)

  • We can then get the transaction cost (getTransactionCost)

  • Set the maxFee and gasLimit for the request

The following snippet shows the flow in action.


  const request = new ScriptTransactionRequest();

  request.addCoinOutput(
    recipent,
    transferAmount.toString(),
    provider.getBaseAssetId()
  );

  const resourcesToSpend = await provider.getResourcesToSpend(
    owner.address,
    request.getCoinOutputs()
  )
  request.addResources(resourcesToSpend);

  const txCost = await provider.getTransactionCost(request);
  request.gasLimit = txCost.gasUsed;
  request.maxFee = txCost.maxFee;

  const tx = await owner.sendTransaction(request);
  const result = await tx.waitForResult();