-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
247 additions
and
44 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
app/src/lib/components/TransferFrom/components/DebugBox.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<!-- Debug.svelte --> | ||
<script lang="ts"> | ||
import CopyUrlButton from "$lib/components/TransferFrom/components/CopyUrlButton.svelte" | ||
import ResetButton from "$lib/components/TransferFrom/components/ResetButton.svelte" | ||
import type { IntentStore } from "./intents.ts" | ||
import type { Chain, UserAddresses } from "$lib/types" | ||
import type { Readable } from "svelte/store" | ||
import type { Balance } from "./transfer.ts" // Assuming you have Balance type exported | ||
interface DebugProps { | ||
intents: IntentStore | ||
chains: Array<Chain> | ||
userAddress: Readable<UserAddresses> | ||
sourceChain: Readable<Chain | undefined> | ||
destinationChain: Readable<Chain | undefined> | ||
balances: Readable<Array<Balance>> | ||
assetInfo: Readable<Balance | undefined> | ||
} | ||
export let intents: DebugProps["intents"] | ||
export let chains: DebugProps["chains"] | ||
export let userAddress: DebugProps["userAddress"] | ||
export let sourceChain: DebugProps["sourceChain"] | ||
export let destinationChain: DebugProps["destinationChain"] | ||
export let balances: DebugProps["balances"] | ||
export let assetInfo: DebugProps["assetInfo"] | ||
</script> | ||
|
||
<div class="p-4 w-full"> | ||
<div class="p-4 bg-black w-full"> | ||
<div class="mb-4 flex items-center gap-4"> | ||
<CopyUrlButton/> | ||
<ResetButton onReset={intents.reset}/> | ||
</div> | ||
|
||
<h2 class="mb-4">TRANSFER DEBUG</h2> | ||
|
||
<div class="summary mb-4"> | ||
<h3 class="text-union-accent-500">Raw Intents</h3> | ||
{#each Object.entries($intents) as [key, value]} | ||
{#if key !== "errors" && key !== "isValid"} | ||
<p class="text-sm">{key}: "{value}"</p> | ||
{/if} | ||
{/each} | ||
</div> | ||
|
||
<div class="summary mb-4"> | ||
<h3 class="text-red-500">Errors:</h3> | ||
{#each Object.entries($intents.errors) as [key, value]} | ||
<p class="text-sm">{key}: "{value}"</p> | ||
{/each} | ||
</div> | ||
|
||
<div class="summary mb-4"> | ||
<h3 class="text-orange-500">User Addresses:</h3> | ||
{#if $userAddress} | ||
{#each Object.entries($userAddress) as [key, value]} | ||
<p class="text-sm">{key}: "{value?.canonical}"</p> | ||
{/each} | ||
{:else} | ||
<p class="text-sm">No user addresses available</p> | ||
{/if} | ||
</div> | ||
|
||
<div class="summary mb-4"> | ||
<h3 class="text-yellow-500">Selected Asset Info:</h3> | ||
{#if $assetInfo} | ||
{#each Object.entries($assetInfo) as [key, value]} | ||
<p class="text-sm"> | ||
{key}: "{typeof value === 'bigint' ? value.toString() : value}" | ||
</p> | ||
{/each} | ||
{:else} | ||
<p class="text-sm">No asset selected</p> | ||
{/if} | ||
</div> | ||
|
||
<div class="summary mb-4"> | ||
<h3 class="text-purple-500">Balances:</h3> | ||
{#if $balances.length} | ||
{#each $balances as balance} | ||
<div class="ml-2 mb-2"> | ||
{#each Object.entries(balance) as [key, value]} | ||
<p class="text-sm"> | ||
{key}: "{typeof value === 'bigint' ? value.toString() : value}" | ||
</p> | ||
{/each} | ||
</div> | ||
{/each} | ||
{:else} | ||
<p class="text-sm">No balances available</p> | ||
{/if} | ||
</div> | ||
|
||
<div class="summary mb-4"> | ||
<h3 class="text-blue-500">Source Chain:</h3> | ||
{#if $sourceChain} | ||
{#each Object.entries($sourceChain) as [key, value]} | ||
<p class="text-sm">{key}: "{value}"</p> | ||
{/each} | ||
{:else} | ||
<p class="text-sm">No source chain selected</p> | ||
{/if} | ||
</div> | ||
|
||
<div class="summary mb-4"> | ||
<h3 class="text-green-500">Destination Chain:</h3> | ||
{#if $destinationChain} | ||
{#each Object.entries($destinationChain) as [key, value]} | ||
<p class="text-sm">{key}: "{value}"</p> | ||
{/each} | ||
{:else} | ||
<p class="text-sm">No destination chain selected</p> | ||
{/if} | ||
</div> | ||
|
||
<div class="summary mb-4"> | ||
<h3 class="text-indigo-500">Available Chains:</h3> | ||
{#if chains.length} | ||
{#each chains as chain} | ||
<div class="ml-2 mb-2"> | ||
{#each Object.entries(chain) as [key, value]} | ||
<p class="text-sm">{key}: "{value}"</p> | ||
{/each} | ||
</div> | ||
{/each} | ||
{:else} | ||
<p class="text-sm">No chains available</p> | ||
{/if} | ||
</div> | ||
|
||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { derived, get, type Readable } from "svelte/store" | ||
import { createIntentStore, type IntentStore } from "./intents.ts" | ||
import { userAddrCosmos } from "$lib/wallet/cosmos" | ||
import { userAddrEvm } from "$lib/wallet/evm" | ||
import { userAddressAptos } from "$lib/wallet/aptos" | ||
import { userBalancesQuery } from "$lib/queries/balance" | ||
import type { Chain, UserAddresses } from "$lib/types" | ||
import { useQueryClient } from "@tanstack/svelte-query" | ||
import type { Address } from "$lib/wallet/types.ts" | ||
|
||
type AddressBalance = { | ||
balance: bigint | ||
gasToken: boolean | ||
address: Address | ||
symbol: string | ||
} | ||
|
||
type NamedBalance = { | ||
balance: bigint | ||
address: string | ||
name: string | null | ||
symbol: string | ||
gasToken: boolean | ||
} | ||
|
||
type EmptyBalance = {} | ||
|
||
type Balance = AddressBalance | NamedBalance | EmptyBalance | ||
|
||
interface TransferStore { | ||
intents: IntentStore | ||
chains: Array<Chain> | ||
userAddress: Readable<UserAddresses> | ||
sourceChain: Readable<Chain | undefined> | ||
destinationChain: Readable<Chain | undefined> | ||
balances: Readable<Array<Balance>> | ||
assetInfo: Readable<Balance | undefined> | ||
} | ||
export function createTransferStore(): TransferStore { | ||
const intents = createIntentStore() | ||
const queryClient = useQueryClient() | ||
|
||
function queryData<T extends Array<unknown>>( | ||
key: Array<string>, | ||
filter?: (value: T[number]) => boolean | ||
): T { | ||
const data = queryClient.getQueryData<T>(key) ?? [] | ||
return (filter ? data.filter(filter) : data) as T | ||
} | ||
|
||
const chains = queryData<Array<Chain>>(["chains"], chain => chain.enabled_staging) | ||
|
||
const userAddress = derived( | ||
[userAddrCosmos, userAddrEvm, userAddressAptos], | ||
([cosmos, evm, aptos]) => ({ evm, aptos, cosmos }) | ||
) as Readable<UserAddresses> | ||
|
||
const sourceChain = derived(intents, intentsValue => | ||
chains.find(chain => chain.chain_id === intentsValue.source) | ||
) | ||
|
||
const destinationChain = derived(intents, intentsValue => | ||
chains.find(chain => chain.chain_id === intentsValue.destination) | ||
) | ||
|
||
const balances = derived( | ||
[ | ||
intents, | ||
userAddress, | ||
userBalancesQuery({ chains, connected: true, userAddr: get(userAddress) }) | ||
], | ||
([intentsValue, _userAddressValue, rawBalances]) => { | ||
const sourceChain = chains.find(chain => chain.chain_id === intentsValue.source) | ||
if (!sourceChain) return [] | ||
|
||
const chainIndex = chains.findIndex(c => c.chain_id === sourceChain.chain_id) | ||
const balanceResult = rawBalances[chainIndex] | ||
|
||
if (!balanceResult?.isSuccess || balanceResult.data instanceof Error) { | ||
console.log("No balances fetched yet for selected chain") | ||
return [] | ||
} | ||
|
||
return balanceResult.data.map(balance => ({ | ||
...balance, | ||
balance: BigInt(balance.balance) | ||
})) | ||
} | ||
) | ||
|
||
const assetInfo = derived([balances, intents], ([balancesValue, intentsValue]) => | ||
balancesValue.find(x => x?.address === intentsValue.asset) | ||
) | ||
|
||
return { | ||
intents, | ||
chains, | ||
userAddress, | ||
sourceChain, | ||
destinationChain, | ||
balances, | ||
assetInfo | ||
} | ||
} |