Klever SC invoke via Node SDK / Troubleshooting + Fix

Hi @Andreas_Hennersdorf,

Yes, it’s possible to send a Smart Contract (SC) transaction via the Node SDK. Let me show you how.

First, here’s the correct approach to send a Smart Contract invoke transaction:

const unsignedTx = await account.buildTransaction(
  [
    {
      payload: {
        scType: InvokeType,
        address: contractAddress,
      },
      type: SmartContractType,
    },
  ],
  [metadata],
  {
    nonce: account.getNonce(),
  }
);

The metadata variable is where you specify the function to be called and its encoded arguments, following this format:

function_name@param1@param2@param3@…@lastparam

If you check any smart contract transaction, you’ll see the metadata structured in this way:

withdraw@0000000000000006

The arguments must be encoded according to their types defined in the contract’s ABI.

Example ABI:

(...)
{
  "name": "change_expiration_date",
  "onlyOwner": true,
  "mutability": "mutable",
  "inputs": [
    {
      "name": "expiration_date",
      "type": "u64"
    }
  ],
  "outputs": []
}
(...)

Encoding the data:

import { abiEncoder } from '@klever/sdk-node';
(...)

const expirationDateEncoded = abiEncoder.encodeABIValue(expirationDate, 'u64');

const metadata = ["change_expiration_date", expirationDateEncoded].join('@');

Final Result is

change_expiration_date@0000000067fe6684

A more complete version of the examples I used can be found here:

Smart Contract: GitHub - klever-io/klever-certification-sc
JS-SDK: GitHub - klever-io/klever-certificate-issuer

1 Like