Reputation: 11
Im trying to make swap on PancakeSwapRouterV3(0x13f4EA83D0bd40E75C8222255bc855a974568Dd4) with web3js using method exactInputSingle. With v2 version i could do this with swapExactTokensForETH method but is no longer in new contract.
I want to swap native token in SmartChain and swap it to other token(busd for example) but my transaction keeps getting reverted.
Want to buy/sell some token by web3js.
For example i tried to make BUSD-USDT swap. Heres my code:
1.BUSD approval for contract spending
exports.approve_busd = async function (pancakeSwapContractAddress) { let BUSDContractAddress = process.env.BUSD_CONTRACT_ADDRESS; let BUSDContractFile = process.env.BUSD_CONTRACT_FILE; let BUSDAbi = JSON.parse(fs.readFileSync(BUSDContractFile)); let BUSDContract = new web3.eth.Contract(BUSDAbi, BUSDContractAddress);
try {
let fromAddress = process.env.USER_ADDRESS;
let gasPrice = await web3.eth.getGasPrice();
let gas = 320000;
let private_key = process.env.USER_PK;
let count = await web3.eth.getTransactionCount(fromAddress);
let txHash = null;
try {
let rawTxApproveBUSD = {
nonce: web3.utils.toHex(count),
gasPrice: web3.utils.toHex(gasPrice),
from: fromAddress,
to: BUSDContractAddress,
value: '0x00',
gas: gas,
data: BUSDContract.methods.approve(
pancakeSwapContractAddress,
BigInt(10000000000000000000000000),
).encodeABI(),
};
let signedTransactionApprove = await web3.eth.accounts.signTransaction(rawTxApproveBUSD, private_key, function () {
log.info(`Transaction signed from ${fromAddress} to ${BUSDContractAddress} for approve;`);
}
);
await web3.eth.sendSignedTransaction(signedTransactionApprove.rawTransaction, async function (error, hash) {
txHash = hash;
});
} catch (e) {
log.error('Error while transfer eth: ', e);
return;
}
log.info(`Transaction sent from ${fromAddress} to ${BUSDContractAddress} for approve`);
console.log("Transaction hash: " + txHash);
console.log(`\n \n ***** END *****`);
} catch (e) {
log.error('Error while transfer eth: ', e);
}
}
2.My transaction send method:
exports.buy_tokens_by_busd_v3 = async function (pancakeSwapContractAddress, pancakeSwapContractFile) { await this.approve_busd(pancakeSwapContractAddress)
const BUSDContractAddress = process.env.BUSD_CONTRACT_ADDRESS;
const USDTContractAddress = process.env.USDT_CONTRACT_ADDRESS;
const BUSDContractFile = process.env.BUSD_CONTRACT_FILE;
const fromAddress = process.env.USER_ADDRESS;
try {
const BUSDAbi = JSON.parse(fs.readFileSync(BUSDContractFile));
const BUSDContract = new web3.eth.Contract(BUSDAbi, BUSDContractAddress);
const pancakeSwapAbi = JSON.parse(fs.readFileSync(pancakeSwapContractFile));
const pancakeSwapContract = new web3.eth.Contract(pancakeSwapAbi, pancakeSwapContractAddress);
const BUSDBalance = await BUSDContract.methods.balanceOf(fromAddress).call();
const count = await web3.eth.getTransactionCount(fromAddress);
const deadline = (await web3.eth.getBlock("latest")).timestamp + 10000;
console.log("BUSD BALANCE");
console.log(BUSDBalance);
console.log('deadline');
console.log(deadline);
console.log('actual timestamp');
console.log((await web3.eth.getBlock("latest")).timestamp);
let gasPrice = await web3.eth.getGasPrice();
let gas = 2100000;
const valueToSend = web3.utils.toWei("0.1", 'ether');
const params = {
tokenIn: BUSDContractAddress,
tokenOut: USDTContractAddress,
fee: 3000,
recipient: fromAddress,
deadline: deadline,
amountIn: web3.utils.toHex(valueToSend),
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
}
const tx = pancakeSwapContract.methods.exactInputSingle(params);
// const gas = await tx.estimateGas({from: fromAddress});
console.log('gas');
console.log(gas);
console.log('value to send: ');
console.log(valueToSend);
const txArgs = {
nonce: web3.utils.toHex(count),
gasPrice: web3.utils.toHex(gasPrice),
from: fromAddress,
to: pancakeSwapContractAddress,
value: '0x00',
gas: gas,
data: tx.encodeABI()
};
let private_key = process.env.USER_PK;
let txHash = null;
try {
let signedTransaction = await web3.eth.accounts.signTransaction(txArgs, private_key, function () {
log.info(`Transaction signed from ${fromAddress} to ${pancakeSwapContractAddress} on ${valueToSend} WEI`);
}
);
await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction, async function (error, hash) {
console.log(error);
txHash = hash;
});
} catch (e) {
console.log(e);
log.error('Error while transfer: ', e);
console.log(` Balance: ${balance}, ValueToSend: ${valueToSend}. Gas price ${gasPrice}, Gas Value ${gasValue}`);
return;
}
log.info(`Transaction sent from ${fromAddress} to ${pancakeSwapContractAddress} on ${valueToSend} WEI`);
console.log("Transaction hash: " + txHash);
console.log(`\n \n ***** END *****`);
} catch (e) {
log.error('Error occurred: ', e);
}
}
Upvotes: 0
Views: 536
Reputation: 23
If the transaction hits the network and the rates are well calculated you need to debug the tx, for that I recommend you tools like tenderly
Upvotes: 0