[Help][caver-js] Error: Returned error: unknown account when interacting to Smart Contract

Hi, I tried to call mint function to my own contract but it return below error. I added my sender key. to wallet but not works.

(node:86412) UnhandledPromiseRejectionWarning: Error: Returned error: unknown account
    at Object.ErrorResponse (/Users/tobi/Projects/tutta/wallet/node_modules/caver-js/packages/caver-core-helpers/src/errors.js:87:16)
    at /Users/tobi/Projects/tutta/wallet/node_modules/caver-js/packages/caver-core-requestmanager/src/index.js:155:44
    at XMLHttpRequest.request.onreadystatechange (/Users/tobi/Projects/tutta/wallet/node_modules/caver-js/packages/caver-core-requestmanager/caver-providers-http/src/index.js:119:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/tobi/Projects/tutta/wallet/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/Users/tobi/Projects/tutta/wallet/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (/Users/tobi/Projects/tutta/wallet/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (/Users/tobi/Projects/tutta/wallet/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1220:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:86412) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:86412) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
const fs = require('fs');
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651')

async function testFunction() {
  const tuttaContractAddress = "0xbdbafcae0a2fe1bc24effdf54415f5f7ba22077d"
  const result = await caver.kct.kip7.detectInterface(tuttaContractAddress)
  console.log(result)

  let rawdata = fs.readFileSync('build/contracts/TtutaToken.json');
  let build = JSON.parse(rawdata);
  // console.log(build.abi);

  const contract = await caver.contract.create(build.abi, tuttaContractAddress)
  // Add private key of caller to account list
  caver.klay.accounts.wallet.add('0x{my private key}')
  // Check again my accounts list to make sure new key has been added
  console.log(caver.klay.accounts)

  await contract.methods
    .mint("0x0b6dB2E2234AA703954a80CCd5F6f152a4Da25E8", 10000000000000)
    .send({
      from: "0x0b6dB2E2234AA703954a80CCd5F6f152a4Da25E8",
      gas: 2000000
    });
}

testFunction()

// console.log(caver.klay.accounts)

  wallet: Wallet {
    '0': Account {
      address: [Getter/Setter],
      accountKey: [Getter/Setter],
      privateKey: [Getter/Setter],
      signTransaction: [Function: signTransaction],
      feePayerSignTransaction: [Function: feePayerSignTransaction],
      sign: [Function: sign],
      encrypt: [Function: encrypt],
      getKlaytnWalletKey: [Function: getKlaytnWalletKey],
      index: 0
    },
    _accounts: [Circular],
    length: 1,
    defaultKeyName: 'caverjs_wallet',
    '0x0b6db2e2234aa703954a80ccd5f6f152a4da25e8': Account {
      address: [Getter/Setter],
      accountKey: [Getter/Setter],
      privateKey: [Getter/Setter],
      signTransaction: [Function: signTransaction],
      feePayerSignTransaction: [Function: feePayerSignTransaction],
      sign: [Function: sign],
      encrypt: [Function: encrypt],
      getKlaytnWalletKey: [Function: getKlaytnWalletKey],
      index: 0
    },
    '0X0B6DB2E2234AA703954A80CCD5F6F152A4DA25E8': Account {
      address: [Getter/Setter],
      accountKey: [Getter/Setter],
      privateKey: [Getter/Setter],
      signTransaction: [Function: signTransaction],
      feePayerSignTransaction: [Function: feePayerSignTransaction],
      sign: [Function: sign],
      encrypt: [Function: encrypt],
      getKlaytnWalletKey: [Function: getKlaytnWalletKey],
      index: 0
    },
    '0x0b6dB2E2234AA703954a80CCd5F6f152a4Da25E8': Account {
      address: [Getter/Setter],
      accountKey: [Getter/Setter],
      privateKey: [Getter/Setter],
      signTransaction: [Function: signTransaction],
      feePayerSignTransaction: [Function: feePayerSignTransaction],
      sign: [Function: sign],
      encrypt: [Function: encrypt],
      getKlaytnWalletKey: [Function: getKlaytnWalletKey],
      index: 0
    }
  }
}

MacOs
Node version: 12.18.3
Caver-js 1.16.1 (latest)

Thanks!

I used another way to add the key to accounts list and it works. I think it’s most likely a bug

caver.wallet.newKeyring("0x0b6dB2E2234AA703954a80CCd5F6f152a4Da25E8", '0x{my-private-key}')

But I also looking for solution for the first way

cc @Jamie, Forgive me if I bother you

You can use caver.wallet.add(caver.wallet.keyring.createFromPrivateKey('0x{private key}'), instead of caver.klay.accounts.wallet.add('0x{my private key}').

caver.kaly.accounts is the old accounts/allet package which is used before SDK common architecture.
In caver-js v1.5.0, SDK common architecture is implemented, so i recomemded you to use new packages named caver.wallet and caver.wallet.keyring.
In new common architecture, the data structure which includes address and private key(s) is called keyring.
caver.contract is also one of the common architecture package, so that’s why did not work with caver.klay.accounts.
Please check here to see what packages you can use after common architecture.

1 Like

Thank you very much!, I understood.