Error: Error happened during instantiation

Hi! I a, trying to create a contract and inside of it I am using module rand. First I’ve started to get this exception:

Compiling getrandom v0.2.3
error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the “js” feature. For more information see: getrandom - Rust
|
219 | / compile_error!(“the wasm32-unknown-unknown target is not supported by
220 | | default, you may need to enable the “js” feature.
221 | | For more information see:
222 | | getrandom - Rust”);
| |_________________________________________________________________________^

error[E0433]: failed to resolve: use of undeclared crate or module imp
|
246 | imp::getrandom_inner(dest)
| ^^^ use of undeclared crate or module imp

I found that I can solve it adding this line to Cargo.toml:

getrandom = { version = “0.2”, features = [“js”], default-features = false }

Now it compiles well, but any ,ethod call on the contract results as this error:

Ex.:

$ near call mentos.bobr.testnet init ‘{}’ --accountId mentos.bobr.testnet
Scheduling a call: mentos.bobr.testnet.init({})
Doing account.functionCall()
Receipt: GFZuhMkeBACN81yNeCY2pe4oNfGzCyUXttehgDuuBXXt
Failure [mentos.bobr.testnet]: Error: Error happened during instantiation
ServerTransactionError: Error happened during instantiation
at Object.parseResultError (…\npm\node_modules\near-cli\node_modules\near-api-js\lib\utils\rpc_errors.js:31:29)
at Account.signAndSendTransactionV2 (…\npm\node_modules\near-cli\node_modules\near-api-js\lib\account.js:160:36)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async scheduleFunctionCall (…\npm\node_modules\near-cli\commands\call.js:57:38)
at async Object.handler (…\npm\node_modules\near-cli\utils\exit-on-error.js:52:9) {
type: ‘Instantiate’,
context: undefined,
index: 0,
transaction_outcome: {
proof: [ [Object] ],
block_hash: ‘AoTtgTRYdJTggfP32T5ppTUHPSrpYNS3FZtmMxQsxEVf’,
id: ‘Fkj4St9QmNdwPW8JsgxxjC81aFZ6PheDfXWXxetixqH7’,
outcome: {
logs: [],
receipt_ids: [Array],
gas_burnt: 2427934415604,
tokens_burnt: ‘242793441560400000000’,
executor_id: ‘mentos.bobr.testnet’,
status: [Object],
metadata: [Object]
}
}
}

mentos.bobr.testnet is the contract name, thanks in advance.

1 Like

Hey, I am facing the same issue. Did you resolve this issue?

1 Like

best to post these kinds of questions on stackoverflow and tag with nearprotocol

here’s an answer to your question (something similar) rust - rand::thread_rng() panicking - Stack Overflow

1 Like

I faced the similar issue as the crate being imported in the contract was using get-random crate. I fixed it using the custom implementation of get-random. You can create a crate failure as stated in the getrandom docs of registering custom macro.

Import the failure crate in your crate and import custom feature

failure =  { path = "../failure" }
getrandom = { version = "0.2", features = ["custom"] }