forked from leonardocustodio/polkadart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples folder (leonardocustodio#363)
* Add examples folder * remove test folder
- Loading branch information
1 parent
9b7ec8e
commit 8b9122d
Showing
7 changed files
with
159 additions
and
2 deletions.
There are no files selected for viewing
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
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 |
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,3 @@ | ||
## 1.0.0 | ||
|
||
- Initial version. |
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 @@ | ||
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` | ||
|
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,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 |
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,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}}")); | ||
} |
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,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 |