estimateOperation method

dynamic estimateOperation(
  1. String server,
  2. String chainId,
  3. dynamic opResults
)

Implementation

estimateOperation(String server, String chainId, opResults) async {
  var gas = 0;
  var storageCost = 0;
  var staticFee = 0;
  for (var ele in opResults['contents']) {
    try {
      gas += (int.parse(ele['metadata']['operation_result']
                      ['consumed_milligas']
                  .toString()) ~/
              1000) +
          GAS_BUFFER;
      storageCost += int.parse((ele['metadata']['operation_result']
                  ['paid_storage_size_diff'] ??
              '0')
          .toString());

      if (ele['kind'] == 'origination' ||
          ele['metadata']['operation_result']
                  ['allocated_destination_contract'] !=
              null) {
        storageCost += TezosConstants.EmptyAccountStorageBurn;
      } else if (ele['kind'] == 'reveal') {
        staticFee += 1270;
      }
    } catch (e) {
      throw "Error while estimating operation: $e";
    }

    var internalOperations = ele['metadata']['internal_operation_results'];
    if (internalOperations == null) {
      continue;
    }

    for (var internalOperation in internalOperations) {
      var result = internalOperation['result'];
      gas += (int.parse(result['consumed_milligas'] ?? '0') ~/ 1000) +
          GAS_BUFFER;
      storageCost += int.parse(result['paid_storage_size_diff'] ?? '0');
      if (internalOperation['kind'] == 'origination') {
        storageCost += TezosConstants.EmptyAccountStorageBurn;
      }
    }
  }

  var validBranch = 'BMLxA4tQjiu1PT2x3dMiijgvMTQo8AVxkPBPpdtM8hCfiyiC1jz';
  var forgedOperationGroup =
      await TezosNodeWriter.forgeOperations(server, validBranch, operations);
  var operationSize = forgedOperationGroup.length / 2 + 64;
  var estimatedFee = staticFee +
      (gas / 10).ceil() +
      TezosConstants.BaseOperationFee +
      operationSize +
      TezosConstants.DefaultBakerVig;
  var estimatedStorageBurn =
      (storageCost * TezosConstants.StorageRate).ceil();

  return {
    'gas': gas,
    'storageCost': storageCost,
    'estimatedFee': estimatedFee.toInt(),
    'estimatedStorageBurn': estimatedStorageBurn
  };
}