From fc9b5559333f50cbe5b5d8a4c76a1d71c5ddfc91 Mon Sep 17 00:00:00 2001 From: cor Date: Tue, 4 Feb 2025 13:51:37 +0000 Subject: [PATCH] fix(app): don't throw in cosmos balances --- app/package-lock.json | 1 + app/package.json | 7 ++-- app/src/lib/queries/balance/cosmos.ts | 54 ++++++++++++++------------- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/app/package-lock.json b/app/package-lock.json index 9a2f6777efd..fbf95bca99c 100644 --- a/app/package-lock.json +++ b/app/package-lock.json @@ -27,6 +27,7 @@ "gql.tada": "1.8.10", "graphql-request": "7.1.2", "mode-watcher": "0.5.0", + "neverthrow": "^8.1.1", "svelte-persisted-store": "^0.11.0", "svelte-radix": "^1.1.1", "svelte-sonner": "^0.3.27", diff --git a/app/package.json b/app/package.json index d34db82eaec..7ef842d6d12 100644 --- a/app/package.json +++ b/app/package.json @@ -15,9 +15,6 @@ "postinstall": "patch-package" }, "dependencies": { - "temporal-polyfill": "^0.2.5", - "mode-watcher": "0.5.0", - "svelte-sonner": "^0.3.27", "@cosmjs/amino": "^0.32.4", "@cosmjs/cosmwasm-stargate": "0.32.4", "@cosmjs/encoding": "^0.32.4", @@ -35,8 +32,12 @@ "cmdk-sv": "^0.0.18", "gql.tada": "1.8.10", "graphql-request": "7.1.2", + "mode-watcher": "0.5.0", + "neverthrow": "^8.1.1", "svelte-persisted-store": "^0.11.0", "svelte-radix": "^1.1.1", + "svelte-sonner": "^0.3.27", + "temporal-polyfill": "^0.2.5", "three": "0.170.0", "valibot": "0.42.1", "vaul-svelte": "^0.3.2", diff --git a/app/src/lib/queries/balance/cosmos.ts b/app/src/lib/queries/balance/cosmos.ts index d6ccd382e66..6bd884d0a14 100644 --- a/app/src/lib/queries/balance/cosmos.ts +++ b/app/src/lib/queries/balance/cosmos.ts @@ -1,5 +1,6 @@ import * as v from "valibot" import { raise } from "$lib/utilities" +import { err, ok, Result, ResultAsync } from "neverthrow" const cosmosBalancesResponseSchema = v.object({ balances: v.array( @@ -10,32 +11,33 @@ const cosmosBalancesResponseSchema = v.object({ ) }) -export async function getCosmosChainBalances({ +const fetchJson = (url: string) => { + return ResultAsync.fromPromise( + fetch(url).then(response => { + if (!response.ok) { + throw new Error(`HTTP error for url ${url} status: ${response.status}`) + } + return response.json() + }), + e => + new Error(`Failed to fetch data from url ${url} with error: ${(e as Error).message}`, { + cause: e + }) + ) +} + +export function getCosmosChainBalances({ url, walletAddress -}: { url: string; walletAddress: string }) { - let json: undefined | unknown - - try { - url = url.startsWith("https") ? url : `https://${url}` - const response = await fetch(`${url}/cosmos/bank/v1beta1/balances/${walletAddress}`) - if (!response.ok) raise("invalid response") - - json = await response.json() - } catch (error) { - if (error instanceof Error) { - raise(`error fetching balances from /cosmos/bank: ${error.message}`) - } - raise(`unknown error while fetching from /cosmos/bank: ${JSON.stringify(error)}`) - } - - const result = v.safeParse(cosmosBalancesResponseSchema, json) - - if (!result.success) raise(`error parsing result ${JSON.stringify(result.issues)}`) - return result.output.balances.map(x => ({ - address: x.denom, - symbol: x.denom, - balance: BigInt(x.amount), - decimals: 0 - })) +}: { url: string; walletAddress: string }): ResultAsync< + v.InferOutput, + Error +> { + url = url.startsWith("https") ? url : `https://${url}` + return fetchJson(`${url}/cosmos/bank/v1beta1/balances/${walletAddress}`).andThen(json => { + const result = v.safeParse(cosmosBalancesResponseSchema, json) + return result.success + ? ok(result.output) + : err(new Error("Validation failed:", { cause: result.issues })) + }) }