user702991
user702991

Reputation: 33

ExecutionError":"Smart contract panicked: panicked at 'Should be initialized before usage' error while running test:deploy [NEAR PROTOCOL]

I am trying to run 'yarn test:deploy' on this repo Coin Flip and getting all the test cases failed. I haven't edited the code yet. This is the terminal output I am encountering:

yarn run v1.22.17 $ yarn dev:deploy && yarn test $ yarn build:contracts && rm -rf neardev && (near dev-deploy || exit 0) && yarn patch:config $ (cd contracts && ./build.sh) warning: unused imports: Base58PublicKey, Promise, PublicKey --> src/lib.rs:3:44 | 3 | env, near_bindgen, AccountId, Balance, PublicKey, Promise, |
^^^^^^^^^ ^^^^^^^ 4 | collections::{ UnorderedMap }, 5 |
json_types::{ U128, Base58PublicKey }, |
^^^^^^^^^^^^^^^ | = note: #[warn(unused_imports)] on by default

warning: unused import: serde::Serialize --> src/lib.rs:7:5 | 7 | use serde::Serialize; | ^^^^^^^^^^^^^^^^

warning: basic-token (lib) generated 2 warnings Finished release [optimized] target(s) in 0.10s Starting deployment. Account id: dev-1643646762507-80701581766916, node: https://rpc.testnet.near.org, helper: https://helper.testnet.near.org, file: ./out/main.wasm Transaction Id EgJ3v7HW5oea4aUmWN35E8DBD3DznRz8pKReWaRdiAx1 Done deploying to dev-1643646762507-80701581766916 $ node ./utils/patch-config.js Done! $ REACT_APP_ENV=dev jest app.test.js --useStderr console.log Loading Credentials: /root/.near-credentials/testnet/dev-1643646762507-80701581766916.json

  at Object.<anonymous> (test/near-utils.js:16:9)

console.log Receipts: JCteRULCHjv3xCqUBQrrkGibgNMFPFzp2aryB3tSpB2r, 4yzsCsEX8w13T3S1jnYTnrHwfwh8Va7b5BMCHgALqke7

  at Account.printLogsAndFailures (node_modules/near-api-js/lib/account.js:62:21)

console.warn Failure [test-1643646780358-957084]: Error: {"index":0,"account_id":"test-1643646780358-957084","predecessor_id":"dev-1643646762507-80701581766916","registrar_account_id":"registrar","kind":{"account_id":"test-1643646780358-957084","registrar_account_id":"registrar","predecessor_id":"dev-1643646762507-80701581766916"}}

  30 |   const contractAccount = new Account(connection, contractName);
  31 |   const newKeyPair = KeyPair.fromRandom("ed25519");
> 32 |   await contractAccount.createAccount(
     |   ^
  33 |     accountId,
  34 |     newKeyPair.publicKey,
  35 |     new BN(parseNearAmount(fundingAmount))

  at Account.printLogsAndFailures (node_modules/near-api-js/lib/account.js:65:25)
  at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:144:14)
  at createAccount (test/test-utils.js:32:3)
  at getAccount (test/test-utils.js:95:10)
  at Object.<anonymous> (test/app.test.js:20:11)

console.log Receipts: DbXqegvztxpwYgaAhn5ougLoa6SFCJqK7WVPL2UBfG5H, GWECkQApTXTdNGRPUabuF3grVK451PXBwX5e5qU4NtaF

  at Account.printLogsAndFailures (node_modules/near-api-js/lib/account.js:62:21)

console.warn Failure [dev-1643646762507-80701581766916]: Error: {"index":0,"kind":{"ExecutionError":"Smart contract panicked: panicked at 'Should be initialized before usage', src/lib.rs:25:9"}}

  29 |      test('check deposit', async () => {
  30 |         contract = await getContract(alice)
> 31 |              await contract.deposit({}, GAS, parseNearAmount('19'));
     |              ^
  32 |         const credits = await contract.get_credits({ account_id: alice.accountId })
  33 |         expect(credits).toEqual(parseNearAmount('19'))
  34 |      });

  at Account.printLogsAndFailures (node_modules/near-api-js/lib/account.js:65:25)
  at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:144:14)
  at node_modules/near-api-js/lib/contract.js:49:39
  at Object.<anonymous> (test/app.test.js:31:3)

console.log Receipt: CETdj9Wn8idE52xn9pRWVq1MQy3ADWofcxNcg9sYX6Ed

  at Account.printLogsAndFailures (node_modules/near-api-js/lib/account.js:62:21)

console.warn Failure [dev-1643646762507-80701581766916]: Error: {"index":0,"kind":{"ExecutionError":"Smart contract panicked: panicked at 'Should be initialized before usage', src/lib.rs:25:9"}}

  37 |         
  38 |         for (let i = 0; i < 5; i++) {
> 39 |             const rand = await contract.play({}, GAS);
     |                          ^
  40 |             console.log(rand)
  41 |             const credits = await contract.get_credits({ account_id: alice.accountId })
  42 |             console.log(credits)

  at Account.printLogsAndFailures (node_modules/near-api-js/lib/account.js:65:25)
  at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:144:14)
  at node_modules/near-api-js/lib/contract.js:49:39
  at Object.<anonymous> (test/app.test.js:39:26)

FAIL test/app.test.js (17.076 s) deploy contract dev-1643646762507-80701581766916 ✕ contract hash (3 ms) ✕ check deposit (3837 ms) ✕ check play (2368 ms)

deploy contract dev-1643646762507-80701581766916 › contract hash

{"index":0,"account_id":"test-1643646780358-957084","predecessor_id":"dev-1643646762507-80701581766916","registrar_account_id":"registrar","kind":{"account_id":"test-1643646780358-957084","registrar_account_id":"registrar","predecessor_id":"dev-1643646762507-80701581766916"}}

  30 |   const contractAccount = new Account(connection, contractName);
  31 |   const newKeyPair = KeyPair.fromRandom("ed25519");
> 32 |   await contractAccount.createAccount(
     |   ^
  33 |     accountId,
  34 |     newKeyPair.publicKey,
  35 |     new BN(parseNearAmount(fundingAmount))

  at Object.parseResultError (node_modules/near-api-js/lib/utils/rpc_errors.js:48:29)
  at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:151:36)
  at createAccount (test/test-utils.js:32:3)
  at getAccount (test/test-utils.js:95:10)
  at Object.<anonymous> (test/app.test.js:20:11)

deploy contract dev-1643646762507-80701581766916 › check deposit

{"index":0,"account_id":"test-1643646780358-957084","predecessor_id":"dev-1643646762507-80701581766916","registrar_account_id":"registrar","kind":{"account_id":"test-1643646780358-957084","registrar_account_id":"registrar","predecessor_id":"dev-1643646762507-80701581766916"}}

  30 |   const contractAccount = new Account(connection, contractName);
  31 |   const newKeyPair = KeyPair.fromRandom("ed25519");
> 32 |   await contractAccount.createAccount(
     |   ^
  33 |     accountId,
  34 |     newKeyPair.publicKey,
  35 |     new BN(parseNearAmount(fundingAmount))

  at Object.parseResultError (node_modules/near-api-js/lib/utils/rpc_errors.js:48:29)
  at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:151:36)
  at createAccount (test/test-utils.js:32:3)
  at getAccount (test/test-utils.js:95:10)
  at Object.<anonymous> (test/app.test.js:20:11)

deploy contract dev-1643646762507-80701581766916 › check deposit

{"index":0,"kind":{"ExecutionError":"Smart contract panicked: panicked at 'Should be initialized before usage', src/lib.rs:25:9"}}

  29 |      test('check deposit', async () => {
  30 |         contract = await getContract(alice)
> 31 |              await contract.deposit({}, GAS, parseNearAmount('19'));
     |              ^
  32 |         const credits = await contract.get_credits({ account_id: alice.accountId })
  33 |         expect(credits).toEqual(parseNearAmount('19'))
  34 |      });

  at Object.parseResultError (node_modules/near-api-js/lib/utils/rpc_errors.js:48:29)
  at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:151:36)
  at node_modules/near-api-js/lib/contract.js:49:39
  at Object.<anonymous> (test/app.test.js:31:3)

deploy contract dev-1643646762507-80701581766916 › check play

{"index":0,"account_id":"test-1643646780358-957084","predecessor_id":"dev-1643646762507-80701581766916","registrar_account_id":"registrar","kind":{"account_id":"test-1643646780358-957084","registrar_account_id":"registrar","predecessor_id":"dev-1643646762507-80701581766916"}}

  30 |   const contractAccount = new Account(connection, contractName);
  31 |   const newKeyPair = KeyPair.fromRandom("ed25519");
> 32 |   await contractAccount.createAccount(
     |   ^
  33 |     accountId,
  34 |     newKeyPair.publicKey,
  35 |     new BN(parseNearAmount(fundingAmount))

  at Object.parseResultError (node_modules/near-api-js/lib/utils/rpc_errors.js:48:29)
  at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:151:36)
  at createAccount (test/test-utils.js:32:3)
  at getAccount (test/test-utils.js:95:10)
  at Object.<anonymous> (test/app.test.js:20:11)

deploy contract dev-1643646762507-80701581766916 › check play

{"index":0,"kind":{"ExecutionError":"Smart contract panicked: panicked at 'Should be initialized before usage', src/lib.rs:25:9"}}

  37 |         
  38 |         for (let i = 0; i < 5; i++) {
> 39 |             const rand = await contract.play({}, GAS);
     |                          ^
  40 |             console.log(rand)
  41 |             const credits = await contract.get_credits({ account_id: alice.accountId })
  42 |             console.log(credits)

  at Object.parseResultError (node_modules/near-api-js/lib/utils/rpc_errors.js:48:29)
  at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:151:36)
  at node_modules/near-api-js/lib/contract.js:49:39
  at Object.<anonymous> (test/app.test.js:39:26)
> Test Suites: 1 failed, 1 total
> Tests:       3 failed, 3 total
> Snapshots:   0 total
> Time:        17.145 s, estimated 35 s
> Ran all test suites matching /app.test.js/i. error Command failed with exit
> code 1. info Visit https://yarnpkg.com/en/docs/cli/run for
> documentation about this command. error Command failed with exit code
> 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

What am I doing wrong?

This is the state of my machine:

node --version
v16.13.2

npm --version
8.1.2

near --version
3.1.0

rustc --version
rustc 1.58.1 (db9d1b20b 2022-01-20)

yarn --version
1.22.17

Upvotes: 1

Views: 1924

Answers (3)

Muhammad Fahad Ado
Muhammad Fahad Ado

Reputation: 117

Please you need to initialize your contract after deployment.

run: near call mycontract.myaccount.testnet init --accountId=myaccount.testnet

Then call you smartcontract.

Upvotes: 0

amgando
amgando

Reputation: 1591

the error you're seeing is because you need to call the init function on the contract.

Upvotes: 1

KROM
KROM

Reputation: 1

Looks like there are a lot of dependencies needed before running the project. From the top of the Readme - npm, node (latest versions) and rust lang seem like a requirement. Try following the readme step by step and see if the errors persist.

Upvotes: 0

Related Questions