Skip to content

Commit 2600abe

Browse files
committed
fix getting balance add integration tests
1 parent f8446d1 commit 2600abe

10 files changed

+140
-15
lines changed

packages/tfchain_client/bin/tfchain_client.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ void main() async {
1717
"secret add bag cluster deposit beach illness letter crouch position rain arctic",
1818
"sr25519");
1919
await client.connect();
20+
final extrinsic = await client.clientBalances.transfer(
21+
address: "5CJrCjZvsudNoJApTGG5PKcZfhAzAyGqgSK8bysoCV2oRBMC", amount: 10);
2022

21-
// await client.apply(extrinsic);
22-
client.kvStrore.list();
23+
await client.apply(extrinsic);
24+
// await client.kvStrore.list();
2325

2426
// await client.disconnect();
2527
}

packages/tfchain_client/lib/src/balances.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:polkadart/scale_codec.dart';
2+
import 'package:polkadart_keyring/polkadart_keyring.dart';
23
import 'package:tfchain_client/generated/dev/types/frame_system/account_info.dart';
34
import 'package:tfchain_client/generated/dev/types/sp_runtime/multiaddress/multi_address.dart';
45
import 'package:tfchain_client/generated/dev/types/tfchain_runtime/runtime_call.dart';
@@ -9,9 +10,9 @@ class QueryBalances {
910
QueryBalances(this.client);
1011

1112
Future<AccountInfo?> get({required String address}) async {
12-
// TODO: should get pair.publicKey.bytes, How to get keypair if i don't have mnemonic
13-
final res = await client.api.query.system
14-
.account(Address32(address.codeUnits).value0);
13+
final keyring = Keyring();
14+
final publicKey = keyring.decodeAddress(address);
15+
final res = await client.api.query.system.account(publicKey);
1516
return res;
1617
}
1718
}

packages/tfchain_client/lib/src/kvstore.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,25 @@ class KVStore {
4343
...client.keypair!.publicKey.bytes
4444
]);
4545

46-
final keys = await client.api.rpc.state.getKeys(key: partialKey);
46+
List<Uint8List> keys = [];
47+
int count = 2;
48+
Uint8List? startKey;
49+
bool hasMore = true;
50+
51+
while (hasMore) {
52+
final keysPage = await client.api.rpc.state.getKeysPaged(
53+
key: partialKey,
54+
count: count,
55+
startKey: startKey,
56+
);
57+
58+
if (keysPage.isEmpty) {
59+
hasMore = false;
60+
} else {
61+
keys.addAll(keysPage);
62+
startKey = keysPage.last;
63+
}
64+
}
4765

4866
final keysValues = await client.api.rpc.state.queryStorageAt(keys);
4967

@@ -59,6 +77,7 @@ class KVStore {
5977
keyValueMap[String.fromCharCodes(added)] = String.fromCharCodes(value);
6078
}
6179

80+
print(keyValueMap);
6281
return keyValueMap;
6382
}
6483

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({required String address}) 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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:polkadart_keyring/polkadart_keyring.dart';
12
import 'package:tfchain_client/generated/dev/types/pallet_tfgrid/types/twin.dart';
23
import 'package:tfchain_client/generated/dev/types/tfchain_runtime/runtime_call.dart';
34
import 'package:tfchain_client/tfchain_client.dart';
@@ -11,7 +12,9 @@ class QueryTwins {
1112
return res;
1213
}
1314

14-
Future<int?> getTwinIdByAccountId({required List<int> accountId}) async {
15+
Future<int?> getTwinIdByAccountId({required String address}) async {
16+
final keyring = Keyring();
17+
final accountId = keyring.decodeAddress(address);
1518
final res =
1619
await client.api.query.tfgridModule.twinIdByAccountID(accountId);
1720
return res;
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import 'package:test/test.dart';
2+
import 'package:tfchain_client/generated/dev/types/frame_system/account_info.dart';
3+
import 'package:tfchain_client/tfchain_client.dart';
24

35
void main() {
46
group("Query Balances Test", () {
5-
// late QueryClient queryClient;
6-
// setUp(() {
7-
// queryClient = QueryClient("wss://tfchain.dev.grid.tf/ws");
8-
// });
7+
late QueryClient queryClient;
8+
setUp(() {
9+
queryClient = QueryClient("wss://tfchain.dev.grid.tf/ws");
10+
});
911

10-
// test('Test Get Balance', () {
11-
12-
// });
12+
test('Test Get Balance', () async {
13+
String address = "5CJrCjZvsudNoJApTGG5PKcZfhAzAyGqgSK8bysoCV2oRBMC";
14+
AccountInfo? accountInfo =
15+
await queryClient.balances.get(address: address);
16+
expect(accountInfo, isNotNull);
17+
});
18+
19+
test('Test Get Balance with Invalid address', () async {
20+
String address = "address";
21+
try {
22+
AccountInfo? accountInfo =
23+
await queryClient.balances.get(address: address);
24+
expect(accountInfo, isNull);
25+
} catch (e) {
26+
expect(e, isNotNull);
27+
}
28+
});
1329
});
1430
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:test/test.dart';
2+
import 'package:tfchain_client/generated/dev/types/pallet_tfgrid/types/pricing_policy.dart';
3+
import 'package:tfchain_client/tfchain_client.dart';
4+
5+
void main() {
6+
group("Query Pricing Policies", () {
7+
late QueryClient queryClient;
8+
setUp(() {
9+
queryClient = QueryClient("wss://tfchain.dev.grid.tf/ws");
10+
});
11+
12+
test('Test Get Pricing Policy', () async {
13+
PricingPolicy? res = await queryClient.policies.get(id: 1);
14+
expect(res, isNotNull);
15+
});
16+
});
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'package:test/test.dart';
2+
import 'package:tfchain_client/tfchain_client.dart';
3+
4+
void main() {
5+
group("Query Bridge Test", () {
6+
late QueryClient queryClient;
7+
setUp(() {
8+
queryClient = QueryClient("wss://tfchain.dev.grid.tf/ws");
9+
});
10+
11+
test('Test Get Withdraw fee', () async {
12+
BigInt? fee = await queryClient.bridge.getWithdrawFee();
13+
expect(fee, isNotNull);
14+
});
15+
16+
test('Test Get Deposit fee', () async {
17+
BigInt? fee = await queryClient.bridge.getDepositFee();
18+
expect(fee, isNotNull);
19+
});
20+
});
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:test/test.dart';
2+
import 'package:tfchain_client/tfchain_client.dart';
3+
4+
void main() {
5+
group("Query Price Test", () {
6+
late QueryClient queryClient;
7+
setUp(() {
8+
queryClient = QueryClient("wss://tfchain.dev.grid.tf/ws");
9+
});
10+
11+
test('Test Get TFT price', () async {
12+
final price = await queryClient.price.get();
13+
expect(price, isNotNull);
14+
});
15+
});
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:test/test.dart';
2+
import 'package:tfchain_client/tfchain_client.dart';
3+
4+
void main() {
5+
group("Query Twins Test", () {
6+
late QueryClient queryClient;
7+
setUp(() {
8+
queryClient = QueryClient("wss://tfchain.dev.grid.tf/ws");
9+
});
10+
11+
test('Test Get Twin with id', () async {
12+
int id = 214;
13+
final twin = await queryClient.twins.get(id: id);
14+
expect(twin, isNotNull);
15+
});
16+
17+
test('Test Get Twin with zero id', () async {
18+
int id = 0;
19+
final twin = await queryClient.twins.get(id: id);
20+
expect(twin, null);
21+
});
22+
23+
test('Test Get Twin Id with account Id', () async {
24+
String address = "5CJrCjZvsudNoJApTGG5PKcZfhAzAyGqgSK8bysoCV2oRBMC";
25+
final twin =
26+
await queryClient.twins.getTwinIdByAccountId(address: address);
27+
expect(twin, 7845);
28+
});
29+
});
30+
}

0 commit comments

Comments
 (0)