Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(refactor): Tidy up TxMeta context #2130

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/contexts/TxMeta/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
70 changes: 35 additions & 35 deletions src/contexts/TxMeta/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyJson>(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.
Expand All @@ -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(
{
Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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;
Expand All @@ -155,24 +155,24 @@ export const TxMetaProvider = ({ children }: { children: ReactNode }) => {
return (
<TxMetaContext.Provider
value={{
controllerSignerAvailable,
sender,
setSender,
txFees,
notEnoughFunds,
txFeesValid,
setTxFees,
resetTxFees,
txFeesValid,
sender,
setSender,
incrementPayloadUid,
notEnoughFunds,
getPayloadUid,
getTxPayload,
setTxPayload,
resetTxPayloads,
incrementPayloadUid,
resetTxPayload,
getTxSignature,
setTxSignature,
pendingNonces,
addPendingNonce,
removePendingNonce,
pendingNonces,
controllerSignerAvailable,
}}
>
{children}
Expand Down
22 changes: 11 additions & 11 deletions src/contexts/TxMeta/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
4 changes: 2 additions & 2 deletions src/hooks/useSubmitExtrinsic/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const useSubmitExtrinsic = ({
getTxPayload,
getTxSignature,
setTxSignature,
resetTxPayloads,
resetTxPayload,
incrementPayloadUid,
} = useTxMeta();

Expand Down Expand Up @@ -193,7 +193,7 @@ export const useSubmitExtrinsic = ({
};

const resetTx = () => {
resetTxPayloads();
resetTxPayload();
setTxSignature(null);
setSubmitting(false);
};
Expand Down
Loading