stacks-blockchain-api icon indicating copy to clipboard operation
stacks-blockchain-api copied to clipboard

Client: Contract call arguments should be serialized by default

Open agraebe opened this issue 4 years ago • 4 comments

Currently, the client requires contract call arguments to be manually serialized:

await smartContracts.callReadOnlyFunction({
    contractAddress,
    contractName,
    functionName,
    readOnlyFunctionArgs: {
      sender: stxAddress,
      arguments: [
        `0x${serializeCV(
          tupleCV({
            hashbytes,
            version,
          })
        ).toString("hex")}`,
        `0x${serializeCV(uintCV(microSTXoLockup)).toString("hex")}`,
        `0x${serializeCV(uintCV(poxInfo.reward_cycle_id)).toString("hex")}`,
        `0x${serializeCV(uintCV(numberOfCycles)).toString("hex")}`,
      ],
    },
  });

It seems that 0x${serializeCV(<ClarityValue>).toString("hex")} is the default required. The client should handle this, so an API call could look like this:

await smartContracts.callReadOnlyFunction({
    contractAddress,
    contractName,
    functionName,
    readOnlyFunctionArgs: {
      sender: stxAddress,
      arguments: [
        tupleCV({
            hashbytes,
            version,
          }),
        uintCV(microSTXoLockup),
        uintCV(poxInfo.reward_cycle_id),
        uintCV(numberOfCycles),
      ],
    },
  });

agraebe avatar Oct 09 '20 23:10 agraebe

@zone117x I wonder if this is best handled on the API level so that the endpoint implicitly converts the input from the client?

agraebe avatar Oct 09 '20 23:10 agraebe

This is a proxied core-node endpoint, but we could make a new endpoint under /extended that provided this feature. A bit of this logic already exists in the debug /contract-call POST handler https://github.com/blockstack/stacks-blockchain-api/blob/8612ea838717ef8d0fe4dee0603f6cf370e23819/src/api/routes/debug.ts#L625-L631

Also the same logic (automate Clarity encoding via ABI inspection) could be used client-side as well. We could add it to the client lib in this repo, or in stacks-transactions if that is more appropriate.

zone117x avatar Oct 12 '20 14:10 zone117x

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 12 '21 12:09 stale[bot]

+1 to not leak the SerDe responsibility to the end-users. In general, encapsulating (complexity like the SerDes) internally would be a welcome improvement for the end-users. I am also liberally assuming this would be an end-user-facing touchpoint; please correct me if I am wrong in my naive understanding. 😆

sabbyanandan avatar Mar 04 '22 19:03 sabbyanandan

Closing this due to no activity.

sabbyanandan avatar Jul 31 '23 16:07 sabbyanandan