WalletConnect / walletconnect-example-dapp

Example Dapp
https://example.walletconnect.org/
MIT License
363 stars 346 forks source link

Please add send erc20 token button #74

Open mobilekosmos opened 2 years ago

mobilekosmos commented 2 years ago

Please add a button that sends an erc20 token so I can test if the wallets works or not. I'm trying to build a code that sends erc20 tokens through walletconnect but it's not working and I couldn't find any sample, tests or anything about it, always only native transactions which are not the same.

oli-mo commented 2 years ago

Hello, I encountered the same problem.

Somehow Trust Wallet made a lot of problems, at least for MetaMask no problem.

// Some preparation ...
// DISCLAIMER: No compiler used right now, please check on your own.

// Get the wallet address that is connected.
// Check official example ...
const connectedWalletAddress = "0xFFFF..."

// ERC-20 information based on USDT on Ethereum
// Check: https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7
const erc20ContractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"
const erc20Decimals = 6

// Define recipient address
let recipientAddress = "0xABCDEFG..."

// Remove the "0x" part if available.
// The reason is that the data string
// we build below expects a format without that.
if (recipientAddress.indexOf("0x") >= 0)
    recipientAddress = recipientAddress.replace("0x", "")

// Our data string starts with "0x"
let dataStr = "0x"
// Add the function signature of "transfer"
dataStr += "a9059cbb"
// Add some padding ...
dataStr += "000000000000000000000000"
// Then, add our prepared recipient address
dataStr += recipientAddress

// Use tokenAmount
let paddedTokenAmount = pad(tokenAmount, erc20Decimals)

let hexTokenAmount = BigInt(paddedTokenAmount).toString(16)
dataStr += pad(hexTokenAmount, 64)

myTx = {
    from: connectedWalletAddress,
    to: erc20ContractAddress,
    value: "0x0",
    data: dataStr
}
await connector.sendTransaction(myTx);