|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:developer'; |
| 3 | + |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | + |
| 6 | +import 'package:walletconnect_flutter_dapp/utils/crypto/contract.dart'; |
| 7 | +import 'package:walletconnect_flutter_dapp/utils/dart_defines.dart'; |
| 8 | +import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart'; |
| 9 | + |
| 10 | +import 'package:walletconnect_modal_flutter/walletconnect_modal_flutter.dart'; |
| 11 | + |
| 12 | +class PolkadotSamplePage extends StatefulWidget { |
| 13 | + const PolkadotSamplePage({super.key}); |
| 14 | + |
| 15 | + @override |
| 16 | + State<PolkadotSamplePage> createState() => _MyHomePageState(); |
| 17 | +} |
| 18 | + |
| 19 | +class _MyHomePageState extends State<PolkadotSamplePage> { |
| 20 | + bool _initialized = false; |
| 21 | + IWalletConnectModalService? _modalService; |
| 22 | + |
| 23 | + @override |
| 24 | + void initState() { |
| 25 | + super.initState(); |
| 26 | + initialize(); |
| 27 | + } |
| 28 | + |
| 29 | + Future<void> initialize() async { |
| 30 | + _modalService = WalletConnectModalService( |
| 31 | + projectId: DartDefines.projectId, |
| 32 | + metadata: const PairingMetadata( |
| 33 | + name: 'Flutter Dapp Example', |
| 34 | + description: 'Flutter Dapp Example', |
| 35 | + url: 'https://www.walletconnect.com/', |
| 36 | + icons: ['https://walletconnect.com/walletconnect-logo.png'], |
| 37 | + redirect: Redirect( |
| 38 | + native: 'flutterdapp://', |
| 39 | + universal: 'https://www.walletconnect.com', |
| 40 | + ), |
| 41 | + ), |
| 42 | + requiredNamespaces: { |
| 43 | + 'eip155': const RequiredNamespace( |
| 44 | + chains: ['eip155:1'], |
| 45 | + methods: [ |
| 46 | + 'personal_sign', |
| 47 | + 'eth_sendTransaction', |
| 48 | + ], |
| 49 | + events: [ |
| 50 | + 'chainChanged', |
| 51 | + 'accountsChanged', |
| 52 | + ], |
| 53 | + ), |
| 54 | + }, |
| 55 | + optionalNamespaces: { |
| 56 | + 'polkadot': const RequiredNamespace( |
| 57 | + chains: ['polkadot:91b171bb158e2d3848fa23a9f1c25182'], |
| 58 | + methods: [ |
| 59 | + 'polkadot_signMessage', |
| 60 | + 'polkadot_signTransaction', |
| 61 | + ], |
| 62 | + events: [], |
| 63 | + ), |
| 64 | + }, |
| 65 | + ); |
| 66 | + |
| 67 | + await _modalService!.init(); |
| 68 | + |
| 69 | + _modalService!.web3App!.onSessionConnect.subscribe(_onSessionConnect); |
| 70 | + _modalService!.web3App!.onSessionDelete.subscribe(_onSessionDisonnect); |
| 71 | + _modalService!.web3App!.onSessionEvent.subscribe(_onSessionEvent); |
| 72 | + |
| 73 | + setState(() => _initialized = true); |
| 74 | + } |
| 75 | + |
| 76 | + void _onSessionConnect(SessionConnect? args) { |
| 77 | + log('_onSessionConnect ${jsonEncode(args?.session.toJson())}'); |
| 78 | + setState(() {}); |
| 79 | + } |
| 80 | + |
| 81 | + void _onSessionDisonnect(SessionDelete? args) { |
| 82 | + log('_onSessionDisonnect ${args.toString()}'); |
| 83 | + setState(() {}); |
| 84 | + } |
| 85 | + |
| 86 | + void _onSessionEvent(SessionEvent? args) { |
| 87 | + log('_onSessionEvent ${args.toString()}'); |
| 88 | + setState(() {}); |
| 89 | + } |
| 90 | + |
| 91 | + void requestReadContract() { |
| 92 | + final deployedContract = DeployedContract( |
| 93 | + ContractAbi.fromJson( |
| 94 | + jsonEncode(ContractDetails.readContractAbi), |
| 95 | + 'Tether USD', |
| 96 | + ), |
| 97 | + EthereumAddress.fromHex(ContractDetails.contractAddress), |
| 98 | + ); |
| 99 | + |
| 100 | + _modalService!.web3App!.requestReadContract( |
| 101 | + deployedContract: deployedContract, |
| 102 | + functionName: 'balanceOf', |
| 103 | + rpcUrl: 'https://eth.drpc.org', |
| 104 | + parameters: [ |
| 105 | + // address to read balance of |
| 106 | + EthereumAddress.fromHex('0x......'), |
| 107 | + ], |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + void requestWriteContract() { |
| 112 | + final deployedContract = DeployedContract( |
| 113 | + ContractAbi.fromJson( |
| 114 | + jsonEncode(ContractDetails.readContractAbi), |
| 115 | + 'Tether USD', |
| 116 | + ), |
| 117 | + EthereumAddress.fromHex(ContractDetails.contractAddress), |
| 118 | + ); |
| 119 | + |
| 120 | + _modalService!.web3App!.requestWriteContract( |
| 121 | + deployedContract: deployedContract, |
| 122 | + topic: _modalService!.session!.topic, |
| 123 | + chainId: 'eip155:1', |
| 124 | + rpcUrl: 'https://eth.drpc.org', |
| 125 | + functionName: 'transfer', |
| 126 | + transaction: Transaction( |
| 127 | + from: EthereumAddress.fromHex(_modalService!.address!), |
| 128 | + ), |
| 129 | + parameters: [ |
| 130 | + // address to transfer to |
| 131 | + EthereumAddress.fromHex('0x....'), |
| 132 | + BigInt.from(0.0000000000001), |
| 133 | + ], |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + @override |
| 138 | + void dispose() { |
| 139 | + _modalService!.web3App!.onSessionConnect.unsubscribe(_onSessionConnect); |
| 140 | + _modalService!.web3App!.onSessionDelete.unsubscribe(_onSessionDisonnect); |
| 141 | + _modalService!.web3App!.onSessionEvent.unsubscribe(_onSessionEvent); |
| 142 | + super.dispose(); |
| 143 | + } |
| 144 | + |
| 145 | + @override |
| 146 | + Widget build(BuildContext context) { |
| 147 | + if (!_initialized) { |
| 148 | + return Center( |
| 149 | + child: CircularProgressIndicator( |
| 150 | + color: WalletConnectModalTheme.getData(context).primary100, |
| 151 | + ), |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + return Scaffold( |
| 156 | + appBar: AppBar( |
| 157 | + title: const Text('WalletConnectModal'), |
| 158 | + ), |
| 159 | + body: Center( |
| 160 | + child: Column( |
| 161 | + children: [ |
| 162 | + WalletConnectModalConnect( |
| 163 | + service: _modalService!, |
| 164 | + ), |
| 165 | + Visibility( |
| 166 | + visible: _modalService!.isConnected, |
| 167 | + child: ElevatedButton( |
| 168 | + onPressed: () async { |
| 169 | + final account = _modalService! |
| 170 | + .session!.namespaces['polkadot']!.accounts.last; |
| 171 | + final address = account.split(":").last; |
| 172 | + final result = await _modalService!.web3App!.request( |
| 173 | + topic: _modalService!.session!.topic, |
| 174 | + chainId: 'polkadot:91b171bb158e2d3848fa23a9f1c25182', |
| 175 | + request: SessionRequestParams( |
| 176 | + method: 'polkadot_signMessage', |
| 177 | + params: { |
| 178 | + 'address': address, |
| 179 | + 'message': 'This is an example message', |
| 180 | + }, |
| 181 | + ), |
| 182 | + ); |
| 183 | + debugPrint('result $result'); |
| 184 | + }, |
| 185 | + child: const Text('polkadot_signMessage'), |
| 186 | + ), |
| 187 | + ), |
| 188 | + ], |
| 189 | + ), |
| 190 | + ), |
| 191 | + ); |
| 192 | + } |
| 193 | +} |
0 commit comments