Skip to content

Commit

Permalink
refactor: add types util
Browse files Browse the repository at this point in the history
  • Loading branch information
lissavxo committed May 17, 2024
1 parent d0fee82 commit b4c6f29
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 147 deletions.
5 changes: 3 additions & 2 deletions react/lib/components/PayButton/PayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import React, { useState, useEffect, useCallback, useRef } from 'react';

import { Theme, ThemeName, ThemeProvider, useTheme } from '../../themes';
import Button, { ButtonProps } from '../Button/Button';
import { Transaction, currency, isFiat, getFiatPrice } from '../../util/api-client';
import { isFiat, getFiatPrice } from '../../util/api-client';
import { PaymentDialog } from '../PaymentDialog/PaymentDialog';
import { getCurrencyTypeFromAddress, isValidCashAddress, isValidXecAddress } from '../../util/address';
import { currencyObject, getCurrencyObject } from '../../util/satoshis';
import { getCurrencyObject } from '../../util/satoshis';
import { generatePaymentId } from '../../util/opReturn';
import { currency, Transaction, currencyObject } from '../../util/types';

export interface PayButtonProps extends ButtonProps {
to: 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 @@ -5,8 +5,7 @@ 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 { Transaction, currency } from '../../util/api-client';
import { currencyObject } from '../../util/satoshis';
import { currency, currencyObject, Transaction } from '../../util/types';

export interface PaymentDialogProps extends ButtonProps {
to: string;
Expand Down
14 changes: 3 additions & 11 deletions react/lib/components/Widget/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,12 @@ import { formatPrice, DECIMALS } from '../../util/format';
import { Button, animation } from '../Button/Button';
import BarChart from '../BarChart/BarChart';

import { getCurrencyObject, currencyObject } from '../../util/satoshis';
import {
currency,
getAddressBalance,
getAddressDetails,
isFiat,
setListener,
Transaction,
getCashtabProviderStatus,
cryptoCurrency,
} from '../../util/api-client';
import PencilIcon from '../../assets/edit-pencil';
import io, { Socket } from 'socket.io-client';
import { encodeOpReturnProps } from '../../util/opReturn';
import { getAddressDetails, setListener, getAddressBalance, isFiat, getCashtabProviderStatus } from '../../util/api-client';
import { getCurrencyObject } from '../../util/satoshis';
import { currency, currencyObject, Transaction, cryptoCurrency } from '../../util/types';

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

Expand Down
4 changes: 1 addition & 3 deletions react/lib/components/Widget/WidgetContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import {
getCurrencyTypeFromAddress,
} from '../../util/address';
import {
Transaction,
isCrypto,
currency,
isValidCurrency,
isFiat,
getFiatPrice,
} from '../../util/api-client';
import { currencyObject } from '../../util/satoshis';
import Widget, { WidgetProps } from './Widget';
import BigNumber from 'bignumber.js';
import { generatePaymentId } from '../../util/opReturn';
import { currency, currencyObject, Transaction } from '../../util/types';

export interface WidgetContainerProps
extends Omit<WidgetProps, 'success' | 'setNewTxs' | 'setCurrencyObject'> {
Expand Down
2 changes: 1 addition & 1 deletion react/lib/util/address.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as xecaddr from 'xecaddrjs';
import { cryptoCurrency } from '../util/api-client';
import { cryptoCurrency } from './types';

export const isValidCashAddress = (address: string): boolean => {
if (!address) return false;
Expand Down
121 changes: 1 addition & 120 deletions react/lib/util/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import _ from 'lodash';
import { Socket } from 'socket.io-client'
import config from '../config.json'
import { isValidCashAddress, isValidXecAddress } from './address';
import { Transaction, Output, TokenMetaData, TransactionDetails, cryptoCurrency, fiatCurrency, currency } from './types';

export const getAddressDetails = async (
address: string,
Expand Down Expand Up @@ -108,15 +109,8 @@ export default {
};

export const fiatCurrencies = ['USD', 'CAD'] as const;

type fiatCurrenciesTuple = typeof fiatCurrencies; // readonly ['USD', 'CAD' ]
export type fiatCurrency = fiatCurrenciesTuple[number]; // "USD" | "CAD"

export const cryptoCurrencies = ['BCH', 'XEC'] as const;
type cryptoCurrenciesTuple = typeof cryptoCurrencies; // readonly ['BCH', 'XEC']
export type cryptoCurrency = cryptoCurrenciesTuple[number]; // "BCH" | "XEC"

export type currency = cryptoCurrency | fiatCurrency;

export function isFiat(unknownString: string): unknownString is fiatCurrency {
return fiatCurrencies.includes(unknownString as fiatCurrency);
Expand All @@ -142,124 +136,11 @@ export const getCashtabProviderStatus = () => {
return false;
};


export interface Transaction {
hash: string
amount: string
paymentId: string
confirmed?: boolean
message: string
timestamp: number
address: string
}

export interface UtxoDetails {
outputsList: [Output];
tokenMetadataList: [TokenMetaData];
}

export interface Output {
outpoint: {
hash: string;
index: number;
};
pubkeyScript: string;
value: number;
isCoinbase: boolean;
blockHeight: number;
}

export interface TokenMetaData {
tokenId: string;
tokenType: number;
v1Fungible: {
tokenTicker: string;
tokenName: string;
tokenDocumentUrl: string;
tokenDocumentHash: string;
decimals: number;
mintBatonHash: string;
mintBatonVout: number;
};
}

export interface PriceData {
price: number;
}

export interface TransactionDetails {
transaction: {
hash: string;
version: number;
inputsList: {
index: number;
outpoint: {
hash: string;
index: number;
};
signatureScript: string;
sequence: number;
value: number;
previousScript: string;
address: string;
slpToken: {
tokenId: string;
amount: string;
isMintBaton: boolean;
address: string;
decimals: number;
slpAction: number;
tokenType: number;
};
}[];
outputsList: {
index: number;
value: number;
pubkeyScript: string;
address: string;
scriptClass: string;
disassembledScript: string;
slpToken?: {
tokenId: string;
amount: string;
isMintBaton: boolean;
address: string;
decimals: number;
slpAction: number;
tokenType: number;
};
}[];
lockTime: number;
size: number;
timestamp: number;
confirmations: number;
blockHeight: number;
blockHash: string;
slpTransactionInfo: {
slpAction: number;
validityJudgement: number;
parseError: string;
tokenId: string;
burnFlagsList: [number];
v1Nft1ChildGenesis: {
name: string;
ticker: string;
documentUrl: string;
documentHash: string;
decimals: number;
groupTokenId: string;
};
};
};
tokenMetadata: {
tokenId: string;
tokenType: number;
v1Nft1Child: {
tokenTicker: string;
tokenName: string;
tokenDocumentUrl: string;
tokenDocumentHash: string;
groupId: string;
};
};
}
2 changes: 1 addition & 1 deletion react/lib/util/format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import currencyFormat from 'currency-formatter';
import { currency } from './api-client';
import { currency } from './types';

export const DECIMALS = {
BCH: 8,
Expand Down
2 changes: 1 addition & 1 deletion react/lib/util/randomizeSats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cryptoCurrency } from './api-client';
import { cryptoCurrency } from "./types";

const DEFAULT = 3
const MAX = 4
Expand Down
8 changes: 2 additions & 6 deletions react/lib/util/satoshis.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import BigNumber from 'bignumber.js';
import { formatPrice, formatBCH, formatXEC, DECIMALS } from './format';

import { currency, isCrypto } from './api-client';
import { isCrypto } from './api-client';
import { randomizeSatoshis } from './randomizeSats';
import { currency, currencyObject } from './types';

export type currencyObject = {
float: number;
string: string;
currency: string;
};

export const getCurrencyObject = (
amount: number,
Expand Down
Loading

0 comments on commit b4c6f29

Please sign in to comment.