Use of undeclared type or module near_blockchain

941 views Asked by At

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.

2

There are 2 answers

1
Mikko Ohtamaa On BEST ANSWER

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 {

   ...
}

0
mikeDOTexe On

If you come across this error while breaking out your Rust smart contract into separate files, please use use crate::*; instead of what your IDE might suggest.

For instance, let's say we have this file structure in our project:

.
├── Cargo.lock
├── Cargo.toml
├── src
│  ├── my_mod.rs.     ⟵ here is the module file
│  └── lib.rs
└── build-and-test.sh

When you add a new file like my_mod.rs, you'll want to make sure lib.rs has:

mod my_mod;

Inside of my_mod.rs you might have your IDE suggest this:

use crate::Contract;

which should be replaced with:

use crate::*;