WalletConnect / Web3ModalFlutter

The Web3Modal for WalletConnect built using Flutter.
https://pub.dev/packages/web3modal_flutter
Apache License 2.0
43 stars 42 forks source link

having an error when signing a safe multisig wallet transaction connected with flutter #157

Open ezrablackfish2 opened 2 months ago

ezrablackfish2 commented 2 months ago

i am trying to connect flutter with safe multisig wallet in app.safe.global/sepolia and i tried to connect it with the documentation like this code snippet below and everything other than safe works like metamask but safe says the image i posted below and my code snippet is also attached help MicrosoftTeams-image

my code snippet is


      if (session != null && session.topic != null && session.address != null) {
      final accountAddress = session.address!;
      final result = await _w3mService.requestReadContract(
        deployedContract: _contract,
        functionName: 'balanceOf',
        rpcUrl: 'https://rpc.sepolia.org/',
        parameters: [
          EthereumAddress.fromHex(accountAddress.toString()),
        ],
     );

     if (result != null) {
        setState(() {
          _walletBalance = result.toString();
        });
        debugPrint('Wallet Balance: $result');
      } else {
        debugPrint('Error: result is null');
        setState(() {
          _walletBalance = 'Error';
        });
      }

      }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
      setState(() {
        _walletBalance = 'Error';
      });
    }
  }

  Future<void> _getTotalSupply() async {
    try {
      setState(() {
          _totalSupply = "has passed";
        });
      _contractAddress = EthereumAddress.fromHex(constants.CONTRACT_ADDRESS);
      _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.CONTRACT_ABI), "steeloDiamond"), _contractAddress!);
      final result = await _w3mService.requestReadContract(
        deployedContract: _contract,
        functionName: 'totalSupply',
        rpcUrl: 'https://rpc.sepolia.org/', 
     );      
     if (result != null) {
        setState(() {
          _totalSupply = result.toString();
        });
        debugPrint('Wallet Balance: $result');
      } else {
        debugPrint('Error: result is null');
        setState(() {
          _totalSupply = 'Error';
        });
      }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
      setState(() {
        _totalSupply = 'Error';
      });
    }
  }

  Future<void> name() async {
    try {
      setState(() {
          _name = "has passed";
        });
      _contractAddress = EthereumAddress.fromHex(constants.CONTRACT_ADDRESS);
      _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.CONTRACT_ABI), "steeloDiamond"), _contractAddress!);
      final result = await _w3mService.requestReadContract(
        deployedContract: _contract,
        functionName: 'name',
        rpcUrl: 'https://rpc.sepolia.org/', 
     );      
     if (result != null) {
        setState(() {
          _name = result.toString();
        });
        debugPrint('Wallet Balance: $result');
      } else {
        debugPrint('Error: result is null');
        setState(() {
          _name = 'Error';
        });
      }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
      setState(() {
        _name = 'Error';
      });
    }
  }

  Future<void> symbol() async {
    try {
      setState(() {
          _symbol = "has passed";
        });
      _contractAddress = EthereumAddress.fromHex(constants.CONTRACT_ADDRESS);
      _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.CONTRACT_ABI), "steeloDiamond"), _contractAddress!);
      final result = await _w3mService.requestReadContract(
        deployedContract: _contract,
        functionName: 'symbol',
        rpcUrl: 'https://rpc.sepolia.org/', 
     );      
     if (result != null) {
        setState(() {
          _symbol = result.toString();
        });
        debugPrint('Wallet Balance: $result');
      } else {
        debugPrint('Error: result is null');
        setState(() {
          _symbol = 'Error';
        });
      }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
      setState(() {
        _symbol = 'Error';
      });
    }
  }

  void _onPersonalSign() async {
    const _chainId = "11155111";
      var session = _w3mService.session;
    await _w3mService.launchConnectedWallet();
    if (session!.topic != null) {
    var hash = await _w3mService.web3App?.request(
      topic: session.topic!,
      chainId: 'eip155:$_chainId',
      request: SessionRequestParams(
        method: 'personal_sign',
        params: ['GM from W3M flutter!!', '0x009d1ce4bcc09492cb1dbe87724bdfbda9694612'],
      ),
    );
    debugPrint(hash);
  }
  }

  void transferUSDT() async {
    try {
    const _chainId = "11155111";
    await _w3mService.launchConnectedWallet();
        _contractAddress = EthereumAddress.fromHex(constants.USDT_ADDRESS);
        _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.USDT_ABI), "USDT"), _contractAddress!);
        var session = _w3mService.session;
        if (session != null && session.topic != null) {
    final accountAddress = session.address!;    
        final result = await _w3mService.requestWriteContract(
        topic: session.topic!,
        deployedContract: _contract,
        functionName: 'transfer',
        rpcUrl: 'https://rpc.sepolia.org/',
    chainId: 'eip155:$_chainId',
    transaction: Transaction(
            from: EthereumAddress.fromHex(accountAddress.toString()),
            to: EthereumAddress.fromHex('0x0d7bCe289D74790A4771e38b73F4CB56085cc296'),
            value: EtherAmount.inWei(BigInt.from(10).pow(6)),
        ),
     );  
     }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
    }
  }

  void transferSTLO() async {
    try {
    const _chainId = "11155111";
    await _w3mService.launchConnectedWallet();
        _contractAddress = EthereumAddress.fromHex(constants.CONTRACT_ADDRESS);
        _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.CONTRACT_ABI), "steeloDiamond"), _contractAddress!);
        var session = _w3mService.session;
        if (session != null && session.topic != null) {
    final accountAddress = session.address!;    
        final result = await _w3mService.requestWriteContract(
        topic: session.topic!,
        deployedContract: _contract,
        functionName: 'transfer',
        rpcUrl: 'https://rpc.sepolia.org/',
    chainId: 'eip155:$_chainId',
    transaction: Transaction(
            from: EthereumAddress.fromHex(accountAddress.toString()),
            to: EthereumAddress.fromHex('0x0d7bCe289D74790A4771e38b73F4CB56085cc296'),
            value: EtherAmount.fromInt(EtherUnit.finney, 10000),
        ),
     );  
     }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
    }
  }

void approveUSDT() async {
    try {
    const _chainId = "11155111";
    await _w3mService.launchConnectedWallet();
        _contractAddress = EthereumAddress.fromHex(constants.USDT_ADDRESS);
        _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.USDT_ABI), "USDT"), _contractAddress!);
        var session = _w3mService.session;
        if (session != null && session.topic != null) {
    final accountAddress = session.address!;    
        final result = await _w3mService.requestWriteContract(
        topic: session.topic!,
        deployedContract: _contract,
        functionName: 'approve',
        rpcUrl: 'https://rpc.sepolia.org/',
    chainId: 'eip155:$_chainId',
    transaction: Transaction(
            from: EthereumAddress.fromHex(accountAddress.toString()),
        ),
    parameters: [EthereumAddress.fromHex(constants.SWAPPER_ADRESS), BigInt.from(1000000)],
     );  
     }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
    }
  }

  void createSteeloUser() async {
    try {
    const _chainId = "11155111";
    await _w3mService.launchConnectedWallet();
      _contractAddress = EthereumAddress.fromHex(constants.CONTRACT_ADDRESS);
      _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.CONTRACT_ABI), "steeloDiamond"), _contractAddress!);
      var session = _w3mService.session;
      if (session != null && session.topic != null) {
      final accountAddress = session.address!;
      final result = await _w3mService.requestWriteContract(
        topic: session.topic!,
        deployedContract: _contract,
        functionName: 'createSteeloUser',
        rpcUrl: 'https://rpc.sepolia.org/',
    chainId: 'eip155:$_chainId',
    transaction: Transaction(
            from: EthereumAddress.fromHex(accountAddress.toString()),
        ),
    parameters: ['Hello world!'],
     );  
     }
    } catch (e) {
      debugPrint('Error : $e');
    }
  }

  void stakeSteelo() async {
    try {
    const _chainId = "11155111";
    await _w3mService.launchConnectedWallet();
        _contractAddress = EthereumAddress.fromHex(constants.CONTRACT_ADDRESS);
        _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.CONTRACT_ABI), "steeloDiamond"), _contractAddress!);
        var session = _w3mService.session;
        if (session != null && session.topic != null) {
        final accountAddress = EthereumAddress.fromHex(session.address!);
    final result = await _w3mService.requestWriteContract(
        topic: session.topic!,
        deployedContract: _contract,
        functionName: 'steeloBalanceOf',
        rpcUrl: 'https://rpc.sepolia.org/',
    chainId: 'eip155:$_chainId',
    transaction: Transaction(
            value: EtherAmount.fromInt(EtherUnit.finney, 10),
            from: EthereumAddress.fromHex('0x007e867674976AC9c22C1fcEd1721893A937a187'),
        ),
//  parameters: [],
     );  
     }
    } catch (e) {
      debugPrint('Error : $e');
    }
  }

   void swapUSDT() async {
    try {
    const _chainId = "11155111";
    await _w3mService.launchConnectedWallet();
        _contractAddress = EthereumAddress.fromHex(constants.SWAPPER_ADRESS);
        _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.SWAPPER_ABI), "Swapper"), _contractAddress!);
        var session = _w3mService.session;
        if (session != null && session.topic != null) {
        final accountAddress = session.address!;
        final result = await _w3mService.requestWriteContract(
        topic: session.topic!,
        deployedContract: _contract,
        functionName: 'swapUSDT',
        rpcUrl: 'https://rpc.sepolia.org/',
    chainId: 'eip155:$_chainId',
    transaction: Transaction(
            from: EthereumAddress.fromHex(accountAddress.toString()),
        ),
    parameters: [BigInt.from(1000000)],
     );  
     }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
    }
  }

  void approveSTLO() async {
    try {
    const _chainId = "11155111";
    await _w3mService.launchConnectedWallet();
        _contractAddress = EthereumAddress.fromHex(constants.CONTRACT_ADDRESS);
        _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.CONTRACT_ABI), "steeloDiamond"), _contractAddress!);
        var session = _w3mService.session;
        if (session != null && session.topic != null) {
    final accountAddress = session.address!;    
        final result = await _w3mService.requestWriteContract(
        topic: session.topic!,
        deployedContract: _contract,
        functionName: 'approve',
        rpcUrl: 'https://rpc.sepolia.org/',
    chainId: 'eip155:$_chainId',
    transaction: Transaction(
            from: EthereumAddress.fromHex(accountAddress.toString()),
        ),

    parameters: [EthereumAddress.fromHex(constants.SWAPPER_ADRESS), BigInt.from(190) * BigInt.from(10).pow(18)],
     );  
     }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
    }
  }

  void swapSTLO() async {
    try {
    const _chainId = "11155111";
    await _w3mService.launchConnectedWallet();
        _contractAddress = EthereumAddress.fromHex(constants.SWAPPER_ADRESS);
        _contract = DeployedContract(ContractAbi.fromJson(jsonEncode(constants.SWAPPER_ABI), "Swapper"), _contractAddress!);
        var session = _w3mService.session;
        if (session != null && session.topic != null) {
        final accountAddress = session.address!;
        final result = await _w3mService.requestWriteContract(
        topic: session.topic!,
        deployedContract: _contract,
        functionName: 'swapSTLO',
        rpcUrl: 'https://rpc.sepolia.org/',
    chainId: 'eip155:$_chainId',
    transaction: Transaction(
            from: EthereumAddress.fromHex(accountAddress.toString()),
        ),
    parameters: [BigInt.from(190) * BigInt.from(10).pow(18)],
     );  
     }
    } catch (e) {
      debugPrint('Error fetching wallet balance: $e');
    }
  }

  void _initializeService() async {
    W3MChainPresets.chains.putIfAbsent('11155111', () => _sepoliaChain);
    _w3mService = W3MService(
      projectId: "f346fee6117a12f3844a1f37fd6166b3",
      logLevel: LogLevel.error,
      metadata: const PairingMetadata(
        name: "W3M Flutter",
        description: "W3M Flutter test app",
        url: 'https://www.walletconnect.com/',
        icons: ['https://web3modal.com/images/rpc-illustration.png'],
        redirect: Redirect(
          native: 'w3m://',
          universal: 'https://www.walletconnect.com',
        ),
      ),
    );
    await _w3mService.init();
    _setUpNetwork();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
        SizedBox(height: 5),
        Text("Wallet Balance: $_walletBalance"),
        SizedBox(height: 5),
        Text("Total Supply: $_totalSupply"),
        SizedBox(height: 5),
        Text("Token Name: $_name"),
        SizedBox(height: 5),
        Text("Total Symbol: $_symbol"),
          ],
        ),
        W3MConnectWalletButton(service: _w3mService),
        W3MNetworkSelectButton(service: _w3mService),
        W3MAccountButton(service: _w3mService),
    ElevatedButton(onPressed: transferUSDT, child: const Text("transfer 1 USDT")),
    ElevatedButton(onPressed: transferSTLO, child: const Text("transfer 10 STLO")),
    ElevatedButton(onPressed: approveUSDT, child: const Text("approve 1 USDT")),
    ElevatedButton(onPressed: swapUSDT, child: const Text("swap 1 USDT")),
    ElevatedButton(onPressed: approveSTLO, child: const Text("approve 190 STLO")),
    ElevatedButton(onPressed: swapSTLO, child: const Text("swap 190 STLO")),
    ElevatedButton(onPressed: createSteeloUser, child: const Text("createSteeloUser")),
    ElevatedButton(onPressed: stakeSteelo, child: const Text("stakeSteelo")),

      ],
    );
  }
}

const _chainId = "11155111";

final _sepoliaChain = W3MChainInfo(
  chainName: 'Sepolia',
  namespace: 'eip155:$_chainId',
  chainId: _chainId,
  tokenName: 'ETH',
  rpcUrl: 'https://rpc.sepolia.org/',
  blockExplorer: W3MBlockExplorer(
    name: 'Sepolia Explorer',
    url: 'https://sepolia.etherscan.io/',
  ),
);
quetool commented 2 months ago

Hello @ezrablackfish2, I'll take a look as soon as I can, in the meantime, are you sure this is the complete code? Seems to be missing a part at the beginning.

quetool commented 2 months ago

Also, do you mind checking other wallets? Just to see if could be an issue on Safe side.

ezrablackfish2 commented 2 months ago

thank you so much for taking the time this simply is my plan i want to connect flutter with safe wallet and be able to call a smart contract that was a simple plan if there are other ways i be happy to do that

ezrablackfish2 commented 2 months ago

yea @quetool that pretty much is the whole code the main homepage