Search code examples
flutterdartweb3dart

Create USDT Wallet on ERC20 Dart


I want to understand for myself how to generate USDT ERC20 wallet in dart language. I found the web3dart library. But what does it take to generate a koschel using the 12-word phrase provided by the bip39 library? And I don’t understand, is it necessary to write a smart contract? I would like to see a small code example of how to generate a wallet. Many thanks.

Update:

I seem to have managed to generate a wallet. But how to make exactly USDT on ERC20?

var random = Random.secure();
var mnemonic = 'obvious width mechanic wheat cargo toe bike seek spirit jungle enlist thumb';
String mnemonicToSeedHex = bip39.mnemonicToSeedHex(mnemonic);
EthPrivateKey credentials = EthPrivateKey.fromHex(mnemonicToSeedHex);
Wallet wallet = Wallet.createNew(credentials, mnemonic, random);
var address = await credentials.extractAddress();
dev.log(address.hex);

Solution

  • Since USDT is an erc-20 token, you can use the erc-20 abi for contract interactions.

    final _erc20ContractAbi = web3.ContractAbi.fromJson(
            '[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]',
            'Erc20');
    

    Now write a class to interact with each functions in the abi. You need to pass the contract address, web3client (infura or any other) and chainID (1 for ethereum mainnet)

    class ERC20 extends web3.GeneratedContract {
      ERC20({
        required web3.EthereumAddress address,
        required web3.Web3Client client,
        int? chainId,
      }) : super(web3.DeployedContract(_erc20ContractAbi, address), client,
                chainId);
    

    Now you can get the balance of your USDT by writing a balanceOf method inside the class like this,

    Future<BigInt> balanceOf(
        web3.EthereumAddress account, {
        web3.BlockNum? atBlock,
      }) async {
        final function = self.abi.functions[2];
        assert(checkSignature(function, '70a08231'));
        final params = [account];
        final response = await read(function, params, atBlock);
        return (response[0] as BigInt);
      }
    

    Function to Transafer USDT tokens,

    Future<String> transfer(
        web3.EthereumAddress recipient,
        BigInt amount, {
        required web3.Credentials credentials,
        web3.Transaction? transaction,
      }) async {
        final function = self.abi.functions[7];
        assert(checkSignature(function, 'a9059cbb'));
        final params = [recipient, amount];
        return write(credentials, transaction, function, params);
      }
    

    Check out my article on Medium , Crypto-wallet app using flutter to get an idea on how to build your own erc-20 token and use flutter mobile application to build a wallet that can transfer the coins.