From 4af156966231a3f5fc4a4e8e2f289302f1987948 Mon Sep 17 00:00:00 2001 From: sophian Date: Wed, 16 Oct 2024 19:52:39 -0400 Subject: [PATCH 1/3] Fix how chain id is fetched --- .../InvestRedeemLiquidityPoolsProvider.tsx | 11 ++++++++++- centrifuge-js/src/utils/signERC2612Permit.ts | 9 ++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx b/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx index 411c5c070d..d867ad0b80 100644 --- a/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx +++ b/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx @@ -10,6 +10,7 @@ import { import BN from 'bn.js' import { TransactionRequest } from 'ethers' import * as React from 'react' +import { useEffect } from 'react' import { Dec } from '../../utils/Decimal' import { useEvmTransaction } from '../../utils/tinlake/useEvmTransaction' import { useAddress } from '../../utils/useAddress' @@ -40,12 +41,20 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children isLoading: isInvestmentLoading, } = useLiquidityPoolInvestment(poolId, trancheId, lpIndex) const provider = useEvmProvider() + const [chainId, setChainId] = React.useState(1) const isAllowedToInvest = lpInvest?.isAllowedToInvest const tranche = pool.tranches.find((t) => t.id === trancheId) const { data: metadata, isLoading: isMetadataLoading } = usePoolMetadata(pool) const trancheMeta = metadata?.tranches?.[trancheId] - const chainId = Number(provider?._network.chainId) + + useEffect(() => { + const getChainId = async () => { + const chainId = Number((await provider?.provider!.getNetwork())?.chainId || 1n) + setChainId(chainId) + } + getChainId() + }, [provider]) if (!tranche) throw new Error(`Token not found. Pool id: ${poolId}, token id: ${trancheId}`) diff --git a/centrifuge-js/src/utils/signERC2612Permit.ts b/centrifuge-js/src/utils/signERC2612Permit.ts index 0a3d70badf..4d9258d45d 100644 --- a/centrifuge-js/src/utils/signERC2612Permit.ts +++ b/centrifuge-js/src/utils/signERC2612Permit.ts @@ -1,4 +1,4 @@ -import { ethers } from 'ethers' +import { Contract, Signature } from 'ethers' export async function signERC2612Permit( provider: any, @@ -9,7 +9,7 @@ export async function signERC2612Permit( deadline: number ) { const tokenAddress = typeof token === 'string' ? token : token.verifyingContract - const tokenContract = new ethers.Contract( + const tokenContract = new Contract( tokenAddress, ['function name() view returns (string)', 'function nonces(address) view returns (uint256)'], provider @@ -51,9 +51,8 @@ export async function signERC2612Permit( deadline, } - const signer = await provider.getSigner(owner) - const signature = await signer.signTypedData(domain, types, values) - const { v, r, s } = ethers.Signature.from(signature) + const signature = await provider.signTypedData(domain, types, values) + const { v, r, s } = Signature.from(signature) return { owner, From ec8b499ad5c066e67a591a7fb108da44f18f18ff Mon Sep 17 00:00:00 2001 From: sophian Date: Thu, 17 Oct 2024 09:55:11 -0400 Subject: [PATCH 2/3] Get chain id from useWallet --- .../InvestRedeemLiquidityPoolsProvider.tsx | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx b/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx index d867ad0b80..746e8a3e1d 100644 --- a/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx +++ b/centrifuge-app/src/components/InvestRedeem/InvestRedeemLiquidityPoolsProvider.tsx @@ -10,7 +10,6 @@ import { import BN from 'bn.js' import { TransactionRequest } from 'ethers' import * as React from 'react' -import { useEffect } from 'react' import { Dec } from '../../utils/Decimal' import { useEvmTransaction } from '../../utils/tinlake/useEvmTransaction' import { useAddress } from '../../utils/useAddress' @@ -41,21 +40,14 @@ export function InvestRedeemLiquidityPoolsProvider({ poolId, trancheId, children isLoading: isInvestmentLoading, } = useLiquidityPoolInvestment(poolId, trancheId, lpIndex) const provider = useEvmProvider() - const [chainId, setChainId] = React.useState(1) + const { evm } = useWallet() + const chainId = evm.chainId || 1 const isAllowedToInvest = lpInvest?.isAllowedToInvest const tranche = pool.tranches.find((t) => t.id === trancheId) const { data: metadata, isLoading: isMetadataLoading } = usePoolMetadata(pool) const trancheMeta = metadata?.tranches?.[trancheId] - useEffect(() => { - const getChainId = async () => { - const chainId = Number((await provider?.provider!.getNetwork())?.chainId || 1n) - setChainId(chainId) - } - getChainId() - }, [provider]) - if (!tranche) throw new Error(`Token not found. Pool id: ${poolId}, token id: ${trancheId}`) const trancheBalance = lpInvest?.tokenBalance?.toDecimal() ?? Dec(0) From 95a35cc5cb077ebdb11896afe79d1a334b54e72a Mon Sep 17 00:00:00 2001 From: sophian Date: Thu, 17 Oct 2024 11:37:52 -0400 Subject: [PATCH 3/3] Fix onboarding signer --- centrifuge-app/src/components/OnboardingAuthProvider.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/centrifuge-app/src/components/OnboardingAuthProvider.tsx b/centrifuge-app/src/components/OnboardingAuthProvider.tsx index 4c15f1bb01..91b7769afe 100644 --- a/centrifuge-app/src/components/OnboardingAuthProvider.tsx +++ b/centrifuge-app/src/components/OnboardingAuthProvider.tsx @@ -59,12 +59,13 @@ export function OnboardingAuthProvider({ children }: { children: React.ReactNode const { mutate: login, isLoading: isLoggingIn } = useMutation(async () => { try { + const signer = await provider?.getSigner() if (selectedAccount?.address && selectedWallet?.signer) { await loginWithSubstrate(selectedAccount?.address, selectedWallet.signer as Signer, cent, proxy) - } else if (isEvmOnSubstrate && selectedAddress && provider?.getSigner()) { - await loginWithEvm(selectedAddress, provider.getSigner(), evmChainId, isEvmOnSubstrate) - } else if (selectedAddress && provider?.getSigner()) { - await loginWithEvm(selectedAddress, provider.getSigner(), evm.chainId) + } else if (isEvmOnSubstrate && selectedAddress && signer) { + await loginWithEvm(selectedAddress, signer, evmChainId, isEvmOnSubstrate) + } else if (selectedAddress && signer) { + await loginWithEvm(selectedAddress, signer, evm.chainId) } throw new Error('network not supported') } catch {