【问题标题】:When execute Delegator example, I get a "ContractTrapped" error执行 Delegator 示例时,出现“ContractTrapped”错误
【发布时间】:2021-06-03 11:42:12
【问题描述】:

建立委托合同...

  1. Git 克隆“https://github.com/paritytech/ink.git”。
  2. 移动“示例/委托人”。
  3. 使用“build-all.sh”构建合同。

部署和执行

  1. 使用 Contract Pallet 执行 Substrate-Node-Template。
  2. 访问 Polkadot/Substrate 门户(https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/accounts)
  3. 转到合同页面(https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/contracts)
  4. 部署“delegator/target/ink/accumulator/accumulator.contract”。
  5. 通过将“accumlator”地址作为参数来部署“delegator/target/ink/adder/adder.contract”。
  6. 执行加法器合约的“inc”。
  7. 我收到错误“system.ExtrinsicFailedcontracts.ContractTrapped”

我该如何解决这个问题?

【问题讨论】:

    标签: substrate polkadot-js rust-ink


    【解决方案1】:

    最近,如果您的合约构造函数不是payable,则可以返回ContractTrapped。你目前需要用payable装饰如下:

    impl Dapp {
        /// Creates a new payable contract
        #[ink(constructor, payable)]
        pub fn new(initial_supply: Balance) -> Self {
            ...
        }
    

    【讨论】:

    • 带有cargo contract new的默认Flipper模板实际上缺少payable装饰器,您的评论为我节省了大量的调试时间
    【解决方案2】:

    我在尝试从链下工作人员测试环境中调用智能合约时遇到了类似的问题。

    DispatchError::Module { index: 0, error: 17, message: Some("ContractTrapped") }
    
    

    在这里找到:primitives/sandbox/with_std.rs:275 错误:

    Trap(Trap { kind: Unreachable })
    

    但仍然不知道为什么会发生以及如何解决它。

    【讨论】:

      【解决方案3】:

      可能已经晚了,但我会留在这里给未来的奋斗者。如果您遇到TrapCode,请仔细查看它以及它是什么类型的TrapCode。这里有更多关于TrapCodeshttps://docs.wasmtime.dev/api/wasmtime/enum.TrapCode.html

      例如,我收到了TrapCode。我的底层节点告诉我这是一个TrapCode::UnreachableCodeReached,这是因为我的智能合约没有实现某些功能,其他合约试图从第一个智能合约调用这些功能,因此无法访问代码。

      【讨论】:

        【解决方案4】:

        如果您的底层节点和polkadot-js 版本不兼容,您可能会遇到ContractTrapped

        尝试在您的 packages.json 中更新以下内容

        "@polkadot/api": "<add latest version>",
        "@polkadot/api-contract": "<add latest version>",
        "@polkadot/types": "<add latest version>",
        "@polkadot/util": "<add latest version>"
        

        最新版本可以在 GitHub 上的标签中找到。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-24
          • 2017-07-09
          • 2015-08-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多