From 66523fd5bdd41bdb152a6fb0533854108863ae82 Mon Sep 17 00:00:00 2001 From: Johnny D Date: Wed, 20 Nov 2024 09:58:44 +1100 Subject: [PATCH] feat: read chainId from connectedWallet to launch v4 projects (#4506) --- next.config.js | 2 + src/constants/networks.ts | 18 +++++++++ src/hooks/Wallet/useWallet.ts | 8 ++-- src/models/networkName.ts | 2 + src/packages/v4/hooks/useLaunchProjectTx.ts | 20 +++++----- .../v4/hooks/useLaunchProjectWithNftsTx.ts | 38 ++++++++++--------- src/packages/v4/wagmiConfig.ts | 4 +- 7 files changed, 58 insertions(+), 34 deletions(-) diff --git a/next.config.js b/next.config.js index dc1943d8d1..b488ecfc17 100644 --- a/next.config.js +++ b/next.config.js @@ -72,6 +72,8 @@ const CONNECT_SRC = [ 'https://api.ensideas.com', 'https://cloudflare-eth.com', 'https://rpc.sepolia.org/', + 'https://sepolia-rollup.arbitrum.io/rpc', + 'https://sepolia.optimism.io' ] const FRAME_ANCESTORS = [ diff --git a/src/constants/networks.ts b/src/constants/networks.ts index f8a52a9322..1fdb6da8b3 100644 --- a/src/constants/networks.ts +++ b/src/constants/networks.ts @@ -39,6 +39,24 @@ export const NETWORKS: Record = { rpcUrl: `https://sepolia.infura.io/v3/${infuraId}`, token: 'SepETH', }, + 421614: { + name: NetworkName.arbitrumSepolia, + label: 'Arbitrum Sepolia Testnet', + color: '#96bedc', + chainId: 421614, + token: 'ArbETH', + rpcUrl: `https://sepolia-rollup.arbitrum.io/rpc`, + blockExplorer: 'https://sepolia-explorer.arbitrum.io', + }, + 11155420: { + name: NetworkName.optimismSepolia, + label: 'Optimism Sepolia Testnet', + color: '#f01f70', + chainId: 11155420, + token: 'OpETH', + rpcUrl: `https://sepolia.optimism.io`, + blockExplorer: 'https://optimism-sepolia.blockscout.com', + }, } export const NETWORKS_BY_NAME = Object.values(NETWORKS).reduce( diff --git a/src/hooks/Wallet/useWallet.ts b/src/hooks/Wallet/useWallet.ts index 08f5910a0b..3d01614a7e 100644 --- a/src/hooks/Wallet/useWallet.ts +++ b/src/hooks/Wallet/useWallet.ts @@ -1,21 +1,21 @@ -import { useConnectWallet } from '@web3-onboard/react' import { useChain, - useChainUnsupported, useChangeNetworks, useDisconnect, useIsConnected, useSigner, useUserAddress, - useWalletBalance, + useWalletBalance } from './hooks' +import { useConnectWallet } from '@web3-onboard/react' + export function useWallet() { const signer = useSigner() const userAddress = useUserAddress() const isConnected = useIsConnected() const chain = useChain() - const chainUnsupported = useChainUnsupported() + const chainUnsupported = false//useChainUnsupported() const balance = useWalletBalance() const [, connect] = useConnectWallet() diff --git a/src/models/networkName.ts b/src/models/networkName.ts index 9653ec39bb..d3e5e35266 100644 --- a/src/models/networkName.ts +++ b/src/models/networkName.ts @@ -2,4 +2,6 @@ export enum NetworkName { localhost = 'localhost', mainnet = 'mainnet', sepolia = 'sepolia', + arbitrumSepolia = 'arbitrumSepolia', + optimismSepolia = 'optimismSepolia', } diff --git a/src/packages/v4/hooks/useLaunchProjectTx.ts b/src/packages/v4/hooks/useLaunchProjectTx.ts index cf1d758f64..e2817e6740 100644 --- a/src/packages/v4/hooks/useLaunchProjectTx.ts +++ b/src/packages/v4/hooks/useLaunchProjectTx.ts @@ -14,9 +14,8 @@ import { NATIVE_TOKEN } from 'juice-sdk-core' import { useWriteJbControllerLaunchProjectFor } from 'juice-sdk-react' import { LaunchV2V3ProjectData } from 'packages/v2v3/hooks/transactor/useLaunchProjectTx' import { DEFAULT_MUST_START_AT_OR_AFTER } from 'redux/slices/editingV2Project' -import { sepolia } from 'viem/chains' +import { useChainId } from 'wagmi' import { wagmiConfig } from '../wagmiConfig' -import { useCurrentRouteChainId } from './useCurrentRouteChainId' const CREATE_EVENT_IDX = 2 const PROJECT_ID_TOPIC_IDX = 1 @@ -69,14 +68,15 @@ export function useLaunchProjectTx() { const { writeContractAsync: writeLaunchProject } = useWriteJbControllerLaunchProjectFor() - const chainId = useCurrentRouteChainId() ?? sepolia.id // default to sepolia - const terminalAddress = chainId - ? SUPPORTED_JB_MULTITERMINAL_ADDRESS[chainId] - : undefined - - const controllerAddress = chainId - ? SUPPORTED_JB_CONTROLLER_ADDRESS[chainId] - : undefined + const chainId = useChainId() + const chainIdStr = chainId?.toString() as keyof typeof SUPPORTED_JB_MULTITERMINAL_ADDRESS + const terminalAddress = chainId + ? SUPPORTED_JB_MULTITERMINAL_ADDRESS[chainIdStr] + : undefined + + const controllerAddress = chainId + ? SUPPORTED_JB_CONTROLLER_ADDRESS[chainIdStr] + : undefined const { addTransaction } = useContext(TxHistoryContext) diff --git a/src/packages/v4/hooks/useLaunchProjectWithNftsTx.ts b/src/packages/v4/hooks/useLaunchProjectWithNftsTx.ts index ae2f584691..bf24a7ccc1 100644 --- a/src/packages/v4/hooks/useLaunchProjectWithNftsTx.ts +++ b/src/packages/v4/hooks/useLaunchProjectWithNftsTx.ts @@ -1,7 +1,3 @@ -import { waitForTransactionReceipt } from '@wagmi/core' -import { JUICEBOX_MONEY_PROJECT_METADATA_DOMAIN } from 'constants/metadataDomain' -import { TxHistoryContext } from 'contexts/Transaction/TxHistoryContext' -import { useWallet } from 'hooks/Wallet' import { DEFAULT_MEMO, NATIVE_TOKEN, NATIVE_TOKEN_DECIMALS } from 'juice-sdk-core' import { jbPricesAddress, @@ -9,34 +5,38 @@ import { useReadJb721TiersHookStoreTiersOf, useWriteJb721TiersHookProjectDeployerLaunchProjectFor, } from 'juice-sdk-react' -import { isValidMustStartAtOrAfter } from 'packages/v2v3/utils/fundingCycle' import { JBDeploy721TiersHookConfig, LaunchProjectWithNftsTxArgs, } from 'packages/v4/models/nfts' -import { useContext } from 'react' -import { DEFAULT_MUST_START_AT_OR_AFTER } from 'redux/slices/editingV2Project' -import { ipfsUri } from 'utils/ipfs' import { Address, + WaitForTransactionReceiptReturnType, toBytes, toHex, - WaitForTransactionReceiptReturnType, zeroAddress, } from 'viem' -import { sepolia } from 'viem/chains' import { LaunchV2V3ProjectArgs, transformV2V3CreateArgsToV4, } from '../utils/launchProjectTransformers' -import { wagmiConfig } from '../wagmiConfig' -import { useCurrentRouteChainId } from './useCurrentRouteChainId' import { LaunchTxOpts, SUPPORTED_JB_CONTROLLER_ADDRESS, SUPPORTED_JB_MULTITERMINAL_ADDRESS } from './useLaunchProjectTx' +import { waitForTransactionReceipt } from '@wagmi/core' +import { JUICEBOX_MONEY_PROJECT_METADATA_DOMAIN } from 'constants/metadataDomain' +import { TxHistoryContext } from 'contexts/Transaction/TxHistoryContext' +import { useWallet } from 'hooks/Wallet' +import { isValidMustStartAtOrAfter } from 'packages/v2v3/utils/fundingCycle' +import { useContext } from 'react' +import { DEFAULT_MUST_START_AT_OR_AFTER } from 'redux/slices/editingV2Project' +import { ipfsUri } from 'utils/ipfs' +import { useChainId } from 'wagmi' +import { wagmiConfig } from '../wagmiConfig' + function createSalt() { const base: string = '0x' + Math.random().toString(16).slice(2) // idk lol const salt = toHex(toBytes(base, { size: 32 })) @@ -79,15 +79,17 @@ export function useLaunchProjectWithNftsTx() { const { addTransaction } = useContext(TxHistoryContext) const { userAddress } = useWallet() - const chainId = useCurrentRouteChainId() ?? sepolia.id // default to sepolia + const chainId = useChainId() + const chainIdStr = chainId?.toString() as keyof typeof SUPPORTED_JB_MULTITERMINAL_ADDRESS + const defaultJBController = chainId - ? SUPPORTED_JB_CONTROLLER_ADDRESS[chainId] + ? SUPPORTED_JB_CONTROLLER_ADDRESS[chainIdStr] : undefined const defaultJBETHPaymentTerminal = chainId - ? SUPPORTED_JB_MULTITERMINAL_ADDRESS[chainId] + ? SUPPORTED_JB_MULTITERMINAL_ADDRESS[chainIdStr] : undefined const JBTiered721DelegateStoreAddress = chainId - ? SUPPORTED_JB_721_TIER_STORE[chainId] + ? SUPPORTED_JB_721_TIER_STORE[chainIdStr] : undefined const { writeContractAsync: writeLaunchProject } = @@ -158,7 +160,7 @@ export function useLaunchProjectWithNftsTx() { tiersConfig: { currency, decimals: NATIVE_TOKEN_DECIMALS, - prices: jbPricesAddress[chainId], + prices: jbPricesAddress[chainIdStr], tiers, }, reserveBeneficiary: zeroAddress, @@ -209,7 +211,7 @@ export function useLaunchProjectWithNftsTx() { // }) const hash = await writeLaunchProject({ - chainId, + chainId: chainId as 84532 | 421614 | 11155111 | 11155420, // TODO: cleanup args, }) diff --git a/src/packages/v4/wagmiConfig.ts b/src/packages/v4/wagmiConfig.ts index 98f732ad02..58831225e0 100644 --- a/src/packages/v4/wagmiConfig.ts +++ b/src/packages/v4/wagmiConfig.ts @@ -13,13 +13,13 @@ export const wagmiConfig = createConfig({ `https://sepolia.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_ID}`, ), [optimismSepolia.id]: http( - `https://optimism-sepolia.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_ID}`, + 'https://sepolia.optimism.io' ), [baseSepolia.id]: http( `https://api.developer.coinbase.com/rpc/v1/base-sepolia/${process.env.NEXT_PUBLIC_BASE_ID}`, ), [arbitrumSepolia.id]: http( - `https://arbitrum-sepolia.infura.io/v3/${process.env.NEXT_PUBLIC_INFURA_ID}`, + `https://sepolia-rollup.arbitrum.io/rpc`, ), }, })