'Web3 Python Gas Gwei not working: intrinsic gas too low

Why is the gas fee not working? If I remove the gas fee, the transaction works. But if I add a gas fee, the transaction fails and returns error:

{'code': -32000, 'message': 'intrinsic gas too low'}

web3matic = Web3(Web3.HTTPProvider(matic))

                    nonce = web3matic.eth.get_transaction_count(walletAddress)
                    result = contract.functions.buy(item, int(price)).buildTransaction({
                        'from': walletAddress,
                        'nonce': nonce,
                        'gas': 21000,
                        'gasPrice': web3matic.toWei(700, 'gwei'),
                    })
                    print(result)


Solution 1:[1]

Try setting gas to 3000000 and using w3.eth.gasPrice for gasPrice

Context: Polygon increased the min gas price for 1 Gwei to 30 in Oct 2021 https://forum.matic.network/t/recommended-min-gas-price-setting/2531

web3matic = Web3(Web3.HTTPProvider(matic))

                    nonce = web3matic.eth.get_transaction_count(walletAddress)
                    result = contract.functions.buy(item, int(price)).buildTransaction({
                        'from': walletAddress,
                        'nonce': nonce,
                        'gas': 3000000,
                        'gasPrice': web3matic.eth.gasPrice,
                    })
                    print(result)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Banjo Obayomi