RootSoft / walletconnect-dart-sdk

Open protocol for connecting dApps to mobile wallets with QR code scanning or deep linking.
MIT License
104 stars 61 forks source link

Send tokens other than ETH #69

Open LongTien15 opened 1 year ago

LongTien15 commented 1 year ago

EthereumWalletConnectProvider send transasction only to send eth, i want to send other tokens in my metamask wallet like DAI,..

RootSoft commented 1 year ago

A transaction sending an ERC-20 token needs to have the token contract as the recipient (the to field), and the data field containing encoded instructions to execute its transfer() function along with address of the token receiver and amount.

Here's some more information: https://stackoverflow.com/a/70847856/798314

LongTien15 commented 1 year ago

I did it but metamask does not recognize the token. I think it is because in wallet_connect send_transaction, the value is always in ETH?

RootSoft commented 1 year ago

@LongTien15 You can try and write your own sendTransaction and play with the values using sendCustomRequest

  Future<String> sendTransaction({
    required String from,
    String? to,
    Uint8List? data,
    int? gas,
    BigInt? gasPrice,
    BigInt? value,
    int? nonce,
  }) async {
    final result = await connector.sendCustomRequest(
      method: 'eth_sendTransaction',
      params: [
        {
          'from': from,
          if (data != null) 'data': hex.encode(List<int>.from(data)),
          if (to != null) 'to': to,
          if (gas != null) 'gas': '0x${gas.toRadixString(16)}',
          if (gasPrice != null) 'gasPrice': '0x${gasPrice.toRadixString(16)}',
          if (value != null) 'value': '0x${value.toRadixString(16)}',
          if (nonce != null) 'nonce': '0x${nonce.toRadixString(16)}',
        }
      ],
    );

    return result;
  }
LongTien15 commented 1 year ago

Capture

It's always ETH. I want it to be my ERC20 tokens

LongTien15 commented 1 year ago

I called the transfer from, from my account to the DAI ERC20 smart contract

LongTien15 commented 1 year ago

Sorry, my bad. My parameters are wrong. Thank you

LongTien15 commented 1 year ago

Managed to send some DAI to the contract. But i cannot approve it. calling send transaction with the approve function as the data not working.

moesaid commented 1 year ago

@LongTien15

Managed to send some DAI to the contract. But i cannot approve it. calling send transaction with the approve function as the data not working.

any update ?

bansalgaurav852 commented 1 year ago

@LongTien15 You can try and write your own sendTransaction and play with the values using sendCustomRequest

  Future<String> sendTransaction({
    required String from,
    String? to,
    Uint8List? data,
    int? gas,
    BigInt? gasPrice,
    BigInt? value,
    int? nonce,
  }) async {
    final result = await connector.sendCustomRequest(
      method: 'eth_sendTransaction',
      params: [
        {
          'from': from,
          if (data != null) 'data': hex.encode(List<int>.from(data)),
          if (to != null) 'to': to,
          if (gas != null) 'gas': '0x${gas.toRadixString(16)}',
          if (gasPrice != null) 'gasPrice': '0x${gasPrice.toRadixString(16)}',
          if (value != null) 'value': '0x${value.toRadixString(16)}',
          if (nonce != null) 'nonce': '0x${nonce.toRadixString(16)}',
        }
      ],
    );

    return result;
  }

hey when i use this result coming null how i can fix this?? i wanted write on smartcontract using walletconnect