python - 如何使用geth在钱包之间转移代币

标签 python ethereum web3py

过去几天我一直在努力尝试在 2 个以太坊钱包之间转移自定义代币。

我正在使用 populus (python),进行 ETH 转账似乎很容易,但我不明白如何使用自定义代币进行同样的操作。

这是我的Python代码:

from decimal import Decimal
import populus
from populus.utils.accounts import is_account_locked
from populus.utils.cli import request_account_unlock
from eth_utils import from_wei
from ico.utils import check_succesful_tx

# Which network we deployed our contract
chain_name = "horton"

# Owner account on geth
owner_address = "0xaeCb8415d5553F080d351e82b2000f123BFBc23C"

# Where did we deploy our token
contract_address = "0x15f173b7aca7cd4a01d5f8360e65fb4491d270c1"

receiver = "0x4c042bf285689891117AED16005004a6de2cC4FB"

amount = Decimal("1.0")

project = populus.Project()

with project.get_chain(chain_name) as chain:

    web3 = chain.web3
    print("Web3 provider is", web3.currentProvider)
    print("Owner address is", owner_address)
    print("Owner balance is", from_wei(web3.eth.getBalance(owner_address), "ether"), "ETH")

    # Goes through geth account unlock process if needed
    if is_account_locked(web3, owner_address):
        request_account_unlock(chain, owner_address, None)

    transaction = {"from": owner_address}
    FractionalERC20 = chain.contract_factories.FractionalERC20

    token = FractionalERC20(address=contract_address)
    decimals = token.call().decimals()
    decimal_multiplier = 10 ** decimals

    decimals = 18
    decimal_multiplier = 10 ** decimals
    print("Token has", decimals, "decimals")
    print("Owner token balance is", token.call().balanceOf(owner_address) / decimal_multiplier)

    # Use lowest denominator amount
    normalized_amount = int(amount * decimal_multiplier)

    # Transfer the tokens
    txid = token.transact({"from": owner_address}).transfer(receiver, normalized_amount)
    print("TXID is", txid)
    check_succesful_tx(web3, txid)

但是我在执行上述代码时遇到错误:

File "ICO_transfering_tokens.py", line 39, in <module>
    FractionalERC20 = chain.contract_factories.FractionalERC20
AttributeError: 'LocalGethChain' object has no attribute 'contract_factories'

我了解该错误,但不知道如何修复它。

最佳答案

显然您已从 ICO project 复制了此代码。为了让用户更容易回答,有必要告诉你源代码是从哪里获得的。 This project has its own chat group here .

出现异常的原因是,在 Populous 的某些版本中,API 已更改,而您使用的示例适用于旧版本的 API。请将 ICO 存储库和 Populous 升级到最新的兼容版本配置,您可以在 in ICO repo Travis output 找到.

请替换此行:

FractionalERC20 = chain.contract_factories.FractionalERC20

这样:

from ico.utils import get_contract_by_name

FractionalERC20 = = get_contract_by_name(chain, "FractionalERC20")

关于python - 如何使用geth在钱包之间转移代币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46745305/

相关文章:

python - OCR应用前图像清洗

python - 如何对二维列表中的元素进行排序?

python - 从图形工具中的邻接矩阵创建加权图,python 接口(interface)

jquery - 刷新 HTML 页面

compiler-errors - 为什么我的代码在重新混音时显示编译指示错误?

javascript - Solidity 函数修改值失败

blockchain - 尝试更新最新区 block 时遇到错误

python - 如何将 web3 导入到 pyscript 中?

python - 安装 web3 时出错 [tester] : Failed building wheel for blake2b-py

python - 如何在谷歌colab中安装web3