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 NodeJS package.

SoproxABI is usually installed along with all SoproX template. However, if you would like to install it manually, you can the following to install the latest version.

npm install soprox-abi

Import SoproxABI

const soproxABI = require('soprox-abi');

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

u8

number

u8

1

u16

number

u16

2

u32

number

u32

4

u64

bigInt

u64

8

array

array

[<type>;<length>]

depends on length

tuple

object

(<type>;<type>;...)

depends on length

Pubkey

Buffer

pub

32

Struct

Struct is a complex type which 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.

create function won't effect on struct type.

To define 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

const schema = [
{ "key": "pet", "type": "(char;u32;bool)" },
{ "key": "owner", "types": "[char;12]" },
{ "key": "male", "types": "bool" },
]
const petStruct = new soproxABI.struct(schema);

Nested struct

const schema = [
{ "key": "productCode", "type": "pub" },
{
"key": "description", "schema": [
{ "key": "colorized", "type": "bool" },
{ "key": "amount", "type": "u64" },
]
}
]
const productStruct = new soproxABI.struct(schema);

Static functions

You can call these function directly from soproxABI. For example,

const toggle = soproxABI.create('[u8;5]', [1,2,3,4,5]);

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>, ...)

Instance functions

These function is only staying in a type instance including struct. For example,

const toggle = new soproxABI.bool(false);
toggle.toBuffer() // toBuffer is an instance function

Function

Description

Interface

toBuffer

Return a buffer represents value of a type

toBuffer()

fromBuffer

Assign value to a type from a buffer

fromBuffer(buffer: NodeJS Buffer)

Examples

Parse account data

Parse a dummy account in a hello-world program

// Please refer @solana/web3 for fetch account data
// https://solana-labs.github.io/solana-web3.js/class/src/connection.js~Connection.html#instance-method-getAccountInfo
const { data } = await connection.getAccountInfo(dummyAccount.publicKey);
โ€‹
const schema = [
{ key: "numGreets", type: "u32" },
{ key: "toggleState", type: "bool" }
]
const layout = new soproxABI.struct(schema);
layout.fromBuffer(data);
โ€‹
// Finally
console.log(layout.value);

Calling function

Call sayHello function to a hello-world program

Approach #1

const schema = [
{ key: "numGreets", type: "u32" },
{ key: "toggleState", type: "bool" }
]
const layout = new soproxABI.struct(schema, {
numGreets: 1,
toggleState: true
});
const code = new soproxABI.u8(0); // 0 is corresponding to sayHello
const data = soproxABI.pack(code, layout);
โ€‹
// Finally
console.log(data);

Approach #2

const schema = [
{ key: "code", type: "u8" },
{ key: "numGreets", type: "u32" },
{ key: "toggleState", type: "bool" }
]
const layout = new soproxABI.struct(schema, {
code: 0, // 0 is corresponding to sayHello
numGreets: 1,
toggleState: true
});
const data = soproxABI.pack(code, layout);
โ€‹
// Finally
console.log(data.toBuffer());

Two approaches will end up at the same result.