Skip to content

Commit 2738d33

Browse files
committed
add contracts and balances tests
1 parent 23ce369 commit 2738d33

File tree

5 files changed

+112
-18
lines changed

5 files changed

+112
-18
lines changed

packages/tfchain_client/lib/src/balances.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:math';
2+
13
import 'package:polkadart_keyring/polkadart_keyring.dart';
24
import 'package:tfchain_client/generated/dev/types/frame_system/account_info.dart';
35
import 'package:tfchain_client/generated/dev/types/sp_runtime/multiaddress/multi_address.dart';
@@ -20,19 +22,17 @@ class Balances extends QueryBalances {
2022

2123
final Client client;
2224

23-
Future<AccountInfo?> transfer(
24-
{required String address, required int amount}) async {
25+
Future<void> transfer({required String address, required int amount}) async {
2526
if (amount.isNaN || amount <= 0) {
2627
throw Exception("Amount must be a positive numeric value");
2728
}
2829
final keyring = Keyring();
2930
final publicKey = keyring.decodeAddress(address);
3031
MultiAddress multiAddress = Address32(publicKey);
3132

32-
final extrinsic = client.api.tx.balances
33-
.transfer(dest: multiAddress, value: BigInt.from(amount));
34-
35-
return await this.get(address: client.address);
33+
final extrinsic = client.api.tx.balances.transfer(
34+
dest: multiAddress, value: BigInt.from(amount * pow(10, 7).toInt()));
35+
await client.apply(extrinsic);
3636
}
3737

3838
Future<AccountInfo?> getMyBalance() async {

packages/tfchain_client/lib/src/contracts.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/grid_contract/name_contract_name.dart';
21
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/contract.dart';
32
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/contract_lock.dart';
43
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/service_contract.dart';
@@ -31,7 +30,7 @@ class QueryContracts {
3130

3231
Future<BigInt?> getContractIdByName({required String name}) async {
3332
final res = await client.api.query.smartContractModule
34-
.contractIDByNameRegistration(name as NameContractName);
33+
.contractIDByNameRegistration(name.codeUnits);
3534
return res;
3635
}
3736

@@ -85,6 +84,7 @@ class Contracts extends QueryContracts {
8584
Contracts(Client this.client) : super(client);
8685
final Client client;
8786

87+
// TODO: should i return bigint or convert it to int ??
8888
Future<BigInt?> createNode(
8989
{required int nodeId,
9090
required List<int> deploymentHash,
@@ -125,13 +125,13 @@ class Contracts extends QueryContracts {
125125
Future<BigInt?> createRent(
126126
{required int nodeId, required int solutionProviderId}) async {
127127
final extrinsic = client.api.tx.smartContractModule.createRentContract(
128-
nodeId: nodeId, solutionProviderId: solutionProviderId);
128+
nodeId: nodeId, solutionProviderId: BigInt.from(solutionProviderId));
129129
await client.apply(extrinsic);
130130

131131
return await getContractIdByActiveRentForNode(nodeId: nodeId);
132132
}
133133

134-
Future<Contract?> cancel({required BigInt contractId}) async {
134+
Future<void> cancel({required BigInt contractId}) async {
135135
final contract = await get(contractId: contractId);
136136
if (contract == null) {
137137
throw Exception("Contract not found");
@@ -140,14 +140,12 @@ class Contracts extends QueryContracts {
140140
final extrinsic = client.api.tx.smartContractModule
141141
.cancelContract(contractId: contractId);
142142
await client.apply(extrinsic);
143-
// IF not found it means its cancelled successfully
144-
return await get(contractId: contractId);
145143
}
146144

147145
Future<BigInt?> setDedicatedNodeExtraFee(
148146
{required int nodeId, required int extraFee}) async {
149147
final extrinsic = client.api.tx.smartContractModule
150-
.setDedicatedNodeExtraFee(nodeId: nodeId, extraFee: extraFee);
148+
.setDedicatedNodeExtraFee(nodeId: nodeId, extraFee: BigInt.from(extraFee));
151149
await client.apply(extrinsic);
152150

153151
return await getDedicatedNodeExtraFee(nodeId: nodeId);

packages/tfchain_client/lib/tfchain_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import 'package:polkadart/polkadart.dart'
1515
import 'package:polkadart/scale_codec.dart';
1616
import 'package:polkadart_keyring/polkadart_keyring.dart';
1717
import 'package:tfchain_client/generated/dev/types/tfchain_runtime/runtime_call.dart';
18-
import 'package:tfchain_client/src/Error_mapping.dart';
1918
import 'package:tfchain_client/src/balances.dart' as balance;
2019
import 'package:tfchain_client/src/contracts.dart';
2120
import 'package:tfchain_client/generated/dev/dev.dart' as polkadot;
2221
import 'package:tfchain_client/src/dao.dart' as Dao;
22+
import 'package:tfchain_client/src/error_mapper.dart';
2323
import 'package:tfchain_client/src/farms.dart';
2424
import 'package:tfchain_client/src/kvstore.dart';
2525
import 'package:tfchain_client/src/nodes.dart';

packages/tfchain_client/test/balances_test.dart

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,48 @@ void main() {
2222
AccountInfo? accountInfo =
2323
await queryClient.balances.get(address: address);
2424
expect(accountInfo, isNull);
25-
} catch (e) {
26-
expect(e, isNotNull);
25+
} catch (error) {
26+
expect(error, isNotNull);
2727
}
2828
});
2929
});
30+
31+
group("Test Balances", () {
32+
late Client client;
33+
setUp(() async {
34+
client = Client(
35+
"wss://tfchain.dev.grid.tf/ws",
36+
"secret add bag cluster deposit beach illness letter crouch position rain arctic",
37+
"sr25519");
38+
await client.connect();
39+
});
40+
41+
test('Test Transfer TFTs with invalid amount', () async {
42+
try {
43+
await client.balances.transfer(
44+
address:
45+
"oven strong mention shoulder night ghost correct exercise surge lady jungle hundred",
46+
amount: 0);
47+
} catch (error) {
48+
expect(error, isNotNull);
49+
}
50+
});
51+
52+
// TODO:
53+
test('Test Transfer TFTs', () async {
54+
try {
55+
await client.balances.transfer(
56+
address: "5EcSXeEH35LriE2aWxX6v4yZSMq47vdJ1GgHEXDdhJxg9XjG",
57+
amount: 10);
58+
} catch (error) {
59+
print(error);
60+
expect(error, isNull);
61+
}
62+
});
63+
64+
test('Test get my balance', () async {
65+
AccountInfo? info = await client.balances.getMyBalance();
66+
expect(info, isNotNull);
67+
});
68+
});
3069
}

packages/tfchain_client/test/contracts_test.dart

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import 'package:tfchain_client/tfchain_client.dart';
66
void main() {
77
group("Query Contracts Test", () {
88
late QueryClient queryClient;
9-
setUp(() {
9+
setUp(() async {
1010
queryClient = QueryClient("wss://tfchain.dev.grid.tf/ws");
11+
await queryClient.connect();
1112
});
1213

1314
test('Test Get Deleted Contract by Id', () async {
@@ -83,11 +84,67 @@ void main() {
8384
}
8485
});
8586

86-
test('Test Get ADedicated Node extra fee', () async {
87+
test('Test Get a Dedicated Node extra fee', () async {
8788
BigInt? fee =
8889
await queryClient.contracts.getDedicatedNodeExtraFee(nodeId: 206);
8990
print(fee);
9091
expect(fee, BigInt.from(0));
9192
});
9293
});
94+
95+
group("Test Contracts", () {
96+
late Client client;
97+
setUp(() async {
98+
client = Client(
99+
"wss://tfchain.dev.grid.tf/ws",
100+
"secret add bag cluster deposit beach illness letter crouch position rain arctic",
101+
"sr25519");
102+
await client.connect();
103+
});
104+
105+
test('Test Create Node Contract with empty data', () async {
106+
try {
107+
BigInt? contractId = await client.contracts.createNode(
108+
nodeId: 11, deploymentData: [], deploymentHash: [], publicIps: 0);
109+
} catch (error) {
110+
expect(error, isNotNull);
111+
}
112+
});
113+
114+
test('Test Update Node Contract with wrong data', () async {
115+
try {
116+
Contract? contract = await client.contracts
117+
.updateNode(contractId: 11, deploymentData: [], deploymentHash: []);
118+
} catch (error) {
119+
expect(error, isNotNull);
120+
}
121+
});
122+
123+
test('Test Create Name Contract then cancel it', () async {
124+
BigInt? contractId =
125+
await client.contracts.createName(name: "contractname");
126+
expect(contractId, isNotNull);
127+
print(contractId);
128+
try {
129+
await client.contracts.cancel(contractId: contractId!);
130+
} catch (error) {
131+
expect(error, isNull);
132+
}
133+
});
134+
135+
test('Test Create Rent Contract with no solution provider', () async {
136+
try {
137+
BigInt? contractId = await client.contracts
138+
.createRent(nodeId: 72, solutionProviderId: 0);
139+
} catch (error) {
140+
expect(error.toString(), contains('NoSuchSolutionProvider'));
141+
}
142+
});
143+
144+
test('Test set dedicated node extra fee', () async {
145+
BigInt? extraFee = await client.contracts
146+
.setDedicatedNodeExtraFee(nodeId: 140, extraFee: 2);
147+
expect(extraFee, BigInt.from(2));
148+
});
149+
});
93150
}

0 commit comments

Comments
 (0)