-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathConnected.tsx
More file actions
564 lines (494 loc) · 19.6 KB
/
Connected.tsx
File metadata and controls
564 lines (494 loc) · 19.6 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import type { CheckoutSettings } from '@0xsequence/checkout'
import {
ContractVerificationStatus,
signEthAuthProof,
useOpenConnectModal,
useStorage,
useWaasFeeOptions,
useWallets,
validateEthProof
} from '@0xsequence/connect'
import { Button, Card, cn, Select, Text } from '@0xsequence/design-system'
import { useIndexerClient } from '@0xsequence/hooks'
import { allNetworks, ChainId } from '@0xsequence/network'
import { useOpenWalletModal } from '@0xsequence/wallet-widget'
import { CardButton, Header, WalletListItem } from 'example-shared-components'
import { useEffect, useState, type ComponentProps } from 'react'
import { encodeFunctionData, formatUnits, parseAbi, parseUnits } from 'viem'
import { useAccount, useChainId, usePublicClient, useSendTransaction, useWalletClient, useWriteContract } from 'wagmi'
import { isDebugMode, sponsoredContractAddresses } from '../../config'
import { messageToSign } from '@/constants'
import { abi } from '@/constants/nft-abi'
export const Connected = () => {
const { address } = useAccount()
const { setOpenConnectModal } = useOpenConnectModal()
const { setOpenWalletModal } = useOpenWalletModal()
const { data: walletClient } = useWalletClient()
const storage = useStorage()
const { wallets, setActiveWallet, disconnectWallet } = useWallets()
const onClickConnect = () => {
setOpenConnectModal(true)
}
const { data: txnData, sendTransaction, isPending: isPendingSendTxn, error: sendTransactionError } = useSendTransaction()
const { data: txnData2, isPending: isPendingMintTxn, writeContract } = useWriteContract()
const {
data: txnData3,
sendTransaction: sendUnsponsoredTransaction,
isPending: isPendingSendUnsponsoredTxn,
error: sendUnsponsoredTransactionError
} = useSendTransaction()
const [isSigningMessage, setIsSigningMessage] = useState(false)
const [isMessageValid, setIsMessageValid] = useState<boolean | undefined>()
const [messageSig, setMessageSig] = useState<string | undefined>()
const [lastTxnDataHash, setLastTxnDataHash] = useState<string | undefined>()
const [lastTxnDataHash2, setLastTxnDataHash2] = useState<string | undefined>()
const [lastTxnDataHash3, setLastTxnDataHash3] = useState<string | undefined>()
const [pendingFeeOptionConfirmation, confirmPendingFeeOption] = useWaasFeeOptions()
const [selectedFeeOptionTokenName, setSelectedFeeOptionTokenName] = useState<string | undefined>()
useEffect(() => {
if (pendingFeeOptionConfirmation) {
setSelectedFeeOptionTokenName(pendingFeeOptionConfirmation.options[0].token.name)
}
}, [pendingFeeOptionConfirmation])
useEffect(() => {
if (sendTransactionError) {
if (sendTransactionError instanceof Error) {
console.error(sendTransactionError.cause)
} else {
console.error(sendTransactionError)
}
}
if (sendUnsponsoredTransactionError) {
if (sendUnsponsoredTransactionError instanceof Error) {
console.error(sendUnsponsoredTransactionError.cause)
} else {
console.error(sendUnsponsoredTransactionError)
}
}
return
}, [sendTransactionError, sendUnsponsoredTransactionError])
const chainId = useChainId()
const indexerClient = useIndexerClient(chainId)
const [feeOptionBalances, setFeeOptionBalances] = useState<{ tokenName: string; decimals: number; balance: string }[]>([])
const [feeOptionAlert, setFeeOptionAlert] = useState<AlertProps | undefined>(undefined)
useEffect(() => {
if (pendingFeeOptionConfirmation) {
checkTokenBalancesForFeeOptions()
}
}, [pendingFeeOptionConfirmation])
const checkTokenBalancesForFeeOptions = async () => {
if (pendingFeeOptionConfirmation) {
const [account] = await walletClient!.getAddresses()
const nativeTokenBalance = await indexerClient.getNativeTokenBalance({ accountAddress: account })
const tokenBalances = await indexerClient.getTokenBalancesSummary({
filter: {
accountAddresses: [account],
contractStatus: ContractVerificationStatus.ALL,
omitNativeBalances: true
}
})
const balances = pendingFeeOptionConfirmation.options.map(option => {
if (option.token.contractAddress === null) {
return {
tokenName: option.token.name,
decimals: option.token.decimals || 0,
balance: nativeTokenBalance.balance.balance
}
}
return {
tokenName: option.token.name,
decimals: option.token.decimals || 0,
balance:
tokenBalances.balances.find(b => b.contractAddress.toLowerCase() === option.token.contractAddress!.toLowerCase())
?.balance || '0'
}
})
setFeeOptionBalances(balances)
}
}
const networkForCurrentChainId = allNetworks.find(n => n.chainId === chainId)!
const publicClient = usePublicClient({ chainId })
const generateEthAuthProof = async () => {
if (!walletClient || !publicClient || !storage) {
return
}
try {
const proof = await signEthAuthProof(walletClient, storage)
console.log('proof:', proof)
const isValid = await validateEthProof(walletClient, publicClient, proof)
console.log('isValid?:', isValid)
} catch (e) {
console.error(e)
}
}
useEffect(() => {
if (txnData) {
setLastTxnDataHash((txnData as any).hash ?? txnData)
}
if (txnData2) {
setLastTxnDataHash2((txnData2 as any).hash ?? txnData2)
}
if (txnData3) {
setLastTxnDataHash3((txnData3 as any).hash ?? txnData3)
}
}, [txnData, txnData2, txnData3])
const signMessage = async () => {
if (!walletClient || !publicClient) {
return
}
setIsSigningMessage(true)
try {
const message = messageToSign
// sign
const sig = await walletClient.signMessage({
account: address || ('' as `0x${string}`),
message
})
console.log('address', address)
console.log('signature:', sig)
console.log('chainId in homepage', chainId)
const [account] = await walletClient.getAddresses()
const isValid = await publicClient.verifyMessage({
address: account,
message,
signature: sig
})
setIsSigningMessage(false)
setIsMessageValid(isValid)
setMessageSig(sig)
console.log('isValid?', isValid)
} catch (e) {
setIsSigningMessage(false)
if (e instanceof Error) {
console.error(e.cause)
} else {
console.error(e)
}
}
}
const runSendTransaction = async () => {
if (!walletClient) {
return
}
if (networkForCurrentChainId.testnet) {
const [account] = await walletClient.getAddresses()
sendTransaction({ to: account, value: BigInt(0), gas: null })
} else {
const sponsoredContractAddress = sponsoredContractAddresses[chainId]
const data = encodeFunctionData({ abi: parseAbi(['function demo()']), functionName: 'demo', args: [] })
sendTransaction({
to: sponsoredContractAddress,
data,
gas: null
})
}
}
const runSendUnsponsoredTransaction = async () => {
if (!walletClient) {
return
}
const [account] = await walletClient.getAddresses()
sendUnsponsoredTransaction({ to: account, value: BigInt(0), gas: null })
}
const runMintNFT = async () => {
if (!walletClient) {
return
}
const [account] = await walletClient.getAddresses()
writeContract({
address: '0x0d402C63cAe0200F0723B3e6fa0914627a48462E',
abi,
functionName: 'awardItem',
args: [account, 'https://dev-metadata.sequence.app/projects/277/collections/62/tokens/0.json']
})
}
useEffect(() => {
setLastTxnDataHash(undefined)
setLastTxnDataHash2(undefined)
setLastTxnDataHash3(undefined)
setIsMessageValid(undefined)
}, [chainId])
return (
<>
<Header />
<div className="flex px-4 flex-col justify-center items-center" style={{ margin: '140px 0' }}>
<div className="flex flex-col gap-4">
<div className="flex my-3 flex-col gap-2">
<Text fontWeight="semibold" variant="small" color="muted">
Connected Wallets
</Text>
<div className="flex flex-col gap-2 p-2">
{[...wallets]
.sort((a, b) => {
// Sort embedded wallet to the top
if (a.isEmbedded && !b.isEmbedded) {
return -1
}
if (!a.isEmbedded && b.isEmbedded) {
return 1
}
return 0
})
.map(wallet => (
<WalletListItem
key={wallet.id}
id={wallet.id}
name={wallet.name}
address={wallet.address}
isActive={wallet.isActive}
isEmbedded={wallet.isEmbedded}
onSelect={() => setActiveWallet(wallet.address)}
onDisconnect={() => disconnectWallet(wallet.address)}
/>
))}
</div>
</div>
<div className="flex gap-2 flex-row items-center justify-center">
<Button shape="square" onClick={onClickConnect} variant="feature" size="sm" label="Connect another wallet" />
</div>
<div className="flex flex-col gap-2">
<Text variant="small" color="muted" fontWeight="medium">
Demos
</Text>
<CardButton title="Inventory" description="View all tokens in your wallet" onClick={() => setOpenWalletModal(true)} />
{/* <CardButton
title="Checkout"
description="Checkout screen before placing a purchase on coins or collections"
onClick={onClickCheckout}
/> */}
{(sponsoredContractAddresses[chainId] || networkForCurrentChainId.testnet) && (
<CardButton
title="Send sponsored transaction"
description="Send a transaction with your wallet without paying any fees"
isPending={isPendingSendTxn}
onClick={runSendTransaction}
/>
)}
{networkForCurrentChainId.blockExplorer && lastTxnDataHash && ((txnData as any)?.chainId === chainId || txnData) && (
<Text className="ml-4" variant="small" underline color="primary" asChild>
<a
href={`${networkForCurrentChainId.blockExplorer.rootUrl}/tx/${(txnData as any).hash ?? txnData}`}
target="_blank"
rel="noreferrer"
>
View on {networkForCurrentChainId.blockExplorer.name}
</a>
</Text>
)}
{networkForCurrentChainId.testnet && (
<CardButton
title="Send unsponsored transaction"
description="Send an unsponsored transaction with your wallet"
isPending={isPendingSendUnsponsoredTxn}
onClick={runSendUnsponsoredTransaction}
/>
)}
{networkForCurrentChainId.blockExplorer &&
lastTxnDataHash3 &&
((txnData3 as any)?.chainId === chainId || txnData3) && (
<Text className="ml-4" variant="small" underline color="primary" asChild>
<a
href={`${networkForCurrentChainId.blockExplorer.rootUrl}/tx/${(txnData3 as any).hash ?? txnData3}`}
target="_blank"
rel="noreferrer"
>
View on {networkForCurrentChainId.blockExplorer.name}
</a>
</Text>
)}
<CardButton
title="Sign message"
description="Sign a message with your wallet"
onClick={signMessage}
isPending={isSigningMessage}
/>
{isMessageValid && (
<Card className="flex text-primary flex-col gap-2" style={{ width: '332px' }}>
<Text variant="medium">Signed message:</Text>
<Text>{messageToSign}</Text>
<Text variant="medium">Signature:</Text>
<Text variant="code" ellipsis asChild>
<p>{messageSig}</p>
</Text>
<Text variant="medium">
isValid: <Text variant="code">{isMessageValid.toString()}</Text>
</Text>
</Card>
)}
<CardButton
title="Mint an NFT"
description="Test minting an NFT to your wallet"
isPending={isPendingMintTxn}
onClick={runMintNFT}
/>
{networkForCurrentChainId.blockExplorer &&
lastTxnDataHash2 &&
((txnData2 as any)?.chainId === chainId || txnData2) && (
<Text className="ml-4" variant="small" underline color="primary" asChild>
<a
href={`${networkForCurrentChainId.blockExplorer.rootUrl}/tx/${(txnData2 as any).hash ?? txnData2}`}
target="_blank"
rel="noreferrer"
>
View on {networkForCurrentChainId.blockExplorer.name}
</a>
</Text>
)}
{isDebugMode && (
<CardButton title="Generate EthAuth proof" description="Generate EthAuth proof" onClick={generateEthAuthProof} />
)}
</div>
{pendingFeeOptionConfirmation && feeOptionBalances.length > 0 && (
<div className="my-3">
<Select
name="feeOption"
labelLocation="top"
label="Pick a fee option"
onValueChange={val => {
const selected = pendingFeeOptionConfirmation?.options?.find(option => option.token.name === val)
if (selected) {
setSelectedFeeOptionTokenName(selected.token.name)
setFeeOptionAlert(undefined)
}
}}
value={selectedFeeOptionTokenName}
options={
pendingFeeOptionConfirmation?.options?.map(option => ({
label: (
<div className="flex items-start flex-col">
<div className="flex flex-row">
<Text variant="xsmall">Fee (in {option.token.name}): </Text>{' '}
<Text variant="xsmall">{formatUnits(BigInt(option.value), option.token.decimals || 0)}</Text>
</div>
<div className="flex flex-row">
<Text>Wallet balance for {option.token.name}: </Text>{' '}
<Text>
{formatUnits(
BigInt(feeOptionBalances.find(b => b.tokenName === option.token.name)?.balance || '0'),
option.token.decimals || 0
)}
</Text>
</div>
</div>
),
value: option.token.name
})) || []
}
/>
<div className="flex my-2 items-center justify-center flex-col">
<Button
onClick={() => {
const selected = pendingFeeOptionConfirmation?.options?.find(
option => option.token.name === selectedFeeOptionTokenName
)
if (selected?.token.contractAddress !== undefined) {
// check if wallet has enough balance, should be balance > feeOption.value
const balance = parseUnits(
feeOptionBalances.find(b => b.tokenName === selected.token.name)?.balance || '0',
selected.token.decimals || 0
)
const feeOptionValue = parseUnits(selected.value, selected.token.decimals || 0)
if (balance && balance < feeOptionValue) {
setFeeOptionAlert({
title: 'Insufficient balance',
description: `You do not have enough balance to pay the fee with ${selected.token.name}, please make sure you have enough balance in your wallet for the selected fee option.`,
secondaryDescription: 'You can also switch network to Arbitrum Sepolia to test a gasless transaction.',
variant: 'warning'
})
return
}
confirmPendingFeeOption(pendingFeeOptionConfirmation?.id, selected.token.contractAddress)
}
}}
label="Confirm fee option"
/>
{feeOptionAlert && (
<div className="mt-3" style={{ maxWidth: '332px' }}>
<Alert
title={feeOptionAlert.title}
description={feeOptionAlert.description}
secondaryDescription={feeOptionAlert.secondaryDescription}
variant={feeOptionAlert.variant}
buttonProps={feeOptionAlert.buttonProps}
/>
</div>
)}
</div>
</div>
)}
</div>
</div>
</>
)
}
export type AlertProps = {
title: string
description: string
secondaryDescription?: string
variant: 'negative' | 'warning' | 'positive'
buttonProps?: ComponentProps<typeof Button>
children?: React.ReactNode
}
const variants = {
negative: 'bg-negative',
warning: 'bg-warning',
positive: 'bg-positive'
}
export const Alert = ({ title, description, secondaryDescription, variant, buttonProps, children }: AlertProps) => {
return (
<div className={cn('rounded-xl', variants[variant])}>
<div className="flex bg-background-overlay rounded-xl py-4 w-full flex-col gap-3">
<div className="flex w-full gap-2 justify-between">
<div className="flex flex-col gap-1">
<Text variant="normal" color="primary" fontWeight="medium">
{title}
</Text>
<Text variant="normal" color="muted" fontWeight="medium">
{description}
</Text>
{secondaryDescription && (
<Text variant="normal" color="secondary" fontWeight="medium">
{secondaryDescription}
</Text>
)}
</div>
{buttonProps ? (
<div className="rounded-lg w-min h-min">
<Button className="shrink-0" variant="emphasis" shape="square" {...buttonProps} />
</div>
) : null}
</div>
{children}
</div>
</div>
)
}
export const getCheckoutSettings = (_address?: string) => {
const checkoutSettings: CheckoutSettings = {
cryptoCheckout: {
chainId: ChainId.POLYGON,
triggerTransaction: async () => {
console.log('triggered transaction')
},
coinQuantity: {
contractAddress: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
amountRequiredRaw: '10000000000'
}
},
orderSummaryItems: [
{
chainId: ChainId.POLYGON,
contractAddress: '0x631998e91476da5b870d741192fc5cbc55f5a52e',
tokenId: '66597',
quantityRaw: '100'
},
{
chainId: ChainId.POLYGON,
contractAddress: '0x624e4fa6980afcf8ea27bfe08e2fb5979b64df1c',
tokenId: '1741',
quantityRaw: '100'
}
]
}
return checkoutSettings
}