nearprotocol - 为什么near-cli找不到我的合约方法?

标签 nearprotocol

所以我为我的合约实现了这个功能

#[payable]
fn send_message(mut self, message: &str, receiver: &str) {

当我尝试使用 near-cli 调用它时

近调用 v1.messenger.ijelis.testnet send_message '{"message": "test","sender": "iejlis.near"}' --account-id ijelis.testnet

它给了我这个

Scheduling a call: v1.messenger.ijelis.testnet.send_message({"message": "test","sender": "iejlis.near"})
Doing account.functionCall()
Receipt: DDXubobUugwsGnr9GqXxMv7PJqT3YsLjTn14xWML4vx
    Failure [v1.messenger.ijelis.testnet]: Error: Contract method is not found
ServerTransactionError: Contract method is not found
    at Object.parseResultError (/usr/local/lib/node_modules/near-cli/node_modules/near-api-js/lib/utils/rpc_errors.js:31:29)
    at Account.signAndSendTransactionV2 (/usr/local/lib/node_modules/near-cli/node_modules/near-api-js/lib/account.js:160:36)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async scheduleFunctionCall (/usr/local/lib/node_modules/near-cli/commands/call.js:57:38)
    at async Object.handler (/usr/local/lib/node_modules/near-cli/utils/exit-on-error.js:52:9) {
  type: 'MethodNotFound',
  context: undefined,
  index: 0,
  transaction_outcome: {
    proof: [ [Object] ],
    block_hash: 'G4QJ5PPykJWieCyi9P5Rzxu73t1YCKLGKnWL7Z5nUbhd',
    id: 'ExJyHwyCpjrFBXGXyr35Y6itkTPVpN1MtE4RnDbo2HBz',
    outcome: {
      logs: [],
      receipt_ids: [Array],
      gas_burnt: 2428039504502,
      tokens_burnt: '242803950450200000000',
      executor_id: 'ijelis.testnet',
      status: [Object],
      metadata: [Object]
    }
  }
}

我做错了什么?

编辑:我尝试使用 near-create-app 界面部署它,一切正常。

最佳答案

您似乎没有将合约部署到此帐户。

像这样检查:

1。是否有任何部署到帐户?

使用 NEAR CLI 快速查看账户状态 (https://docs.near.org/docs/tools/near-cli#near-state)

near state v1.messenger.ijelis.testnet send_message

结果

{
  amount: '2099882635532834400000000',
  locked: '0',
  code_hash: 'E8jZ1giWcVrps8PcV75ATauu6gFRkcwjNtKp7NKmipZG',
  storage_usage: 268,
  storage_paid_at: 0,
  block_height: 74201501,
  block_hash: '46fdR4oFfEDDuNmLMM8Le6FbKPR3VG5zzNni7u9uE3XQ',
  formattedAmount: '2.0998826355328344'
}

结论

是的,该帐户部署了东西,因为 code_hash 的值不是默认值 1111111111111111111111111111111,而是 E8jZ1giWcVrps8PcV75ATauu6gFRkcwjNtKp7NKmipZG code> 这是合约字节码的 base58 编码 sha256 摘要,部署到帐户上可用“合约插槽”的数据指纹

2。那么究竟部署到帐户中的是什么?

使用API​​查看合约代码:https://docs.near.org/docs/api/rpc/contracts#view-contract-code

http post https://rpc.testnet.near.org jsonrpc=2.0 id=dontcare method=query \
  params:='{
    "request_type": "view_code",
    "finality": "final",
    "account_id": "v1.messenger.ijelis.testnet"
  }'

结果

{
    "id": "dontcare",
    "jsonrpc": "2.0",
    "result": {
        "block_hash": "48uCNbhhB9FSg35bJgMDvTpjS5f9n4jrnU4MmMmbHsqj",
        "block_height": 74201992,
        "code_base64": "AGFzbQEAAAAEBQFwAQEBBQMBABAGGQN/AUGAgMAAC38AQYCAwAALfwBBgIDAAAsHJQMGbWVtb3J5AgAKX19kYXRhX2VuZAMBC19faGVhcF9iYXNlAwI=",
        "hash": "E8jZ1giWcVrps8PcV75ATauu6gFRkcwjNtKp7NKmipZG"
    }
}

结论

AGFzbQEAAAAAEBQFwAQEBBQMBABAGGQN/AUGAgMAAC38AQYCAwAALfwBBgIDAAAsHJQMGbWVtb3J5AgAKX19kYXRhX2VuZAMBC19faGVhcF9iYXNlAwI= 看起来太小了,不能成为契约(Contract),尤其是用 Rust 编写的契约(Contract)(通常比 AssemblyScript 契约(Contract)大一点)

此 base64 编码字符串解码为以下 WAT

(module
  (table $T0 1 1 funcref)
  (memory $memory (export "memory") 16)
  (global $g0 (mut i32) (i32.const 1048576))
  (global $__data_end (export "__data_end") i32 (i32.const 1048576))
  (global $__heap_base (export "__heap_base") i32 (i32.const 1048576)))

所以您认为存在的契约(Contract)实际上并不存在。

解决方案

将合约部署到账户v1.messenger.ijelis.testnet


注意:上面的WAT是分两步生成的:

(1)。 回声“AGFzbQEAAAAEBQFwAQEBBQMBABAGGQN/AUGAgMAAC38AQYCAwAALfwBBgIDAAAsHJQMGbWVtb3J5AgAKX19kYXRhX2VuZAMBC19faGVhcF9iYXNlAwI=” | base64 -d > contract.wasm

(2)。上传wasm文件到https://webassembly.github.io/wabt/demo/wasm2wat/

关于nearprotocol - 为什么near-cli找不到我的合约方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70267510/

相关文章:

nearprotocol - 所有 NEAR 区 block 链交易都需要接收账户吗?

nearprotocol - Meme 博物馆教程 - 'exceed prepaid gas'

nearprotocol - 跨合约调用是原子的吗?

nearprotocol - 您如何估算 NEAR 智能合约方法调用的 gas 使用量?

rust - NEP-141 实现

nearprotocol - 如何从地址调用不同的合约?

nearprotocol - 如何将数据写回存储器?

rust - NEAR跨合约调用异常如何处理?

nearprotocol - 如何查看 NEAR 协议(protocol)中的存储使用情况?

nearprotocol - 在 NEAR 平台上如何处理交易的这张图片有多准确?