js type conversion reference (#63)
This commit is contained in:
parent
a73d595e5c
commit
2823a811af
|
@ -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)
|
||||
|
|
|
@ -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`.
|
||||
|
||||
|
|
|
@ -0,0 +1,148 @@
|
|||
# Javascript Type Conversion Reference
|
||||
|
||||
This reference shows you how rust types are mapped to javascript/typescript types in the client.
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Rust Type</th>
|
||||
<th>Javascript Type</th>
|
||||
<th>Example</th>
|
||||
<th>Note</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>bool</td>
|
||||
<td>bool</td>
|
||||
<td >
|
||||
<pre><code>await program
|
||||
.methods
|
||||
.init(true)
|
||||
.rpc();</code></pre>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>u64/u128/i64/i128</td>
|
||||
<td>anchor.BN</td>
|
||||
<td >
|
||||
<pre><code>await program
|
||||
.methods
|
||||
.init(new anchor.BN(99))
|
||||
.rpc();</code></pre>
|
||||
</td>
|
||||
<td>
|
||||
https://github.com/indutny/bn.js/
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>u8/u16/u32/i8/i16/i32</td>
|
||||
<td>number</td>
|
||||
<td >
|
||||
<pre><code>await program
|
||||
.methods
|
||||
.init(99)
|
||||
.rpc();</code></pre>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>f32/f64</td>
|
||||
<td>number</td>
|
||||
<td >
|
||||
<pre><code>await program
|
||||
.methods
|
||||
.init(1.0)
|
||||
.rpc();</code></pre>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Option<T></td>
|
||||
<td><code>null</code> or T</td>
|
||||
<td >
|
||||
<pre><code>await program
|
||||
.methods
|
||||
.init(null)
|
||||
.rpc();</code></pre>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Enum</td>
|
||||
<td nowrap><code>{ variantName: {} }</code></td>
|
||||
<td>
|
||||
<pre><code>// Rust
|
||||
enum MyEnum { One, Two };
|
||||
// JS
|
||||
await program
|
||||
.methods
|
||||
.init({ one: {} })
|
||||
.rpc();
|
||||
</code></pre>
|
||||
<pre><code>// Rust
|
||||
enum MyEnum { One: { val: u64 }, Two };
|
||||
// JS
|
||||
await program
|
||||
.methods
|
||||
.init({ one: { val: 99 } })
|
||||
.rpc();
|
||||
</code></pre>
|
||||
</td>
|
||||
<td>
|
||||
No support for tuple variants
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Struct</td>
|
||||
<td nowrap><code>{ val: {} }</code></td>
|
||||
<td>
|
||||
<pre><code>// Rust
|
||||
struct MyStruct { val: u64 };
|
||||
// JS
|
||||
await program
|
||||
.methods
|
||||
.init({ val: 99 })
|
||||
.rpc();
|
||||
</code></pre>
|
||||
</td>
|
||||
<td>
|
||||
No support for tuple structs
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[T; N]</td>
|
||||
<td>[ T ]</td>
|
||||
<td >
|
||||
<pre><code>await program
|
||||
.methods
|
||||
.init([1,2,3])
|
||||
.rpc();</code></pre>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>String</td>
|
||||
<td>string</td>
|
||||
<td >
|
||||
<pre><code>await program
|
||||
.methods
|
||||
.init("hello")
|
||||
.rpc();</code></pre>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Vec<T></td>
|
||||
<td>[ T ]</td>
|
||||
<td >
|
||||
<pre><code>await program
|
||||
.methods
|
||||
.init([1,2,3])
|
||||
.rpc();</code></pre>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
Loading…
Reference in New Issue