Skip to content

Commit

Permalink
refactor(app): save progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Swepool committed Dec 11, 2024
1 parent 950ad39 commit 5cb2d34
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 44 deletions.
133 changes: 133 additions & 0 deletions app/src/lib/components/TransferFrom/components/DebugBox.svelte
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>
50 changes: 8 additions & 42 deletions app/src/lib/components/TransferFrom/index.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
<script lang="ts">
import { createIntentStore } from "./intents.ts"
import { debounce } from "$lib/utilities"
import type { Chain } from "$lib/types.ts"
import { TRANSFER_DEBUG } from "$lib/components/TransferFrom/config.ts"
import CopyUrlButton from "$lib/components/TransferFrom/components/CopyUrlButton.svelte"
import ResetButton from "$lib/components/TransferFrom/components/ResetButton.svelte"
import {TRANSFER_DEBUG} from "$lib/components/TransferFrom/config.ts"
import CopyUrlButton from "$lib/components/TransferFrom/components/CopyUrlButton.svelte"
import ResetButton from "$lib/components/TransferFrom/components/ResetButton.svelte"
import {createTransferStore} from "$lib/components/TransferFrom/transfer.ts"
import DebugBox from "$lib/components/TransferFrom/components/DebugBox.svelte"
export let chains: Array<Chain>
const intents = createIntentStore()
function handleSubmit(event) {
event.preventDefault()
event.stopPropagation()
}
const {intents, chains, userAddress, sourceChain, destinationChain, balances, assetInfo} =
createTransferStore()
</script>

<form
Expand All @@ -22,7 +15,6 @@ function handleSubmit(event) {
action="transfer"
data-form="transfer"
class="flex flex-col p-4"
on:submit={handleSubmit}
>
<div class="flex flex-col gap-4">
<div class="flex flex-col gap-1">
Expand Down Expand Up @@ -125,31 +117,5 @@ function handleSubmit(event) {
</form>

{#if TRANSFER_DEBUG}
<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>
</div>
<DebugBox {intents} {chains} {userAddress} {sourceChain} {destinationChain} {balances} {assetInfo}/>
{/if}
4 changes: 2 additions & 2 deletions app/src/lib/components/TransferFrom/intents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { debounce } from "$lib/utilities"
//Need to clean up the types so they make sense
//RawTransferIntents should not contain errors etc just the raw inputs

type FormFields = {
export type FormFields = {
source: string
destination: string
asset: string
Expand All @@ -23,7 +23,7 @@ interface RawTransferIntents extends FormFields {
isValid: boolean
}

interface IntentStore {
export interface IntentStore {
subscribe: (callback: (value: RawTransferIntents) => void) => () => void
set: (value: Partial<FormFields>) => void
updateField: (field: keyof FormFields, valueOrEvent: string | Event) => void
Expand Down
104 changes: 104 additions & 0 deletions app/src/lib/components/TransferFrom/transfer.ts
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
}
}

0 comments on commit 5cb2d34

Please sign in to comment.