From 37576df48f0239503559a574f25020c07e53732b Mon Sep 17 00:00:00 2001 From: kkatusic Date: Wed, 18 Dec 2024 00:56:03 +0100 Subject: [PATCH] fixing adding anchor contract --- .../create/AlloProtocol/AlloProtocolModal.tsx | 112 +++++++++--------- .../AlloProtocolFirstDonationModal.tsx | 110 ++++++++--------- .../ModifySuperToken/WithDrawSuperToken.tsx | 9 +- .../Recurring/RecurringDonationCard.tsx | 4 +- .../RecurringDonationModal.tsx | 1 + .../projectActionCard/AdminActions.tsx | 1 + .../ModifyStreamInnerModal.tsx | 3 +- .../UpdateStreamInnerModal.tsx | 3 +- .../projectsTab/ClaimWithdrawalModal.tsx | 32 +++-- .../projectsTab/ProjectActions.tsx | 1 + src/helpers/superfluid.ts | 7 +- 11 files changed, 157 insertions(+), 126 deletions(-) diff --git a/src/components/views/create/AlloProtocol/AlloProtocolModal.tsx b/src/components/views/create/AlloProtocol/AlloProtocolModal.tsx index ffd95248f0..532c2e14c7 100644 --- a/src/components/views/create/AlloProtocol/AlloProtocolModal.tsx +++ b/src/components/views/create/AlloProtocol/AlloProtocolModal.tsx @@ -122,64 +122,70 @@ const AlloProtocolModal: FC = ({ ? chain.id === config.OPTIMISM_NETWORK_NUMBER : false; + const isOnBase = chain ? chain.id === config.BASE_NETWORK_NUMBER : false; + const handleButtonClick = async () => { - if (!isOnOptimism) { - switchChain?.({ chainId: config.OPTIMISM_NETWORK_NUMBER }); - } else { - try { - setIsLoading(true); - const hash = await writeContract(wagmiConfig, { - address: config.OPTIMISM_CONFIG.anchorRegistryAddress, - functionName: 'createProfile', - abi: createProfileABI.abi, - chainId: config.OPTIMISM_NETWORK_NUMBER, - args: [ - generateRandomNonce(), //nonce - addedProjectState?.id!, - { - protocol: 1, - pointer: '', - }, - addedProjectState?.adminUser?.walletAddress, //admin user wallet address - [], - ], + try { + setIsLoading(true); + const hash = await writeContract(wagmiConfig, { + address: isOnOptimism + ? config.OPTIMISM_CONFIG.anchorRegistryAddress + : config.BASE_CONFIG.anchorRegistryAddress, + functionName: 'createProfile', + abi: createProfileABI.abi, + chainId: isOnOptimism + ? config.OPTIMISM_NETWORK_NUMBER + : config.BASE_NETWORK_NUMBER, + args: [ + generateRandomNonce(), //nonce + addedProjectState?.id!, + { + protocol: 1, + pointer: '', + }, + addedProjectState?.adminUser?.walletAddress, //admin user wallet address + [], + ], + }); + setTxResult(hash); + if (hash) { + const data = await waitForTransactionReceipt(wagmiConfig, { + hash: hash, + chainId: isOnOptimism + ? config.OPTIMISM_NETWORK_NUMBER + : config.BASE_NETWORK_NUMBER, }); - setTxResult(hash); - if (hash) { - const data = await waitForTransactionReceipt(wagmiConfig, { - hash: hash, - chainId: config.OPTIMISM_NETWORK_NUMBER, - }); - - const contractAddress = extractContractAddressFromString( - data.logs[0].data, + + const contractAddress = extractContractAddressFromString( + data.logs[0].data, + ); + //Call backend to update project + await client.mutate({ + mutation: CREATE_ANCHOR_CONTRACT_ADDRESS_QUERY, + variables: { + projectId: Number(addedProjectState.id), + networkId: isOnOptimism + ? config.OPTIMISM_NETWORK_NUMBER + : config.BASE_NETWORK_NUMBER, + address: contractAddress, + txHash: hash, + }, + }); + if (!isEditMode || (isEditMode && isDraft)) { + await router.push( + slugToSuccessView(addedProjectState.slug), + ); + } else { + await router.push( + slugToProjectView(addedProjectState.slug), ); - //Call backend to update project - await client.mutate({ - mutation: CREATE_ANCHOR_CONTRACT_ADDRESS_QUERY, - variables: { - projectId: Number(addedProjectState.id), - networkId: config.OPTIMISM_NETWORK_NUMBER, - address: contractAddress, - txHash: hash, - }, - }); - if (!isEditMode || (isEditMode && isDraft)) { - await router.push( - slugToSuccessView(addedProjectState.slug), - ); - } else { - await router.push( - slugToProjectView(addedProjectState.slug), - ); - } } - setShowModal(false); // Close the modal - } catch (error) { - console.error('Error Contract', error); - } finally { - setIsLoading(false); } + setShowModal(false); // Close the modal + } catch (error) { + console.error('Error Contract', error); + } finally { + setIsLoading(false); } }; diff --git a/src/components/views/donate/Recurring/AlloProtocolFirstDonationModal.tsx b/src/components/views/donate/Recurring/AlloProtocolFirstDonationModal.tsx index f0322bc7d7..70c35f8f1e 100644 --- a/src/components/views/donate/Recurring/AlloProtocolFirstDonationModal.tsx +++ b/src/components/views/donate/Recurring/AlloProtocolFirstDonationModal.tsx @@ -64,62 +64,66 @@ const AlloProtocolFirstDonationModal: FC = ({ : false; const handleButtonClick = async () => { - if (!isOnOptimism) { - switchChain?.({ chainId: config.OPTIMISM_NETWORK_NUMBER }); - } else { - try { - setIsLoading(true); - if ( - !project?.adminUser?.walletAddress || - !isAddress(project?.adminUser?.walletAddress) - ) { - throw new Error('Invalid Project Admin Address'); - } - const hash = await writeContract(wagmiConfig, { - address: config.OPTIMISM_CONFIG.anchorRegistryAddress, - functionName: 'createProfile', - abi: createProfileABI.abi, - chainId: config.OPTIMISM_NETWORK_NUMBER, - args: [ - generateRandomNonce(), //nonce - project?.id!, - { - protocol: 1, - pointer: '', - }, - project?.adminUser?.walletAddress, //admin user wallet address - [], - ], + try { + setIsLoading(true); + if ( + !project?.adminUser?.walletAddress || + !isAddress(project?.adminUser?.walletAddress) + ) { + throw new Error('Invalid Project Admin Address'); + } + const hash = await writeContract(wagmiConfig, { + address: isOnOptimism + ? config.OPTIMISM_CONFIG.anchorRegistryAddress + : config.BASE_CONFIG.anchorRegistryAddress, + functionName: 'createProfile', + abi: createProfileABI.abi, + chainId: isOnOptimism + ? config.OPTIMISM_NETWORK_NUMBER + : config.BASE_NETWORK_NUMBER, + args: [ + generateRandomNonce(), //nonce + project?.id!, + { + protocol: 1, + pointer: '', + }, + project?.adminUser?.walletAddress, //admin user wallet address + [], + ], + }); + setTxResult(hash); + if (hash) { + const data = await waitForTransactionReceipt(wagmiConfig, { + hash: hash, + chainId: isOnOptimism + ? config.OPTIMISM_NETWORK_NUMBER + : config.BASE_NETWORK_NUMBER, }); - setTxResult(hash); - if (hash) { - const data = await waitForTransactionReceipt(wagmiConfig, { - hash: hash, - chainId: config.OPTIMISM_NETWORK_NUMBER, - }); - const contractAddress = extractContractAddressFromString( - data.logs[0].data, - ); - //Call backend to update project - await client.mutate({ - mutation: CREATE_ANCHOR_CONTRACT_ADDRESS_QUERY, - variables: { - projectId: Number(project.id), - networkId: config.OPTIMISM_NETWORK_NUMBER, - address: contractAddress, - txHash: hash, - }, - }); - await fetchProject(); - onModalCompletion(); - } - setShowModal(false); // Close the modal - } catch (error) { - showToastError(error); - } finally { - setIsLoading(false); + const contractAddress = extractContractAddressFromString( + data.logs[0].data, + ); + //Call backend to update project + await client.mutate({ + mutation: CREATE_ANCHOR_CONTRACT_ADDRESS_QUERY, + variables: { + projectId: Number(project.id), + networkId: isOnOptimism + ? config.OPTIMISM_NETWORK_NUMBER + : config.BASE_NETWORK_NUMBER, + address: contractAddress, + txHash: hash, + }, + }); + await fetchProject(); + onModalCompletion(); } + setShowModal(false); // Close the modal + } catch (error) { + showToastError(error); + } finally { + setIsLoading(false); } }; diff --git a/src/components/views/donate/Recurring/ModifySuperToken/WithDrawSuperToken.tsx b/src/components/views/donate/Recurring/ModifySuperToken/WithDrawSuperToken.tsx index f0f1e5cd9e..8c3d1501db 100644 --- a/src/components/views/donate/Recurring/ModifySuperToken/WithDrawSuperToken.tsx +++ b/src/components/views/donate/Recurring/ModifySuperToken/WithDrawSuperToken.tsx @@ -11,7 +11,7 @@ import { IModifySuperTokenInnerModalProps } from './ModifySuperTokenModal'; import { ISuperToken, IToken } from '@/types/superFluid'; import { actionButtonLabel, EModifySuperTokenSteps } from './common'; import { ModifyWrapper, Wrapper } from './common.sc'; -import config, { isProduction } from '@/configuration'; +import { isProduction } from '@/configuration'; import { showToastError } from '@/lib/helpers'; import { Item } from '../RecurringDonationModal/Item'; import { RunOutInfo } from '../RunOutInfo'; @@ -36,7 +36,8 @@ export const WithDrawSuperToken: FC = ({ closeModal, }) => { const [amount, setAmount] = useState(0n); - const { address } = useAccount(); + const { address, chain } = useAccount(); + const recurringNetworkID = chain?.id ?? 0; const { formatMessage } = useIntl(); const tokenPrice = useTokenPrice(token); const [isWarning, setIsWarning] = useState(false); @@ -60,7 +61,7 @@ export const WithDrawSuperToken: FC = ({ const onWithdraw = async () => { setStep(EModifySuperTokenSteps.WITHDRAWING); try { - await ensureCorrectNetwork(config.OPTIMISM_NETWORK_NUMBER); + await ensureCorrectNetwork(recurringNetworkID); if (!address) { throw new Error('address not found1'); } @@ -76,7 +77,7 @@ export const WithDrawSuperToken: FC = ({ throw new Error('Provider or signer not found'); const _options = { - chainId: config.OPTIMISM_CONFIG.id, + chainId: recurringNetworkID, provider: provider, resolverAddress: isProduction ? undefined diff --git a/src/components/views/donate/Recurring/RecurringDonationCard.tsx b/src/components/views/donate/Recurring/RecurringDonationCard.tsx index 5250b55dc0..0a29355b58 100644 --- a/src/components/views/donate/Recurring/RecurringDonationCard.tsx +++ b/src/components/views/donate/Recurring/RecurringDonationCard.tsx @@ -163,8 +163,8 @@ export const RecurringDonationCard = () => { tokenStreams[selectedRecurringToken?.token.id.toLowerCase() || '']; const anchorContractAddress = useMemo( - () => findAnchorContractAddress(project.anchorContracts), - [project.anchorContracts], + () => findAnchorContractAddress(project.anchorContracts, chain?.id), + [project.anchorContracts, chain?.id], ); // otherStreamsPerSec is the total flow rate of all streams except the one to the project diff --git a/src/components/views/donate/Recurring/RecurringDonationModal/RecurringDonationModal.tsx b/src/components/views/donate/Recurring/RecurringDonationModal/RecurringDonationModal.tsx index c764dd5052..c4bb4c05aa 100644 --- a/src/components/views/donate/Recurring/RecurringDonationModal/RecurringDonationModal.tsx +++ b/src/components/views/donate/Recurring/RecurringDonationModal/RecurringDonationModal.tsx @@ -190,6 +190,7 @@ const RecurringDonationInnerModal: FC = ({ setStep(EDonationSteps.DONATING); const projectAnchorContract = findAnchorContractAddress( project?.anchorContracts, + recurringNetworkID, ); if (!projectAnchorContract) { throw new Error('Project anchor address not found'); diff --git a/src/components/views/project/projectActionCard/AdminActions.tsx b/src/components/views/project/projectActionCard/AdminActions.tsx index 2460486d83..bafef0f83c 100644 --- a/src/components/views/project/projectActionCard/AdminActions.tsx +++ b/src/components/views/project/projectActionCard/AdminActions.tsx @@ -67,6 +67,7 @@ export const AdminActions = () => { const anchorContractAddress = findAnchorContractAddress( project.anchorContracts, + chain?.id, ); const options: IOption[] = [ diff --git a/src/components/views/userProfile/donationsTab/recurringTab/ModifyStreamModal/ModifyStreamInnerModal.tsx b/src/components/views/userProfile/donationsTab/recurringTab/ModifyStreamModal/ModifyStreamInnerModal.tsx index 0ee6388caa..c27bb2f01f 100644 --- a/src/components/views/userProfile/donationsTab/recurringTab/ModifyStreamModal/ModifyStreamInnerModal.tsx +++ b/src/components/views/userProfile/donationsTab/recurringTab/ModifyStreamModal/ModifyStreamInnerModal.tsx @@ -62,7 +62,7 @@ export const ModifyStreamInnerModal: FC = ({ otherStreamsTotalFlowRate: 0n, }); const { formatMessage } = useIntl(); - const { address } = useAccount(); + const { address, chain } = useAccount(); // Get the balance of the super token const { data: balance } = useBalance({ @@ -117,6 +117,7 @@ export const ModifyStreamInnerModal: FC = ({ }; const anchorContractAddress = findAnchorContractAddress( donation.project.anchorContracts, + chain?.id, ); for (let i = 0; i < tokenStream.length; i++) { const ts = tokenStream[i]; diff --git a/src/components/views/userProfile/donationsTab/recurringTab/ModifyStreamModal/UpdateStreamInnerModal.tsx b/src/components/views/userProfile/donationsTab/recurringTab/ModifyStreamModal/UpdateStreamInnerModal.tsx index 0305bcde03..783c444c3c 100644 --- a/src/components/views/userProfile/donationsTab/recurringTab/ModifyStreamModal/UpdateStreamInnerModal.tsx +++ b/src/components/views/userProfile/donationsTab/recurringTab/ModifyStreamModal/UpdateStreamInnerModal.tsx @@ -51,7 +51,7 @@ export const UpdateStreamInnerModal: FC = ({ const [tx, setTx] = useState(''); const { formatMessage } = useIntl(); const tokenPrice = useTokenPrice(token); - const { address } = useAccount(); + const { address, chain } = useAccount(); const { refetchTokenStream } = useProfileDonateTabData(); const onDonate = async () => { @@ -60,6 +60,7 @@ export const UpdateStreamInnerModal: FC = ({ await ensureCorrectNetwork(config.OPTIMISM_NETWORK_NUMBER); const projectAnchorContract = findAnchorContractAddress( donation.project.anchorContracts, + chain?.id, ); if (!projectAnchorContract) { throw new Error('Project anchor address not found'); diff --git a/src/components/views/userProfile/projectsTab/ClaimWithdrawalModal.tsx b/src/components/views/userProfile/projectsTab/ClaimWithdrawalModal.tsx index a28cd8695d..d8de2ce4eb 100644 --- a/src/components/views/userProfile/projectsTab/ClaimWithdrawalModal.tsx +++ b/src/components/views/userProfile/projectsTab/ClaimWithdrawalModal.tsx @@ -10,6 +10,7 @@ import { Address, encodeFunctionData } from 'viem'; import { useState } from 'react'; import { useIntl } from 'react-intl'; import { writeContract, waitForTransactionReceipt } from '@wagmi/core'; +import { useAccount } from 'wagmi'; import { Modal } from '@/components/modals/Modal'; import { IModal } from '@/types/common'; import { useModalAnimation } from '@/hooks/useModalAnimation'; @@ -75,18 +76,29 @@ const ClaimWithdrawalModal = ({ useState(ClaimTransactionState.NOT_STARTED); const [txHash, setTxHash] = useState
(); const { formatMessage } = useIntl(); + const { chain } = useAccount(); + const recurringNetworkID = chain?.id ?? 0; const projectName = project.title || ''; - const optimismAddress = project.addresses?.find( - address => address.networkId === config.OPTIMISM_NETWORK_NUMBER, - )?.address; + let projectAddress = ''; + if (recurringNetworkID === config.OPTIMISM_NETWORK_NUMBER) { + projectAddress = + project.addresses?.find( + address => address.networkId === config.OPTIMISM_NETWORK_NUMBER, + )?.address || ''; + } else if (recurringNetworkID === config.BASE_NETWORK_NUMBER) { + projectAddress = + project.addresses?.find( + address => address.networkId === config.BASE_NETWORK_NUMBER, + )?.address || ''; + } const isETHx = selectedStream.token.symbol.toLowerCase() === 'ethx'; const handleConfirm = async () => { console.log('anchorContractAddress', anchorContractAddress); try { console.log('isETHx', isETHx); - await ensureCorrectNetwork(config.OPTIMISM_NETWORK_NUMBER); + await ensureCorrectNetwork(recurringNetworkID); const encodedDowngradeTo = isETHx ? encodeFunctionData({ abi: ISETH.abi, @@ -97,7 +109,7 @@ const ClaimWithdrawalModal = ({ abi: superTokenABI.abi, functionName: 'downgradeTo', args: [ - optimismAddress, + projectAddress, +selectedStream.balance.toString(), ], }); @@ -114,7 +126,7 @@ const ClaimWithdrawalModal = ({ abi: anchorContractABI.abi, address: anchorContractAddress, functionName: 'execute', - chainId: config.OPTIMISM_NETWORK_NUMBER, + chainId: recurringNetworkID, args: [selectedStream.token.id, '', encodedDowngradeTo], }); @@ -124,7 +136,7 @@ const ClaimWithdrawalModal = ({ wagmiConfig, { hash: tx, - chainId: config.OPTIMISM_NETWORK_NUMBER, + chainId: recurringNetworkID, }, ); @@ -141,9 +153,9 @@ const ClaimWithdrawalModal = ({ abi: anchorContractABI.abi, address: anchorContractAddress, functionName: 'execute', - chainId: config.OPTIMISM_NETWORK_NUMBER, + chainId: recurringNetworkID, args: [ - optimismAddress, + projectAddress, +selectedStream.balance.toString(), '', ], @@ -152,7 +164,7 @@ const ClaimWithdrawalModal = ({ if (transferEthTx) { await waitForTransactionReceipt(wagmiConfig, { hash: transferEthTx, - chainId: config.OPTIMISM_NETWORK_NUMBER, + chainId: recurringNetworkID, }); } setTransactionState(ClaimTransactionState.SUCCESS); diff --git a/src/components/views/userProfile/projectsTab/ProjectActions.tsx b/src/components/views/userProfile/projectsTab/ProjectActions.tsx index 10b897446b..176619f8d0 100644 --- a/src/components/views/userProfile/projectsTab/ProjectActions.tsx +++ b/src/components/views/userProfile/projectsTab/ProjectActions.tsx @@ -62,6 +62,7 @@ const ProjectActions: FC = ({ const anchorContractAddress = findAnchorContractAddress( project.anchorContracts, + chain?.id, ); const chainId = chain?.id; diff --git a/src/helpers/superfluid.ts b/src/helpers/superfluid.ts index 0c1dc054f7..83b80f23f1 100644 --- a/src/helpers/superfluid.ts +++ b/src/helpers/superfluid.ts @@ -14,7 +14,10 @@ export const findTokenByAddress = (address?: Address) => { export const findAnchorContractAddress = ( anchorContracts?: IAnchorContractData[], + chainId?: number, ) => { - if (!anchorContracts) return undefined; - return anchorContracts.find(contract => contract.isActive)?.address; + if (!anchorContracts || !chainId) return undefined; + return anchorContracts.find( + contract => contract.isActive && contract.networkId == chainId, + )?.address; };