Skip to content

Commit 11e701f

Browse files
committed
added crypto options
1 parent 4b98362 commit 11e701f

File tree

3 files changed

+56
-47
lines changed

3 files changed

+56
-47
lines changed

examples/react/src/components/CustomCheckout/index.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Text, Button, Spinner, NetworkImage, Image } from '@0xsequence/design-system'
2+
import React, { useMemo } from 'react'
23
import { useCheckoutUI, CreditCardProviders } from '@0xsequence/checkout'
4+
import { CryptoOption } from '@0xsequence/connect'
35
import { encodeFunctionData, toHex } from 'viem'
46
import { useAccount } from 'wagmi'
57
import { ERC_1155_SALE_CONTRACT } from '../../constants/erc1155-sale-contract'
@@ -51,7 +53,7 @@ export const CustomCheckout = () => {
5153
recipientAddress: address || '',
5254
currencyAddress,
5355
collectionAddress,
54-
creditCardProvider: 'sardine' as CreditCardProviders,
56+
creditCardProvider: 'transak' as CreditCardProviders,
5557
transakConfig: {
5658
contractId,
5759
apiKey: '5911d9ec-46b5-48fa-a755-d59a715ff0cf'
@@ -67,10 +69,6 @@ export const CustomCheckout = () => {
6769

6870
const { orderSummary, creditCardPayment, cryptoPayment } = useCheckoutUI(checkoutUIParams)
6971

70-
console.log('orderSummary', orderSummary)
71-
console.log('creditCardPayment', creditCardPayment)
72-
console.log('cryptoPayment', cryptoPayment)
73-
7472
const OrderSummary = () => {
7573
if (orderSummary.isLoading) {
7674
return <Spinner />
@@ -91,7 +89,7 @@ export const CustomCheckout = () => {
9189
width: '36px'
9290
}}
9391
>
94-
<Image src={orderSummary.data?.collectibleItem?.collectibleImageUrl} />
92+
<Image disableAnimation src={orderSummary.data?.collectibleItem?.collectibleImageUrl} />
9593
</div>
9694
<div className="flex flex-col gap-0.5">
9795
<Text variant="small" color="secondary" fontWeight="medium">
@@ -105,7 +103,7 @@ export const CustomCheckout = () => {
105103
</div>
106104
<div className="flex gap-1 flex-col">
107105
<div className="flex flex-row gap-2 items-center">
108-
<NetworkImage chainId={chainId} size="sm" />
106+
<NetworkImage disableAnimation chainId={chainId} size="sm" />
109107
<Text
110108
color="white"
111109
variant="large"
@@ -143,7 +141,36 @@ export const CustomCheckout = () => {
143141
}
144142

145143
const CryptoPayment = () => {
146-
return <div>Crypto Payment</div>
144+
if (cryptoPayment.cryptoOptions.isLoading) {
145+
return <Spinner />
146+
}
147+
148+
if (cryptoPayment.cryptoOptions.error) {
149+
return <Text color="error">Error loading crypto payment</Text>
150+
}
151+
152+
return (
153+
<div className="flex flex-col gap-2">
154+
{cryptoPayment.cryptoOptions.data.map(option => (
155+
<CryptoOption
156+
key={option.currencyAddress}
157+
currencyName={option.currencyName}
158+
chainId={option.chainId}
159+
symbol={option.symbol}
160+
price={option.totalPriceDisplay}
161+
onClick={() => {
162+
cryptoPayment.purchaseAction.setSelectedCurrencyAddress(option.currencyAddress)
163+
}}
164+
isSelected={option.isSelected}
165+
showInsufficientFundsWarning={option.isInsufficientFunds}
166+
disabled={option.isInsufficientFunds}
167+
/>
168+
))}
169+
<Button onClick={cryptoPayment.purchaseAction.action} disabled={!cryptoPayment.purchaseAction.isReady}>
170+
Purchase
171+
</Button>
172+
</div>
173+
)
147174
}
148175

149176
return (
@@ -160,7 +187,7 @@ export const CustomCheckout = () => {
160187
<Text fontSize="large" fontWeight="bold" color="primary">
161188
Credit Card Payment section
162189
</Text>
163-
{/* <CreditCardPayment /> */}
190+
<CreditCardPayment />
164191
</div>
165192
)
166193
}

packages/checkout/src/hooks/useCheckoutUI/useCreditCardPayment.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ const TransakEventListener = ({
289289
const transakIframe = transakIframeElement.contentWindow
290290

291291
const readMessage = async (message: any) => {
292+
console.log('message', message)
292293
if (message.source !== transakIframe) {
293294
return
294295
}

packages/checkout/src/hooks/useCheckoutUI/useCryptoPayment.tsx

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
1-
import { useProjectAccessKey } from '@0xsequence/connect'
2-
import {
3-
compareAddress,
4-
ContractVerificationStatus,
5-
TRANSACTION_CONFIRMATIONS_DEFAULT,
6-
formatDisplay,
7-
sendTransactions
8-
} from '@0xsequence/connect'
9-
import {
10-
useIndexerClient,
11-
useConfig,
12-
useClearCachedBalances,
13-
useGetTokenBalancesSummary,
14-
useGetSwapPrices,
15-
useGetContractInfo,
16-
useGetSwapQuote
17-
} from '@0xsequence/hooks'
18-
import { TransactionStatus } from '@0xsequence/indexer'
1+
import { compareAddress, ContractVerificationStatus, formatDisplay, sendTransactions } from '@0xsequence/connect'
2+
import { useIndexerClient, useGetTokenBalancesSummary, useGetSwapPrices, useGetSwapQuote } from '@0xsequence/hooks'
193
import { ContractInfo, TokenMetadata } from '@0xsequence/metadata'
204
import { findSupportedNetwork } from '@0xsequence/network'
21-
import pako from 'pako'
22-
import React, { useEffect, useState } from 'react'
5+
import { useState } from 'react'
236
import { Hex, encodeFunctionData, formatUnits, zeroAddress } from 'viem'
247
import { usePublicClient, useAccount, useReadContract, useWalletClient } from 'wagmi'
258

@@ -65,41 +48,31 @@ export interface UseCryptoPaymentReturn {
6548
interface CryptoOptions {
6649
chainId: number
6750
currencyAddress: string
51+
currencyName: string
6852
totalPriceRaw: string
6953
symbol: string
7054
decimals: number
7155
totalPriceDisplay: string
7256
currrencyLogoUrl?: string
73-
isBalanceSufficient: boolean
57+
isInsufficientFunds: boolean
58+
isSelected: boolean
7459
}
7560

7661
export const useCryptoPayment = ({
7762
chain,
7863
currencyAddress,
7964
totalPriceRaw,
80-
collectible,
81-
collectionAddress,
82-
recipientAddress,
8365
targetContractAddress,
8466
txData,
8567
transactionConfirmations,
8668
onSuccess,
8769
onError,
8870
currencyInfo,
89-
tokenMetadatas,
90-
dataCollectionInfo,
91-
isLoadingCollectionInfo,
92-
errorCollectionInfo,
93-
isLoadingTokenMetadatas,
94-
errorTokenMetadata,
9571
isLoadingCurrencyInfo,
9672
errorCurrencyInfo
9773
}: UseCryptoPaymentArgs): UseCryptoPaymentReturn => {
98-
const [actionInProgress, setActionInProgress] = useState(false)
99-
const [actionError, setActionError] = useState<Error | null>(null)
10074
const [selectedCurrencyAddress, setSelectedCurrencyAddress] = useState<string | undefined>(undefined)
10175
const { address: userAddress, connector } = useAccount()
102-
const { clearCachedBalances } = useClearCachedBalances()
10376
const network = findSupportedNetwork(chain)
10477
const chainId = network?.chainId || 137
10578
const isNativeCurrency = compareAddress(currencyAddress, zeroAddress)
@@ -141,7 +114,11 @@ export const useCryptoPayment = ({
141114
const buyCurrencyAddress = currencyAddress
142115
const sellCurrencyAddress = selectedCurrencyAddress || ''
143116

144-
const { data: swapPrices = [], isLoading: swapPricesIsLoading } = useGetSwapPrices({
117+
const {
118+
data: swapPrices = [],
119+
isLoading: swapPricesIsLoading,
120+
error: swapPricesError
121+
} = useGetSwapPrices({
145122
userAddress: userAddress ?? '',
146123
buyCurrencyAddress,
147124
chainId: chainId,
@@ -176,12 +153,14 @@ export const useCryptoPayment = ({
176153
const mainCurrencyOption = {
177154
chainId,
178155
currencyAddress,
156+
currencyName: currencyInfo?.name || 'unknown',
179157
totalPriceRaw: totalPriceRaw,
180158
decimals: currencyDecimals || 18,
181159
totalPriceDisplay: priceDisplay,
182160
currrencyLogoUrl: currencyInfo?.logoURI,
183161
symbol: currencySymbol || '',
184-
isBalanceSufficient: Number(mainCurrencyBalance) > Number(totalPriceRaw)
162+
isInsufficientFunds: Number(mainCurrencyBalance) < Number(totalPriceRaw),
163+
isSelected: compareAddress(currencyAddress, selectedCurrencyAddress || '')
185164
}
186165

187166
const swapOptions = swapPrices.map(swapPrice => {
@@ -197,13 +176,15 @@ export const useCryptoPayment = ({
197176
return {
198177
chainId,
199178
currencyAddress: swapQuoteAddress,
179+
currencyName: swapPrice.info?.name || 'unknown',
200180
totalPriceRaw: swapPrice.price.price,
201181
totalPriceDisplay: swapQuotePriceDisplay,
202182
currrencyLogoUrl: swapPrice.info?.logoURI,
203183
symbol: swapPrice.info?.symbol || '',
204184
decimals: swapPrice.info?.decimals || 18,
205185
// The balance check is done at the API level
206-
isBalanceSufficient: false
186+
isInsufficientFunds: false,
187+
isSelected: compareAddress(swapQuoteAddress, selectedCurrencyAddress || '')
207188
}
208189
})
209190

@@ -370,8 +351,8 @@ export const useCryptoPayment = ({
370351
return {
371352
cryptoOptions: {
372353
data: [mainCurrencyOption, ...swapOptions],
373-
isLoading: isLoadingCurrencyInfo || isLoadingTokenMetadatas || isLoadingCollectionInfo,
374-
error: errorCurrencyInfo || errorTokenMetadata || errorCollectionInfo
354+
isLoading: isLoadingCurrencyInfo || swapPricesIsLoading,
355+
error: errorCurrencyInfo || swapPricesError
375356
},
376357
purchaseAction: {
377358
action: purchaseAction,

0 commit comments

Comments
 (0)