calcEmulateTxBody function

Map calcEmulateTxBody(
  1. String caller,
  2. Map txBody, {
  3. String? gaspayer,
})

Rip an emulated tx body from a normal tx body.

Implementation

Map calcEmulateTxBody(String caller, Map txBody, {String? gaspayer}) {
  if (!Address.isAddress(caller)) {
    throw Exception('Caller $caller is not an address');
  }

  //Caution: in emulation, clauses.clause.value must be of type string
  var eClauses = [];
  for (var clause in txBody['clauses']) {
    if (clause is String) {
      Map c = json.decode(clause);

      eClauses.add(
          {'to': c['to'], 'value': c['value'].toString(), 'data': c['data']});
    } else {
      eClauses.add({
        'to': clause['to'],
        'value': clause['value'].toString(),
        'data': clause['data']
      });
    }
  }

  var eTxBody = {
    "caller": caller,
    "blockRef": txBody["blockRef"],
    "expiration": txBody["expiration"],
    "clauses": eClauses,
  };

  // Set gas field only when the txBody set it.
  if (txBody["gas"] > 0) {
    eTxBody["gas"] = txBody["gas"];
  }

  // Set gas payer only when required.
  if (gaspayer != null) {
    if (!Address.isAddress(gaspayer)) {
      throw Exception("Gaspayer $gaspayer is not an address");
    }
    eTxBody["gasPayer"] = gaspayer;
  }
  return eTxBody;
}