You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复NEAR代币部分Unstake时出现的TxExecutionError错误

问题:NEAR部分取消质押(Unstake)失败,触发余额不足错误

我使用near-api-py库中Account类的function_call方法对NEAR代币进行部分Unstake操作,代码如下:

from near_api.providers import JsonProvider
from near_api.account import Account
from near_api.signer import KeyPair, Signer
rpc_url = "https://rpc.mainnet.near.org"
near_provider = JsonProvider(rpc_url)
key_pair = KeyPair(private_key)
allnodes_validator_id = 'allnodes.poolv1.near'
_signer = Signer(wallet_id, key_pair)
_account = Account(wallet_id, near_provider, _signer)


def unstake_pool():
        method = 'unstake'
        amount_to_unstake = 1 * NEAR
        tx = _account.function_call(contract_id=allnodes_validator_id,
                                    method_name=method,
                                    args={},
                                    amount=amount_to_unstake)
        print(tx)
        return tx

执行后出现如下错误:

near_api.providers.JsonProviderError: {'name': 'HANDLER_ERROR', 'cause': {'info': {}, 'name': 'INVALID_TRANSACTION'}, 'code': -32000, 'message': 'Server error', 'data': {'TxExecutionError': {'InvalidTxError': {'NotEnoughBalance': {'balance': '594751191937359500000001', 'cost': '1019297408483583729293948', 'signer_id': 'my_wallet_id'}}}}}

使用unstake_all方法时一切正常,但我不想将所有代币都取消质押,请问哪里操作有误?该如何解决?


解答

问题根源

你混淆了function_call方法中两个amount的作用:

  • 方法的amount参数是交易附带的NEAR转账金额,即给合约转NEAR的数量;
  • 质押池unstake方法需要的是要取消质押的金额,这个参数应该放在args字典里,而不是交易的amount字段。

你的代码把要取消质押的1 NEAR传给了交易的amount参数,相当于尝试给合约转1 NEAR,但你的账户余额不足以覆盖这笔转账+交易手续费,因此触发了NotEnoughBalance错误。而unstake_all不需要指定金额,不会有这个参数混淆的问题,所以能正常执行。

修正后的代码

from near_api.providers import JsonProvider
from near_api.account import Account
from near_api.signer import KeyPair, Signer
rpc_url = "https://rpc.mainnet.near.org"
near_provider = JsonProvider(rpc_url)
key_pair = KeyPair(private_key)
allnodes_validator_id = 'allnodes.poolv1.near'
_signer = Signer(wallet_id, key_pair)
_account = Account(wallet_id, near_provider, _signer)


def unstake_pool():
        method = 'unstake'
        amount_to_unstake = 1 * NEAR
        tx = _account.function_call(contract_id=allnodes_validator_id,
                                    method_name=method,
                                    args={"amount": str(amount_to_unstake)},  # 将取消质押金额放入args
                                    amount=0)  # 交易无需转NEAR,设为0
        print(tx)
        return tx

关键说明

  • 质押池合约的unstake方法要求在args中传入amount字段,值为字符串格式的yoctoNEAR(NEAR最小单位,1 NEAR = 10^24 yoctoNEAR);
  • 如果你的NEAR常量已经是转换后的yoctoNEAR数值,直接转成字符串传入即可;如果是浮点型的NEAR数量,需要先乘以10^24再转字符串;
  • 交易的amount参数设置为0,因为取消质押操作不需要给合约转账。

内容的提问来源于stack exchange,提问作者Alesya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 19:52:56