Skip to content

Commit

Permalink
Merge pull request #121 from burnt-labs/fix/abstraxion-config
Browse files Browse the repository at this point in the history
Refactor Abstraxion to fetch config
  • Loading branch information
BurntVal authored Mar 26, 2024
2 parents 6531008 + 2ab0bac commit 2f9ad6d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/twelve-bulldogs-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@burnt-labs/abstraxion": minor
"@burnt-labs/constants": minor
---

Refactor Abstraxion to fetch config
2 changes: 0 additions & 2 deletions packages/abstraxion/src/components/Abstraxion/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -81,7 +80,6 @@ export function AbstraxionProvider({
return (
<AbstraxionContextProvider
contracts={config.contracts}
dashboardUrl={config.dashboardUrl}
rpcUrl={config.rpcUrl}
restUrl={config.restUrl}
stake={config.stake}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ export interface AbstraxionContextProps {
setGranterAddress: React.Dispatch<React.SetStateAction<string>>;
contracts?: ContractGrantDescription[];
dashboardUrl?: string;
rpcUrl?: string;
restUrl?: string;
setDashboardUrl: React.Dispatch<React.SetStateAction<string>>;
rpcUrl: string;
restUrl: string;
stake?: boolean;
bank?: SpendLimit[];
logout?: () => void;
Expand All @@ -41,7 +42,6 @@ export const AbstraxionContext = createContext<AbstraxionContextProps>(
export function AbstraxionContextProvider({
children,
contracts,
dashboardUrl = "https://dashboard.burnt.com",
rpcUrl = testnetChainInfo.rpc,
restUrl = testnetChainInfo.rest,
stake = false,
Expand All @@ -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);
Expand Down Expand Up @@ -96,6 +97,7 @@ export function AbstraxionContextProvider({
setGranterAddress,
contracts,
dashboardUrl,
setDashboardUrl,
rpcUrl,
restUrl,
stake,
Expand Down
18 changes: 15 additions & 3 deletions packages/abstraxion/src/components/AbstraxionSignin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AbstraxionContext,
ContractGrantDescription,
} from "../AbstraxionContext";
import { fetchConfig } from "@burnt-labs/constants";

interface GrantsResponse {
grants: Grant[];
Expand Down Expand Up @@ -49,11 +50,14 @@ export function AbstraxionSignin(): JSX.Element {
const {
setIsConnecting,
setIsConnected,
setAbstraxionError,
setAbstraxionAccount,
setGranterAddress,
granterAddress,
contracts,
dashboardUrl,
setDashboardUrl,
rpcUrl,
restUrl,
stake,
bank,
Expand All @@ -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();

Expand All @@ -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<DirectSecp256k1HdWallet> {
Expand Down Expand Up @@ -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);
Expand All @@ -169,6 +180,7 @@ export function AbstraxionSignin(): JSX.Element {
}
} catch (error) {
console.log("Something went wrong: ", error);
setAbstraxionError((error as Error).message);
}
}

Expand All @@ -193,7 +205,7 @@ export function AbstraxionSignin(): JSX.Element {
<BrowserIcon />
<Button
onClick={() => {
openDashboardTab(tempAccountAddress, contracts);
openDashboardTab(tempAccountAddress, contracts, dashboardUrl);
}}
structure="naked"
>
Expand Down
30 changes: 25 additions & 5 deletions packages/constants/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { RpcStatusResponse } from "./types";

export interface Coin {
coinDenom: string;
coinMinimalDenom: string;
Expand All @@ -22,11 +24,6 @@ interface Bip44 {
coinType: number;
}

interface Gas {
price: string;
denom: string;
}

export interface ChainInfo {
rpc: string;
rest: string;
Expand Down Expand Up @@ -88,3 +85,26 @@ export const testChainInfo: ChainInfo = {
chainId: "xion-local-testnet-1",
chainName: "XION Testnet Local",
};

// If mainnet chain-id/network changes be sure to update here.
const DASHBOARD_URLS = {
"xion-mainnet-1": "https://dashboard.burnt.com",
"xion-testnet-1": "https://testnet.dashboard.burnt.com",
};

export async function fetchConfig(rpcUrl: string): Promise<string> {
try {
const fetchReq = await fetch(`${rpcUrl}/status`);
if (!fetchReq.ok) {
throw new Error("Something went wrong querying RPC");
}

const data: RpcStatusResponse = await fetchReq.json();
const lookup = data.result.node_info.network;
const returnUrl = DASHBOARD_URLS[lookup as keyof typeof DASHBOARD_URLS];
if (!returnUrl) throw new Error("Network not found.");
return returnUrl;
} catch (error) {
throw error;
}
}
33 changes: 33 additions & 0 deletions packages/constants/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Interface for rpc status response
export interface RpcStatusResponse {
jsonrpc: string;
id: number;
result: RpcStatusResult;
}

export interface RpcStatusResult {
node_info: RpcStatusResultNodeInfo;
sync_info: RpcStatusResultSyncInfo;
}

export interface RpcStatusResultNodeInfo {
protocol_version: any;
id: string;
network: string;
version: string;
channels: string;
moniker: string;
other: any;
}

export interface RpcStatusResultSyncInfo {
latest_block_hash: string;
latest_app_hash: string;
latest_block_height: string;
latest_block_time: string;
earliest_block_hash: string;
earliest_app_hash: string;
earliest_block_height: string;
earliest_block_time: string;
catching_up: boolean;
}

0 comments on commit 2f9ad6d

Please sign in to comment.