Javascript Anchor Types Reference

Javascript Anchor Types Reference

This reference shows you how anchor maps rust types to javascript/typescript types in the client.

Rust TypeJavascript TypeExampleNote

bool

bool

await program
    .methods
    .init(true)
    .rpc();

u64/u128/i64/i128

anchor.BN

await program
    .methods
    .init(new anchor.BN(99))
    .rpc();

https://github.com/indutny/bn.js/

u8/u16/u32/i8/i16/i32

number

await program
    .methods
    .init(99)
    .rpc();

f32/f64

number

await program
    .methods
    .init(1.0)
    .rpc();

Option<T>

null or T

await program
    .methods
    .init(null)
    .rpc();

Enum

{ variantName: {} }

// Rust
enum MyEnum { One, Two };
// JS
await program
    .methods
    .init({ one: {} })
    .rpc();
// Rust 
enum MyEnum { One: { val: u64 }, Two };
// JS
await program
    .methods
    .init({ one: { val: 99 } })
    .rpc();

No support for tuple variants

Struct

{ val: {} }

// Rust
struct MyStruct { val: u64 };
// JS
await program
    .methods
    .init({ val: 99 })
    .rpc();

No support for tuple structs

[T; N]

[ T ]

await program
    .methods
    .init([1,2,3])
    .rpc();

String

string

await program
    .methods
    .init("hello")
    .rpc();

Vec<T>

[ T ]

await program
    .methods
    .init([1,2,3])
    .rpc();

Last updated