-
Notifications
You must be signed in to change notification settings - Fork 665
Expand file tree
/
Copy pathFiatProviderSelection.tsx
More file actions
218 lines (208 loc) · 6.4 KB
/
FiatProviderSelection.tsx
File metadata and controls
218 lines (208 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"use client";
import { useMemo } from "react";
import type { ThirdwebClient } from "../../../../../client/client.js";
import type { SupportedFiatCurrency } from "../../../../../pay/convert/type.js";
import { checksumAddress } from "../../../../../utils/address.js";
import { formatNumber } from "../../../../../utils/formatNumber.js";
import { toTokens } from "../../../../../utils/units.js";
import {
iconSize,
radius,
spacing,
} from "../../../../core/design-system/index.js";
import { useBuyWithFiatQuotesForProviders } from "../../../../core/hooks/pay/useBuyWithFiatQuotesForProviders.js";
import { formatCurrencyAmount } from "../../ConnectWallet/screens/formatTokenBalance.js";
import { Container } from "../../components/basic.js";
import { Button } from "../../components/buttons.js";
import { Img } from "../../components/Img.js";
import { Spacer } from "../../components/Spacer.js";
import { Spinner } from "../../components/Spinner.js";
import { Text } from "../../components/text.js";
interface FiatProviderSelectionProps {
client: ThirdwebClient;
onProviderSelected: (provider: "coinbase" | "stripe" | "transak") => void;
toChainId: number;
toTokenAddress: string;
toAddress: string;
toAmount?: string;
currency?: SupportedFiatCurrency;
country: string | undefined;
hiddenOnrampProviders?: ("coinbase" | "stripe" | "transak")[];
}
const PROVIDERS = [
{
description: "Fast and secure payments",
iconUri: "https://i.ibb.co/LDJ3Rk2t/Frame-5.png",
id: "coinbase" as const,
name: "Coinbase",
},
{
description: "Trusted payment processing",
iconUri: "https://i.ibb.co/CpgQC2Lf/images-3.png",
id: "stripe" as const,
name: "Stripe",
},
{
description: "Global payment solution",
iconUri: "https://i.ibb.co/Xx2r882p/Transak-official-symbol-1.png",
id: "transak" as const,
name: "Transak",
},
];
export function FiatProviderSelection({
onProviderSelected,
client,
toChainId,
toTokenAddress,
toAddress,
toAmount,
currency,
country,
hiddenOnrampProviders,
}: FiatProviderSelectionProps) {
const visibleProviders = useMemo(() => {
return PROVIDERS.filter(
(provider) => !hiddenOnrampProviders?.includes(provider.id),
);
}, [hiddenOnrampProviders]);
const quoteQueries = useBuyWithFiatQuotesForProviders(
{
amount: toAmount || "0",
chainId: toChainId,
client,
currency: currency || "USD",
receiver: checksumAddress(toAddress),
tokenAddress: checksumAddress(toTokenAddress),
country,
},
visibleProviders.map((p) => p.id),
);
const quotes = useMemo(() => {
return quoteQueries.map((q) => q.data).filter((q) => !!q);
}, [quoteQueries]);
const isPending = quoteQueries.some((q) => q.isLoading);
if (quoteQueries.every((q) => q.isError)) {
return (
<Container
center="both"
flex="column"
style={{ minHeight: "200px", flexGrow: 1 }}
>
<Text color="secondaryText" size="sm">
No quotes available
</Text>
</Container>
);
}
// TODO: add a "remember my choice" checkbox
return (
<Container
flex="column"
gap="xs"
style={{
flexGrow: 1,
}}
>
{!isPending ? (
quotes
.sort((a, b) => a.currencyAmount - b.currencyAmount)
.map((quote) => {
const provider = visibleProviders.find(
(p) => p.id === quote.intent.onramp,
);
if (!provider) {
return null;
}
return (
<Button
key={provider.id}
fullWidth
onClick={() => onProviderSelected(provider.id)}
style={{
borderRadius: radius.lg,
textAlign: "left",
padding: `${spacing.md}`,
}}
variant="secondary"
>
<Container
flex="row"
gap="sm"
style={{ alignItems: "center", width: "100%" }}
>
<Img
alt={provider.name}
client={client}
height={iconSize.lg}
src={provider.iconUri}
width={iconSize.lg}
style={{
borderRadius: radius.full,
}}
/>
<Container flex="column" gap="3xs" style={{ flex: 1 }}>
<Text color="primaryText" size="md" weight={500}>
{provider.name}
</Text>
</Container>
<Container
flex="column"
gap="3xs"
style={{ alignItems: "flex-end" }}
>
<Text
color="primaryText"
size="sm"
style={{ fontWeight: 500 }}
>
{formatCurrencyAmount(
currency || "USD",
quote.currencyAmount,
)}
</Text>
<Text color="secondaryText" size="xs">
{formatNumber(
Number(
toTokens(
quote.destinationAmount,
quote.destinationToken.decimals,
),
),
4,
)}{" "}
{quote.destinationToken.symbol}
</Text>
</Container>
</Container>
</Button>
);
})
) : (
<Container
center="both"
flex="column"
style={{ flexGrow: 1, paddingBottom: spacing.lg }}
px="md"
>
<Spinner color="secondaryText" size="xl" />
<Spacer y="lg" />
<Text center color="primaryText" size="lg" weight={600} trackingTight>
Searching Providers
</Text>
<Spacer y="xs" />
<Text
center
color="secondaryText"
size="sm"
multiline
style={{
textWrap: "pretty",
}}
>
Searching for the best providers for this payment
</Text>
</Container>
)}
</Container>
);
}