2
def send_tx_to_bsc_addr(tx_amount, tx_bsc_addr):
     bscWeb3 = Web3(Web3.HTTPProvider(bscTestnet_url))
     bscContarctABI_USDC = json.loads('[{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"uint8","name":"decimals","type":"uint8"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"constant":true,"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"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"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]')
     bscContarctAddr_USDC = bscWeb3.toChecksumAddress('0x64544969ed7ebf5f083679233325356ebe738930')
     bscContract_USDC = bscWeb3.eth.contract(address = bscContarctAddr_USDC, abi = bscContarctABI_USDC)
     bscNonce = bscWeb3.eth.getTransactionCount(bscAccount)

     bscTransfer_USDC = bscContract_USDC.functions.transfer(bscWeb3.toChecksumAddress(tx_bsc_addr),
     bscWeb3.toWei(tx_amount, 'wei')).buildTransaction({
       'chainId': 97,
       'from': bscAccount,
       'value': bscWeb3.toWei(tx_amount, 'wei'),
       'gas': 210000,
       'gasPrice': web3.toWei('50', 'gwei'),
       'nonce': bscNonce

     })

     bscSignedTx = bscWeb3.eth.account.signTransaction(bscTransfer_USDC, private_key = PRIVATE_KEY)
     print(bscSignedTx)
     bscTxHash = bscWeb3.eth.sendRawTransaction(bscWeb3.toHex(bscSignedTx.rawTransaction))

ERROR: web3.exceptions.ValidationError: Sending non-zero ether to a contract function with payable=False. Please ensure that transaction's value is 0.

IDK whats wrong with code, if someone know where is problem, plz help me.

1 Answer 1

1

In order to send ETH to a function, the function in the contract must be marked as PAYABLE ("allowed to receive ether")

if the function is not payable, value must be 0 'value': 0,

A payable function is something like this:

function deposit() payable {
  // do something
}
2
  • Could u show me please how to send for example 100 USDC token not 0 USDC, im just beginner in blockchain developing :) Aug 31, 2021 at 6:04
  • edited the answer; I suggest to approach solidity with some tutorials like cryptozombies.io
    – Stormsson
    Aug 31, 2021 at 6:40

Your Answer

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

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