Skip to content

Commit 0b2610d

Browse files
committed
Update useQuotes: use v2 endpoint
1 parent a6eca86 commit 0b2610d

File tree

11 files changed

+317
-175
lines changed

11 files changed

+317
-175
lines changed

src/shared/types/Quote.ts

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
interface Exchange {
2-
icon_url: string | null;
3-
share: number;
4-
}
5-
6-
interface ExchangeWithName extends Exchange {
7-
name: string;
8-
}
9-
10-
interface ContractMetadata {
1+
export interface ContractMetadata {
112
id: string;
123
name: string;
134
icon_url: string;
5+
explorer: {
6+
name: string;
7+
tx_url: string;
8+
};
149
}
1510

1611
export interface TransactionDescription {
@@ -22,28 +17,57 @@ export interface TransactionDescription {
2217
chain_id: string;
2318
}
2419

20+
export type QuotedTransaction = Omit<
21+
TransactionDescription,
22+
'chain_id' | 'gas'
23+
> & {
24+
chainId: string;
25+
gasLimit: string;
26+
};
27+
2528
export interface Quote {
26-
input_amount_estimation: string;
27-
input_token_address: string;
29+
contract_metadata: ContractMetadata | null;
30+
slippage_type?: 'normal' | 'zero-slippage';
31+
2832
input_chain: string;
29-
output_amount_estimation: string;
30-
output_token_address: string;
33+
input_asset_id: string;
34+
input_token_address: string;
35+
input_token_id: string | null;
36+
input_amount_estimation: string;
37+
input_amount_max: string;
38+
3139
output_chain: string;
40+
output_asset_id: string;
41+
output_token_address: string;
42+
output_token_id: string | null;
43+
output_amount_estimation: string;
44+
3245
guaranteed_output_amount: string;
46+
output_amount_min: string;
47+
3348
token_spender: string;
34-
exchanges: ExchangeWithName[] | null;
35-
contract_metadata: ContractMetadata | null;
36-
gas_estimation: string | null;
49+
50+
gas_estimation: number | null;
51+
seconds_estimation: number | null;
52+
53+
transaction: TransactionDescription | null;
54+
3755
enough_allowance?: boolean;
3856
enough_balance: boolean;
39-
slippage_type?: 'normal' | 'zero-slippage';
40-
estimated_seconds?: string | null;
57+
4158
base_protocol_fee: number;
42-
marketplace_fee: number;
59+
4360
protocol_fee: number;
4461
protocol_fee_amount: string;
62+
protocol_fee_asset_id: string | null;
63+
protocol_fee_asset_address: string | null;
64+
protocol_fee_taken_on_top: boolean | null;
65+
66+
marketplace_fee: number;
67+
marketplace_fee_amount: number;
68+
4569
bridge_fee_asset_id: string | null;
70+
bridge_fee_asset_address: string | null;
4671
bridge_fee_taken_on_top: boolean | null;
4772
bridge_fee_amount: string;
48-
transaction: TransactionDescription | null;
4973
}

src/ui/pages/BridgeForm/BridgeForm.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ import {
3131
getChainWithMostAssetValue,
3232
useSearchParamsState,
3333
} from '@zeriontech/transactions';
34-
import type { QuotesData } from 'src/ui/shared/requests/useQuotes';
35-
import { useQuotes } from 'src/ui/shared/requests/useQuotes';
3634
import { HStack } from 'src/ui/ui-kit/HStack';
3735
import { UnstyledLink } from 'src/ui/ui-kit/UnstyledLink';
3836
import { WalletAvatar } from 'src/ui/components/WalletAvatar';
@@ -85,6 +83,8 @@ import { getGas } from 'src/modules/ethereum/transactions/getGas';
8583
import type { ZerionApiClient } from 'src/modules/zerion-api/zerion-api-bare';
8684
import { useTxEligibility } from 'src/ui/shared/requests/useTxEligibility';
8785
import { useGasbackEstimation } from 'src/modules/ethereum/account-abstraction/rewards';
86+
import type { QuotesData } from 'src/ui/shared/requests/useQuotes';
87+
import { getQuoteTx, useQuotes } from 'src/ui/shared/requests/useQuotes';
8888
import {
8989
DEFAULT_CONFIGURATION,
9090
applyConfiguration,
@@ -433,13 +433,16 @@ function BridgeFormComponent() {
433433
receivePosition,
434434
});
435435

436-
const {
437-
transaction: bridgeTransaction,
438-
quote,
439-
refetch: refetchQuotes,
440-
} = quotesData;
436+
const { refetch: refetchQuotes } = quotesData;
441437

442-
const selectedQuote = quote;
438+
const defaultQuote = quotesData.quotes?.[0] ?? null;
439+
// TODO: add support for quote selection, useState
440+
const selectedQuote = defaultQuote;
441+
442+
const bridgeTransaction = useMemo(
443+
() => (selectedQuote ? getQuoteTx(selectedQuote) : null),
444+
[selectedQuote]
445+
);
443446

444447
const reverseChains = useCallback(
445448
() =>
@@ -767,6 +770,7 @@ function BridgeFormComponent() {
767770
return (
768771
<SuccessState
769772
hash={transactionHash}
773+
explorer={selectedQuote?.contract_metadata?.explorer ?? null}
770774
spendPosition={spendPosition}
771775
receivePosition={receivePosition}
772776
formState={snapshotRef.current}

src/ui/pages/BridgeForm/BridgeLine/BridgeLine.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { getCommonQuantity } from 'src/modules/networks/asset';
66
import type { Quote } from 'src/shared/types/Quote';
77
import { formatSeconds } from 'src/shared/units/formatSeconds';
88
import { SlidingRectangle } from 'src/ui/components/SlidingRectangle';
9-
import type { QuotesData } from 'src/ui/shared/requests/useQuotes';
109
import { noValueDash } from 'src/ui/shared/typography';
1110
import { HStack } from 'src/ui/ui-kit/HStack';
1211
import { UIText } from 'src/ui/ui-kit/UIText';
1312
import { TokenIcon } from 'src/ui/ui-kit/TokenIcon';
1413
import { formatCurrencyValue } from 'src/shared/units/formatCurrencyValue';
14+
import type { QuotesData } from 'src/ui/shared/requests/useQuotes';
1515
import { getQuotesErrorMessage } from '../../SwapForm/Quotes/getQuotesErrorMessage';
1616

1717
function getFeePriceValue({
@@ -126,8 +126,8 @@ export function BridgeLine({
126126

127127
<HStack gap={4} alignItems="center">
128128
<UIText color="var(--primary)" kind="small/accent">
129-
{quote?.estimated_seconds
130-
? `~${formatSeconds(Number(quote.estimated_seconds))}`
129+
{quote?.seconds_estimation
130+
? `~${formatSeconds(Number(quote.seconds_estimation))}`
131131
: noValueDash}
132132
</UIText>
133133
<UIText color="var(--primary)" kind="small/accent">

src/ui/pages/BridgeForm/SuccessState/SuccessState.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@ import { useActionStatusByHash } from 'src/ui/shared/forms/SuccessState/useActio
1111
import { NavigationTitle } from 'src/ui/components/NavigationTitle';
1212
import { GasbackDecorated } from 'src/ui/components/GasbackDecorated';
1313
import type { BareAddressPosition } from 'src/shared/types/BareAddressPosition';
14+
import type { ContractMetadata } from 'src/shared/types/Quote';
1415
import type { BridgeFormState } from '../shared/types';
1516

1617
export function SuccessState({
1718
formState,
1819
spendPosition,
1920
receivePosition,
2021
hash,
22+
explorer,
2123
onDone,
2224
gasbackValue,
2325
}: {
2426
formState: BridgeFormState;
2527
spendPosition: BareAddressPosition;
2628
receivePosition: BareAddressPosition;
2729
hash: string;
30+
explorer: ContractMetadata['explorer'] | null;
2831
gasbackValue: number | null;
2932
onDone: () => void;
3033
}) {
@@ -57,6 +60,11 @@ export function SuccessState({
5760
const receiveChainName = networks.getChainName(receiveChain);
5861
const receiveChainIconUrl = networks.getNetworkByName(receiveChain)?.icon_url;
5962

63+
const explorerFallbackUrl = hash
64+
? networks.getExplorerTxUrlByName(spendChain, hash)
65+
: undefined;
66+
const explorerUrl = explorer?.tx_url.replace('{HASH}', hash);
67+
6068
return (
6169
<>
6270
<NavigationTitle urlBar="none" title="Bridge Success" />
@@ -81,9 +89,7 @@ export function SuccessState({
8189
pendingTitle="Transferring"
8290
failedTitle="Transfer failed"
8391
dropppedTitle="Transfer cancelled"
84-
explorerUrl={
85-
hash ? networks.getExplorerTxUrlByName(spendChain, hash) : undefined
86-
}
92+
explorerUrl={explorerUrl ?? explorerFallbackUrl}
8793
confirmedContent={
8894
gasbackValue && FEATURE_GASBACK ? (
8995
<GasbackDecorated value={gasbackValue} />

src/ui/pages/BridgeForm/ZerionFeeLine/ZerionFeeLine.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ export function ZerionFeeLine({ quote }: { quote: Quote | null }) {
3737
target="_blank"
3838
>
3939
<QuestionIcon
40-
role="decoration"
40+
role="presentation"
4141
style={{
4242
width: 20,
4343
height: 20,
4444
display: 'block',
45-
color: 'var(--neutral-600)',
45+
color: 'var(--neutral-500)',
4646
}}
4747
/>
4848
</UnstyledAnchor>

src/ui/pages/BridgeForm/shared/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ export type BridgeFormState = {
77

88
spendTokenInput?: string;
99
receiveTokenInput?: string;
10+
11+
receiverAddressInput?: string | null;
12+
showReceiverAddressInput?: boolean;
1013
};

src/ui/pages/SendTransaction/TransactionConfiguration/TransactionConfiguration.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export function GasbackHint() {
4040
<QuestionHintIcon
4141
role="presentation"
4242
style={{
43-
width: 16,
44-
height: 16,
43+
width: 20,
44+
height: 20,
4545
color: 'var(--neutral-500)',
4646
display: 'block',
4747
}}

src/ui/pages/SwapForm/Quotes/RateLine.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { getDecimals } from 'src/modules/networks/asset';
88
import type { Chain } from 'src/modules/networks/Chain';
99
import { createChain } from 'src/modules/networks/Chain';
1010
import { formatTokenValue } from 'src/shared/units/formatTokenValue';
11-
import type { QuotesData } from 'src/ui/shared/requests/useQuotes';
1211
import { SlidingRectangle } from 'src/ui/components/SlidingRectangle';
12+
import type { QuotesData } from 'src/ui/shared/requests/useQuotes';
13+
import type { Quote } from 'src/shared/types/Quote';
1314
import { getQuotesErrorMessage } from './getQuotesErrorMessage';
1415

1516
function getRate({
@@ -66,19 +67,21 @@ export function RateLine({
6667
spendAsset,
6768
receiveAsset,
6869
quotesData,
70+
selectedQuote,
6971
}: {
7072
spendAsset: Asset | null;
7173
receiveAsset: Asset | null;
7274
quotesData: QuotesData;
75+
selectedQuote: Quote | null;
7376
}) {
74-
const { isLoading, quote, error } = quotesData;
77+
const { isLoading, error } = quotesData;
7578

7679
const rate = getRate({
7780
spendAsset,
7881
receiveAsset,
79-
receiveAmountBase: quote?.output_amount_estimation,
80-
spendAmountBase: quote?.input_amount_estimation,
81-
chain: quote ? createChain(quote.input_chain) : undefined,
82+
receiveAmountBase: selectedQuote?.output_amount_estimation,
83+
spendAmountBase: selectedQuote?.input_amount_estimation,
84+
chain: selectedQuote ? createChain(selectedQuote.input_chain) : undefined,
8285
});
8386

8487
// If we decide to _not_ circle the images,
@@ -92,18 +95,19 @@ export function RateLine({
9295
gap={8}
9396
justifyContent="space-between"
9497
style={{
95-
visibility: !isLoading && !quote && !error ? 'hidden' : undefined,
98+
visibility:
99+
!isLoading && !selectedQuote && !error ? 'hidden' : undefined,
96100
}}
97101
>
98102
<UIText kind="small/regular" color="var(--neutral-700)">
99103
Rate
100104
</UIText>
101105
<span>
102-
{isLoading && !quote ? (
106+
{isLoading && !selectedQuote ? (
103107
<span style={{ color: 'var(--neutral-500)' }}>
104108
Fetching offers...
105109
</span>
106-
) : quote ? (
110+
) : selectedQuote ? (
107111
<HStack
108112
// in design it's 4, but design has circle images
109113
gap={gap}
@@ -113,7 +117,7 @@ export function RateLine({
113117
fontVariantNumeric: 'tabular-nums',
114118
}}
115119
>
116-
{quote.contract_metadata?.icon_url ? (
120+
{selectedQuote.contract_metadata?.icon_url ? (
117121
<div
118122
style={{
119123
position: 'relative',
@@ -124,10 +128,10 @@ export function RateLine({
124128
>
125129
<SlidingRectangle
126130
size={20}
127-
src={quote.contract_metadata.icon_url}
131+
src={selectedQuote.contract_metadata.icon_url}
128132
render={(src, index) => (
129133
<img
130-
title={quote.contract_metadata?.name}
134+
title={selectedQuote.contract_metadata?.name}
131135
style={{
132136
position: 'absolute',
133137
left: 0,
@@ -139,7 +143,7 @@ export function RateLine({
139143
}}
140144
src={src}
141145
// The alt here may be from a sibling image, but hopefully it doesn't matter
142-
alt={`${quote.contract_metadata?.name} logo`}
146+
alt={`${selectedQuote.contract_metadata?.name} logo`}
143147
/>
144148
)}
145149
/>
@@ -151,7 +155,7 @@ export function RateLine({
151155
rate.rightAsset.symbol
152156
)}`
153157
) : (
154-
<span>{quote.contract_metadata?.name ?? 'unknown'}</span>
158+
<span>{selectedQuote.contract_metadata?.name ?? 'unknown'}</span>
155159
)}
156160
</HStack>
157161
) : error ? (
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { getError } from 'src/shared/errors/getError';
2-
import type { QuotesData } from 'src/ui/shared/requests/useQuotes';
32

43
const quoteApiErrors: Record<string, string | undefined> = {
54
'Cannot process input_amount parameter': 'Adjust amount value',
65
'Cannot process output_amount parameter': 'Adjust amount value',
76
};
87

9-
export function getQuotesErrorMessage(quotesData: QuotesData) {
10-
const quotesMessage = getError(quotesData.error).message;
8+
export function getQuotesErrorMessage(error: Error | unknown) {
9+
const quotesMessage = getError(error).message;
1110
return quoteApiErrors[quotesMessage] || quotesMessage;
1211
}

0 commit comments

Comments
 (0)