0

I have my smart contract deployed on BSC Testnet:

https://testnet.bscscan.com/address/0x01AbBD3c43d447dA73CDee8d138697Df36f785d2

If I connect my Metamask on bscscan, I can use all the write contract methods, however, when I try to invoke methods using Python, I run into the problem of getting error: {'code': -32000, 'message': 'invalid sender'}, when sending the transaction. Here is my code sample:

import json

from web3 import Web3
from web3.middleware import geth_poa_middleware

web3 = Web3(Web3.HTTPProvider('https://data-seed-prebsc-1-s1.binance.org:8545/'))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)

account_from = {
    'private_key': 'My Private key',
    'address': '0x21Ba7305d6322cD215378D487FfB9C83C42dB82F',
}

contract_address = '0x01AbBD3c43d447dA73CDee8d138697Df36f785d2'

f = open('abi.json',)

abi_file = json.load(f)
abi = abi_file['abi']

contract = web3.eth.contract(address=contract_address, abi=abi)

mint_tx = contract.functions.mintOneNft(Web3.toChecksumAddress('0x46165Fb8A31ab3098BD730876546107271Ced745')).buildTransaction(
    {
        'from': account_from['address'],
        'nonce': web3.eth.get_transaction_count(account_from['address']),
    }
)

tx_create = web3.eth.account.sign_transaction(mint_tx, account_from['private_key'])

tx_hash = web3.eth.send_raw_transaction(tx_create.rawTransaction)

Cannot figure out what the problem is here, everything seems pretty straight forward. Help with how to fix it would be much appreciated!

2
  • I know this is a silly question - but have you verified that the private key is for the correct account?
    – natewelch_
    Jan 19 at 15:52
  • Yeah, checked with web3.eth.account.privateKeyToAccount(account_from['private_key']), got my account address, seems to work correctly.
    – Anil Panda
    Jan 19 at 18:10

1 Answer 1

0

The problem was that web3py library has some problems, when working with BSC.

The problem was that BSC requires gasPrice included into transaction. So, after modifying the code as follow:

mint_tx = contract.functions.mintOneNft(Web3.toChecksumAddress('0x46165Fb8A31ab3098BD730876546107271Ced745')).buildTransaction(
    {
        'from': account_from['address'],
        'nonce': web3.eth.get_transaction_count(account_from['address']),
        'gasPrice': web3.toWei('20', 'gwei'),
    }
)

Everything seems to work properly

2
  • "invalid sender" error has nothing to do with gas prices, it means you are using invalid chainID when signing the transaction
    – Nulik
    Jan 25 at 18:51
  • I suppose you just cross-pasted incorrect network ID somewhere in your code
    – Nulik
    Jan 25 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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