ci / gate (push) Successful in 13m0s
The intents panel can now pay a token it holds out through 1Click (#77): USDC on Ethereum, Base, Arbitrum, Solana or NEAR; USDT or ETH on Ethereum; BTC. - cashout.toml ships the destinations with their 1Click asset ids, checked against GET /v0/tokens today, and the address form each takes. - check_address refuses a mistyped payout address before anything is priced or signed, because nothing refunds a payout to the wrong address: - EVM: 0x and 40 hex digits, with the EIP-55 checksum enforced when the address is mixed case; - Bitcoin: bech32 or base58; - Solana: base58 of 32 bytes; - NEAR: an account id. - near_cashout takes a firm 1Click quote with recipientType DESTINATION_CHAIN, the fee as appFees, and refunds to the account. It checks the echoed recipient, its type, the refund, the assets and the amount against what was asked, funds it with one mt_transfer signed by the owning wallet's key, and has the sponsor relay it. - The 1Click request builder now serves swaps and cash-outs: None pays the account itself, Some(address) pays out on the destination chain. Checked live from the dev account: - 10 wNEAR quoted at 46.38 USDC on Ethereum, Ethereum's withdrawal included, with a floor of 45.91; - the same EIP-55 example address with its last character's case flipped was refused on its checksum. Refs #77 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NUx7SPonBnB3ofU4mH41WT
597 lines
20 KiB
TypeScript
597 lines
20 KiB
TypeScript
import { useState } from 'react'
|
||
import { useNavigate } from 'react-router-dom'
|
||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||
import { copyText } from '../api/clipboard'
|
||
import { useSession } from '../api/hooks'
|
||
import {
|
||
nearCashout,
|
||
nearCashoutDestinations,
|
||
nearCashoutQuote,
|
||
nearCreateAccount,
|
||
nearIntents,
|
||
nearQtcDepositAddress,
|
||
nearSwap,
|
||
nearSwapQuote,
|
||
nearWithdrawQtc,
|
||
} from '../api/wallet'
|
||
import { formatAmount } from '../lib/format'
|
||
|
||
/**
|
||
* The installation's NEAR Intents account: one NEAR account, controlled by
|
||
* one wallet's post-quantum key, that every swap routes through whichever
|
||
* wallet's QTC goes in or out. Named by the sponsor from the key, so it is
|
||
* known before it exists; everything here is read and decided on the Rust
|
||
* side.
|
||
*/
|
||
export function NearIntentsPanel() {
|
||
const qc = useQueryClient()
|
||
const account = useQuery({
|
||
queryKey: ['near-intents'],
|
||
queryFn: nearIntents,
|
||
staleTime: 30_000,
|
||
retry: 1,
|
||
})
|
||
const [owner, setOwner] = useState('')
|
||
const create = useMutation({
|
||
mutationFn: (wallet: string) => nearCreateAccount(wallet),
|
||
onSuccess: (made) => qc.setQueryData(['near-intents'], made),
|
||
})
|
||
const a = account.data
|
||
const active = !!a?.exists && a.key_on_account
|
||
return (
|
||
<section className="panel">
|
||
<div className="panel-head">
|
||
<h2 className="panel-title">NEAR Intents account</h2>
|
||
{a && (
|
||
<span className="muted" style={{ fontSize: 12 }}>
|
||
{a.exists
|
||
? a.key_on_account
|
||
? a.owner_open
|
||
? `active · ${a.wallet}`
|
||
: `${a.wallet} is locked`
|
||
: 'not this wallet’s key'
|
||
: 'not created yet'}
|
||
</span>
|
||
)}
|
||
</div>
|
||
<div className="panel-body">
|
||
{account.isPending && <p className="muted">Reading NEAR…</p>}
|
||
{account.isError && <p className="error">{account.error.message}</p>}
|
||
{a && (
|
||
<div data-testid="near-account">
|
||
<div className="field">
|
||
<label>Account</label>
|
||
<div className="mono address">{a.account_id}</div>
|
||
<span className="muted" style={{ fontSize: 12 }}>
|
||
Every swap runs through this one account. It is controlled only by {a.wallet}’s
|
||
post-quantum key (ML-DSA-65, derived from its phrase at {a.derivation_path}); the
|
||
same phrase finds it again anywhere.
|
||
</span>
|
||
</div>
|
||
{!a.exists && (
|
||
<div className="field">
|
||
<p className="muted">
|
||
The wallet’s sponsor creates it and pays its NEAR fees, so you never need NEAR. It
|
||
is made once: choose the wallet whose phrase will control it.
|
||
</p>
|
||
{a.can_create ? (
|
||
<div className="row">
|
||
{a.candidates.length > 1 && (
|
||
<select
|
||
className="input"
|
||
value={owner || a.wallet}
|
||
onChange={(e) => setOwner(e.target.value)}
|
||
aria-label="Controlled by"
|
||
>
|
||
{a.candidates.map((w) => (
|
||
<option key={w} value={w}>
|
||
{w}
|
||
</option>
|
||
))}
|
||
</select>
|
||
)}
|
||
<button
|
||
className="button"
|
||
type="button"
|
||
onClick={() => create.mutate(owner || a.wallet)}
|
||
disabled={create.isPending}
|
||
>
|
||
{create.isPending ? 'Creating…' : 'Create account'}
|
||
</button>
|
||
</div>
|
||
) : (
|
||
<p className="error">{a.sponsor_note ?? 'The sponsor cannot create it now.'}</p>
|
||
)}
|
||
{create.isError && <p className="error">{create.error.message}</p>}
|
||
</div>
|
||
)}
|
||
{a.exists && !a.key_on_account && (
|
||
<p className="error">
|
||
This account exists but {a.wallet}’s key is not on it, so the wallet cannot use it.
|
||
</p>
|
||
)}
|
||
{a.exists && (
|
||
<table className="table review">
|
||
<tbody>
|
||
{a.intents.length === 0 && (
|
||
<tr>
|
||
<th>Held</th>
|
||
<td className="muted">Nothing in NEAR Intents yet.</td>
|
||
</tr>
|
||
)}
|
||
{a.intents.map((b) => (
|
||
<tr key={b.token}>
|
||
<th>{b.symbol}</th>
|
||
<td className="numeral">
|
||
{formatAmount(b.amount)} {b.symbol}
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
)}
|
||
{active && !a.owner_open && (
|
||
<p className="muted">Unlock {a.wallet} to move anything in or out of it.</p>
|
||
)}
|
||
{active && <QtcBridgeLegs canSign={a.owner_open} onMoved={() => account.refetch()} />}
|
||
{active && a.owner_open && a.intents.length > 0 && (
|
||
<SwapLeg held={a.intents} onMoved={() => account.refetch()} />
|
||
)}
|
||
{active && a.owner_open && a.intents.length > 0 && (
|
||
<CashoutLeg held={a.intents} onMoved={() => account.refetch()} />
|
||
)}
|
||
<div className="row">
|
||
<button className="button" type="button" onClick={() => copyText(a.account_id)}>
|
||
Copy account
|
||
</button>
|
||
<button
|
||
className="button"
|
||
type="button"
|
||
onClick={() => account.refetch()}
|
||
disabled={account.isFetching}
|
||
>
|
||
{account.isFetching ? 'Reading…' : 'Refresh'}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
</section>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* QTC in and out of the intents account over the PoA bridge (#75). In: the
|
||
* account's permanent Quantus deposit address, paid with an ordinary send
|
||
* from any of your accounts. Out: a withdrawal to any of your open
|
||
* accounts, relayed by the sponsor; the bridge keeps 0.002 QTC.
|
||
*/
|
||
function QtcBridgeLegs({ canSign, onMoved }: { canSign: boolean; onMoved: () => void }) {
|
||
const navigate = useNavigate()
|
||
const session = useSession()
|
||
const mine =
|
||
session.data?.wallets.flatMap((w) =>
|
||
w.accounts.filter((x) => x.current_scheme).map((x) => ({ ...x, walletName: w.wallet.name })),
|
||
) ?? []
|
||
const [showDeposit, setShowDeposit] = useState(false)
|
||
const deposit = useQuery({
|
||
queryKey: ['qtc-deposit-address'],
|
||
queryFn: nearQtcDepositAddress,
|
||
enabled: showDeposit,
|
||
staleTime: Infinity,
|
||
})
|
||
const [amount, setAmount] = useState('')
|
||
const [to, setTo] = useState('')
|
||
const withdraw = useMutation({
|
||
mutationFn: () => nearWithdrawQtc(amount, to || mine[0]?.address || ''),
|
||
onSuccess: () => {
|
||
setAmount('')
|
||
// The bridge pays out on Quantus within about a minute; the balance
|
||
// here drops as soon as the withdrawal is final.
|
||
setTimeout(onMoved, 3000)
|
||
},
|
||
})
|
||
return (
|
||
<div className="field" data-testid="qtc-bridge">
|
||
<label>QTC in and out</label>
|
||
{!showDeposit ? (
|
||
<button className="button" type="button" onClick={() => setShowDeposit(true)}>
|
||
Deposit QTC
|
||
</button>
|
||
) : (
|
||
<div>
|
||
{deposit.isPending && <p className="muted">Asking the bridge…</p>}
|
||
{deposit.isError && <p className="error">{deposit.error.message}</p>}
|
||
{deposit.data && (
|
||
<>
|
||
<div className="mono address">{deposit.data}</div>
|
||
<span className="muted" style={{ fontSize: 12 }}>
|
||
The account’s own deposit address on Quantus; it does not change. QTC sent here from
|
||
any of your accounts arrives in NEAR Intents a few minutes later. At least 0.01 QTC.
|
||
</span>
|
||
<div className="row">
|
||
<button
|
||
className="button"
|
||
type="button"
|
||
onClick={() => navigate(`/send?to=${encodeURIComponent(deposit.data)}`)}
|
||
>
|
||
Send QTC here
|
||
</button>
|
||
<button className="button" type="button" onClick={() => copyText(deposit.data)}>
|
||
Copy
|
||
</button>
|
||
</div>
|
||
</>
|
||
)}
|
||
</div>
|
||
)}
|
||
{canSign && (
|
||
<>
|
||
<form
|
||
className="row"
|
||
onSubmit={(e) => {
|
||
e.preventDefault()
|
||
withdraw.mutate()
|
||
}}
|
||
>
|
||
<input
|
||
className="input numeral"
|
||
placeholder="QTC to withdraw"
|
||
value={amount}
|
||
onChange={(e) => setAmount(e.target.value)}
|
||
inputMode="decimal"
|
||
aria-label="QTC to withdraw"
|
||
/>
|
||
<select
|
||
className="input"
|
||
value={to || mine[0]?.address || ''}
|
||
onChange={(e) => setTo(e.target.value)}
|
||
aria-label="Withdraw to"
|
||
>
|
||
{mine.map((x) => (
|
||
<option key={x.address} value={x.address}>
|
||
{x.walletName} · account {(x.account ?? 0) + 1} · {x.address.slice(0, 10)}…
|
||
</option>
|
||
))}
|
||
</select>
|
||
<button className="button" type="submit" disabled={!amount || withdraw.isPending}>
|
||
{withdraw.isPending ? 'Withdrawing…' : 'Withdraw to Quantus'}
|
||
</button>
|
||
</form>
|
||
<span className="muted" style={{ fontSize: 12 }}>
|
||
The bridge keeps 0.002 QTC of each withdrawal; at least 0.01 QTC. The wallet’s sponsor
|
||
pays the NEAR fee.
|
||
</span>
|
||
</>
|
||
)}
|
||
{withdraw.isError && <p className="error">{withdraw.error.message}</p>}
|
||
{withdraw.data && (
|
||
<p className="muted">
|
||
Withdrawal relayed (NEAR transaction{' '}
|
||
<span className="mono">{withdraw.data.slice(0, 12)}…</span>). The QTC reaches Quantus in
|
||
about a minute.
|
||
</p>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** What the intents account can swap into (contract ids). */
|
||
const SWAP_TARGETS: { token: string; symbol: string }[] = [
|
||
{ token: 'qtc.omft.near', symbol: 'QTC' },
|
||
{ token: 'wrap.near', symbol: 'wNEAR' },
|
||
{
|
||
token: '17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1',
|
||
symbol: 'USDC',
|
||
},
|
||
]
|
||
|
||
/**
|
||
* A swap inside the intents account (#76): priced on a Ref pool and on
|
||
* 1Click, the better route taken, the fee shown on its own line. Rust
|
||
* prices it again when it is confirmed; nothing is signed on a stale quote.
|
||
*/
|
||
function SwapLeg({
|
||
held,
|
||
onMoved,
|
||
}: {
|
||
held: { token: string; symbol: string }[]
|
||
onMoved: () => void
|
||
}) {
|
||
const strip = (t: string) => t.replace(/^nep141:/, '')
|
||
const [from, setFrom] = useState(strip(held[0]?.token ?? ''))
|
||
const targets = SWAP_TARGETS.filter((t) => t.token !== from)
|
||
const [to, setTo] = useState('')
|
||
const target = to && to !== from ? to : (targets[0]?.token ?? '')
|
||
const [amount, setAmount] = useState('')
|
||
const quote = useMutation({ mutationFn: () => nearSwapQuote(from, target, amount) })
|
||
const swap = useMutation({
|
||
mutationFn: () => nearSwap(from, target, amount),
|
||
onSuccess: () => {
|
||
quote.reset()
|
||
setAmount('')
|
||
setTimeout(onMoved, 4000)
|
||
},
|
||
})
|
||
const q = quote.data
|
||
return (
|
||
<div className="field" data-testid="intents-swap">
|
||
<label>Swap inside NEAR Intents</label>
|
||
<form
|
||
className="row"
|
||
onSubmit={(e) => {
|
||
e.preventDefault()
|
||
swap.reset()
|
||
quote.mutate()
|
||
}}
|
||
>
|
||
<input
|
||
className="input numeral"
|
||
placeholder="amount"
|
||
value={amount}
|
||
onChange={(e) => {
|
||
setAmount(e.target.value)
|
||
quote.reset()
|
||
}}
|
||
inputMode="decimal"
|
||
aria-label="Amount to swap"
|
||
/>
|
||
<select
|
||
className="input"
|
||
value={from}
|
||
onChange={(e) => {
|
||
setFrom(e.target.value)
|
||
quote.reset()
|
||
}}
|
||
aria-label="Swap from"
|
||
>
|
||
{held.map((h) => (
|
||
<option key={h.token} value={strip(h.token)}>
|
||
{h.symbol}
|
||
</option>
|
||
))}
|
||
</select>
|
||
<span className="muted">→</span>
|
||
<select
|
||
className="input"
|
||
value={target}
|
||
onChange={(e) => {
|
||
setTo(e.target.value)
|
||
quote.reset()
|
||
}}
|
||
aria-label="Swap to"
|
||
>
|
||
{targets.map((t) => (
|
||
<option key={t.token} value={t.token}>
|
||
{t.symbol}
|
||
</option>
|
||
))}
|
||
</select>
|
||
<button className="button" type="submit" disabled={!amount || quote.isPending}>
|
||
{quote.isPending ? 'Pricing…' : 'Quote'}
|
||
</button>
|
||
</form>
|
||
{quote.isError && <p className="error">{quote.error.message}</p>}
|
||
{q && (
|
||
<>
|
||
<table className="table review">
|
||
<tbody>
|
||
<tr>
|
||
<th>Route</th>
|
||
<td>{q.venue}</td>
|
||
</tr>
|
||
<tr>
|
||
<th>You swap</th>
|
||
<td className="numeral">
|
||
{formatAmount(q.amount_in)} {q.symbol_in}
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<th>Fee</th>
|
||
<td className="numeral">
|
||
{q.route === 'pool' ? `${formatAmount(q.fee)} ${q.symbol_in} ` : ''}(
|
||
{(q.fee_bps / 100).toFixed(2)}%)
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<th>You get</th>
|
||
<td className="numeral">
|
||
{formatAmount(q.amount_out)} {q.symbol_out}
|
||
<span className="muted">
|
||
{' '}
|
||
at least {formatAmount(q.min_out)}
|
||
{q.alternative_out && ` · the other route: ${formatAmount(q.alternative_out)}`}
|
||
</span>
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<button
|
||
className="button"
|
||
type="button"
|
||
onClick={() => swap.mutate()}
|
||
disabled={swap.isPending}
|
||
>
|
||
{swap.isPending ? 'Swapping…' : `Swap ${q.symbol_in} for ${q.symbol_out}`}
|
||
</button>
|
||
</>
|
||
)}
|
||
{swap.isError && <p className="error">{swap.error.message}</p>}
|
||
{swap.data && (
|
||
<p className="muted">
|
||
Swap relayed (NEAR transaction <span className="mono">{swap.data.slice(0, 12)}…</span>
|
||
). The result lands in this account within seconds.
|
||
</p>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* Cash-out (#77): a token in the intents account paid to your address on
|
||
* another chain through 1Click. The address is checked by the destination
|
||
* chain's form on the Rust side before anything is priced; a payout sent
|
||
* to a wrong address cannot be refunded.
|
||
*/
|
||
function CashoutLeg({
|
||
held,
|
||
onMoved,
|
||
}: {
|
||
held: { token: string; symbol: string }[]
|
||
onMoved: () => void
|
||
}) {
|
||
const strip = (t: string) => t.replace(/^nep141:/, '')
|
||
const destinations = useQuery({
|
||
queryKey: ['cashout-destinations'],
|
||
queryFn: nearCashoutDestinations,
|
||
staleTime: Infinity,
|
||
})
|
||
const [from, setFrom] = useState(strip(held[0]?.token ?? ''))
|
||
const [dest, setDest] = useState('')
|
||
const destination = dest || destinations.data?.[0]?.id || ''
|
||
const [amount, setAmount] = useState('')
|
||
const [payTo, setPayTo] = useState('')
|
||
const quote = useMutation({
|
||
mutationFn: () => nearCashoutQuote(from, destination, amount, payTo),
|
||
})
|
||
const cashout = useMutation({
|
||
mutationFn: () => nearCashout(from, destination, amount, payTo),
|
||
onSuccess: () => {
|
||
quote.reset()
|
||
setAmount('')
|
||
setTimeout(onMoved, 4000)
|
||
},
|
||
})
|
||
const q = quote.data
|
||
return (
|
||
<div className="field" data-testid="intents-cashout">
|
||
<label>Cash out to another chain</label>
|
||
<form
|
||
className="row"
|
||
onSubmit={(e) => {
|
||
e.preventDefault()
|
||
cashout.reset()
|
||
quote.mutate()
|
||
}}
|
||
>
|
||
<input
|
||
className="input numeral"
|
||
placeholder="amount"
|
||
value={amount}
|
||
onChange={(e) => {
|
||
setAmount(e.target.value)
|
||
quote.reset()
|
||
}}
|
||
inputMode="decimal"
|
||
aria-label="Amount to cash out"
|
||
/>
|
||
<select
|
||
className="input"
|
||
value={from}
|
||
onChange={(e) => {
|
||
setFrom(e.target.value)
|
||
quote.reset()
|
||
}}
|
||
aria-label="Cash out from"
|
||
>
|
||
{held.map((h) => (
|
||
<option key={h.token} value={strip(h.token)}>
|
||
{h.symbol}
|
||
</option>
|
||
))}
|
||
</select>
|
||
<span className="muted">→</span>
|
||
<select
|
||
className="input"
|
||
value={destination}
|
||
onChange={(e) => {
|
||
setDest(e.target.value)
|
||
quote.reset()
|
||
}}
|
||
aria-label="Cash out as"
|
||
>
|
||
{destinations.data?.map((d) => (
|
||
<option key={d.id} value={d.id}>
|
||
{d.symbol} on {d.chain}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</form>
|
||
<div className="row">
|
||
<input
|
||
className="input mono"
|
||
placeholder="your address on that chain"
|
||
value={payTo}
|
||
onChange={(e) => {
|
||
setPayTo(e.target.value)
|
||
quote.reset()
|
||
}}
|
||
aria-label="Pay to"
|
||
style={{ flex: 1 }}
|
||
/>
|
||
<button
|
||
className="button"
|
||
type="button"
|
||
onClick={() => {
|
||
cashout.reset()
|
||
quote.mutate()
|
||
}}
|
||
disabled={!amount || !payTo || quote.isPending}
|
||
>
|
||
{quote.isPending ? 'Pricing…' : 'Quote'}
|
||
</button>
|
||
</div>
|
||
{quote.isError && <p className="error">{quote.error.message}</p>}
|
||
{q && (
|
||
<>
|
||
<table className="table review">
|
||
<tbody>
|
||
<tr>
|
||
<th>Route</th>
|
||
<td>{q.venue}</td>
|
||
</tr>
|
||
<tr>
|
||
<th>You send</th>
|
||
<td className="numeral">
|
||
{formatAmount(q.amount_in)} {q.symbol_in}
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<th>Fee</th>
|
||
<td className="numeral">{(q.fee_bps / 100).toFixed(2)}%, inside the quote</td>
|
||
</tr>
|
||
<tr>
|
||
<th>Paid to</th>
|
||
<td className="mono address">{payTo.trim()}</td>
|
||
</tr>
|
||
<tr>
|
||
<th>You get</th>
|
||
<td className="numeral">
|
||
{formatAmount(q.amount_out)} {q.symbol_out}
|
||
<span className="muted"> at least {formatAmount(q.min_out)}</span>
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<button
|
||
className="button"
|
||
type="button"
|
||
onClick={() => cashout.mutate()}
|
||
disabled={cashout.isPending}
|
||
>
|
||
{cashout.isPending ? 'Sending…' : `Cash out to ${q.symbol_out}`}
|
||
</button>
|
||
</>
|
||
)}
|
||
{cashout.isError && <p className="error">{cashout.error.message}</p>}
|
||
{cashout.data && (
|
||
<p className="muted">
|
||
Cash-out relayed (NEAR transaction{' '}
|
||
<span className="mono">{cashout.data.slice(0, 12)}…</span>). 1Click pays out once it sees
|
||
the transfer, usually within minutes.
|
||
</p>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|