diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d878913c..373221421 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/examples/misc/programs/misc/src/lib.rs b/examples/misc/programs/misc/src/lib.rs index c8abd95b1..aceca638b 100644 --- a/examples/misc/programs/misc/src/lib.rs +++ b/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, 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, +} diff --git a/examples/misc/tests/misc.js b/examples/misc/tests/misc.js index 6b18deb66..1f54d389e 100644 --- a/examples/misc/tests/misc.js +++ b/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); }); diff --git a/ts/src/coder.ts b/ts/src/coder.ts index 3567f68be..010263263 100644 --- a/ts/src/coder.ts +++ b/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); }