transact method

Future<Map> transact(
  1. Wallet wallet,
  2. Contract contract,
  3. String funcName,
  4. List funcParams,
  5. String to, {
  6. BigInt? value,
  7. int expiration = 32,
  8. int gasPriceCoef = 0,
  9. int gas = 0,
  10. String? dependsOn,
  11. bool force = false,
  12. Wallet? gasPayer,
})

Implementation

Future<Map> transact(Wallet wallet, Contract contract, String funcName,
    List funcParams, String to,
    {BigInt? value, // Note: value is in Wei
    int expiration = 32,
    int gasPriceCoef = 0,
    int gas = 0,
    String? dependsOn, // ID of old Tx that this tx depends on, None or string
    bool force = false, // Force execute even if emulation failed
    Wallet? gasPayer // fee delegation feature
    }) async {
  value ??= BigInt.zero;
  RClause clause =
      this.clause(contract, funcName, funcParams, to, value: value);
  var needFeeDelegation = gasPayer != null;
  var b = await getBlock();
  var txBody = buildTransaction([clause.getDevClause()], await getChainTag(),
      calcBlockRef(b["id"]), calcNonce(),
      gasPriceCoef: gasPriceCoef,
      dependsOn: dependsOn = dependsOn,
      expiration: expiration,
      gas: gas,
      feeDelegation: needFeeDelegation);

  Future<List<Map>> eResponses;
  // Emulate the tx first.
  if (!needFeeDelegation) {
    eResponses =
        emulateTx(wallet.adressString, json.decode(txBody.toJsonString()));
  } else {
    eResponses = emulateTx(
        wallet.adressString, json.decode(txBody.toJsonString()),
        gasPayer: gasPayer.adressString);
  }

  if (anyEmulateFailed(await eResponses) && force == false) {
    throw Exception(await eResponses);
  }

  // Get gas estimation from remote node
  // Calculate a safe gas for user
  var vmGas = readVmGases(await eResponses).sum;
  var safeGas = suggestGasForTx(vmGas, json.decode(txBody.toJsonString()));
  if (gas < safeGas) {
    if (gas != 0 && force == false) {
      throw Exception("gas $gas < emulated gas $safeGas");
    }
  }

  // Fill out the gas for user
  if (gas == 0) {
    txBody.gas.big = BigInt.from(safeGas);
  }

//post to remote node
  if (needFeeDelegation) {
    txBody = calcTxSignedWithFeeDelegation(wallet, gasPayer, txBody);
  }

  Uint8List h = blake2b256([txBody.encode()]);
  Uint8List sig = sign(h, wallet.priv).serialize();
  txBody.signature = sig;
  String raw = '0x' + bytesToHex(txBody.encode());

  return postTransaction(raw);
}