Skip to content

Commit 4ed4ec8

Browse files
committed
added polkadot sample
1 parent 22bd118 commit 4ed4ec8

2 files changed

Lines changed: 231 additions & 2 deletions

File tree

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
}

example/sign/lib/solana_sample.dart

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import 'dart:convert';
22
import 'dart:developer';
33

4+
// ignore: depend_on_referenced_packages
5+
import 'package:bs58/bs58.dart';
6+
47
import 'package:flutter/material.dart';
58
import 'package:walletconnect_flutter_dapp/utils/crypto/contract.dart';
69
import 'package:walletconnect_flutter_dapp/utils/dart_defines.dart';
@@ -73,14 +76,17 @@ class _MyHomePageState extends State<SolanaSamplePage> {
7376

7477
void _onSessionConnect(SessionConnect? args) {
7578
log('_onSessionConnect ${jsonEncode(args?.session.toJson())}');
79+
setState(() {});
7680
}
7781

7882
void _onSessionDisonnect(SessionDelete? args) {
7983
log('_onSessionDisonnect ${args.toString()}');
84+
setState(() {});
8085
}
8186

8287
void _onSessionEvent(SessionEvent? args) {
8388
log('_onSessionEvent ${args.toString()}');
89+
setState(() {});
8490
}
8591

8692
void requestReadContract() {
@@ -152,8 +158,38 @@ class _MyHomePageState extends State<SolanaSamplePage> {
152158
title: const Text('WalletConnectModal'),
153159
),
154160
body: Center(
155-
child: WalletConnectModalConnect(
156-
service: _modalService!,
161+
child: Column(
162+
children: [
163+
WalletConnectModalConnect(
164+
service: _modalService!,
165+
),
166+
const SizedBox.square(dimension: 10),
167+
Visibility(
168+
visible: _modalService!.isConnected,
169+
child: ElevatedButton(
170+
onPressed: () async {
171+
final account = _modalService!
172+
.session!.namespaces['solana']!.accounts.last;
173+
final address = account.split(":").last;
174+
final bytes = utf8.encode('This is an example message');
175+
final message = base58.encode(bytes);
176+
final result = await _modalService!.web3App!.request(
177+
topic: _modalService!.session!.topic,
178+
chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
179+
request: SessionRequestParams(
180+
method: 'solana_signMessage',
181+
params: {
182+
'pubkey': address,
183+
'message': message,
184+
},
185+
),
186+
);
187+
debugPrint('result $result');
188+
},
189+
child: const Text('solana_signMessage'),
190+
),
191+
),
192+
],
157193
),
158194
),
159195
);

0 commit comments

Comments
 (0)