SoproX
Search…
Rust Types in Javascript - SoproxABI
To convert Rust types and Javascript types to each other, SoproxABI was born to help you hack it.

Installation

SoproxABI is a separate npm package from Soprox framework.
SoproxABI is usually installed along with all SoproX templates. However, if you would like to install it manually, you can the following to install the latest version.
1
npm install soprox-abi
Copied!
Import SoproxABI
1
const soproxABI = require('soprox-abi');
Copied!

Quick Tutorial

1
// Please refer @solana/web3 for fetch account data
2
// https://solana-labs.github.io/solana-web3.js/class/src/connection.js~Connection.html#instance-method-getAccountInfo
3
const connection = ...
4
const { data } = await connection.getAccountInfo(mintPublicKey)
5
​
6
const MINT_SCHEMA = [
7
{ key: "mint_authority_option", type: "u32" },
8
{ key: "mint_authority", type: "pub" },
9
{ key: "supply", type: "u64" },
10
{ key: "decimals", type: "u8" },
11
{ key: "is_initialized", type: "bool" },
12
{ key: "freeze_authority_option", type: "u32" },
13
{ key: "freeze_authority", type: "pub" },
14
];
15
​
16
const mintLayout = new soproxABI.struct(schema.MINT_SCHEMA);
17
const readableData = mintLayout.fromBuffer(data);
Copied!
More examples are available here.

APIs

Primary types

Rust type
Javascript type
Keyword
Space (bytes)
bool
bool
bool
1
char
string
char
4
i8
number
i8
1
i16
number
i16
2
i32
number
i32
4
i64
bigInt
i64
8
i128
bigInt
i128
16
u8
number
u8
1
u16
number
u16
2
u32
number
u32
4
u64
bigInt
u64
8
u128
bigInt
u128
16
array
array
[<type>;<length>]
depends on length
tuple
object
(<type>;<type>;...)
depends on length
Pubkey
Base58<Buffer>
pub
32

Struct

Struct is a complex type that is a combination of primary types. With this type, we can easily represent inputs of an on-chain function, data forms of on-chain accounts.
The create function won't affect on the struct type.
To define the data schema of a struct, we use an array where each element is an object including key, type, and/or schema for is nested struct.

Simple struct

1
const schema = [
2
{ "key": "pet", "type": "(char;u32;bool)" },
3
{ "key": "owner", "types": "[char;12]" },
4
{ "key": "male", "types": "bool" },
5
]
6
const petStruct = new soproxABI.struct(schema);
Copied!

Nested struct

1
const schema = [
2
{ "key": "productCode", "type": "pub" },
3
{
4
"key": "description", "schema": [
5
{ "key": "colorized", "type": "bool" },
6
{ "key": "amount", "type": "u64" },
7
]
8
}
9
]
10
const productStruct = new soproxABI.struct(schema);
Copied!

Static functions

You can call these functions directly from soproxABI. For example,
1
const counter = soproxABI.create('[u8;5]', [1,2,3,4,5]);
Copied!
Function
Description
Interface
create
Create an instance of type
create(type: string, value: (optional) initial value)
span
Compute space in bytes of a variable
span(variable: a SoproxABI variable)
pack
Concatenate all buffers into a single buffer
pack(buffer_1, <buffer_2>, ...)

Class functions

These functions are only called from a class instance. For example,
1
const toggle = new soproxABI.bool(false);
2
toggle.toBuffer() // toBuffer is an instance function
Copied!
Function
Description
Interface
toBuffer
Return a buffer represents the value of a type
toBuffer()
fromBuffer
Assign buffer data to a type
fromBuffer(buf: Buffer)

Examples

Get data length in bytes - For rent account

To rent an account on Solana, you need to compute the data space for the rent-exemption.
1
const schema = [
2
{ key: "numGreets", type: "u32" },
3
{ key: "toggleState", type: "bool" }
4
]
5
const layout = new soproxABI.struct(schema);
6
const space = layout.space; // 5 bytes - the needed space for the account
Copied!

Parse account data

Parse a dummy account in a hello-world program
1
// Please refer @solana/web3 for fetch account data
2
// https://solana-labs.github.io/solana-web3.js/class/src/connection.js~Connection.html#instance-method-getAccountInfo
3
const { data } = await connection.getAccountInfo(dummyAccount.publicKey);
4
​
5
const schema = [
6
{ key: "numGreets", type: "u32" },
7
{ key: "toggleState", type: "bool" }
8
]
9
const layout = new soproxABI.struct(schema);
10
layout.fromBuffer(data);
11
​
12
// Finally
13
console.log(layout.value);
Copied!

Calling function

Call sayHello function to a hello-world program
Approach #1
1
const schema = [
2
{ key: "numGreets", type: "u32" },
3
{ key: "toggleState", type: "bool" }
4
]
5
const layout = new soproxABI.struct(schema, {
6
numGreets: 1,
7
toggleState: true
8
});
9
const code = new soproxABI.u8(0); // 0 is corresponding to sayHello
10
const data = soproxABI.pack(code, layout);
11
​
12
// Finally
13
console.log(data);
Copied!
Approach #2
1
const schema = [
2
{ key: "code", type: "u8" },
3
{ key: "numGreets", type: "u32" },
4
{ key: "toggleState", type: "bool" }
5
]
6
const layout = new soproxABI.struct(schema, {
7
code: 0, // 0 is corresponding to sayHello
8
numGreets: 1,
9
toggleState: true
10
});
11
const data = soproxABI.pack(code, layout);
12
​
13
// Finally
14
console.log(data.toBuffer());
Copied!
Two approaches will end up with the same result.