Browse Source

Add support for u128 and i128 (#83)

Armani Ferrante 4 years ago
parent
commit
c67dabd1f9

+ 1 - 0
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)).
+* lang/syn, ts: Add support for u128 and i128 ([#83](https://github.com/project-serum/anchor/pull/83)).
 
 ## [0.2.0] - 2021-02-08
 

+ 2 - 0
examples/misc/Anchor.toml

@@ -0,0 +1,2 @@
+cluster = "localnet"
+wallet = "/home/armaniferrante/.config/solana/id.json"

+ 4 - 0
examples/misc/Cargo.toml

@@ -0,0 +1,4 @@
+[workspace]
+members = [
+    "programs/*"
+]

+ 13 - 0
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.
+}

+ 18 - 0
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"] }

+ 2 - 0
examples/misc/programs/misc/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 29 - 0
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<Initialize>, 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,
+}

+ 27 - 0
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)));
+  });
+});

+ 4 - 0
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<u8>" => IdlType::Bytes,
             "String" => IdlType::String,
             "Pubkey" => IdlType::PublicKey,

+ 1 - 1
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",

+ 10 - 0
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":

+ 4 - 4
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"