Skip to content

Commit

Permalink
Add examples folder (leonardocustodio#363)
Browse files Browse the repository at this point in the history
* Add examples folder

* remove test folder
  • Loading branch information
leonardocustodio authored Nov 10, 2023
1 parent 9b7ec8e commit 8b9122d
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ coverage/
.history
.svn/
coverage.lcov
demo

# IntelliJ related
*.iml
Expand Down Expand Up @@ -59,4 +58,4 @@ lib/generated_plugin_registrant.dart
# - https://fvm.app/docs/getting_started/configuration/
# - https://seanconnolly.dev/flutter-version-management
.fvm/flutter_sdk
packages/demo
packages/demo_old
4 changes: 4 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
lib/generated
3 changes: 3 additions & 0 deletions examples/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
17 changes: 17 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Here you can find a example project running Polkadart in a Flutter app.

First you need to generate the types to the network you want to work with.
You can see in pubspec.yaml those lines:
```
polkadart:
output_dir: lib/generated
chains:
polkadot: wss://rpc.polkadot.io
```

Feel free to change to whatever network you want. After that you can generate the classes by running:
`dart run polkadart_cli:generate -v`

Finally to run this example you can do:
`dart run bin/demo.dart`

30 changes: 30 additions & 0 deletions examples/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
77 changes: 77 additions & 0 deletions examples/bin/demo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'package:convert/convert.dart';
import '../lib/generated/polkadot/polkadot.dart';
import '../lib/generated/polkadot/types/sp_runtime/multiaddress/multi_address.dart';
import 'package:polkadart/polkadart.dart'
show AuthorApi, Extrinsic, Provider, SigningPayload, StateApi;
import 'package:polkadart_keyring/polkadart_keyring.dart';

Future<void> main(List<String> arguments) async {
final provider = Provider.fromUri(Uri.parse('wss://rpc.polkadot.io'));
final api = Polkadot(provider);

final stateApi = StateApi(provider);

final runtimeVersion = await stateApi.getRuntimeVersion();

final specVersion = runtimeVersion.specVersion;

final transactionVersion = runtimeVersion.transactionVersion;

final block = await provider.send('chain_getBlock', []);

final blockNumber = int.parse(block.result['block']['header']['number']);

final blockHash = (await provider.send('chain_getBlockHash', []))
.result
.replaceAll('0x', '');

final genesisHash = (await provider.send('chain_getBlockHash', [0]))
.result
.replaceAll('0x', '');

final keyring = await KeyPair.fromMnemonic(
"resource mirror lecture smooth midnight muffin position cup pepper fruit vanish also//0"); // This is a random key

final publicKey = hex.encode(keyring.publicKey.bytes);
print('Public Key: $publicKey');
final dest = $MultiAddress().id(hex.decode(publicKey));
final runtimeCall = api.tx.balances.transferAll(dest: dest, keepAlive: true);
final encodedCall = hex.encode(runtimeCall.encode());
print('Encoded call: $encodedCall');

final payloadToSign = SigningPayload(
method: encodedCall,
specVersion: specVersion,
transactionVersion: transactionVersion,
genesisHash: genesisHash,
blockHash: blockHash,
blockNumber: blockNumber,
eraPeriod: 64,
nonce: 0, // Supposing it is this wallet first transaction
tip: 0,
);

final payload = payloadToSign.encode(api.registry);
print('Payload: ${hex.encode(payload)}');

final signature = keyring.sign(payload);
final hexSignature = hex.encode(signature);
print('Signature: $hexSignature');

final extrinsic = Extrinsic(
signer: publicKey,
method: encodedCall,
signature: hexSignature,
eraPeriod: 64,
blockNumber: blockNumber,
nonce: 0,
tip: 0,
).encode(api.registry);

final hexExtrinsic = hex.encode(extrinsic);
print('Extrinsic: $hexExtrinsic');

final author = AuthorApi(provider);
author.submitAndWatchExtrinsic(
extrinsic, (p0) => print("Extrinsic result: ${p0.type} - {${p0.value}}"));
}
27 changes: 27 additions & 0 deletions examples/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: polkadart_example
description: A sample command-line application.
version: 1.0.0
publish_to: 'none'

environment:
sdk: ^3.0.0

# Add regular dependencies here.
dependencies:
convert: ^3.1.1
polkadart: ^0.2.0
polkadart_cli: ^0.2.0
polkadart_keyring: ^0.2.0
polkadart_scale_codec: ^1.1.0
ss58: ^1.1.0
substrate_bip39: ^0.2.0
substrate_metadata: ^1.1.0

dev_dependencies:
lints: ^2.0.0
test: ^1.21.0

polkadart:
output_dir: lib/generated
chains:
polkadot: wss://rpc.polkadot.io

0 comments on commit 8b9122d

Please sign in to comment.