sarvalabs / js-moi-sdk

JavaScript library to interact with MOI Protocol via RPC API
https://js-moi-sdk.docs.moi.technology/
Apache License 2.0
24 stars 2 forks source link

Enhance `ManifestCoder` for Encoding/Decoding Routine Arguments and Returned Values #81

Closed sarvalabs-harshrastogi closed 1 month ago

sarvalabs-harshrastogi commented 3 months ago

Enhance ManifestCoder for Encoding/Decoding Routine Arguments and Returned Values

[!IMPORTANT] This pull request has breaking changes

This pull request enhances the ManifestCoder class by introducing new methods and improvements to existing ones. These changes aim to simplify the process of encoding and decoding routine arguments and returned values, improving the overall developer experience.

Changes:

  1. ElementDescriptor:

    • The ElementDescriptor class has been relocated from js-moi-logic to js-moi-manifest.
    • Developers can now instantiate an ElementDescriptor object with a manifest object, providing more flexibility and integration with the manifest data.
  2. ManifestCoder:

    • This constructor method now accepts a manifest of type LogicManifest.Manifest as input. (Breaking Change)
  3. Manifest.encodeArguments:

    • This method now supports extracting routine type fields from arguments based on the routine name.
    • It can encode arguments into the POLO format, simplifying the developer experience.
    • The existing functionality remains unaffected, ensuring backward compatibility.
const manifest = await loadManifestFromFile("../../manifests/tokenledger.json");
const manifestCoder = new ManifestCoder(manifest);
const args = ["MOI", 100_000_000];
const calldata = manifestCoder.encodeArguments("Seeder", args);
console.log(calldata) // >>> 0x0d6f0665b6019502737570706c790305f5e10073796d626f6c064d4f49
  1. ManifestCoder.decodeArguments:
    • This new method enables extracting the type of the returned value from a routine.
    • It can decode the POLO output passed as an argument, facilitating the processing of routine results.
const manifest = await loadManifestFromFile("../../manifests/tokenledger.json");
const manifestCoder = new ManifestCoder(manifest);
const outputSerialized = "0x0e1f0305f5e100";
const callsite = "BalanceOf";
const output = manifestCoder.decodeOutput<{ balance: number }>(outputSerialized, callsite);

console.log(output) // >>> { balance: 100000000 }

These changes streamline the process of working with routine arguments and returned values within the ManifestCoder class.

Changes include

Checklist

cloudflare-workers-and-pages[bot] commented 2 months ago

Deploying js-moi-sdk with  Cloudflare Pages  Cloudflare Pages

Latest commit: 412c7cc
Status: ✅  Deploy successful!
Preview URL: https://a707c507.js-moi-sdk.pages.dev
Branch Preview URL: https://refactor-manifest-coder.js-moi-sdk.pages.dev

View logs