Skip to content

Commit

Permalink
Update starknet counter example for Sepolia (devnet and testnet) (#403)
Browse files Browse the repository at this point in the history
* 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
devnet0x authored and PaulRbl committed Oct 3, 2024
1 parent afeed2b commit f0ee46a
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 7 deletions.
93 changes: 93 additions & 0 deletions examples/starknet_counter/lib/services/counter_service.dart
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);
}
102 changes: 102 additions & 0 deletions examples/starknet_counter/lib/ui/counter.dart
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')),
],
),
],
),
),
);
}
}
17 changes: 17 additions & 0 deletions examples/starknet_counter/pubspec_overrides.yaml
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
4 changes: 2 additions & 2 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ scripts:
devnet:start:
description: Start local devnet
run: |
starknet-devnet --seed 0 --dump-path $DEVNET_DUMP_PATH --state-archive-capacity full --port 3030
starknet-devnet --seed 0 --dump-path $DEVNET_DUMP_PATH --state-archive-capacity full --port 5050
devnet:start:dump:
description: Start local devnet from scratch and dump on exit
run: |
rm $DEVNET_DUMP_PATH
starknet-devnet --seed 0 --dump-path $DEVNET_DUMP_PATH --state-archive-capacity full --dump-on exit --port 3030
starknet-devnet --seed 0 --dump-path $DEVNET_DUMP_PATH --state-archive-capacity full --dump-on exit --port 5050
devnet:setup:
description: Setup local devnet
run: |
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet/lib/src/static_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:starknet/starknet.dart';

class StarknetChainId {
static final mainnet = Felt.fromString('SN_MAIN');
static final testNet = Felt.fromString('SN_GOERLI');
static final testNet = Felt.fromString('SN_SEPOLIA');
}

class TransactionHashPrefix {
Expand Down
4 changes: 2 additions & 2 deletions packages/starknet/test/signer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ void main() {
signature,
equals([
Felt.fromIntString(
"3058050571719369759412738987533864549850323224007431810241940044840783019940",
"107701415394463892922670165541267022802175117514579709928775579816864470554",
),
Felt.fromIntString(
"1900499411596333527352644243441454261068804171091393084934334076069283020499",
"1904177602932261497361499193520322287574011759183225218778227195148991255212",
)
]),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class InvokeTransactionV1
required Felt nonce,
required Felt senderAddress,
required List<Felt> calldata,
@Default('0x01') String version,
@Default('0x1') String version,
@Default('INVOKE') String type,
}) = _InvokeTransactionV1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ class _$InvokeTransactionV1Impl implements _InvokeTransactionV1 {
required this.nonce,
required this.senderAddress,
required final List<Felt> calldata,
this.version = '0x01',
this.version = '0x1',
this.type = 'INVOKE'})
: _signature = signature,
_calldata = calldata;
Expand Down

0 comments on commit f0ee46a

Please sign in to comment.