From 2823a811afd260d38abd9966a3a6260087fe0f84 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 16 Apr 2022 14:23:48 -0400 Subject: [PATCH] js type conversion reference (#63) --- src/SUMMARY.md | 1 + .../milestone_project_tic-tac-toe.md | 2 +- .../js_type_conversion_reference.md | 148 ++++++++++++++++++ 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/anchor_references/js_type_conversion_reference.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 5f1aafc..a24450a 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -33,6 +33,7 @@ - [Anchor References](./anchor_references/anchor_references.md) - [Space Reference](./anchor_references/space.md) + - [Javascript Type Conversion Reference](./anchor_references/js_type_conversion_reference.md) - [CLI Reference](./anchor_references/cli.md) - [AVM Reference](./anchor_references/avm.md) - [Anchor.toml Reference](./anchor_references/anchor-toml_reference.md) diff --git a/src/anchor_in_depth/milestone_project_tic-tac-toe.md b/src/anchor_in_depth/milestone_project_tic-tac-toe.md index d91fe7f..bce46dc 100644 --- a/src/anchor_in_depth/milestone_project_tic-tac-toe.md +++ b/src/anchor_in_depth/milestone_project_tic-tac-toe.md @@ -316,7 +316,7 @@ The structure of the transaction function is as follows: First come the instruct We did not have to specify the `system_program` account. This is because anchor recognizes this account and is able to infer it. This is also true for other known accounts such as the `token_program` or the `rent` sysvar account. After the transaction returns, we can fetch the state of the game account. You can fetch account state using the `program.account` namespace. -Finally, we verify the game has been set up properly. Anchor's typescript client deserializes rust enums like this: `{ active: {}}` for a fieldless variant and `{ won: { winner: Pubkey }}` for a variant with fields. The `None` variant of `Option` becomes `null`. The `Some(x)` variant becomes whatever `x` deserializes to. +Finally, we verify the game has been set up properly by comparing the actual state and the expected state. To learn how Anchor maps the Rust types to the js/ts types, check out the [Javascript Type Conversion Reference](./../anchor_references/js_type_conversion_reference.md). Now, run `anchor test`. This starts up (and subsequently shuts down) a local validator (make sure you don't have one running before) and runs your tests using the test script defined in `Anchor.toml`. diff --git a/src/anchor_references/js_type_conversion_reference.md b/src/anchor_references/js_type_conversion_reference.md new file mode 100644 index 0000000..1810f2d --- /dev/null +++ b/src/anchor_references/js_type_conversion_reference.md @@ -0,0 +1,148 @@ +# Javascript Type Conversion Reference + +This reference shows you how rust types are mapped to javascript/typescript types in the client. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Rust TypeJavascript TypeExampleNote
boolbool +
await program
+    .methods
+    .init(true)
+    .rpc();
+
u64/u128/i64/i128anchor.BN +
await program
+    .methods
+    .init(new anchor.BN(99))
+    .rpc();
+
+ https://github.com/indutny/bn.js/ +
u8/u16/u32/i8/i16/i32number +
await program
+    .methods
+    .init(99)
+    .rpc();
+
f32/f64number +
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();
+
Stringstring +
await program
+    .methods
+    .init("hello")
+    .rpc();
+
Vec<T>[ T ] +
await program
+    .methods
+    .init([1,2,3])
+    .rpc();
+