From cbaaeae56321980716f3280b6e65b45fae7a6e15 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Thu, 11 Feb 2021 13:19:47 +0800 Subject: [PATCH 1/3] Add support for u128 and i128 --- CHANGELOG.md | 1 + examples/misc/Anchor.toml | 2 ++ examples/misc/Cargo.toml | 4 ++++ examples/misc/migrations/deploy.js | 13 ++++++++++++ examples/misc/programs/misc/Cargo.toml | 18 ++++++++++++++++ examples/misc/programs/misc/Xargo.toml | 2 ++ examples/misc/programs/misc/src/lib.rs | 29 ++++++++++++++++++++++++++ examples/misc/tests/misc.js | 27 ++++++++++++++++++++++++ lang/syn/src/idl.rs | 4 ++++ ts/package.json | 2 +- ts/src/coder.ts | 10 +++++++++ ts/yarn.lock | 8 +++---- 12 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 examples/misc/Anchor.toml create mode 100644 examples/misc/Cargo.toml create mode 100644 examples/misc/migrations/deploy.js create mode 100644 examples/misc/programs/misc/Cargo.toml create mode 100644 examples/misc/programs/misc/Xargo.toml create mode 100644 examples/misc/programs/misc/src/lib.rs create mode 100644 examples/misc/tests/misc.js diff --git a/CHANGELOG.md b/CHANGELOG.md index b585bf2c06..58befe9ddc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ incremented for features. * lang/attribute/access-control: Allow specifying multiple modifier functions ([845df6](https://github.com/project-serum/anchor/commit/845df6d1960bb544fa0f2e3331ec79cc804edeb6)). * lang/syn: Allow state structs that don't have a ctor or impl block (just trait implementations) ([a78000](https://github.com/project-serum/anchor/commit/a7800026833d64579e5b19c90d724ecc20d2a455)). * ts: Add instruction method to state namespace ([627c27](https://github.com/project-serum/anchor/commit/627c275e9cdf3dafafcf44473ba8146cc7979d44)). +* ts: Add support for u128 and i128 ([]()). ## [0.2.0] - 2021-02-08 diff --git a/examples/misc/Anchor.toml b/examples/misc/Anchor.toml new file mode 100644 index 0000000000..6e90c3dd6f --- /dev/null +++ b/examples/misc/Anchor.toml @@ -0,0 +1,2 @@ +cluster = "localnet" +wallet = "/home/armaniferrante/.config/solana/id.json" diff --git a/examples/misc/Cargo.toml b/examples/misc/Cargo.toml new file mode 100644 index 0000000000..a60de986d3 --- /dev/null +++ b/examples/misc/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +members = [ + "programs/*" +] diff --git a/examples/misc/migrations/deploy.js b/examples/misc/migrations/deploy.js new file mode 100644 index 0000000000..7cca271913 --- /dev/null +++ b/examples/misc/migrations/deploy.js @@ -0,0 +1,13 @@ + +// Migrations are an early feature. Currently, they're nothing more than this +// single deploy script that's invoked from the CLI, injecting a provider +// configured from the workspace's Anchor.toml. + +const anchor = require("@project-serum/anchor"); + +module.exports = async function (provider) { + // Configure client to use the provider. + anchor.setProvider(provider); + + // Add your deploy script here. +} diff --git a/examples/misc/programs/misc/Cargo.toml b/examples/misc/programs/misc/Cargo.toml new file mode 100644 index 0000000000..9cee44f747 --- /dev/null +++ b/examples/misc/programs/misc/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "misc" +version = "0.1.0" +description = "Created with Anchor" +edition = "2018" + +[lib] +crate-type = ["cdylib", "lib"] +name = "misc" + +[features] +no-entrypoint = [] +no-idl = [] +cpi = ["no-entrypoint"] +default = [] + +[dependencies] +anchor-lang = { git = "https://github.com/project-serum/anchor", features = ["derive"] } diff --git a/examples/misc/programs/misc/Xargo.toml b/examples/misc/programs/misc/Xargo.toml new file mode 100644 index 0000000000..1744f098ae --- /dev/null +++ b/examples/misc/programs/misc/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] \ No newline at end of file diff --git a/examples/misc/programs/misc/src/lib.rs b/examples/misc/programs/misc/src/lib.rs new file mode 100644 index 0000000000..fe919a6634 --- /dev/null +++ b/examples/misc/programs/misc/src/lib.rs @@ -0,0 +1,29 @@ +//! Misc example is a catchall program for testing unrelated features. +//! It's not too instructive/coherent by itself, so please see other examples. + +#![feature(proc_macro_hygiene)] + +use anchor_lang::prelude::*; + +#[program] +pub mod misc { + use super::*; + pub fn initialize(ctx: Context, udata: u128, idata: i128) -> ProgramResult { + ctx.accounts.data.udata = udata; + ctx.accounts.data.idata = idata; + Ok(()) + } +} + +#[derive(Accounts)] +pub struct Initialize<'info> { + #[account(init)] + data: ProgramAccount<'info, Data>, + rent: Sysvar<'info, Rent>, +} + +#[account] +pub struct Data { + udata: u128, + idata: i128, +} diff --git a/examples/misc/tests/misc.js b/examples/misc/tests/misc.js new file mode 100644 index 0000000000..475f4cee14 --- /dev/null +++ b/examples/misc/tests/misc.js @@ -0,0 +1,27 @@ +const anchor = require('@project-serum/anchor'); +const assert = require("assert"); + +describe("misc", () => { + // Configure the client to use the local cluster. + anchor.setProvider(anchor.Provider.env()); + + it("Can use u128 and i128", async () => { + const data = new anchor.web3.Account(); + const program = anchor.workspace.Misc; + const tx = await program.rpc.initialize( + new anchor.BN(1234), + new anchor.BN(22), + { + accounts: { + data: data.publicKey, + rent: anchor.web3.SYSVAR_RENT_PUBKEY, + }, + signers: [data], + instructions: [await program.account.data.createInstruction(data)], + } + ); + const dataAccount = await program.account.data(data.publicKey); + assert.ok(dataAccount.udata.eq(new anchor.BN(1234))); + assert.ok(dataAccount.idata.eq(new anchor.BN(22))); + }); +}); diff --git a/lang/syn/src/idl.rs b/lang/syn/src/idl.rs index f0ab9c21b1..5bdcb615cf 100644 --- a/lang/syn/src/idl.rs +++ b/lang/syn/src/idl.rs @@ -104,6 +104,8 @@ pub enum IdlType { I32, U64, I64, + U128, + I128, Bytes, String, PublicKey, @@ -133,6 +135,8 @@ impl std::str::FromStr for IdlType { "i32" => IdlType::I32, "u64" => IdlType::U64, "i64" => IdlType::I64, + "u128" => IdlType::U128, + "i128" => IdlType::I128, "Vec" => IdlType::Bytes, "String" => IdlType::String, "Pubkey" => IdlType::PublicKey, diff --git a/ts/package.json b/ts/package.json index f76006c338..ce62f228fd 100644 --- a/ts/package.json +++ b/ts/package.json @@ -23,7 +23,7 @@ "prepublishOnly": "yarn build" }, "dependencies": { - "@project-serum/borsh": "^0.0.1-beta.0", + "@project-serum/borsh": "^0.1.0", "@solana/web3.js": "^0.90.4", "@types/bn.js": "^4.11.6", "@types/bs58": "^4.0.1", diff --git a/ts/src/coder.ts b/ts/src/coder.ts index 385b91921b..c39e849b75 100644 --- a/ts/src/coder.ts +++ b/ts/src/coder.ts @@ -244,6 +244,12 @@ class IdlCoder { case "i64": { return borsh.i64(fieldName); } + case "u128": { + return borsh.u128(fieldName); + } + case "i128": { + return borsh.i128(fieldName); + } case "bytes": { return borsh.vecU8(fieldName); } @@ -372,6 +378,10 @@ function typeSize(idl: Idl, ty: IdlType): number { return 8; case "i64": return 8; + case "u128": + return 16; + case "i128": + return 16; case "bytes": return 1; case "string": diff --git a/ts/yarn.lock b/ts/yarn.lock index ea7ce29d60..6ce565295c 100644 --- a/ts/yarn.lock +++ b/ts/yarn.lock @@ -654,10 +654,10 @@ "@nodelib/fs.scandir" "2.1.4" fastq "^1.6.0" -"@project-serum/borsh@^0.0.1-beta.0": - version "0.0.1-beta.0" - resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.0.1-beta.0.tgz#8ab465ac23e0d840127c7922f8a1a500b50667d5" - integrity sha512-jBrGi0KBMe1UXkItp1JKR8Qtaal4xPrbkIWKzbglqVLRNnG+DiDWpZk8I9XMrSLiAFYTvQp8MIJmwwoWSyN8yQ== +"@project-serum/borsh@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.1.0.tgz#cdbff90d06901f8206afb6e1998e5c45aae0aea7" + integrity sha512-AWZ/cjThXmb7o2/fMocc8/VaEsqH29yXEwdHnzTXzglxg1vLPZXpBHqGuPfonSfbd7WszgnGXAIHc+9artwMGg== dependencies: bn.js "^5.1.2" buffer-layout "^1.2.0" From 69002253460ff2cdd85541070e1361271253b946 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Thu, 11 Feb 2021 13:22:26 +0800 Subject: [PATCH 2/3] Add pr number to change log --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58befe9ddc..436dc37f0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ incremented for features. * lang/attribute/access-control: Allow specifying multiple modifier functions ([845df6](https://github.com/project-serum/anchor/commit/845df6d1960bb544fa0f2e3331ec79cc804edeb6)). * lang/syn: Allow state structs that don't have a ctor or impl block (just trait implementations) ([a78000](https://github.com/project-serum/anchor/commit/a7800026833d64579e5b19c90d724ecc20d2a455)). * ts: Add instruction method to state namespace ([627c27](https://github.com/project-serum/anchor/commit/627c275e9cdf3dafafcf44473ba8146cc7979d44)). -* ts: Add support for u128 and i128 ([]()). +* ts: Add support for u128 and i128 ([#83](https://github.com/project-serum/anchor/pull/83)). ## [0.2.0] - 2021-02-08 From d01a7717c30a17cb1bd69f018cdc693285cecf96 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Thu, 11 Feb 2021 13:23:49 +0800 Subject: [PATCH 3/3] changelog: Add detail --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 436dc37f0b..17969141d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ incremented for features. * lang/attribute/access-control: Allow specifying multiple modifier functions ([845df6](https://github.com/project-serum/anchor/commit/845df6d1960bb544fa0f2e3331ec79cc804edeb6)). * lang/syn: Allow state structs that don't have a ctor or impl block (just trait implementations) ([a78000](https://github.com/project-serum/anchor/commit/a7800026833d64579e5b19c90d724ecc20d2a455)). * ts: Add instruction method to state namespace ([627c27](https://github.com/project-serum/anchor/commit/627c275e9cdf3dafafcf44473ba8146cc7979d44)). -* ts: Add support for u128 and i128 ([#83](https://github.com/project-serum/anchor/pull/83)). +* lang/syn, ts: Add support for u128 and i128 ([#83](https://github.com/project-serum/anchor/pull/83)). ## [0.2.0] - 2021-02-08