Skip to content

Commit

Permalink
Remove calcAmounts. Add tx.fee
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jun 18, 2024
1 parent 3766623 commit ce727bb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const tx = Transaction.prepare({
// Uses `random` from example above. Alternatively, pass 0x hex string or Uint8Array
const signedTx = tx.signBy(random.privateKey);
console.log('signed tx', signedTx, signedTx.toHex());
console.log('need total', signedTx.calcAmounts()); // wei & humanized formats
console.log('fee', signedTx.fee);
```

We support legacy, EIP2930, EIP1559 and EIP4844 (Dencun / Cancun) transactions.
Expand Down
16 changes: 4 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class Transaction<T extends TxType> {
setWholeAmount(accountBalance: bigint, burnRemaining = true): Transaction<T> {
if (typeof accountBalance !== 'bigint' || accountBalance <= 0n)
throw new Error('account balance must be bigger than 0');
const { fee } = this.calcAmounts().wei;
const fee = this.fee;
const amountToSend = accountBalance - fee;
if (amountToSend <= 0n) throw new Error('account balance must be bigger than fee of ' + fee);
const raw = { ...this.raw, value: amountToSend };
Expand Down Expand Up @@ -221,7 +221,8 @@ export class Transaction<T extends TxType> {
private calcHash(includeSignature: boolean) {
return bytesToHex(keccak_256(this.toRawBytes(includeSignature)));
}
calcAmounts() {
// Calculates MAXIMUM fee in wei that could be spent
get fee() {
const { type, raw } = this;
// Fee calculation is not exact, real fee can be smaller
let gasFee;
Expand All @@ -236,16 +237,7 @@ export class Transaction<T extends TxType> {
gasFee = r.maxFeePerGas;
}
// TODO: how to calculate 4844 fee?
const fee = raw.gasLimit * gasFee;
const amount = raw.value;
const amountWithFee = fee + amount;
const wei = { amount, fee, amountWithFee };
const humanized = {
amount: weieth.encode(amount),
fee: weieth.encode(fee),
amountWithFee: weieth.encode(amountWithFee),
};
return { wei, humanized };
return raw.gasLimit * gasFee;
}
clone() {
return new Transaction(this.type, cloneDeep(this.raw));
Expand Down
45 changes: 5 additions & 40 deletions test/fee.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,15 @@ describe('Fees', () => {
gasPrice: weigwei.decode('2'),
});
// 21k * 2 = 42
deepStrictEqual(tx.calcAmounts(), {
wei: {
amount: 1000000000000000000n,
fee: 42000000000000n,
amountWithFee: 1000042000000000000n,
},
humanized: { amount: '1', fee: '0.000042', amountWithFee: '1.000042' },
});
deepStrictEqual(tx.fee, 42000000000000n);
const tx2 = Transaction.prepare({
type: 'legacy',
to: '0x27b1fdb04752bbc536007a920d24acb045561c26',
nonce: 1n,
value: weieth.decode('1.23'),
gasPrice: weigwei.decode('55.3'),
});
deepStrictEqual(tx2.calcAmounts(), {
wei: {
amount: 1230000000000000000n,
fee: 1161300000000000n,
amountWithFee: 1231161300000000000n,
},
humanized: { amount: '1.23', fee: '0.0011613', amountWithFee: '1.2311613' },
});
deepStrictEqual(tx2.fee, 1161300000000000n);
});
should('EIP1559', () => {
const tx = Transaction.prepare({
Expand All @@ -48,14 +34,7 @@ describe('Fees', () => {
maxPriorityFeePerGas: weigwei.decode('1'),
});
// 21k * 2 = 42
deepStrictEqual(tx.calcAmounts(), {
wei: {
amount: 1000000000000000000n,
fee: 42000000000000n,
amountWithFee: 1000042000000000000n,
},
humanized: { amount: '1', fee: '0.000042', amountWithFee: '1.000042' },
});
deepStrictEqual(tx.fee, 42000000000000n);
const tx2 = Transaction.prepare({
to: '0x27b1fdb04752bbc536007a920d24acb045561c26',
nonce: 1n,
Expand All @@ -64,14 +43,7 @@ describe('Fees', () => {
maxPriorityFeePerGas: weigwei.decode('2'),
});
// 21k * 2 = 42
deepStrictEqual(tx2.calcAmounts(), {
wei: {
amount: 1230000000000000000n,
fee: 1161300000000000n,
amountWithFee: 1231161300000000000n,
},
humanized: { amount: '1.23', fee: '0.0011613', amountWithFee: '1.2311613' },
});
deepStrictEqual(tx2.fee, 1161300000000000n);
});
should('Whole amount', () => {
const tx = Transaction.prepare({
Expand All @@ -82,14 +54,7 @@ describe('Fees', () => {
maxPriorityFeePerGas: weigwei.decode('1'),
});
const tx2 = tx.setWholeAmount(weieth.decode('1'));
deepStrictEqual(tx.calcAmounts(), {
wei: {
amount: 1000000000000000000n,
fee: 42000000000000n,
amountWithFee: 1000042000000000000n,
},
humanized: { amount: '1', fee: '0.000042', amountWithFee: '1.000042' },
});
deepStrictEqual(tx.fee, 42000000000000n);
deepStrictEqual(tx2.raw, {
to: '0x27b1fdb04752bbc536007a920d24acb045561c26',
value: 999958000000000000n,
Expand Down

0 comments on commit ce727bb

Please sign in to comment.