【问题标题】:Web3 bsc token send with python , gas fee calculation. ValueError: {'code': -32000, 'message': 'insufficient funds for gas * price + value'}Web3 bsc token 用 python 发送,gas 费计算。 ValueError: {'code': -32000, 'message': 'gas * price + value 资金不足'}
【发布时间】:2022-06-11 00:43:07
【问题描述】:

我想编写一个小的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)

【问题讨论】:

  • 'insufficient funds for gas * price + value' 的哪一部分不清楚? “我的 acc2 余额获得了代币和 bnb 用于支付汽油费。”你怎么知道的?您是否尝试检查交易成本?
  • 我投票结束这个问题,因为它似乎从根本上说是关于加密货币如何工作的问题,而不是关于编程的问题。

标签: python transactions web3 bsc


【解决方案1】:

我认为这是因为您将 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)

让我知道它是否有效。

【讨论】:

    【解决方案2】:

    账户余额

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 2018-05-03
      • 2021-11-19
      • 2018-03-20
      • 2019-01-03
      • 2019-04-18
      • 1970-01-01
      • 2018-11-30
      • 2018-04-30
      相关资源
      最近更新 更多