Skip to content

Commit

Permalink
fix(app): don't throw in cosmos balances
Browse files Browse the repository at this point in the history
  • Loading branch information
cor committed Feb 4, 2025
1 parent 9c12595 commit 330b828
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
1 change: 1 addition & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
55 changes: 28 additions & 27 deletions app/src/lib/queries/balance/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as v from "valibot"
import { raise } from "$lib/utilities"
import { err, ok, ResultAsync } from "neverthrow"

const cosmosBalancesResponseSchema = v.object({
balances: v.array(
Expand All @@ -10,32 +10,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<typeof cosmosBalancesResponseSchema>,
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 }))
})
}

0 comments on commit 330b828

Please sign in to comment.