skalenetwork / docs.skale.space

https://docs.skale.space/
MIT License
0 stars 2 forks source link

web3Dart #49

Closed manuelbarbas closed 4 months ago

manuelbarbas commented 9 months ago

Web3Dart

Library written in dart that enables interaction with EVM compatible blockchains.

Some of the features of the library are:

For more informatio checkout the Web3Dart documentation page.

Implementation Example

Install web3dart package

dart pub add web3dart

Contract Call

// ignore_for_file: public_member_api_docs
import 'dart:io';

import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
import 'package:path/path.dart' show join, dirname;
import 'package:web_socket_channel/io.dart';

//Private Key
const String privateKey ='place your key';

// SKALE Chaos Chain RPC
// For other SKALE Testnet chains check https://testnet.portal.skale.space/chains
const String rpcUrl = 'https://staging-v3.skalenodes.com/v1/staging-fast-active-bellatrix';
// selected SKALE Chain ID
const int chainId = 1351057110;

// ABI of ERC_721 example contract
final File abiFile = File(join(dirname(Platform.script.path), 'nft_test_abi.json'));

Future<void> main() async {
  // start a client we can use to send transactions
  final client = Web3Client(rpcUrl, Client());

  final EthereumAddress mint_to_address = EthereumAddress.fromHex('Address to receive Mint');
  // Contract adddress deployed on SKALE Chaos Hub
  final EthereumAddress contract_address = EthereumAddress.fromHex('0xcAC71B8faB274429f79F76213fd3BC190041CAA4');

  //Credentials to sign tx
  final credentials = EthPrivateKey.fromHex(privateKey);

  //ABI file as string
  final abiCode = await abiFile.readAsString();

  final contract = DeployedContract(ContractAbi.fromJson(abiCode, 'NFT_721'), contract_address);
  //Selected contract function to call
  final mintFunction = contract.function('_mintTest');

  // Mint function call
  //Note: The chainId needs to be passes otherwise it will throw the error: -32004 Invalid transaction signature.
  final tx = await client.sendTransaction(
    credentials,
    Transaction.callContract(
      contract: contract,
      function: mintFunction,
      parameters: [mint_to_address],
    ),
    chainId: chainId
  );

  // Transaction Receipt
  final receipt = await client.addedBlocks().asyncMap((_) => client.getTransactionReceipt(tx)).firstWhere((receipt) => receipt != null);
  print(receipt);

  await client.dispose();
}

Run Dart Script

dart web/main.dart 
manuelbarbas commented 8 months ago

What is: Library Language: Dart Target: Web, Mobile