微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决智能合约的错误?

如何解决如何解决智能合约的错误?

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

  1. 我已经创建了 Testnet NEAR 帐户。
  2. 我使用这个例子“https://github.com/near-examples/rust-counter/blob/master/contract/src/lib.rs”编译了“Counter”合约。
  3. 我已使用“near cli”将此合约部署到测试网,并且已成功。
  4. 调用了接近 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
}

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。