import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:flutter_wallet/tools/YSTools.dart'; import 'package:web3dart/web3dart.dart'; import 'package:http/http.dart' as http; class YSBip3{ Future fromAssets(String path, String contractAddress) async { final contractJson = jsonDecode(await rootBundle.loadString(path)); DeployedContract value = DeployedContract(ContractAbi.fromJson(jsonEncode(contractJson['abi']),contractJson['contractName'] as String),EthereumAddress.fromHex(contractAddress)); return value; } exchange(String contractAddress) async{ final client = Web3Client(YSData().rpc, http.Client()); final credentials = EthPrivateKey.fromHex(YSData().wallet['private']); final contract = await fromAssets('asset/abi.json',contractAddress); // return; final function = contract.function('swapExactTokensForTokens'); final amountIn = EtherAmount.fromBigInt(EtherUnit.wei, BigInt.parse('1')); // The amount of tokens you want to swap. final parameters = [ BigInt.from(amountIn.getValueInUnit(EtherUnit.wei)), // amountIn in wei BigInt.zero, // amountOutMin, you can set to a lower value or zero [EthereumAddress.fromHex('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'), EthereumAddress.fromHex('0x55d398326f99059fF775485246999027B3197955')], // The token path EthereumAddress.fromHex(YSData().wallet['public']), // Your address (DateTime.now().millisecondsSinceEpoch / 1000).floor() + 3600 // Deadline ]; final transaction = Transaction.callContract( contract: contract, function: function, parameters: parameters, ); await client.sendTransaction(credentials, transaction); } void performSmartContractExchange() async { const url = 'https://example.com/smart-contract-endpoint'; final response = await http.post(Uri.parse(url), body: { 'contractAddress': '0x1234567890abcdef', 'functionName': 'exchange', 'arguments': ['arg1', 'arg2'], }); if (response.statusCode == 200) { // 处理成功响应 final responseData = response.body; // 解析和处理响应数据 // ... } else { // 处理错误响应 print('请求失败:${response.statusCode}'); } } // void swapToken() async { // var httpClient = http.Client(); // var ethClient = Web3Client('https://bsc-quicknode-url', httpClient); // // // Your private key // var credentials = await ethClient.credentialsFromPrivateKey('your-private-key'); // // // PancakeSwap Router address // var pancakeRouter = EthereumAddress.fromHex('0x10ED43C718714eb63d5aA57B78B54704E256024E'); // // // The token you want to swap from // var tokenIn = EthereumAddress.fromHex('token-in-address'); // // // The token you want to swap to // var tokenOut = EthereumAddress.fromHex('token-out-address'); // // // Amount of tokens to swap (in Wei) // BigInt amountIn = EtherAmount.fromUnitAndValue(EtherUnit.ether, 1).getInWei; // // // The contract function to call // var function = ContractFunction('swapExactTokensForTokens', [ // TypeReference(type: 'uint256'), // TypeReference(type: 'uint256'), // TypeReference(type: 'address[]'), // TypeReference(type: 'address'), // TypeReference(type: 'uint256') // ]); // // // The parameters to pass in // var params = [ // amountIn, // BigInt.zero, // [tokenIn, tokenOut], // credentials.address, // (DateTime.now().millisecondsSinceEpoch / 1000).floor() + 60 * 20 // 20 minutes from now // ]; // // // The transaction to submit // var transaction = Transaction.callContract( // contract: Contract(pancakeRouter, [], deployedContract: DeployedContract(ContractAbi.fromJson('[]', 'PancakeRouter'), pancakeRouter)), // function: function, // parameters: params, // gasPrice: EtherAmount.inWei(BigInt.one), // maxGas: 200000, // value: amountIn, // ); // // // Send the transaction // var response = await ethClient.sendTransaction(credentials, transaction, fetchChainIdFromNetworkId: true); // // LogUtil.d(response); // } }