Thanx for your answer!
I’ve already applied the approve function on the front end and Metamask pops up asking the user for permission, but it still doesn’t work.
I will explain the logic which i follow right now. I’ve created two accounts on meta mask one for the user and the another one for the bot and the approve function asks the user to give permission for the bot to make a swap e.g. (using the bot account to make a swap on the user account )
This is my approve function:
const abi = [
{
constant: false,
inputs: [
{ internalType: “address”, name: “spender”, type: “address” },
{ internalType: “uint256”, name: “amount”, type: “uint256” }
],
name: “approve”,
outputs: [{ internalType: “bool”, name: “”, type: “bool” }],
payable: false,
stateMutability: “nonpayable”,
type: “function”
}
const approve = {
contractAddress: fromTokenAddress,
functionName: “approve”,
abi: abi,
params: {
spender: “THE BOT ADDRESS”,
amount: amount
}
};
const app = await Moralis.Web3API.native.runContractFunction(approve);
console.log(app)
and then on the server(backend) I am using the owner address to get the balance and then the bot private key to sign the transaction like the following.
sender_address = “ONER ADDRESS”
sellTokenContract = web3.eth.contract(spend, abi=sellAbi)
#Get Token Balance
balance = sellTokenContract.functions.balanceOf(sender_address).call()
symbol = sellTokenContract.functions.symbol().call()
readable = web3.fromWei(balance,'ether')
pancakeswap2_txn = contract.functions.swapExactTokensForETH(
amount ,0,
[ spend, to_tokenaddress],
sender_address,
(int(time.time()) + 1000000)
).buildTransaction({
'from': sender_address,
'gasPrice': web3.toWei('5','gwei'),
'nonce': web3.eth.get_transaction_count(sender_address),
})
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key='BOT PRIVATE KEY')
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(f"Sold {symbol}: " + web3.toHex(tx_token))
could you tell me why it givs an error pleas
Thanx in advance.