error: encountered unexpected ink! attribute
  --> lib.rs:80:15
80 |         #[ink(constructor)]
   |               ^^^^^^^^^^^

when I execute $cargo +nightly contract test , the above error is happened.

The code is based on the following article. https://learn.figment.io/tutorials/build-polkadot-amm-using-ink#creating-a-frontend-in-react

Here is the code for the part pointed out.

    pub fn new(_fees: Balance) -> Self {
        // Sets fees to zero if not in valid range
        Self {
            fees: if _fees >= 1000 { 0 } else { _fees },
  • 2
    With what version of ink! are you trying to build this example? Can you push your full repo to some GitHub account so that I can give it a try building it? In the most recent version of ink! you should probably replace the HashMap storage type by ink_storage::Mapping which also requires an initialization in the constructor: use.ink/datastructures/mapping
    – Achim
    Oct 7, 2022 at 12:58
  • 1
    @Achim the same error was happened when I changed as you said.... Repo:github.com/tnkshuuhei/WasmAMM/tree/master/amm
    – Steve
    Oct 8, 2022 at 3:50

1 Answer 1


I did check out your repo: you need to move the constructor as well as all message's and other parts of the Smart Contract logic within the actual implementation impl Amm { /* code here */ }

I did a PR to your repo which fixes this part:https://github.com/tnkshuuhei/WasmAMM/pull/1. The code will still not compile, as I've already mentioned in my comment you need to replace the HashMap with the Mapping struct to get it working with an up to date version of ink!. See: https://use.ink/datastructures/mapping/ for details.

  • 1
    I appreciate your help!! also your comment on PR and above are helpful for me. the error wasn't happened but new errors come to me haha I will try Mapping struct. Thank you!
    – Steve
    Oct 8, 2022 at 10:53
  • You are welcome! If this answers your original question, feel free to mark it as a solution. You are also welcome to ask more questions regarding your implementation.
    – Achim
    Oct 8, 2022 at 21:27
  • 1
    Im very happy to meet a kind man as you in my first question!! I like the people at Parity more now haha.
    – Steve
    Oct 9, 2022 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.