youwallet / wallet

🚀 基于Flutter开发的跨终端数字货币交易所✨
https://youwallet.github.io/
116 stars 55 forks source link

【接口】授权交易和查询授权额度 #59

Open zhaobinglong opened 4 years ago

zhaobinglong commented 4 years ago

调用下面的函数完成授权,每个钱包在进行第一次买入和卖出时,需要对当前买入和卖出的token做一次授权,第二次开始就不需要授权了

  static Future approve(String token) async {
    String value = BigInt.from(10).pow(27) .toString();
    String postData = func['approve()'] + formatParam(proxy) + formatParam(value);
    print('approve proxy   => ${formatParam(proxy)}');
    print('approve value   => ${formatParam(value)}');
    print('approve postData=> ${postData}');

    String rpcUrl = "https://ropsten.infura.io/";
    String privateKey = await getPrivateKey('');
    final client = Web3Client(rpcUrl, Client());
    var credentials = await client.credentialsFromPrivateKey(privateKey);
    try {
      var rsp = await client.sendTransaction(
          credentials,
          Transaction(
              to: EthereumAddress.fromHex(token),
              gasPrice: EtherAmount.inWei(BigInt.from(20000000000)),
              maxGas: 7000000,
              value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 0),
              data: hexToBytes(postData)
          ),
          chainId: 3
      );
      print("approve rsp => ${rsp}");
    } catch (e) {
      print("catch error =》 ${e}");
    }
  }
zhaobinglong commented 4 years ago

deadline:2020-01-30 size:1

zhaobinglong commented 4 years ago

一次授权的log打印: image

zhaobinglong commented 4 years ago

接口返回交易的hash,拿到hash就代表授权成功了

zhaobinglong commented 4 years ago

https://github.com/youwallet/wallet/issues/63

zhaobinglong commented 4 years ago

授权之前需要获取密码,给用户提示:

为了便于后续兑换,需要您授权youwallet代理。youwallet只会在你授权的情况下才会执行交易,请放心授权!