Skip to content

Commit 330b828

Browse files
committed
fix(app): don't throw in cosmos balances
1 parent 9c12595 commit 330b828

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

app/package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
"postinstall": "patch-package"
1616
},
1717
"dependencies": {
18-
"temporal-polyfill": "^0.2.5",
19-
"mode-watcher": "0.5.0",
20-
"svelte-sonner": "^0.3.27",
2118
"@cosmjs/amino": "^0.32.4",
2219
"@cosmjs/cosmwasm-stargate": "0.32.4",
2320
"@cosmjs/encoding": "^0.32.4",
@@ -35,8 +32,12 @@
3532
"cmdk-sv": "^0.0.18",
3633
"gql.tada": "1.8.10",
3734
"graphql-request": "7.1.2",
35+
"mode-watcher": "0.5.0",
36+
"neverthrow": "^8.1.1",
3837
"svelte-persisted-store": "^0.11.0",
3938
"svelte-radix": "^1.1.1",
39+
"svelte-sonner": "^0.3.27",
40+
"temporal-polyfill": "^0.2.5",
4041
"three": "0.170.0",
4142
"valibot": "0.42.1",
4243
"vaul-svelte": "^0.3.2",

app/src/lib/queries/balance/cosmos.ts

+28-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as v from "valibot"
2-
import { raise } from "$lib/utilities"
2+
import { err, ok, ResultAsync } from "neverthrow"
33

44
const cosmosBalancesResponseSchema = v.object({
55
balances: v.array(
@@ -10,32 +10,33 @@ const cosmosBalancesResponseSchema = v.object({
1010
)
1111
})
1212

13-
export async function getCosmosChainBalances({
13+
const fetchJson = (url: string) => {
14+
return ResultAsync.fromPromise(
15+
fetch(url).then(response => {
16+
if (!response.ok) {
17+
throw new Error(`HTTP error for url ${url} status: ${response.status}`)
18+
}
19+
return response.json()
20+
}),
21+
e =>
22+
new Error(`Failed to fetch data from url ${url} with error: ${(e as Error).message}`, {
23+
cause: e
24+
})
25+
)
26+
}
27+
28+
export function getCosmosChainBalances({
1429
url,
1530
walletAddress
16-
}: { url: string; walletAddress: string }) {
17-
let json: undefined | unknown
18-
19-
try {
20-
url = url.startsWith("https") ? url : `https://${url}`
21-
const response = await fetch(`${url}/cosmos/bank/v1beta1/balances/${walletAddress}`)
22-
if (!response.ok) raise("invalid response")
23-
24-
json = await response.json()
25-
} catch (error) {
26-
if (error instanceof Error) {
27-
raise(`error fetching balances from /cosmos/bank: ${error.message}`)
28-
}
29-
raise(`unknown error while fetching from /cosmos/bank: ${JSON.stringify(error)}`)
30-
}
31-
32-
const result = v.safeParse(cosmosBalancesResponseSchema, json)
33-
34-
if (!result.success) raise(`error parsing result ${JSON.stringify(result.issues)}`)
35-
return result.output.balances.map(x => ({
36-
address: x.denom,
37-
symbol: x.denom,
38-
balance: BigInt(x.amount),
39-
decimals: 0
40-
}))
31+
}: { url: string; walletAddress: string }): ResultAsync<
32+
v.InferOutput<typeof cosmosBalancesResponseSchema>,
33+
Error
34+
> {
35+
url = url.startsWith("https") ? url : `https://${url}`
36+
return fetchJson(`${url}/cosmos/bank/v1beta1/balances/${walletAddress}`).andThen(json => {
37+
const result = v.safeParse(cosmosBalancesResponseSchema, json)
38+
return result.success
39+
? ok(result.output)
40+
: err(new Error("Validation failed:", { cause: result.issues }))
41+
})
4142
}

0 commit comments

Comments
 (0)