繁体   English   中英

Web3 bsc 令牌发送与 python,gas 费用计算。 ValueError: {'code': -32000, 'message': 'gas * price + value 资金不足'}

[英]Web3 bsc token send with python , gas fee calculation. ValueError: {'code': -32000, 'message': 'insufficient funds for gas * price + value'}

我想编写一个小的 python 程序,如果我的 acc2 有余额程序将检测到正余额并将其发送到我的另一个钱包 acc1。 使用 web3 bsc 创建事务出现错误:

"ValueError: {'code': -32000, 'message': 'insufficient funds for gas * price + value'}"

我不确定,但可能试图在交易中做错事。 我的 acc2 余额获得了代币和 bnb 用于支付汽油费。

from decimal import Decimal
from web3 import Web3
import time
import json


bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())

# acc_collector_private_key = 'acc2_pkpkpkpk'
acc2_pk='pkpkpkpk'

token_contract = web3.toChecksumAddress('contract of token')

token_abi ='abi'

acc1 = '111111'
acc2    = '222222'
  


token = web3.eth.contract(address=token_contract, abi=token_abi) 
target_token_balance = token.functions.balanceOf(acc2).call() 
target_coin_name=token.functions.name().call()
target_coin_symbol=token.functions.symbol().call()

print(target_coin_name)
print(web3.fromWei(target_token_balance,'ether'))
print(target_coin_symbol)


nonce = web3.eth.getTransactionCount(acc2)

tx = {
    'nonce' : nonce,
    'to' : acc1,
    'value':web3.toWei(target_token_balance,'ether'),
    'gas' : 21000,
    'gasPrice': web3.toWei('50','gwei')

}

signed_tx =web3.eth.account.signTransaction(tx,acc2_pk)
tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
trans = web3.toHex(tx_hash)

time.sleep(5)
transaction = web3.eth.get_transaction(trans)
print(transaction)

target_balance  = token.functions.balanceOf(acc2).call() 
print(target_balance)

我认为这是因为您将50 gwei放在了高逗号中。

from decimal import Decimal
from web3 import Web3
import time
import json


bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())

# acc_collector_private_key = 'acc2_pkpkpkpk'
acc2_pk='pkpkpkpk'

token_contract = web3.toChecksumAddress('contract of token')

token_abi ='abi'

acc1 = '111111'
acc2    = '222222'
  


token = web3.eth.contract(address=token_contract, abi=token_abi) 
target_token_balance = token.functions.balanceOf(acc2).call() 
target_coin_name=token.functions.name().call()
target_coin_symbol=token.functions.symbol().call()

print(target_coin_name)
print(web3.fromWei(target_token_balance,'ether'))
print(target_coin_symbol)


nonce = web3.eth.getTransactionCount(acc2)

tx = {
    'nonce' : nonce,
    'to' : acc1,
    'value':web3.toWei(target_token_balance,'ether'),
    'gas' : 21000,
    'gasPrice': web3.toWei(50,'gwei') #<---- without the ""

}

signed_tx =web3.eth.account.signTransaction(tx,acc2_pk)
tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
trans = web3.toHex(tx_hash)

time.sleep(5)
transaction = web3.eth.get_transaction(trans)
print(transaction)

target_balance  = token.functions.balanceOf(acc2).call() 
print(target_balance)

让我知道它是否有效。

账户余额 < gasPrice*gasLimmit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM