Flutter:如何使用flutter_web3包将事务发送到元掩码

htrmnn0y  于 2023-02-20  发布在  Flutter
关注(0)|答案(1)|浏览(152)

我使用flutter_web3包将flutter web应用程序连接到metamask,但是我如何将事务发送到metamask让用户付费呢?我试着根据包文档编写代码,但是不起作用。
连接到元掩码代码

import 'package:flutter/cupertino.dart';
import 'package:flutter_web3/flutter_web3.dart';

class MetaMaskProvider extends ChangeNotifier {
  static const operatingChain = 4;

  String currentAddress = '';

  int currentChain = 80001;

  bool get isEnabled => ethereum != null;

  bool get isInOperatingChain => currentChain == operatingChain;

  bool get isConnected => isEnabled && currentAddress.isNotEmpty;
  //final polygonWc = WalletConnectProvider.polygon();

  Future<void> connect() async {
    if (isEnabled) {
      final accs = await ethereum!.requestAccount();
      if (accs.isNotEmpty) currentAddress = accs.first;

      currentChain = await ethereum!.getChainId();

      notifyListeners();
    }
  }

  clear() {
    currentAddress = '';
    currentChain = 80001;
    notifyListeners();
  }

  init() {
    if (isEnabled) {
      ethereum!.onAccountsChanged((accounts) {
        clear();
      });
      ethereum!.onChainChanged((accounts) {
        clear();
      });
    }
  }
}

连接功能将打开元掩码,用户可以选择一个帐户并批准连接,类似于我如何发送事务到元掩码。

yhived7q

yhived7q1#

这就是:

void transferUsdt(num amount) {
    ContractERC20 contractERC20 = getUsdtContractErc20();

    contractERC20.transfer(
      'Recipient address e.g 0x...78',
      BigInt.from(amount),
    );
  }

  ContractERC20 getUsdtContractErc20() {
    final contractERC20 = ContractERC20(
      '0x337610d27c682E347C9cD60BD4b3b107C9d34dDd',
      provider!.getSigner(),
    );
    return contractERC20;
  }

相关问题