https://github.com/zilliqa/scilla-json-utils
Scilla JSON utils
https://github.com/zilliqa/scilla-json-utils
Last synced: about 1 year ago
JSON representation
Scilla JSON utils
- Host: GitHub
- URL: https://github.com/zilliqa/scilla-json-utils
- Owner: Zilliqa
- License: gpl-3.0
- Created: 2021-12-09T11:28:45.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-11T14:41:02.000Z (almost 3 years ago)
- Last Synced: 2025-02-16T10:42:58.576Z (over 1 year ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@zilliqa-js/scilla-json-utils
- Size: 117 KB
- Stars: 2
- Watchers: 16
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Scilla JSON Utils
Simplifies the way you construct the Scilla JSON data
## Installation
```sh
npm i @zilliqa-js/scilla-json-utils
# or
yarn add @zilliqa-js/scilla-json-utils
```
## Usage
### I. `scillaJSONVal(type: string, value: any)`
#### Integers (`UintX` / `IntX`)
```js
import { scillaJSONVal } from "@zilliqa-js/scilla-json-utils";
scillaJSONVal("Uint256", "1");
// Output: "1"
```
```js
scillaJSONVal("Int256", "-1");
// Output: "-1"
```
```js
scillaJSONVal("Uint256", 1);
// Output: "1"
```
```js
scillaJSONVal("Int256", -1);
// Output: "-1"
```
#### Strings (`String`)
```js
scillaJSONVal("String", "Foo");
// Output: "Foo"
```
#### Byte Strings (`ByStrX`)
```js
scillaJSONVal("ByStr20", "0x85E0bef5F9a11821f9B2BA778a05963436B5e720");
// Output: "0x85e0bef5f9a11821f9b2ba778a05963436b5e720"
// Note that the output is lowercased.
```
#### Block Numbers (`BNum`)
```js
scillaJSONVal("BNum", "1");
// Output: "1"
```
```js
scillaJSONVal("BNum", 1);
// Output: "1"
```
#### Boolean (`Bool`)
```js
scillaJSONVal("Bool", false);
```
Output:
```json
{
"argtypes": [],
"arguments": [],
"constructor": "False"
}
```
#### Option (`Option`)
##### None
```js
scillaJSONVal("Option (ByStr20)", undefined);
```
Output:
```json
{
"argtypes": ["ByStr20"],
"arguments": [],
"constructor": "None"
}
```
##### Some
```js
scillaJSONVal("Option (ByStr20)", "0x0000000000000000000000000000000000000000");
```
Output:
```json
{
"argtypes": ["ByStr20"],
"arguments": ["0x0000000000000000000000000000000000000000"],
"constructor": "Some"
}
```
#### Pair (`Pair`)
```js
scillaJSONVal("Pair (ByStr20) (Uint256)", [
"0x0000000000000000000000000000000000000000",
1,
]);
```
Output:
```json
{
"argtypes": ["ByStr20", "Uint256"],
"arguments": ["0x0000000000000000000000000000000000000000", "1"],
"constructor": "Pair"
}
```
#### List (`List`)
```js
scillaJSONVal("List (Pair (ByStr20) (Uint256))", [
["0x85E0bef5F9a11821f9B2BA778a05963436B5e720", 1],
["0x85E0bef5F9a11821f9B2BA778a05963436B5e720", 2],
]);
```
Output:
```json
[
{
"argtypes": ["ByStr20", "Uint256"],
"arguments": ["0x85e0bef5f9a11821f9b2ba778a05963436b5e720", "1"],
"constructor": "Pair"
},
{
"argtypes": ["ByStr20", "Uint256"],
"arguments": ["0x85e0bef5f9a11821f9b2ba778a05963436b5e720", "2"],
"constructor": "Pair"
}
]
```
#### User-defined ADTs
```ocaml
type Foo =
| Bar of ByStr20 BNum
| Baz of ByStr20
```
```js
scillaJSONVal(
"0x85E0bef5F9a11821f9B2BA778a05963436B5e720.Foo.Bar.of.ByStr20.BNum",
["0x0000000000000000000000000000000000000000", 1]
);
```
Output:
```json
{
"argtypes": [],
"arguments": ["0x0000000000000000000000000000000000000000", "1"],
"constructor": "0x85e0bef5f9a11821f9b2ba778a05963436b5e720.Bar"
}
```
### II. `scillaJSONParams({[vname: string]: [type: string, value: any]})`
```ocaml
type Foo =
| Bar of ByStr20 BNum
| Baz of ByStr20
```
```js
import { scillaJSONParams } from "@zilliqa-js/scilla-json-utils";
scillaJSONParams({
x: [
"0x85E0bef5F9a11821f9B2BA778a05963436B5e720.Foo.Bar.of.ByStr20.BNum",
["0x0000000000000000000000000000000000000000", 1],
],
y: [
"List (Pair (ByStr20) (String))",
[
["0x85E0bef5F9a11821f9B2BA778a05963436B5e720", "Foo"],
["0x85E0bef5F9a11821f9B2BA778a05963436B5e720", "Bar"],
],
],
z: ["Uint256", 1],
});
```
Output:
```json
[
{
"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](src/index.test.ts)
## License
This project is open source software licensed as [GPL-3.0](./LICENSE).