0

I've been tring to code a bot to run on pancakeSwap and for some reason, all the transactions I try with this script keep getting reverted.

API_URL = 'https://data-seed-prebsc-1-s1.binance.org:8545/' #testnet
CONTRACT_ID = Web3.toChecksumAddress('0xc3f9c723ce06ee0fa420c8c88cee115fa0147748')

SENDER_ADDRESS = '00'
SENDER_PRIVATE_KEY = '00'

ROUTER_ADDRESS = '0xD99D1c33F9fC3444f8101754aBC46c52416550D1' #pancakeswap V2 router tesnet
Spend = Web3.toChecksumAddress('0xed24fc36d5ee211ea25a80239fb8c4cfd80f12ee') #spend binance 

Receive = CONTRACT_ID
max_approval_hex = f"0x{64 * 'f'}"
max_approval_int = int(max_approval_hex, 16)
w3 = Web3(Web3.HTTPProvider(API_URL))
contract = w3.eth.contract(address=ROUTER_ADDRESS, abi=ABI)

erc_20_contract = w3.eth.contract(address=Spend, abi=ERC20_ABI)
nonce = w3.eth.get_transaction_count(SENDER_ADDRESS)

#send coin to router
approve_txn = erc_20_contract.functions.transferFrom(SENDER_ADDRESS, ROUTER_ADDRESS, 1).buildTransaction({
    'from': SENDER_ADDRESS,
    'gas': w3.toWei('250000', 'wei'),
    'nonce':  nonce,
})
signed_txn = w3.eth.account.sign_transaction(approve_txn, private_key=SENDER_PRIVATE_KEY)
w3.eth.send_raw_transaction(signed_txn.rawTransaction)  

#approve the coin
approve_txn = erc_20_contract.functions.approve(ROUTER_ADDRESS, max_approval_int).buildTransaction({
    'from': SENDER_ADDRESS,
    'gas': w3.toWei('250000', 'wei'),
    'nonce': nonce + 1,
})
signed_txn = w3.eth.account.sign_transaction(approve_txn, private_key=SENDER_PRIVATE_KEY)
w3.eth.send_raw_transaction(signed_txn.rawTransaction)  

start = time.time()
pancakeswap_txn = contract.functions.swapExactTokensForTokens(
    100,
    10,
    [Spend, Receive],
    SENDER_ADDRESS,
    (int(time.time()) + 1000000)
).buildTransaction({
    'from': SENDER_ADDRESS,
    'gas': 100000,
    'gasPrice': w3.toWei('100', 'gwei'),
    'nonce':  nonce + 2,
})
signed_txn = w3.eth.account.sign_transaction(approve_txn, 
              private_key=SENDER_PRIVATE_KEY)
w3.eth.send_raw_transaction(signed_txn.rawTransaction)  

I've tried approving and not approving and the results are the same. I'm running on binance TestNet and I have BNB on my testnet wallet.

2
  • Why are you calling transferFrom from sender to router for 1 wei? Also 'gas': w3.toWei('250000', 'wei'), is the same as 'gas': 250000,
    – Ismael
    May 19 at 2:48
  • I dont really understand that part. Anyway, I think that pancakeswap router already handles the approval do I dont think thats needed. What i think is the problem is the contract IDs Im using, how do I find the right IDs for each token?
    – UnderScore
    May 19 at 3:42
0

Well, i figured it out. It turns out I was using too less gas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.