Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13115060
D12378.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Subscribers
None
D12378.diff
View Options
diff --git a/web/cashtab/src/components/Send/Send.js b/web/cashtab/src/components/Send/Send.js
--- a/web/cashtab/src/components/Send/Send.js
+++ b/web/cashtab/src/components/Send/Send.js
@@ -51,6 +51,7 @@
toLegacyCash,
toLegacyCashArray,
fromSatoshisToXec,
+ calcFee,
} from 'utils/cashMethods';
import ApiError from 'components/Common/ApiError';
import { formatFiatBalance, formatBalance } from 'utils/formatting';
@@ -215,7 +216,7 @@
setIsModalVisible(false);
};
- const { getRestUrl, sendXec, calcFee } = useBCH();
+ const { getRestUrl, sendXec } = useBCH();
// If the balance has changed, unlock the UI
// This is redundant, if backend has refreshed in 1.75s timeout below, UI will already be unlocked
diff --git a/web/cashtab/src/hooks/__tests__/useBCH.test.js b/web/cashtab/src/hooks/__tests__/useBCH.test.js
--- a/web/cashtab/src/hooks/__tests__/useBCH.test.js
+++ b/web/cashtab/src/hooks/__tests__/useBCH.test.js
@@ -38,13 +38,6 @@
expect(getRestUrl(0)).toBe(expectedApiUrl);
});
- it('calculates fee correctly for 2 P2PKH outputs', () => {
- const { calcFee } = useBCH();
- const utxosMock = [{}, {}];
-
- expect(calcFee(utxosMock, 2, 1.01)).toBe(378);
- });
-
it('sends XEC correctly', async () => {
const { sendXec } = useBCH();
const BCH = new BCHJS();
diff --git a/web/cashtab/src/hooks/useBCH.js b/web/cashtab/src/hooks/useBCH.js
--- a/web/cashtab/src/hooks/useBCH.js
+++ b/web/cashtab/src/hooks/useBCH.js
@@ -11,7 +11,6 @@
generateTokenTxOutput,
signAndBuildTx,
getChangeAddressFromInputUtxos,
- getCashtabByteCount,
} from 'utils/cashMethods';
import ecies from 'ecies-lite';
@@ -33,16 +32,6 @@
return apiArray[apiIndex];
};
- const calcFee = (
- utxos,
- p2pkhOutputNumber = 2,
- satoshisPerByte = currency.defaultFee,
- ) => {
- const byteCount = getCashtabByteCount(utxos.length, p2pkhOutputNumber);
- const txFee = Math.ceil(satoshisPerByte * byteCount);
- return txFee;
- };
-
const createToken = async (
BCH,
chronik,
@@ -557,7 +546,6 @@
return {
getBCH,
- calcFee,
getRestUrl,
sendXec,
sendToken,
diff --git a/web/cashtab/src/utils/__tests__/cashMethods.test.js b/web/cashtab/src/utils/__tests__/cashMethods.test.js
--- a/web/cashtab/src/utils/__tests__/cashMethods.test.js
+++ b/web/cashtab/src/utils/__tests__/cashMethods.test.js
@@ -28,6 +28,7 @@
getUtxoWif,
generateTokenTxOutput,
getCashtabByteCount,
+ calcFee,
} from 'utils/cashMethods';
import { currency } from 'components/Common/Ticker';
import {
@@ -1575,4 +1576,8 @@
BCH.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 2000 }),
);
});
+ it('calculates fee correctly for 2 P2PKH outputs', () => {
+ const utxosMock = [{}, {}];
+ expect(calcFee(utxosMock, 2, 1.01)).toBe(378);
+ });
});
diff --git a/web/cashtab/src/utils/cashMethods.js b/web/cashtab/src/utils/cashMethods.js
--- a/web/cashtab/src/utils/cashMethods.js
+++ b/web/cashtab/src/utils/cashMethods.js
@@ -6,7 +6,6 @@
} from 'utils/validation';
import BigNumber from 'bignumber.js';
import cashaddr from 'ecashaddrjs';
-import useBCH from '../hooks/useBCH';
export const getUtxoWif = (utxo, wallet) => {
if (!wallet) {
@@ -40,6 +39,45 @@
return txBuilder;
};
+export const getCashtabByteCount = (p2pkhInputCount, p2pkhOutputCount) => {
+ // Simplifying bch-js function for P2PKH txs only, as this is all Cashtab supports for now
+ // https://github.com/Permissionless-Software-Foundation/bch-js/blob/master/src/bitcoincash.js#L408
+ /*
+ const types = {
+ inputs: {
+ 'P2PKH': 148 * 4,
+ },
+ outputs: {
+ P2PKH: 34 * 4,
+ },
+ };
+ */
+
+ const inputCount = new BigNumber(p2pkhInputCount);
+ const outputCount = new BigNumber(p2pkhOutputCount);
+ const inputWeight = new BigNumber(148 * 4);
+ const outputWeight = new BigNumber(34 * 4);
+ const nonSegwitWeightConstant = new BigNumber(10 * 4);
+ let totalWeight = new BigNumber(0);
+ totalWeight = totalWeight
+ .plus(inputCount.times(inputWeight))
+ .plus(outputCount.times(outputWeight))
+ .plus(nonSegwitWeightConstant);
+ const byteCount = totalWeight.div(4).integerValue(BigNumber.ROUND_CEIL);
+
+ return Number(byteCount);
+};
+
+export const calcFee = (
+ utxos,
+ p2pkhOutputNumber = 2,
+ satoshisPerByte = currency.defaultFee,
+) => {
+ const byteCount = getCashtabByteCount(utxos.length, p2pkhOutputNumber);
+ const txFee = Math.ceil(satoshisPerByte * byteCount);
+ return txFee;
+};
+
export const generateTokenTxOutput = (
BCH,
txBuilder,
@@ -128,7 +166,6 @@
satoshisToSend,
feeInSatsPerByte,
) => {
- const { calcFee } = useBCH();
let txInputObj = {};
const inputUtxos = [];
let txFee = 0;
@@ -191,7 +228,6 @@
let remainderTokenValue = new BigNumber(0);
let totalXecInputUtxos = [];
let txFee = 0;
- const { calcFee } = useBCH();
let tokenUtxosBeingSpent = [];
try {
@@ -1036,32 +1072,3 @@
ws._subs.length > 0
);
};
-
-export const getCashtabByteCount = (p2pkhInputCount, p2pkhOutputCount) => {
- // Simplifying bch-js function for P2PKH txs only, as this is all Cashtab supports for now
- // https://github.com/Permissionless-Software-Foundation/bch-js/blob/master/src/bitcoincash.js#L408
- /*
- const types = {
- inputs: {
- 'P2PKH': 148 * 4,
- },
- outputs: {
- P2PKH: 34 * 4,
- },
- };
- */
-
- const inputCount = new BigNumber(p2pkhInputCount);
- const outputCount = new BigNumber(p2pkhOutputCount);
- const inputWeight = new BigNumber(148 * 4);
- const outputWeight = new BigNumber(34 * 4);
- const nonSegwitWeightConstant = new BigNumber(10 * 4);
- let totalWeight = new BigNumber(0);
- totalWeight = totalWeight
- .plus(inputCount.times(inputWeight))
- .plus(outputCount.times(outputWeight))
- .plus(nonSegwitWeightConstant);
- const byteCount = totalWeight.div(4).integerValue(BigNumber.ROUND_CEIL);
-
- return Number(byteCount);
-};
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Mar 1, 09:31 (3 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5187212
Default Alt Text
D12378.diff (6 KB)
Attached To
D12378: [Cashtab] [p3 remove BCH.getByteCount] Move calcFee to cashMethods
Event Timeline
Log In to Comment