1
1
import { setupCoin98Wallet } from "@near-wallet-selector/coin98-wallet" ;
2
- import type { AccountState , WalletSelector } from "@near-wallet-selector/core" ;
2
+ import type {
3
+ AccountState ,
4
+ InjectedWalletBehaviour ,
5
+ WalletSelector ,
6
+ } from "@near-wallet-selector/core" ;
3
7
import { setupWalletSelector } from "@near-wallet-selector/core" ;
4
8
import { setupHereWallet } from "@near-wallet-selector/here-wallet" ;
5
9
import { setupMathWallet } from "@near-wallet-selector/math-wallet" ;
@@ -23,6 +27,7 @@ import { setupNearMobileWallet } from "@near-wallet-selector/near-mobile-wallet"
23
27
import { setupMintbaseWallet } from "@near-wallet-selector/mintbase-wallet" ;
24
28
import { setupBitteWallet } from "@near-wallet-selector/bitte-wallet" ;
25
29
import { setupOKXWallet } from "@near-wallet-selector/okx-wallet" ;
30
+ import { setupEthereumWallets } from "@near-wallet-selector/ethereum-wallets" ;
26
31
27
32
import type { ReactNode } from "react" ;
28
33
import React , {
@@ -33,6 +38,17 @@ import React, {
33
38
useMemo ,
34
39
} from "react" ;
35
40
import { distinctUntilChanged , map } from "rxjs" ;
41
+ import { createWeb3Modal } from "@web3modal/wagmi" ;
42
+ import type { GetAccountReturnType } from "@wagmi/core" ;
43
+ import {
44
+ reconnect ,
45
+ http ,
46
+ createConfig ,
47
+ type Config ,
48
+ watchAccount ,
49
+ } from "@wagmi/core" ;
50
+ import { type Chain } from "@wagmi/core/chains" ;
51
+ import { injected , walletConnect } from "@wagmi/connectors" ;
36
52
37
53
import { Loading } from "../components/Loading" ;
38
54
import { CONTRACT_ID } from "../constants" ;
@@ -54,6 +70,60 @@ interface WalletSelectorContextValue {
54
70
const WalletSelectorContext =
55
71
React . createContext < WalletSelectorContextValue | null > ( null ) ;
56
72
73
+ // Get a project ID at https://cloud.walletconnect.com
74
+ const projectId = "30147604c5f01d0bc4482ab0665b5697" ;
75
+
76
+ // NOTE: This is the NEAR wallet playground used in dev mode.
77
+ // TODO: Replace with the NEAR chain after the protocol upgrade.
78
+ const near : Chain = {
79
+ id : 398 ,
80
+ name : "NEAR wallet playground" ,
81
+ nativeCurrency : {
82
+ decimals : 18 ,
83
+ name : "NEAR" ,
84
+ symbol : "NEAR" ,
85
+ } ,
86
+ rpcUrls : {
87
+ default : { http : [ "https://near-wallet-relayer.testnet.aurora.dev" ] } ,
88
+ public : { http : [ "https://near-wallet-relayer.testnet.aurora.dev" ] } ,
89
+ } ,
90
+ blockExplorers : {
91
+ default : {
92
+ name : "NEAR Explorer" ,
93
+ url : "https://testnet.nearblocks.io" ,
94
+ } ,
95
+ } ,
96
+ testnet : true ,
97
+ } ;
98
+
99
+ const wagmiConfig : Config = createConfig ( {
100
+ chains : [ near ] ,
101
+ transports : {
102
+ [ near . id ] : http ( ) ,
103
+ } ,
104
+ connectors : [
105
+ walletConnect ( {
106
+ projectId,
107
+ metadata : {
108
+ name : "NEAR Guest Book" ,
109
+ description : "A guest book with comments stored on the NEAR blockchain" ,
110
+ url : "https://near.github.io/wallet-selector" ,
111
+ icons : [ "https://near.github.io/wallet-selector/favicon.ico" ] ,
112
+ } ,
113
+ showQrModal : false ,
114
+ } ) ,
115
+ injected ( { shimDisconnect : true } ) ,
116
+ ] ,
117
+ } ) ;
118
+ reconnect ( wagmiConfig ) ;
119
+
120
+ const web3Modal = createWeb3Modal ( {
121
+ wagmiConfig : wagmiConfig ,
122
+ projectId,
123
+ enableOnramp : false ,
124
+ allWallets : "SHOW" ,
125
+ } ) ;
126
+
57
127
export const WalletSelectorContextProvider : React . FC < {
58
128
children : ReactNode ;
59
129
} > = ( { children } ) => {
@@ -62,6 +132,25 @@ export const WalletSelectorContextProvider: React.FC<{
62
132
const [ accounts , setAccounts ] = useState < Array < AccountState > > ( [ ] ) ;
63
133
const [ loading , setLoading ] = useState < boolean > ( true ) ;
64
134
135
+ // Log in with Ethereum flow: auto connect to ethereum-wallets without showing the NEAR modal.
136
+ useEffect ( ( ) => {
137
+ if ( ! selector ) {
138
+ return ;
139
+ }
140
+ watchAccount ( wagmiConfig , {
141
+ onChange : ( data : GetAccountReturnType ) => {
142
+ if ( ! data . address || selector . store . getState ( ) . selectedWalletId ) {
143
+ return ;
144
+ }
145
+ selector . wallet ( "ethereum-wallets" ) . then ( ( wallet ) =>
146
+ ( wallet as InjectedWalletBehaviour ) . signIn ( {
147
+ contractId : CONTRACT_ID ,
148
+ } )
149
+ ) ;
150
+ } ,
151
+ } ) ;
152
+ } , [ selector ] ) ;
153
+
65
154
const init = useCallback ( async ( ) => {
66
155
const _selector = await setupWalletSelector ( {
67
156
network : "testnet" ,
@@ -99,6 +188,7 @@ export const WalletSelectorContextProvider: React.FC<{
99
188
setupNearMobileWallet ( ) ,
100
189
setupMintbaseWallet ( { contractId : CONTRACT_ID } ) ,
101
190
setupBitteWallet ( { contractId : CONTRACT_ID } ) ,
191
+ setupEthereumWallets ( { wagmiConfig, web3Modal, devMode : true } ) ,
102
192
] ,
103
193
} ) ;
104
194
const _modal = setupModal ( _selector , {
0 commit comments