Skip to content

Retrofit lend/swap to narrowed wallet helper and drop unsafe receipt casts #476

Description

@its-everdred

PR #465 introduces (or will introduce, after its fix pass) two patterns in services/borrow.ts that lend/swap should also adopt:

  1. Narrowed wallet helper. resolveWalletOrThrow(idToken) returns a SmartWallet narrowed to { borrow: NonNullable<...> } so call sites don't need wallet.borrow! non-null assertions.
  2. Typed receipt return without as Type casts. Receipt-with-URLs shapes are constructed against the narrowed types rather than asserted.

Today:

  • services/lend.ts:51-52 uses wallet.lend!.openPosition(...) / wallet.lend!.closePosition(...) non-null assertions.
  • services/lend.ts:62 casts result with as LendTransactionReceiptWithUrls.
  • services/swap.ts:95 calls wallet.swap.execute(...) after a manual undefined check; could be narrowed by the same helper.
  • services/swap.ts similarly relies on inline shape construction without a typed helper.

If mutation request bodies in lend or swap ever adopt a { params \| quote } discriminated shape, also migrate them to z.discriminatedUnion for branch-targeted 400s (mirroring the borrow fix).

Acceptance

  • Extract a generic resolveWalletForProvider<P>(idToken, 'lend' \| 'swap' \| 'borrow') (or three sibling helpers) that returns the narrowed wallet type.
  • Replace wallet.lend! and wallet.swap usages with calls through the helper.
  • Eliminate as LendTransactionReceiptWithUrls / similar casts by typing the constructor instead.
  • If/when lend or swap adopts discriminated mutation bodies, migrate to z.discriminatedUnion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:todoFollow-up task suitable for agent implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions