Search code examples
nearprotocol

Use of undeclared type or module near_blockchain


I am getting the following error when trying to compile NEAR smart contracts, but only when compiling to wasm target

   Compiling nep9000 v0.1.0 (/Users/mikkoohtamaa/code/advanced-fungible-token/contract)
error[E0433]: failed to resolve: use of undeclared type or module `near_blockchain`
   --> src/token.rs:144:1
    |
144 | #[near_bindgen]
    | ^^^^^^^^^^^^^^^ use of undeclared type or module `near_blockchain```

Normal cargo build is ok.


Solution

  • Looks like you need to declare #[near_bindgen] on both struct and impl of the contract, impl only is not enough.

    #[near_bindgen]
    #[derive(BorshDeserialize, BorshSerialize)]
    pub struct Token {
       ...
    }
    
    
    #[near_bindgen]
    impl Token {
    
       ...
    }