Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for u128 and i128 #83

Merged
merged 3 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions examples/misc/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cluster = "localnet"
wallet = "/home/armaniferrante/.config/solana/id.json"
4 changes: 4 additions & 0 deletions examples/misc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = [
"programs/*"
]
13 changes: 13 additions & 0 deletions examples/misc/migrations/deploy.js
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions examples/misc/programs/misc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions examples/misc/programs/misc/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
29 changes: 29 additions & 0 deletions examples/misc/programs/misc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions examples/misc/tests/misc.js
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions lang/syn/src/idl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ pub enum IdlType {
I32,
U64,
I64,
U128,
I128,
Bytes,
String,
PublicKey,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions ts/src/coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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":
Expand Down
8 changes: 4 additions & 4 deletions ts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down