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

Send transactions and SmartContracts #126

Closed LuisRibeiroo closed 5 months ago

LuisRibeiroo commented 5 months ago

Copy from this.

Hello, everyone. I'm pretty new on web3 and still trying to get some basic items. I have few questions i couldn't found answer 'till now:

1. Can I send a transaction with specific token (like USDC or USDT) without using SmartContract? 1.1. If so, how could I pass the token trough the sendTransaction request? 1.2. If don't, there's any way to send specific tokens without SC?

2. How's the common/recommended way to get the ABI contracts for each token?

LuisRibeiroo commented 5 months ago

I tried to use ERC20 generic ABI contract to test on Metamask and when I created a transaction from that one, a I get a request with ERC identifier, instead of basic SepoliaETH or ETH.

This is the flow we're implementing right now to get the ERC20 transaction on MetaMask:

// Contract creation
final contract = w3m.DeployedContract(
        w3m.ContractAbi.fromJson(
          jsonEncode([
            /// ERC20 DEFAULT
            {
              "constant": false,
              "inputs": [
                {"name": "_to", "type": "address"},
                {"name": "_value", "type": "uint256"}
              ],
              "name": "transfer",
              "outputs": [
                {"name": "", "type": "bool"}
              ],
              "payable": false,
              "stateMutability": "nonpayable",
              "type": "function"
            },
          ]),
          "usdc",
        ),
        toAddress,
      );

// Add contract to Transaction
final transaction = w3m.Transaction.callContract(
        from: w3m.EthereumAddress.fromHex(w3mService.session!.address!),
        nonce: nonce,
        contract: contract,
        function: contract.function("transfer"),
        parameters: [
          toAddress,
          ethAmount.getInWei,
        ],
      ).toJson();

      final requestParams = w3m.SessionRequestParams(
        method: "eth_sendTransaction",
        params: [transaction],
      );

// Send request
responseValue = await w3mService.request(
            topic: w3mService.session!.topic!,
            chainId: chainId,
            request: requestParams,
);

Metamask result

image

quetool commented 5 months ago

Hello @LuisRibeiroo !

  1. Can I send a transaction with specific token (like USDC or USDT) without using SmartContract?

Regarding this the answer is a straight NO. Tokens are inherent to Smart Contracts, in fact, THEY ARE Smart Contracts as the contract decides the token's behavior, supply, price, etc... So in order to interact with specific token you need to interact with the Smart Contract.

That being said, the "weird" value you might be seeing in Metamask approval screen could be due to a wrong parsing of the value on your side.

Every token definition (Smart Contract) comes with a definition of the amount of decimals, meaning how small the smallest unit of the token is. In short, the amount that you would like to transfer would have to be divided but its smallest unit. For instance: You want to send 0.12 USDT and say USDT defines 18 decimals then you will send 0.12 / 10^18

I just pushed a better example on our sample dapp for you to understand it better, you can check the transfer (write) function here https://github.com/WalletConnect/Web3ModalFlutter/blob/master/example/lib/utils/crypto/eip155.dart#L350 and the Contract ABI here https://github.com/WalletConnect/Web3ModalFlutter/blob/master/example/lib/utils/crypto/smart_contracts/usdt_contract.dart

Regarding on how to get the ABI is as simply as follows: Every token has an address on its blockchain, more precisely, every Smart Contract has an address on its blockchain, say for instance USDT on Ethereum https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7 (same for Sepolia or every other test net) Now that you located your Contract you just go to the Contract tab, scroll down to Contract ABI and you just export it or copy it as in our example.

LuisRibeiroo commented 5 months ago

Thank you for the quick answer! With your explanation and example, we're able to send USDC and USDT transaction on Sepolia Testnet. 🚀 🤝

For futures engineers that get this problem here are some tips that I take a while to figure out: