WalletConnect / WalletConnectModalFlutter

The WalletConnectModal for WalletConnect built using Flutter.
https://pub.dev/packages/walletconnect_modal_flutter
Apache License 2.0
20 stars 18 forks source link

smart contract deploy #9

Closed HadiDevLabx closed 12 months ago

HadiDevLabx commented 1 year ago

i have issue to create my own contract i have added gas int custom is i have to add things custom i need help to create contract and need documentation for deployed contract looking forward

import 'dart:ffi';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:web3dart/crypto.dart';
import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart';
import 'package:web3dart/web3dart.dart';

extension TransactionX on Transaction {
  Map<String, dynamic> toJson({
    String? fromAddress,
    int? gas,
  }) {
    return {
      'from': fromAddress ?? from?.hex,
      'to': to?.hex,
      'gas': '0x${gas!.toRadixString(16)}',
      'gasPrice': '0x${gasPrice?.getInWei.toRadixString(16) ?? '0'}',
      'value': '0x${value?.getInWei.toRadixString(16) ?? '0'}',
      'data': data != null ? bytesToHex(data!) : null,
      'nonce': nonce,
    };
  }
}
//testing
Future<String> testContractStore2({
  required Web3App walletConnect,
  required SessionData session,
  required String contractAddress,

  required  double value,
}) async {
  // Getting the deployed contract using ABI and contract address
  debugPrint("12");
  final contract = DeployedContract(
    ContractAbi.fromJson(
      await rootBundle.loadString('assets/abis/tokenAbi.abi.json'),
      'TestStorage1', // The name of the contract
    ),
    EthereumAddress.fromHex(contractAddress),
  );

  // Create a transaction from contract, mint function and data uri.
  final Transaction transaction = Transaction.callContract(
    contract: contract, // The contract to call the function on
    function: contract.function('transfer'), // The function to call
    parameters: [
      EthereumAddress.fromHex(contractAddress),
      BigInt.from(value),
     ],
  );

  // Send the transaction to the wallet to be executed
  return await walletConnect.request(
    topic: session.topic,
    chainId: NamespaceUtils.getChainFromAccount(
      session.namespaces.values.first.accounts.first,
    ),
    request: SessionRequestParams(
      method: 'eth_sendTransaction',
      params: transaction.toJson(
        // From address is the account of the session
        fromAddress: NamespaceUtils.getAccount(
          session.namespaces.values.first.accounts.first,
        ),
        gas: 30000
      ),
    ),
  );

}
/// Store in the smart contract
Future<String> testContractStore({
  required Web3App walletConnect,
  required SessionData session,
  required String contractAddress,
  required int value,
}) async {
  // Getting the deployed contract using ABI and contract address
  final contract = DeployedContract(
    ContractAbi.fromJson(
      await rootBundle.loadString('assets/abis/testContract.abi.json'),
      'TestStorage', // The name of the contract
    ),
    EthereumAddress.fromHex(contractAddress),
  );

  // Create a transaction from contract, mint function and data uri.
  final Transaction transaction = Transaction.callContract(
    contract: contract, // The contract to call the function on
    function: contract.function('store'), // The function to call
    parameters: [
      // These are the parameters that are passed to the function
      value, // The value to store in the contract
    ],
  );

  // Send the transaction to the wallet to be executed
  return await walletConnect.request(
    topic: session.topic,
    chainId: NamespaceUtils.getChainFromAccount(
      session.namespaces.values.first.accounts.first,
    ),
    request: SessionRequestParams(
      method: 'eth_sendTransaction',
      params: transaction.toJson(
        // From address is the account of the session
        fromAddress: NamespaceUtils.getAccount(
          session.namespaces.values.first.accounts.first,
        ),
      ),
    ),
  );
}
Luzzotica commented 1 year ago

What exactly is the issue?

"I need help creating (deploying?) contracts and documentation for deployed contracts" doesn't give me the info I need to help you.

It looks like you are trying to execute a custom contract call. I assume it isn't working. What's the error that you're getting?

HadiDevLabx commented 1 year ago
        Future<dynamic> future =   testContractStore2(walletConnect: widget.web3App, session: widget.session, contractAddress: '0xaaE9BcA71Eb2B03E36E9579C1C263042869d84A9', value: 0.00007355491950105679);

Future testContractStore2({ required Web3App walletConnect, required SessionData session, required String contractAddress,

required double value, }) async { // Getting the deployed contract using ABI and contract address debugPrint("12"); final contract = DeployedContract( ContractAbi.fromJson( await rootBundle.loadString('assets/abis/tokenAbi.abi.json'), 'TestStorage1', // The name of the contract ), EthereumAddress.fromHex(contractAddress), ); // Create a transaction from contract, mint function and data uri. BigInt amountInWei = BigInt.from(0.000001 * 1000000000000000000);

final Transaction transaction = Transaction.callContract( contract: contract, // The contract to call the function on function: contract.function('transfer'), // The function to call parameters: [ EthereumAddress.fromHex(contractAddress),

  amountInWei
   ],

); // Send the transaction to the wallet to be executed return await walletConnect.request( topic: session.topic, chainId: NamespaceUtils.getChainFromAccount( session.namespaces.values.first.accounts.first, ), request: SessionRequestParams( method: 'eth_sendTransaction', params: transaction.toJson( // From address is the account of the session fromAddress: NamespaceUtils.getAccount( session.namespaces.values.first.accounts.first, ), toAddress: "0x710dcda140962b487e6934e7fa8fa6be62a5c61e", ), ), );

}.

OUTPUT I/flutter (31325): │ #0 Pairing._onMessageEvent (package:walletconnect_flutter_v2/apis/core/pairing/pairing.dart:642:17) I/flutter (31325): │ #1 I/flutter (31325): ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ I/flutter (31325): │ 💡 Pairing _onMessageEvent, Received data: {id: 1691473930721016, jsonrpc: 2.0, error: {code: 5000, message: Cannot convert undefined value to object}} I/flutter (31325): └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── I/flutter (31325): snapshot: AsyncSnapshot(ConnectionState.done, null, JsonRpcError(code: 5000, message: Cannot convert undefined value to object), )

Luzzotica commented 12 months ago

I have created a custom contract call here: https://github.com/WalletConnect/Web3ModalFlutter/blob/2981053456ecd3511ca2e3a7f0def7c75e22af62/example/sign/lib/utils/crypto/eip155.dart#L197

Looks like you need to put your transaction json into an array to get it to work.