-
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.
- Loading branch information
1 parent
84d4e25
commit e93fcc2
Showing
6 changed files
with
125 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"cSpell.words": ["polkadot"] | ||
} |
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 |
---|---|---|
@@ -1,5 +1,17 @@ | ||
import 'package:tfchain_client/tfchain_client.dart'; | ||
|
||
void main() { | ||
void main() async { | ||
final client = QueryClient("wss://tfchain.dev.grid.tf/ws"); | ||
client.connect(); | ||
// final id = await client.contracts.get(contractId: BigInt.from(49130)); | ||
// print(id!.twinId); | ||
|
||
// await client.contracts.getDeletionTime(id: BigInt.from(49130)); | ||
|
||
// final id = | ||
// await client.contracts.getContractIdByActiveRentForNode(nodeId: 11); | ||
// print(id); | ||
|
||
final fee = await client.contracts.getDedicatedNodeExtraFee(nodeId: 86); | ||
print("Fee $fee"); | ||
} |
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 |
---|---|---|
@@ -1,15 +1,29 @@ | ||
part of '../tfchain_client.dart'; | ||
// TODO: Disconnect | ||
|
||
class QueryClient { | ||
final String url; | ||
late final Provider api; | ||
late final Provider provider; | ||
late final polkadot.Dev api; | ||
late QueryContracts contracts; | ||
|
||
QueryClient(this.url) { | ||
api = newProvider(); | ||
newProvider(); | ||
contracts = QueryContracts(this); | ||
} | ||
|
||
Provider newProvider() { | ||
return Provider.fromUri(Uri.parse(url)); | ||
// TODO: Disconnect | ||
void newProvider() { | ||
final provider = Provider.fromUri(Uri.parse(url)); | ||
api = polkadot.Dev(provider); | ||
} | ||
|
||
void checkInputs() { | ||
if (url.isEmpty) { | ||
throw Exception("url should be provided"); | ||
} | ||
} | ||
|
||
void connect() async { | ||
checkInputs(); | ||
} | ||
} |
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,82 @@ | ||
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/grid_contract/name_contract_name.dart'; | ||
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/contract.dart'; | ||
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/contract_lock.dart'; | ||
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/service_contract.dart'; | ||
import 'package:tfchain_client/tfchain_client.dart'; | ||
|
||
const twoWeeks = 1209600000; | ||
|
||
class QueryContracts { | ||
final QueryClient client; | ||
QueryContracts(this.client); | ||
|
||
Future<Contract?> get({required BigInt contractId}) async { | ||
final res = | ||
await client.api.query.smartContractModule.contracts(contractId); | ||
return res as Contract; | ||
} | ||
|
||
Future<BigInt?> getContractIdByActiveRentForNode( | ||
{required int nodeId}) async { | ||
final res = await client.api.query.smartContractModule | ||
.activeRentContractForNode(nodeId); | ||
return res; | ||
} | ||
|
||
Future<List<int>> getActiveContracts({required int nodeId}) async { | ||
final res = | ||
await client.api.query.smartContractModule.activeNodeContracts(nodeId); | ||
return res; | ||
} | ||
|
||
Future<BigInt?> getContractIdByName({required String name}) async { | ||
final res = await client.api.query.smartContractModule | ||
.contractIDByNameRegistration(name as NameContractName); | ||
return res; | ||
} | ||
|
||
Future<BigInt?> getContractIdByNodeIdAndHash( | ||
{required int nodeId, required String hash}) async { | ||
final res = await client.api.query.smartContractModule | ||
.contractIDByNodeIDAndHash(nodeId, hash as List<int>); | ||
return res; | ||
} | ||
|
||
Future<ContractLock?> contractLock({required BigInt id}) async { | ||
final res = await client.api.query.smartContractModule.contractLock(id); | ||
return res; | ||
} | ||
|
||
Future<num> getDeletionTime({required BigInt id}) async { | ||
final contract = await get(contractId: id); | ||
if (contract != null && contract.state.toJson()["Created"] == null) { | ||
return 0; | ||
} | ||
|
||
//TODO: double check that GracePeriod typed like that. | ||
final blockNumber = contract!.state.toJson()["GracePeriod"]; | ||
try { | ||
final currentBlockNumber = await client.api.query.system.number(); | ||
|
||
// each block takes 6 seconds | ||
final gracePeriodStartTime = DateTime.now().millisecondsSinceEpoch - | ||
(currentBlockNumber - blockNumber) * 6000; | ||
return gracePeriodStartTime + twoWeeks; | ||
} catch (e) { | ||
throw Exception( | ||
"Error getting current block number for contract $id deletion: $e"); | ||
} | ||
} | ||
|
||
Future<ServiceContract?> getService({required BigInt serviceId}) async { | ||
final res = | ||
await client.api.query.smartContractModule.serviceContracts(serviceId); | ||
return res; | ||
} | ||
|
||
Future<BigInt?> getDedicatedNodeExtraFee({required int nodeId}) async { | ||
final res = await client.api.query.smartContractModule | ||
.dedicatedNodesExtraFee(nodeId); | ||
return res; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
library Client; | ||
library client; | ||
|
||
import 'package:polkadart/polkadart.dart' show Provider; | ||
import 'package:tfchain_client/src/contracts.dart'; | ||
import 'package:tfchain_client/generated/dev/dev.dart' as polkadot; | ||
|
||
part 'src/client.dart'; |