From 95468bfcee68759560d5ea46b399d7c1756c3eaf Mon Sep 17 00:00:00 2001 From: Ross Bulat Date: Wed, 29 May 2024 10:35:53 +0700 Subject: [PATCH] feat(refactor): Tidy up `TxMeta` context (#2130) --- src/contexts/TxMeta/defaults.ts | 16 +++--- src/contexts/TxMeta/index.tsx | 70 +++++++++++++------------- src/contexts/TxMeta/types.ts | 22 ++++---- src/hooks/useSubmitExtrinsic/index.tsx | 4 +- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/contexts/TxMeta/defaults.ts b/src/contexts/TxMeta/defaults.ts index b28d55c730..e67951f8c1 100644 --- a/src/contexts/TxMeta/defaults.ts +++ b/src/contexts/TxMeta/defaults.ts @@ -6,22 +6,22 @@ import BigNumber from 'bignumber.js'; import type { TxMetaContextInterface } from './types'; export const defaultTxMeta: TxMetaContextInterface = { - controllerSignerAvailable: (a, b) => 'ok', - txFees: new BigNumber(0), - notEnoughFunds: false, - setTxFees: (f) => {}, - resetTxFees: () => {}, sender: null, setSender: (s) => {}, + txFees: new BigNumber(0), txFeesValid: false, - incrementPayloadUid: () => 0, + setTxFees: (f) => {}, + resetTxFees: () => {}, + notEnoughFunds: false, getPayloadUid: () => 0, getTxPayload: () => {}, setTxPayload: (p, u) => {}, + incrementPayloadUid: () => 0, + resetTxPayload: () => {}, getTxSignature: () => null, - resetTxPayloads: () => {}, setTxSignature: (s) => {}, + pendingNonces: [], addPendingNonce: (nonce) => {}, removePendingNonce: (nonce) => {}, - pendingNonces: [], + controllerSignerAvailable: (a, b) => 'ok', }; diff --git a/src/contexts/TxMeta/index.tsx b/src/contexts/TxMeta/index.tsx index c5491a2cfe..bb52ad3744 100644 --- a/src/contexts/TxMeta/index.tsx +++ b/src/contexts/TxMeta/index.tsx @@ -49,10 +49,18 @@ export const TxMetaProvider = ({ children }: { children: ReactNode }) => { uid: number; } | null>(null); const txPayloadRef = useRef(txPayload); + const getPayloadUid = () => txPayloadRef.current?.uid || 1; + const getTxPayload = () => txPayloadRef.current?.payload || null; // Store an optional signed transaction if extrinsics require manual signing (e.g. Ledger). const [txSignature, setTxSignatureState] = useState(null); const txSignatureRef = useRef(txSignature); + const getTxSignature = () => txSignatureRef.current; + + // Set the transaction signature. Overwrites any existing signature. + const setTxSignature = (s: AnyJson) => { + setStateWithRef(s, setTxSignatureState, txSignatureRef); + }; // Store the pending nonces of transactions. NOTE: Ref is required as `pendingNonces` is read in // callbacks. @@ -64,18 +72,7 @@ export const TxMetaProvider = ({ children }: { children: ReactNode }) => { accounts: [sender], }); - const senderBalances = getBalance(sender); - - const resetTxFees = () => { - setTxFees(new BigNumber(0)); - }; - - const getPayloadUid = () => txPayloadRef.current?.uid || 1; - - const incrementPayloadUid = () => (txPayloadRef.current?.uid || 0) + 1; - - const getTxPayload = () => txPayloadRef.current?.payload || null; - + // Set the transaction payload and uid. Overwrites any existing payload. const setTxPayload = (p: AnyJson, uid: number) => { setStateWithRef( { @@ -87,23 +84,12 @@ export const TxMetaProvider = ({ children }: { children: ReactNode }) => { ); }; - const resetTxPayloads = () => { + // Removes the transaction payload and uid from state. + const resetTxPayload = () => { setStateWithRef(null, setTxPayloadState, txPayloadRef); }; - const getTxSignature = () => txSignatureRef.current; - - const setTxSignature = (s: AnyJson) => { - setStateWithRef(s, setTxSignatureState, txSignatureRef); - }; - - const txFeesValid = (() => { - if (txFees.isZero() || notEnoughFunds) { - return false; - } - return true; - })(); - + // TODO: Remove controller checks once controller deprecation is completed on chain. const controllerSignerAvailable = ( stash: MaybeAddress, proxySupported: boolean @@ -127,6 +113,7 @@ export const TxMetaProvider = ({ children }: { children: ReactNode }) => { return 'ok'; }; + // Adds a pending nonce to the list of pending nonces. const addPendingNonce = (nonce: string) => { setStateWithRef( [...pendingNoncesRef.current].concat(nonce), @@ -135,6 +122,7 @@ export const TxMetaProvider = ({ children }: { children: ReactNode }) => { ); }; + // Removes a pending nonce from the list of pending nonces. const removePendingNonce = (nonce: string) => { setStateWithRef( pendingNoncesRef.current.filter((n) => n !== nonce), @@ -143,7 +131,19 @@ export const TxMetaProvider = ({ children }: { children: ReactNode }) => { ); }; - // Refresh not enough fee status when txfees or sender changes. + // Utility to reset transaction fees to zero. + const resetTxFees = () => { + setTxFees(new BigNumber(0)); + }; + + // Utility to increment payload uid to maintain unique ids for payloads. + const incrementPayloadUid = () => (txPayloadRef.current?.uid || 0) + 1; + + // Check if the transaction fees are valid. + const txFeesValid = txFees.isZero() || notEnoughFunds ? false : true; + + // Refresh not enough funds status when sender, balance or txFees change. + const senderBalances = getBalance(sender); useEffectIgnoreInitial(() => { const edReserved = getEdReserved(sender, existentialDeposit); const { free, frozen } = senderBalances; @@ -155,24 +155,24 @@ export const TxMetaProvider = ({ children }: { children: ReactNode }) => { return ( {children} diff --git a/src/contexts/TxMeta/types.ts b/src/contexts/TxMeta/types.ts index 5e8ae70697..b9dea49042 100644 --- a/src/contexts/TxMeta/types.ts +++ b/src/contexts/TxMeta/types.ts @@ -5,25 +5,25 @@ import type BigNumber from 'bignumber.js'; import type { AnyJson, MaybeAddress } from 'types'; export interface TxMetaContextInterface { - controllerSignerAvailable: ( - a: MaybeAddress, - b: boolean - ) => 'controller_not_imported' | 'read_only' | 'ok'; - txFees: BigNumber; - notEnoughFunds: boolean; - setTxFees: (f: BigNumber) => void; - resetTxFees: () => void; sender: MaybeAddress; setSender: (s: MaybeAddress) => void; + txFees: BigNumber; txFeesValid: boolean; - incrementPayloadUid: () => number; + setTxFees: (f: BigNumber) => void; + resetTxFees: () => void; + notEnoughFunds: boolean; getPayloadUid: () => number; getTxPayload: () => AnyJson; setTxPayload: (s: AnyJson, u: number) => void; - resetTxPayloads: () => void; + incrementPayloadUid: () => number; + resetTxPayload: () => void; getTxSignature: () => AnyJson; setTxSignature: (s: AnyJson) => void; + pendingNonces: string[]; addPendingNonce: (nonce: string) => void; removePendingNonce: (nonce: string) => void; - pendingNonces: string[]; + controllerSignerAvailable: ( + a: MaybeAddress, + b: boolean + ) => 'controller_not_imported' | 'read_only' | 'ok'; } diff --git a/src/hooks/useSubmitExtrinsic/index.tsx b/src/hooks/useSubmitExtrinsic/index.tsx index 7f67d67013..e87287f4e0 100644 --- a/src/hooks/useSubmitExtrinsic/index.tsx +++ b/src/hooks/useSubmitExtrinsic/index.tsx @@ -40,7 +40,7 @@ export const useSubmitExtrinsic = ({ getTxPayload, getTxSignature, setTxSignature, - resetTxPayloads, + resetTxPayload, incrementPayloadUid, } = useTxMeta(); @@ -193,7 +193,7 @@ export const useSubmitExtrinsic = ({ }; const resetTx = () => { - resetTxPayloads(); + resetTxPayload(); setTxSignature(null); setSubmitting(false); };