Web3 Signing transaction issue Non-hexadecimal digit found (Python)

Question:

I am trying to complete a transaction of buying a token in binance smart chain and I am getting an error at sign transaction part which I am unable to resolve.

Code:

PancakeABI = open('pancakeABI','r').read().replace('n','')
bsc="https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
contract_id = web3.toChecksumAddress(tokaddr)

#Checking balance for test
balance = web3.eth.get_balance(sender_address)
humanReadable = web3.fromWei(balance,'ether')
print(humanReadable)

#Setup the contract
contract = web3.eth.contract(address=router_address, abi=PancakeABI)
nonce = web3.eth.get_transaction_count(sender_address)
start = time.time()
print(web3.toWei('0.02','ether'))

#Send transaction
pancakeswap2_txn = contract.functions.swapExactETHForTokens(
0, # here setup the minimum destination token you want to have, you can do some math, or you can put a 0 if you don't want to care
[spend,contract_id],
sender_address,
(int(time.time()) + 1000000)
).buildTransaction({
'from': sender_address,
'value': web3.toWei(0.02,'ether'),#This is the Token(BNB) amount you want to Swap from
'gas': 250000,
'gasPrice': web3.toWei('5','gwei'),
'nonce': nonce,
})

#Sign Transaction
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=private)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(web3.toHex(tx_token))

Here’s the error that I am recieving:
enter image description here

Asked By: Muneeb

||

Answers:

Issue Resolved:
Was using seed phrase as the private key.
The actual private key can be obtained this way:

  • go to metamask, click the 3 buttons in the corner and click account
    details and export key.
Answered By: Muneeb