Skip to content

Commit

Permalink
ts: Add support for u16 (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante authored May 7, 2021
1 parent 7931090 commit 851720e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions examples/misc/programs/misc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand All @@ -118,3 +130,8 @@ pub struct Data {
udata: u128,
idata: i128,
}

#[account]
pub struct DataU16 {
data: u16,
}
16 changes: 15 additions & 1 deletion examples/misc/tests/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
});
Expand Down
3 changes: 3 additions & 0 deletions ts/src/coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ class IdlCoder {
case "u8": {
return borsh.u8(fieldName);
}
case "u16": {
return borsh.u16(fieldName);
}
case "u32": {
return borsh.u32(fieldName);
}
Expand Down

0 comments on commit 851720e

Please sign in to comment.