Skip to content

NFTGameCo/scilla-json-utils

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Scilla JSON Utils

Simplifies the way you construct the Scilla JSON data

Installation

npm i @zilliqa-js/scilla-json-utils
# or
yarn add @zilliqa-js/scilla-json-utils

Usage

I. getJSONValue(type: string, value: any)

Integers (UintX / IntX)

import { getJSONValue } from "@zilliqa-js/scilla-json-utils";

getJSONValue("Uint256", "1");
// Output: "1"
getJSONValue("Int256", "-1");
// Output: "-1"
getJSONValue("Uint256", 1);
// Output: "1"
getJSONValue("Int256", -1);
// Output: "-1"

Strings (String)

getJSONValue("String", "Foo");
// Output: "Foo"

Byte Strings (ByStrX)

getJSONValue("ByStr20", "0x85E0bef5F9a11821f9B2BA778a05963436B5e720");
// Output: "0x85e0bef5f9a11821f9b2ba778a05963436b5e720"
// Note that the output is lowercased.

Block Numbers (BNum)

getJSONValue("BNum", "1");
// Output: "1"
getJSONValue("BNum", 1);
// Output: "1"

Boolean (Bool)

getJSONValue("Bool", false);

Output:

{
  "argtypes": [],
  "arguments": [],
  "constructor": "False"
}

Option (Option)

None
getJSONValue("Option (ByStr20)", undefined);

Output:

{
  "argtypes": ["ByStr20"],
  "arguments": [],
  "constructor": "None"
}
Some
getJSONValue("Option (ByStr20)", "0x0000000000000000000000000000000000000000");

Output:

{
  "argtypes": ["ByStr20"],
  "arguments": ["0x0000000000000000000000000000000000000000"],
  "constructor": "Some"
}

Pair (Pair)

getJSONValue("Pair (ByStr20) (Uint256)", [
  "0x0000000000000000000000000000000000000000",
  1,
]);

Output:

{
  "argtypes": ["ByStr20", "Uint256"],
  "arguments": ["0x0000000000000000000000000000000000000000", "1"],
  "constructor": "Pair"
}

List (List)

getJSONValue("List (Pair (ByStr20) (Uint256))", [
  ["0x85E0bef5F9a11821f9B2BA778a05963436B5e720", 1],
  ["0x85E0bef5F9a11821f9B2BA778a05963436B5e720", 2],
]);

Output:

[
  {
    "argtypes": ["ByStr20", "Uint256"],
    "arguments": ["0x85e0bef5f9a11821f9b2ba778a05963436b5e720", "1"],
    "constructor": "Pair"
  },
  {
    "argtypes": ["ByStr20", "Uint256"],
    "arguments": ["0x85e0bef5f9a11821f9b2ba778a05963436b5e720", "2"],
    "constructor": "Pair"
  }
]

User-defined ADTs

type Foo =
| Bar of ByStr20 BNum
| Baz of ByStr20
getJSONValue(
  "0x85E0bef5F9a11821f9B2BA778a05963436B5e720.Foo.Bar.of.ByStr20.BNum",
  ["0x0000000000000000000000000000000000000000", 1]
);

Output:

{
  "argtypes": [],
  "arguments": ["0x0000000000000000000000000000000000000000", "1"],
  "constructor": "0x85e0bef5f9a11821f9b2ba778a05963436b5e720.Bar"
}

II. getJSONParams({[vname: string]: [type: string, value: any]})

type Foo =
| Bar of ByStr20 BNum
| Baz of ByStr20
import { getJSONParams } from "@zilliqa-js/scilla-json-utils";

getJSONParams({
  x: [
    "0x85E0bef5F9a11821f9B2BA778a05963436B5e720.Foo.Bar.of.ByStr20.BNum",
    ["0x0000000000000000000000000000000000000000", 1],
  ],
  y: [
    "List (Pair (ByStr20) (String))",
    [
      ["0x85E0bef5F9a11821f9B2BA778a05963436B5e720", "Foo"],
      ["0x85E0bef5F9a11821f9B2BA778a05963436B5e720", "Bar"],
    ],
  ],
  z: ["Uint256", 1],
});

Output:

[
  {
    "type": "0x85e0bef5f9a11821f9b2ba778a05963436b5e720.Foo",
    "value": {
      "argtypes": [],
      "arguments": ["0x0000000000000000000000000000000000000000", "1"],
      "constructor": "0x85e0bef5f9a11821f9b2ba778a05963436b5e720.Bar"
    },
    "vname": "x"
  },
  {
    "type": "List (Pair (ByStr20) (String))",
    "value": [
      {
        "argtypes": ["ByStr20", "String"],
        "arguments": ["0x85e0bef5f9a11821f9b2ba778a05963436b5e720", "Foo"],
        "constructor": "Pair"
      },
      {
        "argtypes": ["ByStr20", "String"],
        "arguments": ["0x85e0bef5f9a11821f9b2ba778a05963436b5e720", "Bar"],
        "constructor": "Pair"
      }
    ],
    "vname": "y"
  },
  {
    "type": "Uint256",
    "value": "1",
    "vname": "z"
  }
]

More cases

License

This project is open source software licensed as GPL-3.0.

Packages

No packages published

Languages

  • JavaScript 56.3%
  • TypeScript 43.7%