Skip to content

Commit c226c23

Browse files
committed
apply comments on code
1 parent 24b95aa commit c226c23

21 files changed

+108
-109
lines changed

.github/workflows/coverage.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ jobs:
3737
- name: Run unit tests and generate coverage
3838
env:
3939
MNEMONIC: ${{ secrets.MNEMONIC }}
40-
LINK: ${{ secrets.LINK }}
41-
SIGN_TYPE: ${{ secrets.SIGN_TYPE}}
40+
URL: ${{ secrets.URL }}
41+
KEYPAIR_TYPE: ${{ secrets.KEYPAIR_TYPE}}
4242
run: |
4343
melos exec rm -rf coverage
4444
melos run unit_test

packages/tfchain_client/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ export MNEMONIC="your-mnemonic"
4242
2- provide chain url and the signing type.
4343
By default the chain url is set to `wss://tfchain.dev.grid.tf/ws` and the signing type is `sr25519`. Note that signing type can be either `sr25519` or `ed25519`.
4444
```bash
45-
export LINK="chain_url"
45+
export URL="chain_url"
4646
```
4747
```bash
48-
export SIGN_TYPE="sr25519"
48+
export KEYPAIR_TYPE="sr25519"
4949
```
5050

5151

packages/tfchain_client/bin/tfchain_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void main() async {
1414

1515
final client = Client(
1616
"wss://tfchain.dev.grid.tf/ws",
17-
"secret add bag cluster deposit beach illness letter crouch position rain arctic",
17+
"your-mnemonic",
1818
"sr25519");
1919
await client.connect();
2020
// final extrinsic = await client.clientBalances.transfer(

packages/tfchain_client/lib/src/balances.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ class Balances extends QueryBalances {
2222

2323
final Client client;
2424

25-
Future<void> transfer({required String address, required int amount}) async {
26-
if (amount.isNaN || amount <= 0) {
25+
Future<void> transfer(
26+
{required String address, required BigInt amount}) async {
27+
if (amount <= BigInt.zero) {
2728
throw Exception("Amount must be a positive numeric value");
2829
}
2930
final keyring = Keyring();
3031
final publicKey = keyring.decodeAddress(address);
3132
MultiAddress multiAddress = Id(publicKey);
3233

33-
final extrinsic = client.api.tx.balances.transfer(
34-
dest: multiAddress, value: BigInt.from(amount * pow(10, 7).toInt()));
34+
final extrinsic = client.api.tx.balances
35+
.transfer(dest: multiAddress, value: amount * BigInt.from(10).pow(7));
3536
await client.apply(extrinsic);
3637
}
3738

packages/tfchain_client/lib/src/client.dart

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,23 @@ class QueryClient {
7474
}
7575

7676
Future<void> disconnect() async {
77-
if (provider!.isConnected()) {
77+
if (provider != null && provider!.isConnected()) {
7878
await api.disconnect();
7979
}
8080
}
8181
}
8282

8383
class Client extends QueryClient {
84-
final String mnemonic;
84+
final String mnemonicOrSecretSeed;
8585
late String address;
8686
final String keypairType;
8787
KeyPair? keypair;
8888
KVStore? _kvStore;
89-
final SUPPORTED_KEYPAIR_TYPES = ["sr25519", "ed25519"];
89+
TermsAndConditions? _termsAndConditions;
90+
static const List<String> SUPPORTED_KEYPAIR_TYPES = ["sr25519", "ed25519"];
9091

91-
Client(String url, this.mnemonic, this.keypairType) : super(url) {}
92+
Client(String url, this.mnemonicOrSecretSeed, this.keypairType)
93+
: super(url) {}
9294

9395
@override
9496
Twins get twins {
@@ -137,17 +139,24 @@ class Client extends QueryClient {
137139
return _kvStore as KVStore;
138140
}
139141

142+
TermsAndConditions get termsAndConditions {
143+
if (_termsAndConditions == null)
144+
_termsAndConditions = TermsAndConditions(this);
145+
return _termsAndConditions as TermsAndConditions;
146+
}
147+
140148
@override
141149
void _checkInputs() {
142150
super._checkInputs();
143-
if (mnemonic.isEmpty) {
151+
if (mnemonicOrSecretSeed.isEmpty) {
144152
throw FormatException("Mnemonic or secret should be provided");
145-
} else if (mnemonic != "//Allice" && !validateMnemonic(mnemonic)) {
146-
if (mnemonic.contains(" ")) {
153+
} else if (mnemonicOrSecretSeed != "//Alice" &&
154+
!validateMnemonic(mnemonicOrSecretSeed)) {
155+
if (mnemonicOrSecretSeed.contains(" ")) {
147156
throw FormatException("Invalid mnemonic! Must be bip39 compliant");
148157
}
149158

150-
if (!mnemonic.startsWith("0x")) {
159+
if (!mnemonicOrSecretSeed.startsWith("0x")) {
151160
throw FormatException(
152161
"Invalid secret seed. Secret seed should start with 0x");
153162
}
@@ -165,34 +174,19 @@ class Client extends QueryClient {
165174
_checkInputs();
166175
final Signer.Signer signer = Signer.Signer();
167176
if (keypairType == "sr25519") {
168-
keypair = await signer.fromMnemonic(mnemonic, Signer.KPType.sr25519);
177+
keypair = await signer.fromMnemonic(
178+
mnemonicOrSecretSeed, Signer.KPType.sr25519);
169179
address = keypair!.address;
170180
} else {
171-
keypair = await signer.fromMnemonic(mnemonic, Signer.KPType.ed25519);
181+
keypair = await signer.fromMnemonic(
182+
mnemonicOrSecretSeed, Signer.KPType.ed25519);
172183
address = keypair!.address;
173184
}
174185
}
175186

176-
@override
177187
Future<void> disconnect() async {
178-
await api.disconnect();
179-
}
180-
181-
Uint8List hexToBytes(String hexString) {
182-
if (hexString.startsWith('0x')) {
183-
hexString = hexString.substring(2);
184-
}
185-
return Uint8List.fromList(hex.decode(hexString));
186-
}
187-
188-
dynamic replaceMapEntry(dynamic value) {
189-
if (value is Map<String, dynamic>) {
190-
return value
191-
.map((key, innerValue) => MapEntry(key, replaceMapEntry(innerValue)));
192-
} else if (value is List) {
193-
return value.map((innerValue) => replaceMapEntry(innerValue)).toList();
194-
} else {
195-
return value;
188+
if (provider != null && provider!.isConnected()) {
189+
await api.disconnect();
196190
}
197191
}
198192

packages/tfchain_client/lib/src/contracts.dart

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/contract.dart';
22
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/contract_lock.dart';
33
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/service_contract.dart';
4+
import 'package:tfchain_client/generated/dev/types/tfchain_runtime/origin_caller.dart';
45
import 'package:tfchain_client/tfchain_client.dart';
56

67
const twoWeeks = 1209600000;
@@ -50,6 +51,8 @@ class QueryContracts {
5051
final contract = await get(contractId: id);
5152
if (contract != null && contract.state.toJson()["Created"] == null) {
5253
return 0;
54+
} else if (contract == null) {
55+
return 0;
5356
}
5457

5558
//TODO: double check that GracePeriod typed like that.
@@ -89,19 +92,21 @@ class Contracts extends QueryContracts {
8992
{required int nodeId,
9093
required List<int> deploymentHash,
9194
required List<int> deploymentData,
92-
required int publicIps}) async {
95+
required int publicIps,
96+
BigInt? solutionProviderId}) async {
9397
final extrinsic = client.api.tx.smartContractModule.createNodeContract(
9498
nodeId: nodeId,
9599
deploymentHash: deploymentHash,
96100
deploymentData: deploymentData,
97-
publicIps: publicIps);
101+
publicIps: publicIps,
102+
solutionProviderId: solutionProviderId!);
98103
await client.apply(extrinsic);
99104

100105
return await getContractIdByNodeIdAndHash(
101106
hash: deploymentHash, nodeId: nodeId);
102107
}
103108

104-
Future<Contract?> updateNode(
109+
Future<void> updateNode(
105110
{required int contractId,
106111
required List<int> deploymentHash,
107112
required List<int> deploymentData}) async {
@@ -110,8 +115,6 @@ class Contracts extends QueryContracts {
110115
deploymentHash: deploymentHash,
111116
deploymentData: deploymentData);
112117
await client.apply(extrinsic);
113-
114-
return await get(contractId: BigInt.from(contractId));
115118
}
116119

117120
Future<BigInt?> createName({required String name}) async {
@@ -123,9 +126,9 @@ class Contracts extends QueryContracts {
123126
}
124127

125128
Future<BigInt?> createRent(
126-
{required int nodeId, required int solutionProviderId}) async {
129+
{required int nodeId, required BigInt solutionProviderId}) async {
127130
final extrinsic = client.api.tx.smartContractModule.createRentContract(
128-
nodeId: nodeId, solutionProviderId: BigInt.from(solutionProviderId));
131+
nodeId: nodeId, solutionProviderId: solutionProviderId);
129132
await client.apply(extrinsic);
130133

131134
return await getContractIdByActiveRentForNode(nodeId: nodeId);
@@ -142,12 +145,10 @@ class Contracts extends QueryContracts {
142145
await client.apply(extrinsic);
143146
}
144147

145-
Future<BigInt?> setDedicatedNodeExtraFee(
146-
{required int nodeId, required int extraFee}) async {
148+
Future<void> setDedicatedNodeExtraFee(
149+
{required int nodeId, required BigInt extraFee}) async {
147150
final extrinsic = client.api.tx.smartContractModule
148-
.setDedicatedNodeExtraFee(nodeId: nodeId, extraFee: BigInt.from(extraFee));
151+
.setDedicatedNodeExtraFee(nodeId: nodeId, extraFee: extraFee);
149152
await client.apply(extrinsic);
150-
151-
return await getDedicatedNodeExtraFee(nodeId: nodeId);
152153
}
153154
}

packages/tfchain_client/lib/src/dao.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ class Dao extends QueryDao {
129129
final Client client;
130130

131131
Future<DaoVotes> vote(
132-
{required String address,
133-
required int farmId,
132+
{required int farmId,
134133
required String hash,
135134
required bool approve}) async {
136135
final extrinsic = client.api.tx.dao

packages/tfchain_client/lib/src/kvstore.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class KVStore {
2020

2121
Future<String> get({required String key}) async {
2222
final res = await client.api.query.tFKVStore
23-
.tFKVStore(client.keypair!.publicKey.bytes, []);
23+
.tFKVStore(client.keypair!.publicKey.bytes, key.codeUnits);
2424
return String.fromCharCodes(res);
2525
}
2626

@@ -82,7 +82,7 @@ class KVStore {
8282
Map<String, String> keys = await list();
8383

8484
for (String key in keys.keys) {
85-
await (key: key);
85+
await delete(key: key);
8686
}
8787
}
8888
}

packages/tfchain_client/lib/src/pricing_policies.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ class QueryPricingPolicies {
77

88
Future<PricingPolicy?> get({required int id}) async {
99
final res = await client.api.query.tfgridModule.pricingPolicies(id);
10-
return res as PricingPolicy;
10+
return res;
1111
}
1212
}

packages/tfchain_client/lib/src/tft_bridge.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class Bridge extends QueryBridge {
2222
final Client client;
2323

2424
Future<void> swapToStellar(
25-
{required String target, required int amount}) async {
25+
{required String target, required BigInt amount}) async {
2626
final extrinsic = client.api.tx.tFTBridgeModule.swapToStellar(
2727
targetStellarAddress: target.codeUnits,
28-
amount: BigInt.from(amount * pow(10, 7).toInt()));
28+
amount: amount * BigInt.from(10).pow(7));
2929
await client.apply(extrinsic);
3030
}
3131
}

packages/tfchain_client/lib/src/tft_price.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class QueryTFTPrice {
44
final QueryClient client;
55
QueryTFTPrice(this.client);
66

7-
Future<int?> get() async {
7+
Future<int> get() async {
88
final res = await client.api.query.tFTPriceModule.tftPrice();
99
return res;
1010
}

packages/tfchain_client/lib/src/twins.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ class Twins extends QueryTwins {
3333
return await getTwinIdByAccountId(address: client.address);
3434
}
3535

36-
Future<Twin?> update(
37-
{required List<int> relay, required List<int> pk}) async {
36+
Future<void> update({required List<int> relay, required List<int> pk}) async {
3837
final extrinsic =
3938
client.api.tx.tfgridModule.updateTwin(relay: relay, pk: pk);
4039
await client.apply(extrinsic);
41-
final twinId = await getTwinIdByAccountId(address: client.address);
42-
final twin = await get(id: twinId!);
43-
44-
return twin;
4540
}
4641
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import 'dart:typed_data';
2+
import 'package:convert/convert.dart';
23

3-
bool isValidSeed(String seed) {
4-
final RegExp hexRegex = RegExp(r'^[0-9a-fA-F]+$');
5-
return hexRegex.hasMatch(seed);
6-
}
74

85
Uint8List dynamicListToUint8List(dynamicList) {
96
List<int> intList = dynamicList.cast<int>().toList();
107
Uint8List data = Uint8List.fromList(intList);
118
return data;
129
}
10+
11+
Uint8List hexToBytes(String hexString) {
12+
if (hexString.startsWith('0x')) {
13+
hexString = hexString.substring(2);
14+
}
15+
return Uint8List.fromList(hex.decode(hexString));
16+
}

packages/tfchain_client/lib/tfchain_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import 'package:tfchain_client/src/farms.dart';
2424
import 'package:tfchain_client/src/kvstore.dart';
2525
import 'package:tfchain_client/src/nodes.dart';
2626
import 'package:tfchain_client/src/pricing_policies.dart';
27+
import 'package:tfchain_client/src/terms_and_conditions.dart';
2728
import 'package:tfchain_client/src/tft_bridge.dart';
2829
import 'package:tfchain_client/src/tft_price.dart';
2930
import 'package:tfchain_client/src/twins.dart';
3031
import 'package:bip39/bip39.dart';
31-
import 'package:convert/convert.dart';
3232
import 'package:signer/signer.dart' as Signer;
3333
import 'package:collection/collection.dart';
3434
import 'package:tfchain_client/src/utils.dart';

packages/tfchain_client/test/balances_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ void main() {
3838
group("Test Balances", () {
3939
late Client client;
4040
final mnemonic = Platform.environment['MNEMONIC']!;
41-
final String link =
42-
Platform.environment['LINK'] ?? 'wss://tfchain.dev.grid.tf/ws';
43-
final String type = Platform.environment['SIGN_TYPE'] ?? 'sr25519';
41+
final String url =
42+
Platform.environment['URL'] ?? 'wss://tfchain.dev.grid.tf/ws';
43+
final String type = Platform.environment['KEYPAIR_TYPE'] ?? 'sr25519';
4444

4545
setUp(() async {
46-
client = Client(link, mnemonic, type);
46+
client = Client(url, mnemonic, type);
4747
await client.connect();
4848
});
4949

@@ -56,7 +56,7 @@ void main() {
5656
await client.balances.transfer(
5757
address:
5858
"oven strong mention shoulder night ghost correct exercise surge lady jungle hundred",
59-
amount: 0);
59+
amount: BigInt.zero);
6060
} catch (error) {
6161
expect(error, isNotNull);
6262
}
@@ -66,7 +66,7 @@ void main() {
6666
try {
6767
await client.balances.transfer(
6868
address: "5EcSXeEH35LriE2aWxX6v4yZSMq47vdJ1GgHEXDdhJxg9XjG",
69-
amount: 10);
69+
amount: BigInt.from(10));
7070
} catch (error) {
7171
print(error);
7272
expect(error, isNull);

packages/tfchain_client/test/client_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ void main() {
5151
group("Full Client Tests", () {
5252
late Client client;
5353
final mnemonic = Platform.environment['MNEMONIC']!;
54-
final String link =
55-
Platform.environment['LINK'] ?? 'wss://tfchain.dev.grid.tf/ws';
56-
final String type = Platform.environment['SIGN_TYPE'] ?? 'sr25519';
54+
final String url =
55+
Platform.environment['URL'] ?? 'wss://tfchain.dev.grid.tf/ws';
56+
final String type = Platform.environment['KEYPAIR_TYPE'] ?? 'sr25519';
5757
setUp(() async {
5858
client = Client(
59-
link,
59+
url,
6060
mnemonic,
6161
type,
6262
);

0 commit comments

Comments
 (0)