transact method
Future<Map>
transact(
- Wallet wallet,
- Contract contract,
- String funcName,
- List funcParams,
- String to, {
- BigInt? value,
- int expiration = 32,
- int gasPriceCoef = 0,
- int gas = 0,
- String? dependsOn,
- bool force = false,
- 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);
}