Closed shuse2 closed 3 years ago
One utility function could be a function that enables computing the required minimum fee for a transaction where the fee property is not set. The challenge here is that the minimum required fee of a transaction depends on the transaction size, which again depends on the value of the fee property of the transaction. Therefore, it is not straightforward to precompute the exact minimum required fee.
We overestimate the size of the fee property and use this approximation to find a sufficient minimum fee, which can be slightly above the minimum required transaction fee. More precisely, we do the following:
trs
be the transaction for which the minimum fee has to be computed.k
be the number of signatures required by the transaction (k=1
for non-multisignature accounts and k=numberOfSignatures
for multisignature account). Then set the signatures
property of trs
to an array of k
bytes of size 64 (e.g., zero bytes).fee
property of trs to 2^64-1
(max unsigned int64)trs.size()*minFeePerByte+trs.getNameFee()
where trs.size()
is the size of the transaction with the changed fee and signatures property.minFeePerByte
value in LIP 13 is 1000, a balance transfer is about 125 bytes
so the minimum fee for a balance transfer would be 125,000. Note that 2^21 = 2,097,152 so that the minimum fee for all transactions up to around 2 kB can be encoded in 3 Bytes.We compute the exact minimum required fee. One approach is the following:
Do 1. and 2. as above
findRequiredMinFee
on the transaction with trs.fee
initialized with 0
.computeMinFee(trs):
return trs.size()*minFeePerByte+trs.getNameFee()
findRequiredMinFee(trs):
minFee=trs.computeMinFee()
while minFee > trs.getFee():
trs.setFee(minFee)
minFee=trs.computeMinFee()
The idea is to use an iteration that iteratively computes the transaction size and then sets the fee of the transaction using that size. Note that both the transaction size and minimum fee are monotonically increasing in the function. Observe the following:
getMinFee(
trx: Record<string, unknown>,
assetSchema: Schema,
{ minFeePerByte: number, baseFees: [{moduleId: number, assetId: number, baseFee: string}]}
): bigint
Description
Add utility function
calculateMinFee
andgetMinFee
to the lisk-transactions library, which can be used by UI/Service to calculate the minimum fee required to be shown to the end-users.Motivation
Currently, lisk-transactions has no utility function to calculate minimum fee for a given transaction with fee set to 0.
Acceptance Criteria
0
and calledgetMinFee
should return transaction with fee set to minimum fee requirement