Skip to content

Commit fccd4aa

Browse files
committed
fix(app): cleanup balance fetching
1 parent 839aa6c commit fccd4aa

File tree

4 files changed

+60
-40
lines changed

4 files changed

+60
-40
lines changed

app/src/lib/components/TransferFrom/index.svelte

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ validation.subscribe(async data => {
8383
})
8484
</script>
8585

86+
<pre>
87+
{#each $balances as balance}{JSON.stringify(balance.data, null, 2)}{/each}
88+
</pre>
8689
<Cube>
8790
<div slot="intent" let:rotateTo class="w-full h-full">
8891
<Intent transferArgs={$transferArgs} {stores} {rotateTo}/>
@@ -112,4 +115,4 @@ validation.subscribe(async data => {
112115
{#if TRANSFER_DEBUG}
113116
<DebugBox {stores}/>
114117
{/if}
115-
</div>
118+
</div>

app/src/lib/components/token.svelte

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export let userAmount: string | null = null
1818
export let expanded = false
1919
2020
$: chain = chains.find(c => c.chain_id === chainId) ?? null
21-
$: graphqlToken =
22-
chain?.tokens.find(t => t.denom?.toLowerCase() === (denom ?? "").toLowerCase()) ?? null
2321
$: tokenInfo = tokenInfoQuery(chainId, (denom ?? "").toLowerCase(), chains)
2422
</script>
2523

app/src/lib/queries/balance/evm/multicall.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { raise } from "$lib/utilities/index.ts"
33
import { config } from "$lib/wallet/evm/config.ts"
44
import { erc20Abi, getAddress, type Address } from "viem"
55
import type { NoRepetition } from "$lib/utilities/types.ts"
6+
import { ResultAsync } from "neverthrow"
67

78
/**
89
* @example
@@ -54,7 +55,9 @@ export async function erc20ReadMulticall({
5455
const currentResult = accumulator.at(-1)
5556
const fn = functionNames[index % functionNames.length]
5657
if (!currentResult) return accumulator
57-
currentResult[fn === "balanceOf" ? "balance" : fn] = result ?? (fn === "decimals" ? 0 : "")
58+
if (result !== undefined) {
59+
currentResult[fn === "balanceOf" ? "balance" : fn] = result ?? (fn === "decimals" ? 0 : "")
60+
}
5861
return accumulator
5962
},
6063
[] as Array<Record<string, any>>

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

+52-36
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { erc20ReadMulticall } from "./evm/multicall.ts"
55
import { getCosmosChainBalances } from "./cosmos.ts"
66
import { getAptosChainBalances } from "./aptos.ts"
77
import { createQueries } from "@tanstack/svelte-query"
8+
import { errAsync, ok, ResultAsync, type Result } from "neverthrow"
89

910
export type AssetMetadata = {
1011
denom: string
@@ -144,6 +145,9 @@ function getAddressForChain(chain: Chain, addresses: UserAddresses): string | nu
144145
}
145146
}
146147

148+
export type Denom = string
149+
export type RawBalances = Record<Denom, string>
150+
147151
export function userBalancesQuery({
148152
userAddr,
149153
chains
@@ -165,48 +169,60 @@ export function userBalancesQuery({
165169
enabled: Boolean(getAddressForChain(chain, userAddr)),
166170
refetchInterval: 4_000,
167171
refetchOnWindowFocus: false,
168-
queryFn: async () => {
172+
queryFn: async (): Promise<Result<RawBalances, Error>> => {
169173
const address = getAddressForChain(chain, userAddr)
170-
if (!address) return null
171-
172-
let rawBalances: Record<string, string> = {}
174+
if (!address) return errAsync(new Error(`no user address for chain ${chain.chain_id}`))
173175

174176
if (chain.rpc_type === "evm") {
175177
const tokenList = chain.tokens.filter(tokens => isAddress(tokens.denom))
176-
const multicallResults = await erc20ReadMulticall({
177-
chainId: chain.chain_id,
178-
functionNames: ["balanceOf"],
179-
address: address as Address,
180-
contractAddresses: tokenList.map(asset => asset.denom) as Array<Address>
181-
})
182-
183-
multicallResults.forEach((result, index) => {
184-
rawBalances[tokenList[index].denom] = result.balance?.toString() ?? "0"
185-
})
186-
} else if (chain.rpc_type === "cosmos") {
187-
const url = chain.rpcs.find(rpc => rpc.type === "rest")?.url
188-
if (!url) throw new Error(`No REST RPC available for chain ${chain.chain_id}`)
189-
190-
const bech32Address = bech32ToBech32Address({
191-
toPrefix: chain.addr_prefix,
192-
address: address
193-
})
194-
195-
const cosmosBalances = await getCosmosChainBalances({ url, walletAddress: bech32Address })
196-
cosmosBalances.forEach(balance => {
197-
rawBalances[balance.address] = balance.balance.toString()
198-
})
199-
} else if (chain.rpc_type === "aptos") {
200-
const url = chain.rpcs.find(rpc => rpc.type === "rpc")?.url
201-
if (!url) throw new Error(`No RPC available for chain ${chain.chain_id}`)
202-
203-
const aptosBalances = await getAptosChainBalances({ url, walletAddress: address })
204-
aptosBalances.forEach(balance => {
205-
rawBalances[balance.address] = balance.balance.toString()
206-
})
178+
return await ResultAsync.fromPromise(
179+
erc20ReadMulticall({
180+
chainId: chain.chain_id,
181+
functionNames: ["balanceOf"],
182+
address: address as Address,
183+
contractAddresses: tokenList.map(asset => asset.denom) as Array<Address>
184+
}),
185+
error => new Error("error fetching evm balances", { cause: error })
186+
).andThen(multicallResultss =>
187+
ok(
188+
multicallResultss.reduce((acc, curr, index) => {
189+
console.log({ curr })
190+
if (curr.balance) {
191+
acc[tokenList[index].denom] = curr.balance.toString()
192+
}
193+
return acc
194+
}, {})
195+
)
196+
)
207197
}
208198

209-
return { chain_id: chain.chain_id, balances: rawBalances }
199+
return errAsync(new Error("unimplemented"))
200+
201+
// if (chain.rpc_type === "cosmos") {
202+
// const url = chain.rpcs.find(rpc => rpc.type === "rest")?.url
203+
// if (!url) throw new Error(`No REST RPC available for chain ${chain.chain_id}`)
204+
205+
// const bech32Address = bech32ToBech32Address({
206+
// toPrefix: chain.addr_prefix,
207+
// address: address
208+
// })
209+
210+
// const cosmosBalances = await getCosmosChainBalances({ url, walletAddress: bech32Address })
211+
// cosmosBalances.forEach(balance => {
212+
// rawBalances[balance.address] = balance.balance.toString()
213+
// })
214+
// }
215+
// if (chain.rpc_type === "aptos") {
216+
// const url = chain.rpcs.find(rpc => rpc.type === "rpc")?.url
217+
// if (!url) throw new Error(`No RPC available for chain ${chain.chain_id}`)
218+
219+
// const aptosBalances = await getAptosChainBalances({ url, walletAddress: address })
220+
// aptosBalances.forEach(balance => {
221+
// rawBalances[balance.address] = balance.balance.toString()
222+
// })
223+
// }
224+
225+
// return { chain_id: chain.chain_id, balances: rawBalances }
210226
}
211227
}))
212228
})

0 commit comments

Comments
 (0)