Skip to content

Commit

Permalink
feat: allow registering and invoking listeners for diamond provider
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor committed Oct 9, 2022
1 parent 3a04e03 commit 49a985b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/common/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './env';
export * from './transactions';
9 changes: 9 additions & 0 deletions packages/common/src/types/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TransactionReceipt } from '@ethersproject/providers';
import { SendTransactionResult } from '@wagmi/core';

export type TransactionData = {
receipt?: TransactionReceipt;
response?: SendTransactionResult;
};

export type TransactionListener = (data: TransactionData) => void;
49 changes: 44 additions & 5 deletions packages/react/src/core/diamonds/providers/DiamondProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Environment } from '@flair-sdk/common';
import {
Environment,
TransactionData,
TransactionListener,
} from '@flair-sdk/common';
import {
ContractCall,
EIP165InterfaceID,
Expand All @@ -23,6 +27,7 @@ type DiamondContextValue = {

configValues?: Record<string, any>;
proposedCalls?: ContractCall[];
proposedCallsData?: TransactionData;
};

isLoading: {
Expand All @@ -39,10 +44,17 @@ type DiamondContextValue = {

setConfigValues: React.Dispatch<React.SetStateAction<Record<string, any>>>;
setProposedCalls: React.Dispatch<React.SetStateAction<ContractCall[]>>;
setProcessingCalls: React.Dispatch<React.SetStateAction<boolean>>;
setProcessingError: React.Dispatch<
setProposedCallsLoading: React.Dispatch<React.SetStateAction<boolean>>;
setProposedCallsError: React.Dispatch<
React.SetStateAction<Error | string | undefined>
>;
setProposedCallsData: React.Dispatch<
React.SetStateAction<TransactionData | undefined>
>;

registerListener: (id: string, listener: TransactionListener) => void;
invokeListeners: (data: TransactionData) => void;

proposeCall: (call: ContractCall) => void;
refresh: () => void;
};
Expand Down Expand Up @@ -70,9 +82,13 @@ export const DiamondProvider = ({
diamondId,
children,
}: Props) => {
const [listeners, setListeners] = useState<
Record<string, TransactionListener>
>({});
const [configValues, setConfigValues] = useState<Record<string, any>>();
const [contractFacets, setContractFacets] = useState<FacetManifest[]>();
const [proposedCalls, setProposedCalls] = useState<ContractCall[]>([]);
const [proposedCallsData, setProposedCallsData] = useState<TransactionData>();
const [proposedCallsLoading, setProposedCallsLoading] =
useState<boolean>(false);
const [proposedCallsError, setProposedCallsError] = useState<
Expand Down Expand Up @@ -161,6 +177,22 @@ export const DiamondProvider = ({
refreshDiamond().then(() => refreshSmartContract());
}, [refreshDiamond, refreshSmartContract]);

const registerListener = useCallback(
(id: string, listener: TransactionListener) => {
setListeners((listeners) => ({ ...listeners, [id]: listener }));
},
[],
);

const invokeListeners = useCallback(
(data: TransactionData) => {
Object.values(listeners).forEach((listener) => {
listener(data);
});
},
[listeners],
);

const value = {
data: {
env,
Expand All @@ -172,6 +204,7 @@ export const DiamondProvider = ({
contractFacets,
contractInterfaces,
proposedCalls,
proposedCallsData,
},

isLoading: { proposedCallsLoading, diamondLoading, smartContractLoading },
Expand All @@ -180,8 +213,14 @@ export const DiamondProvider = ({

setConfigValues,
setProposedCalls,
setProcessingCalls: setProposedCallsLoading,
setProcessingError: setProposedCallsError,

setProposedCallsLoading,
setProposedCallsError,
setProposedCallsData,

registerListener,
invokeListeners,

proposeCall,
refresh,
};
Expand Down

0 comments on commit 49a985b

Please sign in to comment.