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

Unable To Send Transactions #82

Closed
OmkarSsawant opened this issue Jan 17, 2024 · 5 comments
Closed

Unable To Send Transactions #82

OmkarSsawant opened this issue Jan 17, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@OmkarSsawant
Copy link

OmkarSsawant commented Jan 17, 2024

Describe the bug
I am Developing a application that can interact with a locally developed smart contract . The Connection and normal method contract calling works but I did not found any implementation for send transaction . In documentation it said to use the web3dart , but this requires private key to be stored in app . So I am trying this since 2 days now to just send the transaction using the session got from this plugin and tried many ways to do it but any of them did not work . Also looked at example app of this repo and tried to figure out, but is not working .Below are approches and their undesired outputs. I would really appreciate the response as soon as possible.

To Reproduce
Steps to reproduce the behavior:

  1. Run your own Smart Contract Locally and start node and deploy
  2. Setup web3modal_flutter as said in docs
  3. Try to call a method that is not just view and can also be payable
  4. See error or undesired behaviour

Expected behavior
Output should be transaction Id as it ,when done using web3dart . When done only using web3dart output is fine.But Real Wallet needs to be connected.

Screenshots/Codes and Outputs

 function registerDeveloper(
        bytes32 _name,
        bytes32 _profile_photo_ipfs,
        bytes32[] memory _techstack,
        bytes32 _profession
    )
    public returns (bool ){...}

Approches:

using Just web3dart (which works):

 return await _ethClient.sendTransaction(
        _mCreds,
        Transaction.callContract(
            contract: _contract,
            function: _registerDeveloper,
            parameters: [
              name.bytes32,
              profilePhotoIpfs.bytes32,
              techstack.map((e) => e.bytes32).toList(),
              profession.bytes32
            ]),
        chainId: 31337);

But when using wallet Credentials to pass in method sendTransaction. According to this solution

A2:

    var credentials = WalletConnectEthereumCredentials(
      wcClient: dapp,
      session: session,
    );
    final max = await _ethClient.estimateGas(
      data: _registerDeveloper.encodeCall( [
        name.bytes32,
        profilePhotoIpfs.bytes32,
        techstack.map((e) => e.bytes32).toList(),
        profession.bytes32
      ])
    );
    debugPrint(max.toString());

    var txn =  Transaction.callContract(
        contract: _contract,
        function: _registerDeveloper,
        maxGas: max.toInt(),
        parameters: [
          name.bytes32,
          profilePhotoIpfs.bytes32,
          techstack.map((e) => e.bytes32).toList(),
          profession.bytes32
        ]);



    var r  =  await _ethClient.sendTransaction(
        credentials,
       txn);
    debugPrint(r);

    return r;

A3:
using how it was called in example :

    final Transaction t = Transaction.callContract(  contract: _contract,
        function: _registerDeveloper,
        parameters: [
          name.bytes32,
          profilePhotoIpfs.bytes32,
          techstack.map((e) => e.bytes32).toList(),
          profession.bytes32
        ]);

debugPrint("---------------------------------------------------------------------------------");
    var s =  await  EIP155.ethSendTransaction(
        w3mService: _dapp,
        topic: topic,
        chainId: chainId,
        transaction: EthereumTransaction(
            from: from,
            to: _contract.address.hex,
          data: c.hex.encode(List<int>.from(t.data!))
          , value: "0x"),
        method: EIP155UIMethods.ethSendTransaction.name);
    debugPrint(s);
    return s;

The behaviour of above A2 & A3 is different the transaction is getting stuck , the control does not reach down to print s even after some minutes . When Direct web3dart send-transaction takes 2-3 secs. In case of A2 the maxgas is printed but r does not

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser :chrome
  • Version : Version 120.0.6099.217 (Official Build) (64-bit)

Smartphone (please complete the following information):(Emulator)

  • Device: Pixel7Pro
  • OS: Android
  • SDK: API 34
@quetool
Copy link
Collaborator

quetool commented Jan 17, 2024

Hello @OmkarSsawant ! We might need to take a closer look at what you are describing but that won't be quick. What I can tell you right now is that there's no other way to interact with smart contracts than with web3dart (which is indeed the only way it works for you).
You can check this answer WalletConnect/WalletConnectFlutterV2#246 (comment)
And this one just in case WalletConnect/WalletConnectFlutterV2#219 (comment)

@quetool
Copy link
Collaborator

quetool commented Jan 22, 2024

Hello @OmkarSsawant! Could you check this reply? It might help WalletConnect/WalletConnectFlutterV2#246 (comment)

@Mash-Woo
Copy link

I also happened to encounter this problem, and you can go ahead and consult my solution.

Step 1: Please add this

class CustomCredentialsSender extends CustomTransactionSender {
CustomCredentialsSender({
required this.signingEngine,
required this.sessionTopic,
required this.chainId,
required this.credentialsAddress,
});

final ISignEngine signingEngine;
final String sessionTopic;
final String chainId;
final EthereumAddress credentialsAddress;

@OverRide
EthereumAddress get address => credentialsAddress;

@OverRide
Future sendTransaction(Transaction transaction) async {
if (kDebugMode) {
print(
'CustomCredentialsSender: sendTransaction - transaction: ${transaction.prettyPrint}');
}

if (!signingEngine.getActiveSessions().keys.contains(sessionTopic)) {
  if (kDebugMode) {
    print(
        'sendTransaction - called with invalid sessionTopic: $sessionTopic');
  }
  return 'Internal Error - sendTransaction - called with invalid sessionTopic';
}

SessionRequestParams sessionRequestParams = SessionRequestParams(
  method: 'eth_sendTransaction',
  params: [
    {
      'from': transaction.from?.hex ?? credentialsAddress.hex,
      'to': transaction.to?.hex,
      'data':
          (transaction.data != null) ? bytesToHex(transaction.data!) : null,
      if (transaction.value != null)
        'value':
            '0x${transaction.value?.getInWei.toRadixString(16) ?? '0'}',
      if (transaction.maxGas != null)
        'gas': '0x${transaction.maxGas?.toRadixString(16)}',
      if (transaction.gasPrice != null)
        'gasPrice': '0x${transaction.gasPrice?.getInWei.toRadixString(16)}',
      if (transaction.nonce != null) 'nonce': transaction.nonce,
    }
  ],
);

if (kDebugMode) {
  print(
      'CustomCredentialsSender: sendTransaction - blockchain $chainId, sessionRequestParams: ${sessionRequestParams.toJson()}');
}

final hash = await signingEngine.request(
  topic: sessionTopic,
  chainId: chainId,
  request: sessionRequestParams,
);
return hash;

}

@OverRide
Future extractAddress() {
// TODO: implement extractAddress
throw UnimplementedError();
}

@OverRide
Future signToSignature(Uint8List payload,
{int? chainId, bool isEIP1559 = false}) {
// TODO: implement signToSignature
throw UnimplementedError();
}

@OverRide
MsgSignature signToEcSignature(Uint8List payload,
{int? chainId, bool isEIP1559 = false}) {
// TODO: implement signToEcSignature
throw UnimplementedError();
}
}

Step 2:
Use this in your function:

Credentials credentials = CustomCredentialsSender(
signingEngine: w3mService.web3App!.signEngine,
sessionTopic: w3mService.session!.topic!,
chainId: w3mService.selectedChain!.namespace,
credentialsAddress: EthereumAddress.fromHex(w3mService!.address!),
);

final transaction = Transaction.callContract(
contract: (your contract),
function: (name your function),
parameters: [
//According to your contract parameters
]);
final result = ethClient.sendTransaction(credentials, transaction,
chainId:
int.parse(loginProvider.w3mService.selectedChain!.chainId));

   w3mService.launchConnectedWallet();
   w3mService.addListener(() {
      result;
    });
   w3mService.notifyListeners();
        
      Good luck !!!
      
      @OmkarSsawant 

@crypblizz8
Copy link

@quetool Can you follow up here>

@crypblizz8 crypblizz8 added the bug Something isn't working label Feb 7, 2024
@quetool
Copy link
Collaborator

quetool commented Feb 7, 2024

For smart contract interaction please follow this issue WalletConnect/WalletConnectFlutterV2#255

@quetool quetool closed this as completed Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants