Add support for u128 and i128 (#83)

This commit is contained in:
Armani Ferrante 2021-02-11 13:24:29 +08:00 committed by GitHub
parent 79fe508c07
commit c67dabd1f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 115 additions and 5 deletions

View File

@ -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

View File

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

4
examples/misc/Cargo.toml Normal file
View File

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

View File

@ -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.
}

View File

@ -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"] }

View File

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

View File

@ -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,
}

View File

@ -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)));
});
});

View File

@ -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,

View File

@ -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",

View File

@ -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":

View File

@ -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"