瀏覽代碼

ts: Add support for u16 (#255)

Armani Ferrante 4 年之前
父節點
當前提交
851720eb0f
共有 4 個文件被更改,包括 36 次插入1 次删除
  1. 1 0
      CHANGELOG.md
  2. 17 0
      examples/misc/programs/misc/src/lib.rs
  3. 15 1
      examples/misc/tests/misc.js
  4. 3 0
      ts/src/coder.ts

+ 1 - 0
CHANGELOG.md

@@ -14,6 +14,7 @@ incremented for features.
 ## Features
 
 * client: Adds support for state instructions ([#248](https://github.com/project-serum/anchor/pull/248)).
+* ts: Add support for u16 ([#255](https://github.com/project-serum/anchor/pull/255)).
 
 ## Breaking
 

+ 17 - 0
examples/misc/programs/misc/src/lib.rs

@@ -52,6 +52,11 @@ pub mod misc {
         ctx.accounts.my_account.data = data;
         Ok(())
     }
+
+    pub fn test_u16(ctx: Context<TestU16>, data: u16) -> ProgramResult {
+        ctx.accounts.my_account.data = data;
+        Ok(())
+    }
 }
 
 #[derive(Accounts)]
@@ -108,6 +113,13 @@ pub struct TestAssociatedAccount<'info> {
     system_program: AccountInfo<'info>,
 }
 
+#[derive(Accounts)]
+pub struct TestU16<'info> {
+    #[account(init)]
+    my_account: ProgramAccount<'info, DataU16>,
+    rent: Sysvar<'info, Rent>,
+}
+
 #[associated]
 pub struct TestData {
     data: u64,
@@ -118,3 +130,8 @@ pub struct Data {
     udata: u128,
     idata: i128,
 }
+
+#[account]
+pub struct DataU16 {
+    data: u16,
+}

+ 15 - 1
examples/misc/tests/misc.js

@@ -37,6 +37,20 @@ describe("misc", () => {
     assert.ok(dataAccount.idata.eq(new anchor.BN(22)));
   });
 
+  it("Can use u16", async () => {
+    const data = new anchor.web3.Account();
+    const tx = await program.rpc.testU16(99, {
+      accounts: {
+        myAccount: data.publicKey,
+        rent: anchor.web3.SYSVAR_RENT_PUBKEY,
+      },
+      signers: [data],
+      instructions: [await program.account.dataU16.createInstruction(data)],
+    });
+    const dataAccount = await program.account.dataU16(data.publicKey);
+    assert.ok(dataAccount.data === 99);
+  });
+
   it("Can embed programs into genesis from the Anchor.toml", async () => {
     const pid = new anchor.web3.PublicKey(
       "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
@@ -155,7 +169,7 @@ describe("misc", () => {
     const account = await program.account.testData.associated(
       program.provider.wallet.publicKey,
       state,
-      data.publicKey,
+      data.publicKey
     );
     assert.ok(account.data.toNumber() === 1234);
   });

+ 3 - 0
ts/src/coder.ts

@@ -285,6 +285,9 @@ class IdlCoder {
       case "u8": {
         return borsh.u8(fieldName);
       }
+      case "u16": {
+        return borsh.u16(fieldName);
+      }
       case "u32": {
         return borsh.u32(fieldName);
       }