-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update starknet counter example for Sepolia (devnet and testnet) (#403)
* Update starknet counter for Sepolia (devnet and testnet) * Fixtransaction version string for devnet * Fix call to async functions * Change goerli test signature for sepolia signature. And fix devnet port in test (5050 instead of 3030)
- Loading branch information
Showing
8 changed files
with
219 additions
and
7 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
examples/starknet_counter/lib/services/counter_service.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// ignore_for_file: prefer_const_declarations, avoid_print | ||
|
||
import 'package:starknet/starknet.dart'; | ||
import 'package:starknet_provider/starknet_provider.dart'; | ||
|
||
final provider = JsonRpcProvider( | ||
nodeUri: Uri.parse( | ||
'http://localhost:5050')); | ||
final contractAddress = | ||
'0x0662d4b526b2a0e87a449263b131e1831aa166f2d3d97412e8dc67fb85a3de1c'; | ||
final secretAccountAddress = | ||
"0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691"; | ||
final secretAccountPrivateKey = | ||
"0x71d7bb07b9a64f6f78ac4c816aff4da9"; | ||
final signeraccount = getAccount( | ||
accountAddress: Felt.fromHexString(secretAccountAddress), | ||
privateKey: Felt.fromHexString(secretAccountPrivateKey), | ||
nodeUri: Uri.parse( | ||
'http://localhost:5050'), | ||
); | ||
|
||
Future<int> getCurrentCount() async { | ||
final result = await provider.call( | ||
request: FunctionCall( | ||
contractAddress: Felt.fromHexString(contractAddress), | ||
entryPointSelector: getSelectorByName("get_current_count"), | ||
calldata: []), | ||
blockId: BlockId.latest, | ||
); | ||
return result.when( | ||
result: (result) => result[0].toInt(), | ||
error: (error) => throw Exception("Failed to get counter value"), | ||
); | ||
} | ||
|
||
Future<String> increaseCounter() async { | ||
print('print increment'); | ||
final response = await signeraccount.execute(functionCalls: [ | ||
FunctionCall( | ||
contractAddress: Felt.fromHexString(contractAddress), | ||
entryPointSelector: getSelectorByName("increment"), | ||
calldata: [], | ||
), | ||
]); | ||
|
||
final txHash = response.when( | ||
result: (result) => result.transaction_hash, | ||
error: (err) => throw Exception("Failed to execute"), | ||
); | ||
|
||
print('printing increment TX : $txHash'); | ||
return txHash; | ||
// return waitForAcceptance(transactionHash: txHash, provider: provider); | ||
} | ||
|
||
Future<String> increaseCounterBy(String number) async { | ||
print('print increment by '); | ||
final response = await signeraccount.execute(functionCalls: [ | ||
FunctionCall( | ||
contractAddress: Felt.fromHexString(contractAddress), | ||
entryPointSelector: getSelectorByName("increase_count_by"), | ||
calldata: [Felt.fromIntString(number)], | ||
), | ||
]); | ||
|
||
final txHash = response.when( | ||
result: (result) => result.transaction_hash, | ||
error: (err) => throw Exception("Failed to execute"), | ||
); | ||
|
||
print('printing incrementby amount TX : $txHash'); | ||
return txHash; | ||
// return waitForAcceptance(transactionHash: txHash, provider: provider); | ||
} | ||
|
||
Future<String> decreaseCounter() async { | ||
print('decrementing.....'); | ||
final response = await signeraccount.execute(functionCalls: [ | ||
FunctionCall( | ||
contractAddress: Felt.fromHexString(contractAddress), | ||
entryPointSelector: getSelectorByName("decrement"), | ||
calldata: [], | ||
), | ||
]); | ||
|
||
final txHash = response.when( | ||
result: (result) => result.transaction_hash, | ||
error: (err) => throw Exception("Failed to execute"), | ||
); | ||
print('printing decrement TX : $txHash'); | ||
return txHash; | ||
// return waitForAcceptance(transactionHash: txHash, provider: provider); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import 'package:counter/services/counter_service.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class CounterPage extends StatefulWidget { | ||
const CounterPage({super.key, required this.title}); | ||
|
||
final String title; | ||
|
||
@override | ||
State<CounterPage> createState() => _CounterPageState(); | ||
} | ||
|
||
class _CounterPageState extends State<CounterPage> { | ||
int counter = 0; | ||
TextEditingController amount = TextEditingController(); | ||
_increaseCount() async { | ||
await increaseCounter(); | ||
await _getCounter(); | ||
setState(() {}); | ||
} | ||
|
||
_increaseCountBy() async { | ||
await increaseCounterBy(amount.text.trim()); | ||
await _getCounter(); | ||
amount.clear(); | ||
setState(() {}); | ||
} | ||
|
||
_decreaseCount() async { | ||
await decreaseCounter(); | ||
await _getCounter(); | ||
setState(() {}); | ||
} | ||
|
||
_getCounter() async { | ||
int balcounter = await getCurrentCount(); | ||
setState(() { | ||
counter = balcounter; | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | ||
title: Text(widget.title), | ||
), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
const SizedBox( | ||
height: 20, | ||
), | ||
Text("Counter is : $counter"), | ||
const SizedBox( | ||
height: 20, | ||
), | ||
SizedBox( | ||
width: 500, | ||
child: TextField( | ||
controller: amount, | ||
decoration: const InputDecoration( | ||
border: OutlineInputBorder(), | ||
hintText: 'Enter your Amount', | ||
), | ||
), | ||
), | ||
const SizedBox( | ||
height: 20, | ||
), | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
ElevatedButton( | ||
onPressed: _increaseCount, child: const Text('increment')), | ||
const SizedBox( | ||
width: 20, | ||
), | ||
ElevatedButton( | ||
onPressed: _increaseCountBy, | ||
child: const Text('incrementBy')), | ||
const SizedBox( | ||
width: 20, | ||
), | ||
ElevatedButton( | ||
onPressed: (() => _getCounter()), | ||
child: const Text('get count')), | ||
const SizedBox( | ||
width: 20, | ||
), | ||
ElevatedButton( | ||
onPressed: _decreaseCount, child: const Text('decrement')), | ||
], | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
dependency_overrides: | ||
path: ^1.8.3 | ||
http: ^1.0.0 | ||
starknet: | ||
path: ../../packages/starknet | ||
starknet_provider: | ||
path: ../../packages/starknet_provider | ||
# wallet_kit: | ||
# git: | ||
# url: https://github.com/focustree/starknet.dart | ||
# path: packages/wallet_kit | ||
# ref: focustree | ||
# secure_store: | ||
# git: | ||
# url: https://github.com/focustree/starknet.dart | ||
# path: packages/secure_store | ||
# ref: focustree |
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
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
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
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
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