70 lines
2.3 KiB
Dart
70 lines
2.3 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:near_pay_app/core/models/contract.dart';
|
|
import 'package:near_pay_app/data/network/chains/near_rpc_client.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class ContractService {
|
|
final BlockchainClient _blockchainClient;
|
|
|
|
ContractService(this._blockchainClient);
|
|
|
|
/// Deploys a smart contract to the blockchain.
|
|
///
|
|
/// [contractCode]: The code of the smart contract to deploy.
|
|
/// [args]: Arguments to pass to the smart contract constructor.
|
|
Future<Contract> deployContract(String contractCode, Map<String, dynamic> args) async {
|
|
try {
|
|
// Compile contract code, estimate gas, etc.
|
|
final deploymentResult = await _blockchainClient.deployContract(contractCode, args);
|
|
return Contract.fromJson(deploymentResult as Map<String, dynamic>);
|
|
} catch (e) {
|
|
// Handle deployment errors
|
|
if (kDebugMode) {
|
|
print('Failed to deploy contract: $e');
|
|
}
|
|
rethrow; // Rethrow the exception for upstream handling
|
|
}
|
|
}
|
|
|
|
/// Calls a function on a deployed smart contract.
|
|
///
|
|
/// [contract]: The deployed contract instance.
|
|
/// [functionName]: The name of the function to call.
|
|
/// [args]: Arguments to pass to the function.
|
|
Future<dynamic> callContractFunction(Contract contract, String functionName, List<dynamic> args) async {
|
|
try {
|
|
// Execute contract function call
|
|
final result = await _blockchainClient.callContractFunction(contract as String, functionName, args);
|
|
return result;
|
|
} catch (e) {
|
|
// Handle function call errors
|
|
if (kDebugMode) {
|
|
print('Failed to call contract function: $e');
|
|
}
|
|
rethrow; // Rethrow the exception for upstream handling
|
|
}
|
|
}
|
|
|
|
/// Monitors events emitted by a smart contract.
|
|
///
|
|
/// [contract]: The deployed contract instance.
|
|
/// [callback]: The function to call when an event is emitted.
|
|
Future<void> monitorContractEvents(Contract contract, void Function(Map<String, dynamic>) callback) async {
|
|
try {
|
|
// Subscribe to contract events
|
|
await _blockchainClient.monitorContractEvents(contract as String, callback);
|
|
} catch (e) {
|
|
// Handle event monitoring errors
|
|
if (kDebugMode) {
|
|
print('Failed to monitor contract events: $e');
|
|
}
|
|
rethrow; // Rethrow the exception for upstream handling
|
|
}
|
|
}
|
|
|
|
getContractService(String near) {}
|
|
}
|