Created
August 9, 2024 11:14
-
-
Save quetool/0f62bcfb510812843c83774d1c90865d to your computer and use it in GitHub Desktop.
PR 161
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:developer'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:web3modal_flutter/web3modal_flutter.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final W3MService w3mService = W3MService( | |
logLevel: LogLevel.error, | |
enableEmail: true, | |
projectId: 'd3........', | |
metadata: const PairingMetadata( | |
name: 'W3MTest', | |
description: 'W3MTest', | |
url: 'https://github.com/JosesGabriel/w3mtest', | |
icons: <String>[ | |
'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Identicon.svg/1200px-Identicon.svg.png', | |
], | |
redirect: Redirect( | |
universal: 'https://github.com/JosesGabriel/w3mtest', | |
native: 'https://github.com/JosesGabriel/w3mtest', | |
), | |
), | |
includedWalletIds: <String>{ | |
'c57ca95b47569778a828d19178114f4db188b89b763c899ba0be274e97267d96', // metamask | |
}, | |
); | |
Future<DeployedContract> get deployedGreeterContract async { | |
const String abiDirectory = 'lib/contracts/staging/greeter.abi.json'; | |
const String contractAddress = '0x0e10e90f67C67c2cB9DD5071674FDCfb7853a6F5'; | |
final String contractABI = await rootBundle.loadString(abiDirectory); | |
final DeployedContract contract = DeployedContract( | |
ContractAbi.fromJson(contractABI, 'Greeter'), | |
EthereumAddress.fromHex(contractAddress), | |
); | |
return contract; | |
} | |
@override | |
void initState() { | |
super.initState(); | |
initializeW3M(); | |
} | |
void _addExtraChains() { | |
for (final entry in W3MChainPresets.extraChains.entries) { | |
W3MChainPresets.chains.putIfAbsent(entry.key, () => entry.value); | |
} | |
for (final entry in W3MChainPresets.testChains.entries) { | |
W3MChainPresets.chains.putIfAbsent(entry.key, () => entry.value); | |
} | |
} | |
Future<void> initializeW3M() async { | |
try { | |
_addExtraChains(); | |
await w3mService.init(); | |
log('Initialized!'); | |
} catch (e, s) { | |
print('---Instantiate Error---'); | |
print(e); | |
print(s); | |
} | |
} | |
Future<void> read() async { | |
try { | |
await w3mService.selectChain(W3MChainPresets.chains['11155111']!); | |
final List<dynamic> contractData = await w3mService.requestReadContract( | |
deployedContract: await deployedGreeterContract, | |
functionName: 'greet', | |
); | |
print('----Contract Data----'); | |
print(contractData); | |
} catch (e, s) { | |
print('---Read Error---'); | |
print(e); | |
print('---Stack Trace---'); | |
print(s); | |
} | |
} | |
Future<void> write() async { | |
final List<String> accounts = | |
w3mService.session?.getAccounts() ?? <String>[]; | |
if (accounts.isNotEmpty) { | |
final String sender = accounts.first.split(':').last; | |
w3mService.launchConnectedWallet(); | |
await w3mService.requestWriteContract( | |
chainId: 'eip155:11155111', | |
topic: w3mService.session?.topic ?? '', | |
deployedContract: await deployedGreeterContract, | |
functionName: 'setGreeting', | |
parameters: <String>['Update this greeting!'], | |
method: 'setGreeting', | |
transaction: Transaction( | |
from: EthereumAddress.fromHex(sender), | |
), | |
); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
W3MConnectWalletButton( | |
context: context, | |
service: w3mService, | |
), | |
ElevatedButton(onPressed: read, child: const Text('Read')), | |
ElevatedButton(onPressed: write, child: const Text('Write')) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment