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

How can I solve the error of Smart Contract?

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

  1. 我已经创建了测试网 NEAR 账户。
  2. 我已经使用这个例子“https://github.com/near-examples/rust-counter/blob/master/contract/src/lib.rs”编译了“Counter”合约。
  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
}

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