Skip to content

Commit

Permalink
fix: paybutton imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lissavxo committed May 30, 2024
1 parent 2cb7d62 commit 21d2808
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 128 deletions.
5 changes: 4 additions & 1 deletion react/lib/components/PayButton/PayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import {
getCurrencyTypeFromAddress,
isValidCashAddress,
isValidXecAddress,
CurrencyObject
CurrencyObject,
generatePaymentId,
getCurrencyObject
} from '../../util';
import { PaymentDialog } from '../PaymentDialog';
export interface PayButtonProps extends ButtonProps {
to: string;
amount?: number | string;
Expand Down
3 changes: 1 addition & 2 deletions react/lib/components/PaymentDialog/PaymentDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import React, { useState, useEffect } from 'react';
import { Theme, ThemeName, ThemeProvider, useTheme } from '../../themes';
import Button, { ButtonProps } from '../Button/Button';
import { WidgetContainer } from '../Widget/WidgetContainer';
import { isValidCashAddress, isValidXecAddress } from '../../util/address';
import { Currency, CurrencyObject, Transaction } from '../../util/types';
import { Currency, CurrencyObject, Transaction, isValidCashAddress, isValidXecAddress } from '../../util';

export interface PaymentDialogProps extends ButtonProps {
to: string;
Expand Down
24 changes: 10 additions & 14 deletions react/lib/components/Widget/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ import {
import React, { useEffect, useMemo, useState } from 'react';
import copy from 'copy-to-clipboard';
import QRCode, { BaseQRCodeProps } from 'qrcode.react';
import io, { Socket } from 'socket.io-client';
import PencilIcon from '../../assets/edit-pencil';
import config from '../../config.json';

import { Theme, ThemeName, ThemeProvider, useTheme } from '../../themes';
import {
isValidCashAddress,
isValidXecAddress,
getCurrencyTypeFromAddress,
} from '../../util/address';
import { formatPrice } from '../../util/format';
import { Button, animation } from '../Button/Button';
import BarChart from '../BarChart/BarChart';

import { getCurrencyObject } from '../../util/satoshis';
import {
Currency,
getAddressBalance,
Expand All @@ -32,12 +25,15 @@ import {
getCashtabProviderStatus,
CryptoCurrency,
DECIMALS,
CurrencyObject
CurrencyObject,
getCurrencyObject,
formatPrice,
setListener,
encodeOpReturnProps,
isValidCashAddress,
isValidXecAddress,
getCurrencyTypeFromAddress,
} from '../../util';
import { setListener } from '../../util/socket';
import PencilIcon from '../../assets/edit-pencil';
import io, { Socket } from 'socket.io-client';
import { encodeOpReturnProps } from '../../util/opReturn';

type QRCodeProps = BaseQRCodeProps & { renderAs: 'svg' };

Expand Down
15 changes: 8 additions & 7 deletions react/lib/components/Widget/WidgetContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react';

import successSound from '../../assets/success.mp3.json';

import {
getCurrencyTypeFromAddress,
} from '../../util/address';
import {
getFiatPrice,
Currency,
CurrencyObject,
Transaction
Transaction,
generatePaymentId,
getCurrencyTypeFromAddress,
isCrypto,
isFiat,
isGreaterThanZero,
isValidCurrency,
resolveNumber
} from '../../util';

import Widget, { WidgetProps } from './Widget';
import { generatePaymentId } from '../../util/opReturn';
import { isGreaterThanZero, resolveNumber } from '../../util/number';
import { isCrypto, isFiat, isValidCurrency } from '../../util/currency';

export interface WidgetContainerProps
extends Omit<WidgetProps, 'success' | 'setNewTxs' | 'setCurrencyObject'> {
Expand Down
39 changes: 0 additions & 39 deletions react/lib/tests/util/constants.test.ts

This file was deleted.

18 changes: 0 additions & 18 deletions react/lib/util/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,6 @@ export const getCurrencyTypeFromAddress = (address: string): CryptoCurrency => {
}
};

export const isAddressSupported = (address: string): boolean => {
return xecaddr.isValidAddress(address);
};

export const compareAddresses = (
addressA: string,
addressB: string,
): boolean => {
const prefixes = ['bitcoincash', 'bchtest', 'bchreg', 'ecash', 'ectest'];
prefixes.forEach(prefix =>
[addressA, addressB].forEach(a =>
a.replace(prefix, '').toLocaleUpperCase(),
),
);

return addressA === addressB;
};

export default {
isValidCashAddress,
isValidXecAddress,
Expand Down
8 changes: 8 additions & 0 deletions react/lib/util/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ export default {
getXecFiatPrice,
getAddressBalance,
};

export const getCashtabProviderStatus = () => {
const windowAny = window as any
if (window && windowAny.bitcoinAbc && windowAny.bitcoinAbc === 'cashtab') {
return true;
}
return false;
};
11 changes: 1 addition & 10 deletions react/lib/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,4 @@ export * from './satoshis';
export * from './socket';
export * from './types';
export * from './number';
export * from './currency';


export const getCashtabProviderStatus = () => {
const windowAny = window as any;
if (window && windowAny.bitcoinAbc && windowAny.bitcoinAbc === 'cashtab') {
return true;
}
return false;
};
export * from './currency';
2 changes: 1 addition & 1 deletion react/lib/util/opReturn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ export const exportedForTesting = {
generatePushdataPrefixedPaymentId,
stringToHex,
getDataPushdata,
};
};
59 changes: 26 additions & 33 deletions react/lib/util/randomizeSats.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,53 @@
import { CryptoCurrency } from "./types";

const DEFAULT = 3;
const MAX = 4;
const DEFAULT = 3
const MAX = 4

export const getNSatoshis = (
amount: number,
randomSatoshis: boolean | number,
satsPrecision: number,
): number => {
const amountDigits = amount
.toFixed(satsPrecision)
.toString()
.replace('.', '').length;

export const getNSatoshis = (amount: number, randomSatoshis: boolean | number, satsPrecision: number): number => {
const amountDigits = amount.toFixed(satsPrecision).toString().replace('.', '').length;
if (randomSatoshis === true) {
return Math.min(DEFAULT, amountDigits);
return Math.min(DEFAULT, amountDigits)
} else if (randomSatoshis === false) {
throw new Error('Trying to randomize satoshis when not allowed.');
throw new Error("Trying to randomize satoshis when not allowed.")
}
if (randomSatoshis > MAX) {
randomSatoshis = MAX;
randomSatoshis = MAX
}
return Math.min(randomSatoshis, amountDigits);
};
return Math.min(randomSatoshis, amountDigits)
}

export const randomizeSatoshis = (amount: number, addressType: CryptoCurrency, randomSatoshis: boolean | number): number => {
if (amount === 0) {
return 0;
}
let nSatoshis: number;
let random: number;
let randomizedAmount: number;
let ret: number;
let nSatoshis: number
let random: number
let randomizedAmount: number
let ret: number
switch (addressType) {
case 'BCH':
nSatoshis = getNSatoshis(amount, randomSatoshis, 8);
random = Math.floor(Math.random() * 10 ** nSatoshis) * 1e-8;
nSatoshis = getNSatoshis(amount, randomSatoshis, 8)
random = Math.floor(Math.random() * 10 ** nSatoshis) * 1e-8

randomizedAmount =
Math.max(0, +amount.toFixed(nSatoshis)) + // zero out the least-significant digits
random;
random
ret = +randomizedAmount.toFixed(8);
break;
break
case 'XEC':
nSatoshis = getNSatoshis(amount, randomSatoshis, 2);
random = Math.floor(Math.random() * 10 ** nSatoshis) * 1e-2;
nSatoshis = getNSatoshis(amount, randomSatoshis, 2)
random = Math.floor(Math.random() * 10 ** nSatoshis) * 1e-2

const multiplier = 10 ** (nSatoshis - 2);
randomizedAmount =
Math.max(0, +(Math.floor(amount / multiplier) * multiplier)) + // zero out the least-significant digits
random;
const multiplier = 10 ** (nSatoshis - 2)
randomizedAmount = Math.max(0, +(Math.floor(amount / multiplier) * multiplier)) + // zero out the least-significant digits
random
ret = +randomizedAmount.toFixed(2);
break;
break
default:
throw new Error(`Invalid currency: ${addressType}`);
throw new Error(`Invalid currency: ${addressType}`)
}
return ret;
return ret
};

export default randomizeSatoshis;
7 changes: 4 additions & 3 deletions react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
},
"scripts": {
"build": "tsc --p ./tsconfig-build.json && microbundle --jsx React.createElement --format modern,cjs",
"lint": "eslint 'lib/**/*.{js,jsx,ts,tsx,json,css,scss,md}' --fix",
"prettier": "prettier --write 'lib/**/*.{js,jsx,ts,tsx,json,css,scss,md}'",
"lint": "eslint 'lib/**/*.{ts,tsx}' --fix",
"prettier": "prettier --write 'lib/**/*.ts'",
"dev": "concurrently yarn:watch yarn:storybook",
"storybook": "start-storybook -p 6006",
"watch": "microbundle watch --jsx React.createElement --no-compress --format modern,cjs",
"test": "jest"
"test": "jest",
"test:coverage": "jest --coverage"
},
"peerDependencies": {
"react": "17.0.0",
Expand Down

0 comments on commit 21d2808

Please sign in to comment.