When execute Delegator example, I get a "ContractTrapped" error

511 views Asked by At

Build Delegator Contracts...

  1. Git clone "https://github.com/paritytech/ink.git".
  2. Move "examples/delegator".
  3. Build contracts by using "build-all.sh".

Deploy & Execute

  1. Excecute Substrate-Node-Template with Contract Pallet.
  2. Visit Polkadot/Substrate Portal(https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/accounts)
  3. Go to Contract Page(https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/contracts)
  4. Deploy "delegator/target/ink/accumulator/accumulator.contract".
  5. Deploy "delegator/target/ink/adder/adder.contract" by indicating "accumlator" address as a parameter.
  6. Excecute "inc" of adder contract.
  7. I get a error "system.ExtrinsicFailed contracts.ContractTrapped"

How can I solve this problem?

5

There are 5 answers

0
s.Takahashi On BEST ANSWER

I could execute delegator example at the latest commit of the github. I could not recognize this cause of the error,but now the error is solved.

0
Evgeny Svirsky On

I have a similar issue when trying to call Smart Contract from within offchain worker test environment.

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

Found here: primitives/sandbox/with_std.rs:275 The error in:

Trap(Trap { kind: Unreachable })

But still have no idea why it happened and how to fix it.

1
forgetso On

More recently, ContractTrapped can be returned if your contract constructor is not payable. You currently need to decorate with payable as follows:

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

It's probably late but I will leave this here for future strugglers. If you run into a TrapCode, take a closer look at it and what type of TrapCode it is. Here is more on TrapCodes: https://docs.wasmtime.dev/api/wasmtime/enum.TrapCode.html

I was getting a TrapCode for example. My substrate node was telling me that it was a TrapCode::UnreachableCodeReached which was because my smart contract did not implement some functions, which other contract were trying to call from the first smart contract, hence unreachable code.

0
forgetso On

You can run into ContractTrapped if your substrate node and polkadot-js versions are not compatible.

Try updating the following in your packages.json

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

Latest version can be found in the tags on GitHub.