【问题标题】:How can I solve the error of Smart Contract?如何解决智能合约的错误?
【发布时间】:2023-04-05 06:50:01
【问题描述】:

我第一次尝试通过 NEAR 协议调用智能合约。 请告诉我如何解决以下错误。

  1. 我已经创建了 Testnet NEAR 帐户。
  2. 我使用此示例“https://github.com/near-examples/rust-counter/blob/master/contract/src/lib.rs”编译了“计数器”合约。
  3. 我已经使用“near cli”将该合约部署到了测试网,并且已经成功。
  4. 我调用near cli的“veiw函数”,返回错误。
% near view Counter get_num '{}'
View call: Counter.get_num({})
An error occured
Error: [-32000] Server error: Account ID Counter is invalid
    at /Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/providers/json-rpc-provider.js:322:31
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Object.exponentialBackoff [as default] (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/utils/exponential-backoff.js:7:24)
    at async JsonRpcProvider.sendJsonRpc (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/providers/json-rpc-provider.js:297:24)
    at async JsonRpcProvider.query (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/providers/json-rpc-provider.js:109:22)
    at async Account.viewFunction (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/account.js:356:24)
    at async exports.callViewFunction (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/index.js:75:48)
    at async Object.handler (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/utils/exit-on-error.js:52:9)
TypedError: [-32000] Server error: Account ID Counter is invalid
    at /Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/providers/json-rpc-provider.js:322:31
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Object.exponentialBackoff [as default] (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/utils/exponential-backoff.js:7:24)
    at async JsonRpcProvider.sendJsonRpc (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/providers/json-rpc-provider.js:297:24)
    at async JsonRpcProvider.query (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/providers/json-rpc-provider.js:109:22)
    at async Account.viewFunction (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/node_modules/near-api-js/lib/account.js:356:24)
    at async exports.callViewFunction (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/index.js:75:48)
    at async Object.handler (/Users/shin.takahashi/.nodebrew/node/v14.14.0/lib/node_modules/near-cli/utils/exit-on-error.js:52:9) {
  type: 'UntypedError',
  context: undefined
}

【问题讨论】:

    标签: smartcontracts nearprotocol


    【解决方案1】:

    Counter 不是有效的帐户 ID。 Uppercase letters in accounts-id are not allowed)。您需要传递正确的account-id

    我希望您的帐户 ID 采用 takahashi.testnetdev-1623565709996-68004511819798 的形式(如果合约是使用 near dev-deploy 命令部署的)。

    这是您可以使用dev-deploy 部署到测试网并使用near-cli 调用视图功能的方法:

    ❯ near --version
    2.0.2
    
    ❯ near dev-deploy out/main.wasm
    Starting deployment. Account id: dev-1623565709996-68004511819798, node: https://rpc.testnet.near.org, helper: https://helper.testnet.near.org, file: out/main.wasm
    Transaction Id 5eTde2dUw5QTA8hbpWvAw4ABary64Tsnj9FzBCPS9Ne
    Done deploying to dev-1623565709996-68004511819798
    
    ❯ near view dev-1623565709996-68004511819798 get_num '{}'
    View call: dev-1623565709996-68004511819798.get_num({})
    0
    

    如果您使用的是主网网络(而不是测试网),您需要通过设置环境变量让 near-cli 知道这一点:

    ❯ export NEAR_ENV=mainnet
    

    【讨论】:

    • 感谢您的帮助!您的回复解决了我的问题。但是“near cli”的官方文档很难理解。因为“near call”的第一个参数表示为“contractName”,但不是。
    • 感谢您提请我们注意。 contractName 表示合约的 account-id,但我理解这种混淆。
    • 在near-cli上打开了一个关于此的问题:github.com/near/near-cli/issues/793
    猜你喜欢
    • 1970-01-01
    • 2020-04-11
    • 2021-12-30
    • 2022-06-16
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多