diff --git a/packages/commonwealth/client/scripts/controllers/app/login.ts b/packages/commonwealth/client/scripts/controllers/app/login.ts index 45445efd1ac..b8fc5f6c348 100644 --- a/packages/commonwealth/client/scripts/controllers/app/login.ts +++ b/packages/commonwealth/client/scripts/controllers/app/login.ts @@ -23,6 +23,7 @@ import { Magic } from 'magic-sdk'; import axios from 'axios'; import app from 'state'; +import { EXCEPTION_CASE_VANILLA_getCommunityById } from 'state/api/communities/getCommuityById'; import { fetchProfilesByAddress } from 'state/api/profiles/fetchProfilesByAddress'; import { onUpdateEmailError, @@ -35,7 +36,7 @@ import { userStore } from 'state/ui/user'; import Account from '../../models/Account'; import AddressInfo from '../../models/AddressInfo'; import type BlockInfo from '../../models/BlockInfo'; -import type ChainInfo from '../../models/ChainInfo'; +import ChainInfo from '../../models/ChainInfo'; function storeActiveAccount(account: Account) { const user = userStore.getState(); @@ -282,7 +283,18 @@ export async function createUserWithAddress( }); const id = response.data.result.id; - const chainInfo = app.config.chains.getById(chain); + + // HACK: 8762 -- find a way to call getCommunityById trpc in non-react files + // when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the + // call from that function + const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById( + chain || '', + true, + ); + const chainInfo = ChainInfo.fromJSON({ + ...(communityInfo as any), + }); + const account = new Account({ addressId: id, address, @@ -412,8 +424,19 @@ export async function handleSocialLoginCallback({ }): Promise<{ address: string; isAddressNew: boolean }> { // desiredChain may be empty if social login was initialized from // a page without a chain, in which case we default to an eth login - // @ts-expect-error StrictNullChecks - const desiredChain = app.chain?.meta || app.config.chains.getById(chain); + let desiredChain = app.chain?.meta; + if (!desiredChain && chain) { + // HACK: 8762 -- find a way to call getCommunityById trpc in non-react files + // when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the + // call from that function + const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById( + chain || '', + true, + ); + desiredChain = ChainInfo.fromJSON({ + ...(communityInfo as any), + }); + } const isCosmos = desiredChain?.base === ChainBase.CosmosSDK; const magic = await constructMagic(isCosmos, desiredChain?.id); const isEmail = walletSsoSource === WalletSsoSource.Email; @@ -548,10 +571,22 @@ export async function handleSocialLoginCallback({ // This is code from before desiredChain was implemented, and // may not be necessary anymore: if (app.chain) { - const c = - userStore.getState().activeCommunity || - app.config.chains.getById(app.activeChainId()); - await updateActiveAddresses({ chain: c }); + let chainInfo = userStore.getState().activeCommunity; + + // HACK: 8762 -- find a way to call getCommunityById trpc in non-react files + // when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the + // call from that function + if (!chainInfo && chain) { + const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById( + chain || '', + true, + ); + chainInfo = ChainInfo.fromJSON({ + ...(communityInfo as any), + }); + } + + chainInfo && (await updateActiveAddresses({ chain: chainInfo })); } const { Profiles: profiles, email: ssoEmail } = response.data.result; diff --git a/packages/commonwealth/client/scripts/controllers/app/webWallets/walletconnect_web_wallet.ts b/packages/commonwealth/client/scripts/controllers/app/webWallets/walletconnect_web_wallet.ts index d24901222d7..b8dc5b44912 100644 --- a/packages/commonwealth/client/scripts/controllers/app/webWallets/walletconnect_web_wallet.ts +++ b/packages/commonwealth/client/scripts/controllers/app/webWallets/walletconnect_web_wallet.ts @@ -4,6 +4,7 @@ import app from 'state'; import type Web3 from 'web3'; import { SIWESigner } from '@canvas-js/chain-ethereum'; +import { EXCEPTION_CASE_VANILLA_getCommunityById } from 'state/api/communities/getCommuityById'; import { userStore } from 'state/ui/user'; import { hexToNumber } from 'web3-utils'; import BlockInfo from '../../../models/BlockInfo'; @@ -99,8 +100,26 @@ class WalletConnectWebWalletController implements IWebWallet { this._enabling = true; // try { // Create WalletConnect Provider - this._chainInfo = - app.chain?.meta || app.config.chains.getById(this.defaultNetwork); + this._chainInfo = app?.chain?.meta; + + // HACK: 8762 -- find a way to call getCommunityById trpc in non-react files + // when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the + // call from that function + { + if (!this._chainInfo && app.activeChainId()) { + const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById( + app.activeChainId() || '', + true, + ); + + const chainInfo = ChainInfo.fromJSON({ + ...(communityInfo as any), + }); + + this._chainInfo = chainInfo; + } + } + const chainId = this._chainInfo.node?.ethChainId || 1; const EthereumProvider = (await import('@walletconnect/ethereum-provider')) .default; diff --git a/packages/commonwealth/client/scripts/helpers/chain.ts b/packages/commonwealth/client/scripts/helpers/chain.ts index 292cdc42535..6bb84b2d653 100644 --- a/packages/commonwealth/client/scripts/helpers/chain.ts +++ b/packages/commonwealth/client/scripts/helpers/chain.ts @@ -3,6 +3,7 @@ import { updateActiveAddresses } from 'controllers/app/login'; import { DEFAULT_CHAIN } from 'helpers/constants'; import app, { ApiStatus } from 'state'; import ChainInfo from '../models/ChainInfo'; +import { EXCEPTION_CASE_VANILLA_getCommunityById } from '../state/api/communities/getCommuityById'; import { userStore } from '../state/ui/user'; export const deinitChainOrCommunity = async () => { @@ -39,7 +40,16 @@ export const loadCommunityChainInfo = async ( if (activeCommunity) { tempChain = activeCommunity; } else { - tempChain = app.config.chains.getById(DEFAULT_CHAIN); + // HACK: 8762 -- find a way to call getCommunityById trpc in non-react files + // when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the + // call from that function + const communityInfo = await EXCEPTION_CASE_VANILLA_getCommunityById( + DEFAULT_CHAIN, + true, + ); + tempChain = ChainInfo.fromJSON({ + ...(communityInfo as any), + }); } if (!tempChain) { diff --git a/packages/commonwealth/client/scripts/state/api/communities/getCommuityById.ts b/packages/commonwealth/client/scripts/state/api/communities/getCommuityById.ts index da85376a9df..bb98a8492f0 100644 --- a/packages/commonwealth/client/scripts/state/api/communities/getCommuityById.ts +++ b/packages/commonwealth/client/scripts/state/api/communities/getCommuityById.ts @@ -1,6 +1,8 @@ import { Community } from '@hicommonwealth/schemas'; +import axios from 'axios'; import { trpc } from 'utils/trpcClient'; import { z } from 'zod'; +import app from '../../index'; import { queryClient } from '../config'; const COMMUNITIY_STALE_TIME = 60 * 3_000; // 3 mins @@ -43,6 +45,19 @@ export const invalidateAllQueriesForCommunity = async (communityId: string) => { } }; +export const EXCEPTION_CASE_VANILLA_getCommunityById = async ( + communityId: string, + includeNodeInfo = false, +): Promise> => { + // HACK: 8762 -- find a way to call getCommunityById trpc in non-react files + // when u do, update `EXCEPTION_CASE_VANILLA_getCommunityById` name and make the + // call from that function + const response = await axios.get(` + ${app.serverUrl()}/v1/community.getCommunity?batch=1&input=%7B%220%22%3A%7B%22id%22%3A%22${communityId}%22%2C%22include_node_info%22%3A${includeNodeInfo}%7D%7D + `); + return response?.data[0]?.result.data; +}; + const useGetCommunityByIdQuery = ({ id, includeNodeInfo = false,