如何解决尝试使用 web3 和 ethersjs 在 nodejs 中进行煎饼交换交易时,交易失败
node_modules\@ethersproject\logger\lib\index.js:180
var error = new Error(message);
^
Error: transaction Failed (transactionHash="0x03e0911d26d2175d55b233b4a7b17d06202e7c2fb52a2ecfd35f3863814cb374",transaction={"nonce":364,"gasPrice":{"type":"BigNumber","hex":"0x02540be400"},"gasLimit":{"type":"BigNumber","hex":"0x7a1200"},"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","value":{"type":"BigNumber","hex":"0x00"},"data":"0xa5be382e000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000003975383df42df8ed045d636b255bf0829d8d5970000000000000000000000000000000000000000000000000000000006698a9df0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000055d398326f99059ff775485246999027b3197955","chainId":56,"v":147,"r":"0xc1a926e6f3989a50185b2d75c4bc877a76c8a2f30d505a1055fa89271feba035","s":"0x0ebeb2da65954541c82ddf468177cc6a49324a0e7a6e3fb3f5795f41c1055261","from":"0x3975383Df42Df8ED045d636b255Bf0829d8D5970","hash":"0x03e0911d26d2175d55b233b4a7b17d06202e7c2fb52a2ecfd35f3863814cb374","type":null},receipt={"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","contractAddress":null,"transactionIndex":19,"gasUsed":{"type":"BigNumber","hex":"0x5a67"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0xd504dfc3e7703e7657239e04f0d36e8118633689395cfc6fb9b3d6f213893724","transactionHash":"0x03e0911d26d2175d55b233b4a7b17d06202e7c2fb52a2ecfd35f3863814cb374","logs":[],"blockNumber":7497373,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x216ce6"},"status":0,"byzantium":true},code=CALL_EXCEPTION,version=providers/5.1.2)
at Logger.makeError (D:\codeforFun\322-uniswap-Trading-bot\node_modules\@ethersproject\logger\lib\index.js:180:21)
at Logger.throwError (D:\codeforFun\322-uniswap-Trading-bot\node_modules\@ethersproject\logger\lib\index.js:189:20)
at JsonRpcProvider.<anonymous> (D:\codeforFun\322-uniswap-Trading-bot\node_modules\@ethersproject\providers\lib\base-provider.js:1162:36)
at step (D:\codeforFun\322-uniswap-Trading-bot\node_modules\@ethersproject\providers\lib\base-provider.js:48:23)
at Object.next (D:\codeforFun\322-uniswap-Trading-bot\node_modules\@ethersproject\providers\lib\base-provider.js:29:53)
at fulfilled (D:\codeforFun\322-uniswap-Trading-bot\node_modules\@ethersproject\providers\lib\base-provider.js:20:58)
at processticksAndRejections (node:internal/process/task_queues:94:5) {
const ethers = require('ethers');
const {ChainId,Token,TokenAmount,Fetcher,Pair,Route,Trade,TradeType,Percent} =
require('@pancakeswap-libs/sdk');
const Web3 = require('web3');
const {JsonRpcProvider} = require("@ethersproject/providers");
require("dotenv").config();
const provider = new JsonRpcProvider('https://bsc-dataseed1.binance.org/');
const web3 = new Web3('wss://apis.ankr.com/wss/c40792ffe3514537be9fb4109b32d257/946dd909d324e5a6caa2b72ba75c5799/binance/full/main');
const { address: admin } = web3.eth.accounts.wallet.add(process.env.PRIVATE_KEY);
console.log(`Modulos cargados`);
// Command Line Input
const InputTokenAddr = web3.utils.tochecksumAddress(process.argv[2]);
// var BUSD = '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56';
const OutputTokenAddr = web3.utils.tochecksumAddress(process.argv[3]);
// var WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
const InputTokenAmount = process.argv[4]
const Slipage = process.argv[5];
const PANCAKE_ROUTER = process.argv[6];
// const PANCAKE_ROUTER_V2 = '0x10ed43c718714eb63d5aa57b78b54704e256024e';
// const PANCAKE_ROUTER_V1 = '0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F';
// 1/1000 = 0.001
const ONE_ETH_IN_WEI = web3.utils.toBN(web3.utils.toWei('1'));//BN->(BIG NUMBER) || toWei -> Converts any ether value value into wei.
const TradeAmount = ONE_ETH_IN_WEI.div(web3.utils.toBN('1000'));//TradeAmount = ONE_ETH_IN_WEI/1000
console.log(`TradeAmount ` + TradeAmount );
const init = async () => {
const [INPUT_TOKEN,OUTPUT_TOKEN] = await Promise.all(
[InputTokenAddr,OutputTokenAddr].map(tokenAddress => (
new Token(
ChainId.MAINNET,tokenAddress,18
)
)));
console.log(` <<<<<------- pair-------->>>>>`);
const pair = await Fetcher.fetchPairData(INPUT_TOKEN,OUTPUT_TOKEN,provider);
//console.log(JSON.stringify(pair));
console.log(` <<<<<------- route-------->>>>>`);
const route = await new Route([pair],INPUT_TOKEN);
//console.log(JSON.stringify(route));
console.log(` <<<<<------- Trade-------->>>>>`);
const Trade = await new Trade(route,new TokenAmount(INPUT_TOKEN,TradeAmount),TradeType.EXACT_INPUT);
//console.log(JSON.stringify(Trade));
//https://uniswap.org/docs/v2/javascript-SDK/Trading/
const slippagetolerance = new Percent(Slipage,'100'); //
console.log("slippagetolerance: " + JSON.stringify(slippagetolerance));
// create transaction parameters
const amountOutMin = Trade.minimumAmountOut(slippagetolerance).raw;
const path = [INPUT_TOKEN.address,OUTPUT_TOKEN.address];
const to = admin;
const deadline = Math.floor(Date.Now() / 1000) + 60 * 20;
// Create signer
const wallet = new ethers.Wallet(
Buffer.from(
process.env.PRIVATE_KEY,"hex"
)
);
const signer = wallet.connect(provider);
// Create Pancakeswap ethers Contract
const pancakeswap = new ethers.Contract(
PANCAKE_ROUTER,['function swapExactTokensForTokens(uint amountIn,uint amountOutMin,address[] calldata path,address to,uint deadline) external returns (uint[] memory amounts)'],signer
);
//Allow input token
if(true)
{
console.log(`Allow Pancakeswap <<<<<------- START-------->>>>>`);
let abi = ["function approve(address _spender,uint256 _value) public returns (bool success)"];
let contract = new ethers.Contract(INPUT_TOKEN.address,abi,signer);
let aproveResponse = await contract.approve(PANCAKE_ROUTER,ethers.utils.parseUnits('1000.0',18),{gasLimit: 100000,gasPrice: 5e9});
console.log(JSON.stringify(aproveResponse));
console.log(`Allow Pancakeswap <<<<<------- END-------->>>>>`);
}
if(true)
{
console.log(`Ejecutando transaccion`);
var amountInParam = ethers.utils.parseUnits(InputTokenAmount,18);
var amountOutMinParam = ethers.utils.parseUnits(web3.utils.fromWei(amountOutMin.toString()),18);
console.log("amountInParam: " + amountInParam);
console.log("amountOutMinParam: " + amountOutMinParam);
console.log("amountOutMin: " + amountOutMin);
const tx = await pancakeswap.swapExactTokensForTokens(
amountInParam,amountOutMinParam,path,to,deadline,{ gasLimit: ethers.utils.hexlify(300000),gasPrice: ethers.utils.parseUnits("9","gwei") }
);
console.log(`Tx-hash: ${tx.hash}`)
const receipt = await tx.wait();
console.log(`Tx was mined in block: ${receipt.blockNumber}`);
}
}
init();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。