Smart Contract response

Hello,

I am using the following example for credential registry,

I am invoking the credientialEntry endpoint using NodeJS, please see the relevant code below

 async function verifyContract() {
  const client = createConcordiumClient(
    "node.testnet.concordium.com",
    Number(20000),
    credentials.createInsecure()
  );

  const walletFile = readFileSync("/home/yasir/concordium_dev/contracts/3SfHLNkmy61ZUQkAhMvAwKj47EYDBiUPbn3wHghFD6qGr8WDGc.export", 'utf8');
  const wallet = parseWallet(walletFile);
  const sender = new AccountAddress(wallet.value.address);
  const signer = buildAccountSigner(wallet);

  const moduleRef = new ModuleReference(
    'a36eb0e22ba5a43e53deff841ebe98b316f97bd7583bf2c50492db16e48a3b0d'
  );
  const maxCost = 30000n;
  const contractName = 'hash_reg';
  const receiveName = 'hash_reg.credentialEntry';
  // const schema = await client.getEmbeddedSchema(moduleRef);

  const rawModuleSchema = Buffer.from(readFileSync(
    '/home/yasir/concordium_dev/contracts/hash-reg/dist/schema.bin'
  ));

  const updateHeader: AccountTransactionHeader = {
    expiry: new TransactionExpiry(new Date(Date.now() + 3600000)),
    nonce: (await client.getNextAccountNonce(sender)).nonce,
    sender,
  };

  const params = "9e8c6c123351c3405489b0662da9792ba8a7626ad488f571ce50373e9da31672";
  // let params1 = "{ a:1  }"
  // const params2 = Buffer.from(params);
  const updateParams = serializeUpdateContractParameters(
    contractName,
    'credentialEntry',
    params,
    rawModuleSchema
  );

//   const inputParams = serializeInitContractParameters(
//     contractName,
//     params,
//     rawModuleSchema,
    
// );

  const updatePayload: UpdateContractPayload = {
    amount: new CcdAmount(0n),
    address: {"index":BigInt(6990),"subindex":BigInt(0)},
    receiveName,
    message: updateParams,
    maxContractExecutionEnergy: maxCost,
  };

  const updateTransaction: AccountTransaction = {
    header: updateHeader,
    payload: updatePayload,
    type: AccountTransactionType.Update,
  };

  const updateSignature = await signTransaction(updateTransaction, signer);
  const updateTrxHash = await client.sendAccountTransaction(
    updateTransaction,
    updateSignature
  );

  console.log('Transaction submitted, waiting for finalization...');

  const updateStatus = await client.waitForTransactionFinalization(
    updateTrxHash
  );
  console.dir(updateStatus, { depth: null, colors: true });
  return updateStatus;

}

I receive following response

"{\"blockHash\":\"de845a965495a7259c86fa374340d887d0303cc2cf91043b5dbf0b171640dad3\",\"summary\":{\"index\":\"0\",\"energyCost\":\"1627\",\"hash\":\"979a9ea7a99f96ad709324fd6c1a27162a6b92b4657493bd25e4b959cf6ef1e9\",\"type\":\"accountTransaction\",\"cost\":\"6984910\",\"sender\":\"3SfHLNkmy61ZUQkAhMvAwKj47EYDBiUPbn3wHghFD6qGr8WDGc\",\"transactionType\":\"failed\",\"failedTransactionType\":\"update\",\"rejectReason\":{\"tag\":\"RejectedReceive\",\"contractAddress\":{\"index\":\"6990\",\"subindex\":\"0\"},\"receiveName\":\"hash_reg.credentialEntry\",\"rejectReason\":-2,\"parameter\":\"4000000039653863366331323333353163333430353438396230363632646139373932626138613736323661643438386635373163653530333733653964613331363732\"}}}"

If I run the invoke cmd in cli i recieve

Invocation resulted in failure:
 - Energy used: 1346 NRG
 - Reason: 'credentialEntry' in 'hash_reg' at {"index":6990,"subindex":0} failed with code -2 
 - Error value:
      {
          "CredentialNotFound": []
      }

How, can i get the Cli response in NodeJS?
.

You cannot get exactly the same output from sending the transaction.

In particular you cannot get the “Error value” part out which I assume is what you are referring to.

You can use the invokeContract function instead though, as exemplified here ConcordiumGRPCClient | Concordium JS-SDKs

In particular to pretty print the error value “CredentialNotFound” you need to use the Error schema (which you can either extract from the module or get from cargo-concordium output) here

const returnValue: string | undefined = result.returnValue; // If the invoked method has return value
        if (returnValue) {
            console.log('The return value of the invoked method:', returnValue);
        }

and parse the returnValue into JSON.

1 Like

Thx! always great help!