Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Deploy A Smart Contract Using Web3Dart #110

Open
Asadu43 opened this issue Jun 21, 2023 · 1 comment
Open

How to Deploy A Smart Contract Using Web3Dart #110

Asadu43 opened this issue Jun 21, 2023 · 1 comment

Comments

@Asadu43
Copy link

Asadu43 commented Jun 21, 2023

In Javascript We Deploy Smart contract something like that

let contract = new web3.eth.Contract(DonactionsAbi);
let deploy = await contract.deploy({ data: DonactionsByteCode, arguments: [address, Name] });

I just want to know how to Deploy smart contract in Dart

@redDwarf03
Copy link

perhaps try something like that. I hope it's a lead

import 'dart:convert';
import 'dart:developer';
import 'package:flutter/services.dart';
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';

class Contract {
  final String apiUrl = 'http://127.0.0.1:7545';

  Future<void> deploy(
    String privateKey,
    String address,
    String name,
  }) async {
    final privateKey = 'xxxxxxx';

    final web3Client = Web3Client(apiUrl, Client());
    final credentials = EthPrivateKey.fromHex(privateKey);

    final abiStringFile =
        await rootBundle.loadString('truffle/build/contracts/HTLC_ETH.json');
    final jsonAbi = jsonDecode(abiStringFile);
    final contract = DeployedContract(
      jsonAbi,
      EthereumAddress.fromHex(address),
    );

    final transaction = Transaction.callContract(
      contract: contract,
      function: contract.function('constructor'),
      parameters: [
        EthereumAddress.fromHex(address),
        name,
      ],
    );

    final transactionHash = await web3Client.sendTransaction(
      credentials,
      transaction,
    );
    final receipt = await web3Client.getTransactionReceipt(transactionHash);

    log('Contract deployed at ${receipt!.contractAddress!.hex}');
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants