Parameter always empty

Hello,

I am trying to pass parameter in ode using following service

export async function invokeSmartContract(contractAddress: number, entrypoint: string, param: String) {
  //  console.log(param.toString())
  const invoker = AccountAddress.fromBase58('3SfHLNkmy61ZUQkAhMvAwKj47EYDBiUPbn3wHghFD6qGr8WDGc');
  const contract =  ContractAddress.create(contractAddress);

  const receiveName = ReceiveName.fromString("cis2_dynamic_nft."+entrypoint);


  const energy = Energy.create(30000);
  const amount = CcdAmount.fromCcd(0)
  let test = '{"key": "value"}';
  let parameter;
  try {
   parameter = Parameter.fromJSON(test);
  } catch(error) {
    console.log(error) 
  }
  console.log(parameter) 


  const context: ContractContext = {
    // Required
    method: receiveName,
    contract,
    // Optional
    invoker,
    amount,
    parameter,
    energy,
};
// console.log(param)


  // const result = await client.invokeContract(context);
  let result:any;
  try {
    result = await client.invokeContract(context);
    // Handle the result as needed
  } catch (error) {
    console.error('Error invoking contract:', error);
    // Handle the error as needed
  }
  console.log(result)


  return result;
}

The error is following

{
  tag: 'failure',
  usedEnergy: Energy { value: 2207n, __type: 'ccd_energy' },
  reason: {
    tag: 'RejectedReceive',
    contractAddress: ContractAddress {
      index: 9583n,
      subindex: 0n,
      __type: 'ccd_contract_address'
    },
    receiveName: ReceiveName {
      value: 'cis2_dynamic_nft.mint',
      __type: 'ccd_receive_name'
    },
    rejectReason: -1,
    parameter: Parameter { buffer: Uint8Array(0) [], __type: 'ccd_parameter' }
  },
  returnValue: ReturnValue {
    buffer: Uint8Array(2) [ 3, 0 ],
    __type: 'ccd_return_value'
  }
}

The reason is that parameter is empty, I am trying to create parameter with following code

  let test = '{"key": "value"}';
  let parameter;
  try {
   parameter = Parameter.fromJSON(test);
  } catch(error) {
    console.log(error) 
  }

but its returning empty

Parameter { buffer: Uint8Array(0) [], __type: 'ccd_parameter' }

I have tried other methods as well .fromHextString but same result




Parameter.fromJSON, as in the inverse of Parameter.toJSON, is meant to decode the JSON representation of a Parameter which is a hex string.

If you want to use a JSON format corresponding a struct in your smart contract, you’d want to use Parameter.fromBase64SchemaType which would also require you to pass in the schema for that specific parameter (i.e. not the entire contract module).

Alternatively, you could use the contract client generator to interact in a more ergonomic way.

Hope this helps.

Please see the video, I demonstrate the issues

Sorry for the delayed response.

You cannot run the ccd-js-gen package like that when it’s installed locally, as the binary is not available on your path. you’d have to run:

npx ccd-js-gen ...

or create a script in your package.json which has access to locally installed tools:

{
  "scripts": {
    "generate": "ccd-js-gen ..."
  }
}

and run it by doing

npm run generate

Alternatively, you can install the tool globally on your machine

npm install @concordium/ccd-js-gen -g
ccd-js-gen ...