1
+ import { Box , Button , Text } from "@interchain-ui/react" ;
2
+ import { ethers } from "ethers" ;
3
+ import { useEffect , useState } from "react" ;
4
+ import abi from './abi.json'
5
+
6
+ export default function Index ( ) {
7
+ const verifyingContract = '0xf67a42D581eB7d83135De8Dfe2fCccE58e5259bc'
8
+ const addr0 = '0x0000000000000000000000000000000000000000'
9
+ const privateKey = process . env . NEXT_PUBLIC_PRIVATE_KEY
10
+ const prc = process . env . NEXT_PUBLIC_BSC_TESTNET_RPC
11
+
12
+ const [ balance , setBalance ] = useState ( '--' )
13
+ const [ result , setResult ] = useState < object | null > ( null )
14
+ const [ provider , setProvider ] = useState < ethers . JsonRpcProvider | null > ( null )
15
+ const [ wallet , setWallet ] = useState < ethers . Wallet | null > ( null )
16
+
17
+ if ( ! privateKey ) return < Text > Please set NEXT_PUBLIC_PRIVATE_KEY in .env.local</ Text >
18
+
19
+ useEffect ( ( ) => {
20
+ if ( privateKey && prc ) {
21
+ const provider = new ethers . JsonRpcProvider ( process . env . NEXT_PUBLIC_BSC_TESTNET_RPC )
22
+ setProvider ( provider )
23
+ const wallet = new ethers . Wallet ( privateKey , provider )
24
+ setWallet ( wallet )
25
+ }
26
+ } , [ privateKey , prc ] )
27
+
28
+ const send = async ( ) => {
29
+ setResult ( null )
30
+ if ( ! window . ethereum ) {
31
+ alert ( 'Please install MetaMask' )
32
+ return
33
+ }
34
+ // wallet.signTypedData()
35
+ const tx = await wallet ! . sendTransaction ( {
36
+ to : addr0 ,
37
+ value : '1'
38
+ } )
39
+ const res = await tx . wait ( ) ;
40
+ setResult ( res )
41
+ getBalance ( )
42
+ }
43
+
44
+ const getBalance = async ( ) => {
45
+ if ( ! window . ethereum ) {
46
+ alert ( 'Please install MetaMask' )
47
+ return
48
+ }
49
+ const address = await wallet ! . getAddress ( )
50
+ console . log ( 'address' , address )
51
+ const balance = await provider ! . getBalance ( address )
52
+ setBalance ( ethers . formatEther ( balance ) )
53
+ }
54
+
55
+ const signTypedDataTest = async ( ) => {
56
+ if ( ! window . ethereum ) {
57
+ alert ( 'Please install MetaMask' )
58
+ return
59
+ }
60
+
61
+ try {
62
+ // await provider!.send("eth_requestAccounts", []);
63
+ // const wallet = await provider!.getSigner();
64
+
65
+ const domain = {
66
+ name : "ETHTransfer" ,
67
+ version : "1" ,
68
+ chainId : ( await provider ! . getNetwork ( ) ) . chainId ,
69
+ verifyingContract,
70
+ } ;
71
+
72
+ const types = {
73
+ Transfer : [
74
+ { name : "to" , type : "address" } ,
75
+ { name : "amount" , type : "uint256" }
76
+ ]
77
+ } ;
78
+
79
+ const message = {
80
+ to : addr0 ,
81
+ amount : '1'
82
+ } ;
83
+
84
+ const signature = await wallet ! . signTypedData ( domain , types , message ) ;
85
+ console . log ( "Signature:" , signature ) ;
86
+ setResult ( { signature} )
87
+ const contract = new ethers . Contract (
88
+ verifyingContract ,
89
+ abi ,
90
+ wallet
91
+ ) ;
92
+
93
+ const tx = await contract . executeTransfer (
94
+ addr0 ,
95
+ '1' ,
96
+ signature ,
97
+ { value : '1' }
98
+ ) ;
99
+
100
+ const res = await tx . wait ( ) ;
101
+
102
+ setResult ( { signature, res} )
103
+ } catch ( error :any ) {
104
+ console . error ( "Error signing typed data:" , error ) ;
105
+ alert ( `Error: ${ error . message } ` )
106
+ }
107
+ }
108
+
109
+ return < Box display = "flex" flexDirection = "column" alignItems = "center" justifyContent = "center" height = "100vh" >
110
+ < Box display = "flex" flexDirection = "row" alignItems = "center" justifyContent = "center" marginBottom = "1rem" >
111
+ < Text attributes = { { marginRight : '1rem' } } > Balance: { balance } </ Text >
112
+ < Button onClick = { getBalance } size = "sm" > Fetch</ Button >
113
+ </ Box >
114
+ < Button onClick = { send } > Send</ Button >
115
+ < Button onClick = { signTypedDataTest } attributes = { { marginTop : '1rem' } } > signTypedDataTest</ Button >
116
+ { result && < Text attributes = { { whiteSpace : 'pre-line' , padding : '2rem' } } as = "div" > { JSON . stringify ( result , null , 2 ) } </ Text > }
117
+ </ Box >
118
+ }
0 commit comments