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'),
    } catch (e) {
      throw SmartContractException(e);

  final Credentials credentials;

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

  ContractFunction setName() => function('setName');
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter