smart_contract.dart

PHOTO EMBED

Sun Jun 19 2022 14:57:57 GMT+0000 (UTC)

Saved by @alefl10 #dart

import 'dart:convert';

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

class SmartContract extends DeployedContract {
  SmartContract._(super.abi, super.address, this.credentials);

  factory SmartContract.fromData({
    required String abiInfo,
    required String privateKey,
    required String contractAddress,
  }) {
    try {
      final abiDecoded = jsonDecode(abiInfo) as Map<String, dynamic>;
      final abi = jsonEncode(abiDecoded['abi']);
      final address = EthereumAddress.fromHex(contractAddress);
      final credentials = EthPrivateKey.fromHex(privateKey);

      return SmartContract._(
        ContractAbi.fromJson(abi, 'HelloWeb3'),
        address,
        credentials,
      );
    } catch (e) {
      throw SmartContractException(e);
    }
  }

  final Credentials credentials;

  ContractFunction getName() => function('getName');

  ContractFunction setName() => function('setName');
}
content_copyCOPY