YSBip3.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'dart:convert';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_wallet/tools/YSTools.dart';
  4. import 'package:web3dart/web3dart.dart';
  5. import 'package:http/http.dart' as http;
  6. class YSBip3{
  7. Future<DeployedContract> fromAssets(String path, String contractAddress) async {
  8. final contractJson = jsonDecode(await rootBundle.loadString(path));
  9. DeployedContract value = DeployedContract(ContractAbi.fromJson(jsonEncode(contractJson['abi']),contractJson['contractName'] as String),EthereumAddress.fromHex(contractAddress));
  10. return value;
  11. }
  12. exchange(String contractAddress) async{
  13. final client = Web3Client(YSData().rpc, http.Client());
  14. final credentials = EthPrivateKey.fromHex(YSData().wallet['private']);
  15. final contract = await fromAssets('asset/abi.json',contractAddress);
  16. // return;
  17. final function = contract.function('swapExactTokensForTokens');
  18. final amountIn = EtherAmount.fromBigInt(EtherUnit.wei, BigInt.parse('1')); // The amount of tokens you want to swap.
  19. final parameters = [
  20. BigInt.from(amountIn.getValueInUnit(EtherUnit.wei)), // amountIn in wei
  21. BigInt.zero, // amountOutMin, you can set to a lower value or zero
  22. [EthereumAddress.fromHex('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'), EthereumAddress.fromHex('0x55d398326f99059fF775485246999027B3197955')], // The token path
  23. EthereumAddress.fromHex(YSData().wallet['public']), // Your address
  24. (DateTime.now().millisecondsSinceEpoch / 1000).floor() + 3600 // Deadline
  25. ];
  26. final transaction = Transaction.callContract(
  27. contract: contract,
  28. function: function,
  29. parameters: parameters,
  30. );
  31. await client.sendTransaction(credentials, transaction);
  32. }
  33. void performSmartContractExchange() async {
  34. const url = 'https://example.com/smart-contract-endpoint';
  35. final response = await http.post(Uri.parse(url), body: {
  36. 'contractAddress': '0x1234567890abcdef',
  37. 'functionName': 'exchange',
  38. 'arguments': ['arg1', 'arg2'],
  39. });
  40. if (response.statusCode == 200) {
  41. // 处理成功响应
  42. final responseData = response.body;
  43. // 解析和处理响应数据
  44. // ...
  45. } else {
  46. // 处理错误响应
  47. print('请求失败:${response.statusCode}');
  48. }
  49. }
  50. // void swapToken() async {
  51. // var httpClient = http.Client();
  52. // var ethClient = Web3Client('https://bsc-quicknode-url', httpClient);
  53. //
  54. // // Your private key
  55. // var credentials = await ethClient.credentialsFromPrivateKey('your-private-key');
  56. //
  57. // // PancakeSwap Router address
  58. // var pancakeRouter = EthereumAddress.fromHex('0x10ED43C718714eb63d5aA57B78B54704E256024E');
  59. //
  60. // // The token you want to swap from
  61. // var tokenIn = EthereumAddress.fromHex('token-in-address');
  62. //
  63. // // The token you want to swap to
  64. // var tokenOut = EthereumAddress.fromHex('token-out-address');
  65. //
  66. // // Amount of tokens to swap (in Wei)
  67. // BigInt amountIn = EtherAmount.fromUnitAndValue(EtherUnit.ether, 1).getInWei;
  68. //
  69. // // The contract function to call
  70. // var function = ContractFunction('swapExactTokensForTokens', [
  71. // TypeReference(type: 'uint256'),
  72. // TypeReference(type: 'uint256'),
  73. // TypeReference(type: 'address[]'),
  74. // TypeReference(type: 'address'),
  75. // TypeReference(type: 'uint256')
  76. // ]);
  77. //
  78. // // The parameters to pass in
  79. // var params = [
  80. // amountIn,
  81. // BigInt.zero,
  82. // [tokenIn, tokenOut],
  83. // credentials.address,
  84. // (DateTime.now().millisecondsSinceEpoch / 1000).floor() + 60 * 20 // 20 minutes from now
  85. // ];
  86. //
  87. // // The transaction to submit
  88. // var transaction = Transaction.callContract(
  89. // contract: Contract(pancakeRouter, [], deployedContract: DeployedContract(ContractAbi.fromJson('[]', 'PancakeRouter'), pancakeRouter)),
  90. // function: function,
  91. // parameters: params,
  92. // gasPrice: EtherAmount.inWei(BigInt.one),
  93. // maxGas: 200000,
  94. // value: amountIn,
  95. // );
  96. //
  97. // // Send the transaction
  98. // var response = await ethClient.sendTransaction(credentials, transaction, fetchChainIdFromNetworkId: true);
  99. //
  100. // LogUtil.d(response);
  101. // }
  102. }