smart_contract_client.dart

PHOTO EMBED

Sun Jun 19 2022 15:00:27 GMT+0000 (UTC)

Saved by @alefl10 #dart

import 'package:smart_contract_client/smart_contract_client.dart';
import 'package:web3dart/web3dart.dart';

class SmartContractClient {
  SmartContractClient({
    required Web3Client web3client,
    required String privateKey,
    required String abi,
    required String contractAddress,
  })  : _web3Client = web3client,
        _privateKey = privateKey,
        _abi = abi,
        _contractAddress = contractAddress;

  SmartContract get _contract => SmartContract.fromData(
        abiInfo: _abi,
        privateKey: _privateKey,
        contractAddress: _contractAddress,
      );

  final Web3Client _web3Client;
  final String _privateKey;
  final String _abi;
  final String _contractAddress;

  Future<String> getName() async {
    try {
      final name = await _web3Client.call(
        contract: _contract,
        function: _contract.getName(),
        params: <dynamic>[],
      );

      return name[0] as String;
    } catch (e) {
      throw SmartContractClientException(e);
    }
  }

  Future<String> setName({required String name}) async {
    try {
      final transactionHash = await _web3Client.sendTransaction(
        _contract.credentials,
        Transaction.callContract(
          contract: _contract,
          function: _contract.setName(),
          parameters: <dynamic>[name],
        ),
      );
      return transactionHash;
    } catch (e) {
      throw SmartContractClientException(e);
    }
  }
}
content_copyCOPY