44using Nethereum . ABI . EIP712 ;
55using Nethereum . JsonRpc . Client ;
66using Nethereum . Web3 ;
7- using TMPro ;
87using UnityEngine ;
9- using UnityEngine . UI ;
8+ using UnityEngine . UIElements ;
9+ using ButtonUtk = UnityEngine . UIElements . Button ;
1010
1111namespace WalletConnect . Web3Modal . Sample
1212{
1313 public class Dapp : MonoBehaviour
1414 {
15- private bool _resumed ;
15+ [ SerializeField ] private UIDocument _uiDocument ;
1616
17- [ SerializeField ] private TMP_Text _activeChainId ;
18- [ SerializeField ] private TMP_Text _initializingLabel ;
19-
20- [ Space ] [ SerializeField ] private Button _connectButton ;
21- [ SerializeField ] private Button _networkButton ;
22- [ SerializeField ] private Button _accountButton ;
23- [ SerializeField ] private Button _signTypedDataButton ;
24- [ SerializeField ] private Button _transactionButton ;
25- [ SerializeField ] private Button _personalSignButton ;
26- [ SerializeField ] private Button _getBalanceButton ;
27- [ SerializeField ] private Button _readContractButton ;
28- [ SerializeField ] private Button _disconnectButton ;
17+ private ButtonStruct [ ] _buttons ;
18+ private VisualElement _buttonsContainer ;
2919
3020 private void Awake ( )
3121 {
3222 Application . targetFrameRate = Screen . currentResolution . refreshRate ;
33- _connectButton . interactable = false ;
23+
24+ _buttonsContainer = _uiDocument . rootVisualElement . Q < VisualElement > ( "ButtonsContainer" ) ;
25+
26+ BuildButtons ( ) ;
27+ }
28+
29+ private void BuildButtons ( )
30+ {
31+ _buttons = new [ ]
32+ {
33+ new ButtonStruct
34+ {
35+ Text = "Connect" ,
36+ OnClick = OnConnectButton ,
37+ AccountRequired = false
38+ } ,
39+ new ButtonStruct
40+ {
41+ Text = "Network" ,
42+ OnClick = OnNetworkButton
43+ } ,
44+ new ButtonStruct
45+ {
46+ Text = "Account" ,
47+ OnClick = OnAccountButton ,
48+ AccountRequired = true
49+ } ,
50+ new ButtonStruct
51+ {
52+ Text = "Personal Sign" ,
53+ OnClick = OnPersonalSignButton ,
54+ AccountRequired = true
55+ } ,
56+ new ButtonStruct
57+ {
58+ Text = "Sign Typed Data" ,
59+ OnClick = OnSignTypedDataV4Button ,
60+ AccountRequired = true
61+ } ,
62+ new ButtonStruct
63+ {
64+ Text = "Send Transaction" ,
65+ OnClick = OnSendTransactionButton ,
66+ AccountRequired = true
67+ } ,
68+ new ButtonStruct
69+ {
70+ Text = "Get Balance" ,
71+ OnClick = OnGetBalanceButton ,
72+ AccountRequired = true
73+ } ,
74+ new ButtonStruct
75+ {
76+ Text = "Read Contract" ,
77+ OnClick = OnReadContractClicked ,
78+ AccountRequired = true ,
79+ ChainIds = new HashSet < string >
80+ {
81+ "eip155:1"
82+ }
83+ } ,
84+ new ButtonStruct
85+ {
86+ Text = "Disconnect" ,
87+ OnClick = OnDisconnectButton ,
88+ AccountRequired = true
89+ }
90+ } ;
91+ }
92+
93+ private void RefreshButtons ( )
94+ {
95+ _buttonsContainer . Clear ( ) ;
96+
97+ foreach ( var button in _buttons )
98+ {
99+ if ( button . ChainIds != null && ! button . ChainIds . Contains ( Web3Modal . NetworkController ? . ActiveChain ? . ChainId ) )
100+ continue ;
101+
102+ var buttonUtk = new ButtonUtk
103+ {
104+ text = button . Text
105+ } ;
106+ buttonUtk . clicked += button . OnClick ;
107+
108+ if ( button . AccountRequired . HasValue )
109+ {
110+ switch ( button . AccountRequired )
111+ {
112+ case true when ! Web3Modal . IsAccountConnected :
113+ buttonUtk . SetEnabled ( false ) ;
114+ break ;
115+ case true when Web3Modal . IsAccountConnected :
116+ buttonUtk . SetEnabled ( true ) ;
117+ break ;
118+ case false when Web3Modal . IsAccountConnected :
119+ buttonUtk . SetEnabled ( false ) ;
120+ break ;
121+ case false when ! Web3Modal . IsAccountConnected :
122+ buttonUtk . SetEnabled ( true ) ;
123+ break ;
124+ }
125+ }
126+
127+ _buttonsContainer . Add ( buttonUtk ) ;
128+ }
34129 }
35130
36131 private async void Start ( )
@@ -40,68 +135,30 @@ private async void Start()
40135 Notification . ShowMessage ( "Web3Modal is not initialized. Please initialize Web3Modal first." ) ;
41136 return ;
42137 }
43-
138+
139+ RefreshButtons ( ) ;
140+
44141 try
45142 {
46- _initializingLabel . gameObject . SetActive ( false ) ;
47-
48143 Web3Modal . ChainChanged += ( _ , e ) =>
49144 {
145+ RefreshButtons ( ) ;
146+
50147 if ( e . Chain == null )
51148 {
52- _activeChainId . text = "Unsupported chain" ;
149+ Notification . ShowMessage ( "Unsupported chain" ) ;
53150 return ;
54151 }
55-
56- _activeChainId . text = e . Chain . ChainId ;
57152 } ;
58153
59- Web3Modal . AccountConnected += async ( _ , e ) =>
60- {
61- _connectButton . interactable = false ;
62- _accountButton . interactable = true ;
63- _signTypedDataButton . interactable = true ;
64- _transactionButton . interactable = true ;
65- _personalSignButton . interactable = true ;
66- _getBalanceButton . interactable = true ;
67- _readContractButton . interactable = true ;
68- _disconnectButton . interactable = true ;
69-
70- var account = await e . GetAccount ( ) ;
71- _activeChainId . text = account . ChainId ;
72- } ;
154+ Web3Modal . AccountConnected += async ( _ , e ) => { RefreshButtons ( ) ; } ;
73155
74- Web3Modal . AccountDisconnected += ( _ , _ ) =>
75- {
76- _connectButton . interactable = true ;
77- _accountButton . interactable = false ;
78- _signTypedDataButton . interactable = false ;
79- _transactionButton . interactable = false ;
80- _personalSignButton . interactable = false ;
81- _getBalanceButton . interactable = false ;
82- _readContractButton . interactable = false ;
83- _disconnectButton . interactable = false ;
84-
85- _activeChainId . text = string . Empty ;
86- } ;
156+ Web3Modal . AccountDisconnected += ( _ , _ ) => { RefreshButtons ( ) ; } ;
87157
88- Web3Modal . AccountChanged += ( _ , e ) =>
89- {
90- var account = e . Account ;
91- _activeChainId . text = account . ChainId ;
92- } ;
158+ Web3Modal . AccountChanged += ( _ , e ) => { RefreshButtons ( ) ; } ;
93159
94- _resumed = await Web3Modal . ConnectorController . TryResumeSessionAsync ( ) ;
95-
96- _networkButton . interactable = true ;
97- _connectButton . interactable = ! _resumed ;
98- _accountButton . interactable = _resumed ;
99- _signTypedDataButton . interactable = _resumed ;
100- _transactionButton . interactable = _resumed ;
101- _personalSignButton . interactable = _resumed ;
102- _getBalanceButton . interactable = _resumed ;
103- _readContractButton . interactable = _resumed ;
104- _disconnectButton . interactable = _resumed ;
160+ var sessionResumed = await Web3Modal . ConnectorController . TryResumeSessionAsync ( ) ;
161+ Debug . Log ( $ "Session resumed: { sessionResumed } ") ;
105162 }
106163 catch ( Exception e )
107164 {
@@ -149,15 +206,15 @@ public async void OnGetBalanceButton()
149206 public async void OnPersonalSignButton ( )
150207 {
151208 Debug . Log ( "[Web3Modal Sample] OnPersonalSignButton" ) ;
152-
209+
153210 try
154211 {
155212 var account = await Web3Modal . GetAccountAsync ( ) ;
156213
157214 const string message = "Hello from Unity!" ;
158215 var signature = await Web3Modal . Evm . SignMessageAsync ( message ) ;
159216 var isValid = await Web3Modal . Evm . VerifyMessageSignatureAsync ( account . Address , message , signature ) ;
160-
217+
161218 Notification . ShowMessage ( $ "Signature valid: { isValid } ") ;
162219 }
163220 catch ( RpcResponseException e )
@@ -187,13 +244,13 @@ public async void OnDisconnectButton()
187244 public async void OnSendTransactionButton ( )
188245 {
189246 Debug . Log ( "[Web3Modal Sample] OnSendTransactionButton" ) ;
190-
247+
191248 const string toAddress = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045" ;
192249
193250 try
194251 {
195252 Notification . ShowMessage ( "Sending transaction..." ) ;
196-
253+
197254 var value = Web3 . Convert . ToWei ( 0.001 ) ;
198255 var result = await Web3Modal . Evm . SendTransactionAsync ( toAddress , value ) ;
199256 Debug . Log ( "Transaction hash: " + result ) ;
@@ -248,13 +305,13 @@ public async void OnSignTypedDataV4Button()
248305 typedData . SetMessage ( mail ) ;
249306
250307 var jsonMessage = typedData . ToJson ( ) ;
251-
308+
252309 try
253- {
310+ {
254311 var signature = await Web3Modal . Evm . SignTypedDataAsync ( jsonMessage ) ;
255-
312+
256313 var isValid = await Web3Modal . Evm . VerifyTypedDataSignatureAsync ( account . Address , jsonMessage , signature ) ;
257-
314+
258315 Notification . ShowMessage ( $ "Signature valid: { isValid } ") ;
259316 }
260317 catch ( Exception e )
@@ -263,21 +320,21 @@ public async void OnSignTypedDataV4Button()
263320 Debug . LogException ( e , this ) ;
264321 }
265322 }
266-
323+
267324 public async void OnReadContractClicked ( )
268325 {
269- if ( Web3Modal . NetworkController . ActiveChain . ChainId != "1" )
326+ if ( Web3Modal . NetworkController . ActiveChain . ChainId != "eip155: 1" )
270327 {
271328 Notification . ShowMessage ( "Please switch to Ethereum mainnet." ) ;
272329 return ;
273330 }
274-
331+
275332 const string contractAddress = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb" ; // on Ethereum mainnet
276333 const string yugaLabsAddress = "0xA858DDc0445d8131daC4d1DE01f834ffcbA52Ef1" ;
277334 const string abi = CryptoPunksAbi ;
278-
335+
279336 Notification . ShowMessage ( "Reading smart contract state..." ) ;
280-
337+
281338 try
282339 {
283340 var tokenName = await Web3Modal . Evm . ReadContractAsync < string > ( contractAddress , abi , "name" ) ;
@@ -288,7 +345,7 @@ public async void OnReadContractClicked()
288345 yugaLabsAddress
289346 } ) ;
290347 var result = $ "Yuga Labs owns: { balance } { tokenName } tokens active chain.";
291-
348+
292349 Notification . ShowMessage ( result ) ;
293350 }
294351 catch ( Exception e )
@@ -313,9 +370,17 @@ private TypedData<Domain> GetMailTypedDefinition()
313370 PrimaryType = nameof ( Mail )
314371 } ;
315372 }
316-
373+
317374 public const string CryptoPunksAbi =
318375 @"[{""constant"":true,""inputs"":[{""name"":""_owner"",""type"":""address""}],""name"":""balanceOf"",""outputs"":[{""name"":""balance"",""type"":""uint256""}],""payable"":false,""stateMutability"":""view"",""type"":""function""},
319376 {""constant"":true,""inputs"":[],""name"":""name"",""outputs"":[{""name"":"""",""type"":""string""}],""payable"":false,""stateMutability"":""view"",""type"":""function""}]" ;
320377 }
378+
379+ internal struct ButtonStruct
380+ {
381+ public string Text ;
382+ public Action OnClick ;
383+ public bool ? AccountRequired ;
384+ public HashSet < string > ChainIds ;
385+ }
321386}
0 commit comments