diff --git a/.changeset/twelve-bulldogs-collect.md b/.changeset/twelve-bulldogs-collect.md new file mode 100644 index 00000000..2c4bbb8f --- /dev/null +++ b/.changeset/twelve-bulldogs-collect.md @@ -0,0 +1,6 @@ +--- +"@burnt-labs/abstraxion": minor +"@burnt-labs/constants": minor +--- + +Refactor Abstraxion to fetch config diff --git a/packages/abstraxion/src/components/Abstraxion/index.tsx b/packages/abstraxion/src/components/Abstraxion/index.tsx index cf05b018..4fb2779b 100644 --- a/packages/abstraxion/src/components/Abstraxion/index.tsx +++ b/packages/abstraxion/src/components/Abstraxion/index.tsx @@ -64,7 +64,6 @@ export function Abstraxion({ onClose }: ModalProps): JSX.Element | null { export interface AbstraxionConfig { contracts?: ContractGrantDescription[]; - dashboardUrl?: string; rpcUrl?: string; restUrl?: string; stake?: boolean; @@ -81,7 +80,6 @@ export function AbstraxionProvider({ return ( >; contracts?: ContractGrantDescription[]; dashboardUrl?: string; - rpcUrl?: string; - restUrl?: string; + setDashboardUrl: React.Dispatch>; + rpcUrl: string; + restUrl: string; stake?: boolean; bank?: SpendLimit[]; logout?: () => void; @@ -41,7 +42,6 @@ export const AbstraxionContext = createContext( export function AbstraxionContextProvider({ children, contracts, - dashboardUrl = "https://dashboard.burnt.com", rpcUrl = testnetChainInfo.rpc, restUrl = testnetChainInfo.rest, stake = false, @@ -63,6 +63,7 @@ export function AbstraxionContextProvider({ DirectSecp256k1HdWallet | undefined >(undefined); const [granterAddress, setGranterAddress] = useState(""); + const [dashboardUrl, setDashboardUrl] = useState(""); useEffect(() => { const searchParams = new URLSearchParams(window.location.search); @@ -96,6 +97,7 @@ export function AbstraxionContextProvider({ setGranterAddress, contracts, dashboardUrl, + setDashboardUrl, rpcUrl, restUrl, stake, diff --git a/packages/abstraxion/src/components/AbstraxionSignin/index.tsx b/packages/abstraxion/src/components/AbstraxionSignin/index.tsx index 3b56ede9..6d2406b7 100644 --- a/packages/abstraxion/src/components/AbstraxionSignin/index.tsx +++ b/packages/abstraxion/src/components/AbstraxionSignin/index.tsx @@ -7,6 +7,7 @@ import { AbstraxionContext, ContractGrantDescription, } from "../AbstraxionContext"; +import { fetchConfig } from "@burnt-labs/constants"; interface GrantsResponse { grants: Grant[]; @@ -49,11 +50,14 @@ export function AbstraxionSignin(): JSX.Element { const { setIsConnecting, setIsConnected, + setAbstraxionError, setAbstraxionAccount, setGranterAddress, granterAddress, contracts, dashboardUrl, + setDashboardUrl, + rpcUrl, restUrl, stake, bank, @@ -70,7 +74,12 @@ export function AbstraxionSignin(): JSX.Element { function openDashboardTab( userAddress: string, grantContracts?: ContractGrantDescription[], + dashUrl?: string, ): void { + if (!dashUrl) { + console.warn("Failed to fetch dashboard url"); + setAbstraxionError("Failed to fetch dashboard url"); + } const currentUrl = window.location.href; const urlParams = new URLSearchParams(); @@ -87,7 +96,7 @@ export function AbstraxionSignin(): JSX.Element { } urlParams.set("redirect_uri", currentUrl); const queryString = urlParams.toString(); // Convert URLSearchParams to string - window.location.href = `${dashboardUrl}?${queryString}`; + window.location.href = `${dashUrl}?${queryString}`; } async function generateAndStoreTempAccount(): Promise { @@ -158,7 +167,9 @@ export function AbstraxionSignin(): JSX.Element { setTempAccountAddress(address); if (!isGranted && !granterAddress) { - openDashboardTab(address, contracts); + const dashUrl = await fetchConfig(rpcUrl); + setDashboardUrl(dashUrl); + openDashboardTab(address, contracts, dashUrl); } else if (isGranted && !granterAddress) { await pollForGrants(address); setIsConnecting(false); @@ -169,6 +180,7 @@ export function AbstraxionSignin(): JSX.Element { } } catch (error) { console.log("Something went wrong: ", error); + setAbstraxionError((error as Error).message); } } @@ -193,7 +205,7 @@ export function AbstraxionSignin(): JSX.Element {