0

我一直在玩 Hedera SDK 和它的 Python 包装器。几个小时前,我有一个工作示例,我能够创建一个具有初始余额的新帐户。

现在,几个小时后,相同的代码失败并出现以下错误(Testnet):

Exception has occurred: JavaException
JVM exception occurred: Hedera transaction `0.0.29467648@1642981813.928678616` failed pre-check with the status `INSUFFICIENT_TX_FEE` com.hedera.hashgraph.sdk.PrecheckStatusException
  File "/home/.../src/utils/hedera.py", line 101, in __init__
    AccountCreateTransaction()
  File "/home/.../src/main.py", line 21, in <module>
    new_account: HederaAccount = HederaAccount(client, initial_balance=100_000)

我一直试图找到根本原因并修复,但到目前为止没有运气(官方文档/github/anywhere)。我在 GitHub 中找到了一些评论,其中提到在配置客户端时必须指定最大交易费用,其值大于 100_000 tinybars。我做到了,但仍然是同样的错误。

这是我用来配置客户端的辅助函数:

def get_client(
    account_id: Optional[AccountId] = None, private_key: Optional[PrivateKey] = None
) -> Client:
    logger.debug(f"Hedera::get_client - create account_id")
    _account_id: AccountId = (
        account_id if account_id else Hedera.load_account_from_id()
    )

    logger.debug(f"Hedera::get_client - create account_id")
    private_key: PrivateKey = (
        private_key
        if private_key
        else Hedera.load_private_key(config["account"]["private_key"])
    )

    logger.debug(f"Hedera::get_client - create client")
    client: Client = (
        Client.forTestnet()
        if config["env"] == DeploymentEnv.Development.value
        else Client.forMainnet()
    )

    logger.debug(f"Hedera::get_client - set operator")
    client.setOperator(_account_id, private_key)

    # I just added this line (not required a couple of hours ago), still same error
    client.setMaxTransactionFee(Hbar.fromTinybars(200_000))

    logger.debug(f"Hedera::get_client - client: {client.toString()}")
    return client

这是创建新帐户和引发错误的类:

class HederaAccount:
    def __init__(
        self,
        client,
        account_id: Optional[AccountId] = None,
        private_key: Optional[PrivateKey] = None,
        initial_balance: Optional[int] = 1_000_000,
    ) -> None:
        self.client: Client = client

        if account_id:
            logger.debug(
                f"HederaAccount::init - existent account id: {account_id.toString()}"
            )

            if not private_key:
                raise Exception(
                    "When loading an existing account, 'private_key' is required"
                )

            self.account_id = account_id
            self.private_key = private_key
            self.public_key = self.private_key.getPublicKey()
            self.node_id: Optional[AccountId] = None

        else:
            self.private_key: PrivateKey = PrivateKey.generate()
            self.public_key: PublicKey = self.private_key.getPublicKey()

            tx_resp: TransactionResponse = (
                AccountCreateTransaction()
                .setKey(self.public_key)
                .setInitialBalance(Hbar.fromTinybars(initial_balance))
                .execute(client)
            )
            self.node_id: AccountId = tx_resp.nodeId

            tx_receipt: TransactionReceipt = tx_resp.getReceipt(self.client)
            self.account_id: AccountId = tx_receipt.accountId

            logger.debug(
                f"HederaAccount::init - new account id: {self.account_id.toString()}"
            )

这就是我创建/加载帐户的方式:

root_account_id = Hedera.load_account_id()
root_private_key = Hedera.load_private_key()
client = Hedera.get_client(account_id=root_account_id, private_key=root_private_key)

# Load root account (no problems here)
root_account: HederaAccount = HederaAccount(
    client, account_id=root_account_id, private_key=root_private_key
)
logger.info(f"Root account: {root_account}")
logger.info("\n\n")

# Create a new account (Failing here)
new_account: HederaAccount = HederaAccount(client, initial_balance=100_000)
logger.info(f"New account: {new_account}")
logger.info("\n\n")

由于我在测试网中运行我的代码,我的帐户 (root_account) 有足够的 HBAR 来支付创建新帐户 (new_account) 的费用。

hedera-sdk-py:2.6.0

回购: https ://github.com/ccddan/hbar-hello-world/blob/feature/nfts/src/nft.py

欢迎任何提示,包括指向良好Hedera 文档的链接。谢谢!

4

1 回答 1

2

事实证明,在撰写本文时,最低金额client.setMaxTransactionFee() 必须为 1 HBAR。这在文档中没有明确提及,您不能尝试使用较低的值:

https://docs.hedera.com/guides/getting-started/environment-set-up

文档截图在这里

更新我的客户端配置后,client.setMaxTransactionFee(Hbar(1))一切都按预期工作

于 2022-01-27T03:31:12.797 回答