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 interact with smart contract #1

Closed
royacodes opened this issue Jun 1, 2022 · 2 comments
Closed

how to interact with smart contract #1

royacodes opened this issue Jun 1, 2022 · 2 comments

Comments

@royacodes
Copy link

preparing an example code for interacting with smart contract would be appreciated

@mrtnetwork
Copy link

mrtnetwork commented Aug 2, 2022

I use web3dart and tron package to call the methods of the contract
For example, to call the transfer method

//class from web3dart you need contract abi and address
final DeployedContract _contract =
        DeployedContract(abi, contractAddress); 
    // contractAddress is EthereumAddress, You have to rewrite EthereumAddress  class to support Tron network address as well

  // Only this part changes to call other methods
    final _encodeParameters = _contract
        .function('transfer')
        .encodeCall([receiver!, _currentTransactionValue]);


    final block =
        await _getNowBlock(); // get now block from Tron protocol wallet/getnowblock

    // class from Tron package
    tron.TriggerSmartContract _value = tron.TriggerSmartContract(
        data: _encodeParameters,
        ownerAddress: hexToBytes(account.account.ethereumAddress.hex),
        contractAddress: hexToBytes(token!.ethAddress.hex));

     // class from Tron package
    any.Any parameter = any.Any(
        typeUrl: "type.googleapis.com/protocol.TriggerSmartContract",
        value: _value.writeToBuffer());

   // class from Tron package
    tron.Transaction_Contract _transactionContract = tron.Transaction_Contract(
      parameter: parameter,
      type: tron.Transaction_Contract_ContractType.TriggerSmartContract,
    );

    // class from Tron package
    tron.Transaction_raw rawData = tron.Transaction_raw(
        contract: [_transactionContract],
        expiration: fixnum.Int64(DateTime.now()
            .toUtc()
            .add(const Duration(hours: 1))
            .millisecondsSinceEpoch),
        timestamp: fixnum.Int64(
            (DateTime.now().toUtc().millisecondsSinceEpoch / 1000).round()),
        refBlockBytes: hexToBytes(block["raw_data"]["ref_block_bytes"]),
        refBlockHash: hexToBytes(block["raw_data"]["ref_block_hash"]),
        feeLimit: fixnum.Int64(10000000));

    // class from web3dart package
    Credentials credentials = account.account.accountCredentials;
    final trSignature = await credentials.signTron(rawData
        .writeToBuffer()); // I override  Credentials class on web3dart to sign tron

// class from tron package
    tron.Transaction _tr = tron.Transaction(
        rawData: rawData,
        signature: [hexToBytes(trSignature)]);

    final String signedTransacation = bytesToHex(_tr.writeToBuffer());
    final _send = {
      "transaction": signedTransacation,
    };

    final txId = await httpClient.normalPost(
        tronApi.buildApi("wallet/broadcasthex"),
        data: _send); // send transaction to network
    print("transaction id: ${txId["txId"]}");

If you want to call pure or View methods, you can use the following API and create a parameter like the one above.

for example:

  call() async{
    final DeployedContract _contract =
        DeployedContract(abi, contractAddress);
    final _encodeParameters =
        _contract.function('balanceOf').encodeCallWitoutSelector([address]);
    final Map<String, dynamic> _data = {
      "owner_address": "410000000000000000000000000000000000000000",
      "contract_address": "41a614f803b6fd780986a42c78ec9c7f77e6ded13c",
      "function_selector": "balanceOf(address)",
      "parameter": bytesToHex(_encodeParameters)
    };
    final data = await httpClient.normalPost(tronApi.buildApi("wallet/triggerconstantcontract"),data: _data);
  }

For such calls, you can only use the Web3Dart package and do it with JSON RPC.

@xclud
Copy link
Owner

xclud commented Dec 20, 2022

Sorry for my late answer. I assume this issue is fixed by now and closing this one. Please feel free to open a new issue any time.

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

3 participants