stacks-archive / stacks-transactions-js

The JavaScript library for generating Stacks 2.0 transactions
19 stars 17 forks source link

Add fee estimate #45

Closed yknl closed 4 years ago

yknl commented 4 years ago

This adds the estimateTransfer function to fetch and calculate the estimated fee for the token transfer transaction. The makeSTXTokenTransfer and smart contract tx builders will now also estimate and set fees automatically if not specified. This necessitated changing the builder functions to async and moving the fee argument into the options object.

Usage

const options = {
  memo: "test memo",
  nonce: new BigNum(0)
};

// The tx fee is now estimated and set automatically
const transaction = await makeSTXTokenTransfer(
  recipientAddress,
  amount,
  secretKey,
  options
);

// To check the estimated fee
console.log(transaction.auth.spendingCondition.fee.toString(10));

To set fees manually

const options = {
  ...
  fee: new BigNum(100),
};

const transaction = await makeSTXTokenTransfer(
  ...
  options
);

The estimate function can also be used independently

const fee = await estimateTransfer(transaction, apiUrl);

For details refer to issue #14

Type of Change

Does this introduce a breaking change?

Yes, the transaction builder functions are now all async. The function signatures have been changed as well, removing fee as an argument.

Are documentation updates required?

Yes, readme updated.

Checklist

codecov[bot] commented 4 years ago

Codecov Report

Merging #45 into master will decrease coverage by 1.74%. The diff coverage is 62.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #45      +/-   ##
==========================================
- Coverage   87.20%   85.45%   -1.75%     
==========================================
  Files          25       26       +1     
  Lines        1321     1403      +82     
  Branches      238      250      +12     
==========================================
+ Hits         1152     1199      +47     
- Misses        168      203      +35     
  Partials        1        1              
Impacted Files Coverage Δ
src/builders.ts 75.16% <51.35%> (-22.46%) :arrow_down:
src/authorization.ts 76.70% <100.00%> (+0.26%) :arrow_up:
src/index.ts 100.00% <100.00%> (ø)
src/network.ts 100.00% <100.00%> (ø)
src/transaction.ts 78.57% <100.00%> (-0.55%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update cfac1ba...b6b57fe. Read the comment docs.