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

ts: i8, i16, and i32 coding #282

Merged
merged 2 commits into from
May 15, 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
34 changes: 34 additions & 0 deletions examples/misc/programs/misc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ pub mod misc {
emit!(E3 { data: 9 });
Ok(())
}

pub fn test_i8(ctx: Context<TestI8>, data: i8) -> ProgramResult {
ctx.accounts.data.data = data;
Ok(())
}

pub fn test_i16(ctx: Context<TestI16>, data: i16) -> ProgramResult {
ctx.accounts.data.data = data;
Ok(())
}
}

#[derive(Accounts)]
Expand Down Expand Up @@ -127,9 +137,23 @@ pub struct TestU16<'info> {
rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
pub struct TestI16<'info> {
#[account(init)]
data: ProgramAccount<'info, DataI16>,
rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
pub struct TestSimulate {}

#[derive(Accounts)]
pub struct TestI8<'info> {
#[account(init)]
data: ProgramAccount<'info, DataI8>,
rent: Sysvar<'info, Rent>,
}

#[associated]
pub struct TestData {
data: u64,
Expand All @@ -146,6 +170,16 @@ pub struct DataU16 {
data: u16,
}

#[account]
pub struct DataI8 {
data: i8,
}

#[account]
pub struct DataI16 {
data: i16,
}

#[event]
pub struct E1 {
data: u32,
Expand Down
28 changes: 28 additions & 0 deletions examples/misc/tests/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,32 @@ describe("misc", () => {
assert.ok(resp.events[2].name === "E3");
assert.ok(resp.events[2].data.data === 9);
});

it("Can use i8 in the idl", async () => {
const data = anchor.web3.Keypair.generate();
await program.rpc.testI8(-3, {
accounts: {
data: data.publicKey,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
instructions: [await program.account.dataI8.createInstruction(data)],
signers: [data],
});
const dataAccount = await program.account.dataI8(data.publicKey);
assert.ok(dataAccount.data === -3);
});

it("Can use i16 in the idl", async () => {
const data = anchor.web3.Keypair.generate();
await program.rpc.testI16(-2048, {
accounts: {
data: data.publicKey,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
instructions: [await program.account.dataI16.createInstruction(data)],
signers: [data],
});
const dataAccount = await program.account.dataI16(data.publicKey);
assert.ok(dataAccount.data === -2048);
});
});
2 changes: 1 addition & 1 deletion ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"docs": "typedoc --excludePrivate --out ../docs/src/.vuepress/dist/ts/ src/index.ts"
},
"dependencies": {
"@project-serum/borsh": "^0.2.1",
"@project-serum/borsh": "^0.2.2",
"@solana/web3.js": "^1.11.0",
"@types/bn.js": "^4.11.6",
"@types/bs58": "^4.0.1",
Expand Down
13 changes: 13 additions & 0 deletions ts/src/coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,21 @@ class IdlCoder {
case "u8": {
return borsh.u8(fieldName);
}
case "i8": {
return borsh.i8(fieldName);
}
case "u16": {
return borsh.u16(fieldName);
}
case "i16": {
return borsh.i16(fieldName);
}
case "u32": {
return borsh.u32(fieldName);
}
case "i32": {
return borsh.i32(fieldName);
}
case "u64": {
return borsh.u64(fieldName);
}
Expand Down Expand Up @@ -442,10 +451,14 @@ function typeSize(idl: Idl, ty: IdlType): number {
return 1;
case "i8":
return 1;
case "i16":
return 2;
case "u16":
return 2;
case "u32":
return 4;
case "i32":
return 4;
case "u64":
return 8;
case "i64":
Expand Down
10 changes: 8 additions & 2 deletions ts/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,20 @@ export default class Provider {

tx.setSigners(...signerPubkeys);
tx.recentBlockhash = (
await this.connection.getRecentBlockhash(opts.preflightCommitment)
await this.connection.getRecentBlockhash(
opts.preflightCommitment ?? this.opts.preflightCommitment
)
).blockhash;

await this.wallet.signTransaction(tx);
signerKps.forEach((kp) => {
tx.partialSign(kp);
});
return await simulateTransaction(this.connection, tx, opts.commitment);
return await simulateTransaction(
this.connection,
tx,
opts.commitment ?? this.opts.commitment
);
}
}

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.2.1":
version "0.2.1"
resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.2.1.tgz#1d0dc4ccf8be307ddd1e88fcfb6f94b9aa26f857"
integrity sha512-cE5z9iaYN5nC2L8ARslKdyA31EFV6hW2ROriLfNDBqwzbDCCx0uigUdNOBZ4FHEwE12B78vUEQGywVASWXzcKQ==
"@project-serum/borsh@^0.2.2":
version "0.2.2"
resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.2.2.tgz#63e558f2d6eb6ab79086bf499dea94da3182498f"
integrity sha512-Ms+aWmGVW6bWd3b0+MWwoaYig2QD0F90h0uhr7AzY3dpCb5e2S6RsRW02vFTfa085pY2VLB7nTZNbFECQ1liTg==
dependencies:
bn.js "^5.1.2"
buffer-layout "^1.2.0"
Expand Down