简体   繁体   English

在 ERC20 Dart 上创建 USDT 钱包

[英]Create USDT Wallet on ERC20 Dart

I want to understand for myself how to generate USDT ERC20 wallet in dart language.我想自己了解如何用 dart 语言生成 USDT ERC20 钱包。 I found the web3dart library.我找到了web3dart库。 But what does it take to generate a koschel using the 12-word phrase provided by the bip39 library?但是使用bip39库提供的 12 词短语生成 koschel 需要什么? 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?但是如何在 ERC20 上准确地制作 USDT?

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);

Since USDT is an erc-20 token, you can use the erc-20 abi for contract interactions.由于 USDT 是一个erc-20 代币,您可以使用erc-20 abi 进行合约交互。

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.现在编写一个类来与 abi 中的每个函数进行交互。 You need to pass the contract address, web3client (infura or any other) and chainID (1 for ethereum mainnet)您需要传递合约地址、web3client(infura 或任何其他)和 chainID(以太坊主网为 1)

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,现在你可以通过在类中编写 balanceOf 方法来获取你的 USDT 余额,如下所示,

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,转移USDT代币的功能,

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.查看我在 Medium 上的文章,使用 Flutter 的加密钱包应用程序,了解如何构建自己的 erc-20 代币并使用 Flutter 移动应用程序构建可以转移硬币的钱包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM