ts: i8, i16, and i32 coding (#282)

This commit is contained in:
Armani Ferrante 2021-05-15 15:21:16 -07:00 committed by GitHub
parent ead60e2fc4
commit a73c39bbef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 88 additions and 7 deletions

View File

@ -64,6 +64,16 @@ pub mod misc {
emit!(E3 { data: 9 }); emit!(E3 { data: 9 });
Ok(()) 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)] #[derive(Accounts)]
@ -127,9 +137,23 @@ pub struct TestU16<'info> {
rent: Sysvar<'info, Rent>, rent: Sysvar<'info, Rent>,
} }
#[derive(Accounts)]
pub struct TestI16<'info> {
#[account(init)]
data: ProgramAccount<'info, DataI16>,
rent: Sysvar<'info, Rent>,
}
#[derive(Accounts)] #[derive(Accounts)]
pub struct TestSimulate {} pub struct TestSimulate {}
#[derive(Accounts)]
pub struct TestI8<'info> {
#[account(init)]
data: ProgramAccount<'info, DataI8>,
rent: Sysvar<'info, Rent>,
}
#[associated] #[associated]
pub struct TestData { pub struct TestData {
data: u64, data: u64,
@ -146,6 +170,16 @@ pub struct DataU16 {
data: u16, data: u16,
} }
#[account]
pub struct DataI8 {
data: i8,
}
#[account]
pub struct DataI16 {
data: i16,
}
#[event] #[event]
pub struct E1 { pub struct E1 {
data: u32, data: u32,

View File

@ -192,4 +192,32 @@ describe("misc", () => {
assert.ok(resp.events[2].name === "E3"); assert.ok(resp.events[2].name === "E3");
assert.ok(resp.events[2].data.data === 9); 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);
});
}); });

View File

@ -21,7 +21,7 @@
"docs": "typedoc --excludePrivate --out ../docs/src/.vuepress/dist/ts/ src/index.ts" "docs": "typedoc --excludePrivate --out ../docs/src/.vuepress/dist/ts/ src/index.ts"
}, },
"dependencies": { "dependencies": {
"@project-serum/borsh": "^0.2.1", "@project-serum/borsh": "^0.2.2",
"@solana/web3.js": "^1.11.0", "@solana/web3.js": "^1.11.0",
"@types/bn.js": "^4.11.6", "@types/bn.js": "^4.11.6",
"@types/bs58": "^4.0.1", "@types/bs58": "^4.0.1",

View File

@ -285,12 +285,21 @@ class IdlCoder {
case "u8": { case "u8": {
return borsh.u8(fieldName); return borsh.u8(fieldName);
} }
case "i8": {
return borsh.i8(fieldName);
}
case "u16": { case "u16": {
return borsh.u16(fieldName); return borsh.u16(fieldName);
} }
case "i16": {
return borsh.i16(fieldName);
}
case "u32": { case "u32": {
return borsh.u32(fieldName); return borsh.u32(fieldName);
} }
case "i32": {
return borsh.i32(fieldName);
}
case "u64": { case "u64": {
return borsh.u64(fieldName); return borsh.u64(fieldName);
} }
@ -442,10 +451,14 @@ function typeSize(idl: Idl, ty: IdlType): number {
return 1; return 1;
case "i8": case "i8":
return 1; return 1;
case "i16":
return 2;
case "u16": case "u16":
return 2; return 2;
case "u32": case "u32":
return 4; return 4;
case "i32":
return 4;
case "u64": case "u64":
return 8; return 8;
case "i64": case "i64":

View File

@ -197,14 +197,20 @@ export default class Provider {
tx.setSigners(...signerPubkeys); tx.setSigners(...signerPubkeys);
tx.recentBlockhash = ( tx.recentBlockhash = (
await this.connection.getRecentBlockhash(opts.preflightCommitment) await this.connection.getRecentBlockhash(
opts.preflightCommitment ?? this.opts.preflightCommitment
)
).blockhash; ).blockhash;
await this.wallet.signTransaction(tx); await this.wallet.signTransaction(tx);
signerKps.forEach((kp) => { signerKps.forEach((kp) => {
tx.partialSign(kp); tx.partialSign(kp);
}); });
return await simulateTransaction(this.connection, tx, opts.commitment); return await simulateTransaction(
this.connection,
tx,
opts.commitment ?? this.opts.commitment
);
} }
} }

View File

@ -654,10 +654,10 @@
"@nodelib/fs.scandir" "2.1.4" "@nodelib/fs.scandir" "2.1.4"
fastq "^1.6.0" fastq "^1.6.0"
"@project-serum/borsh@^0.2.1": "@project-serum/borsh@^0.2.2":
version "0.2.1" version "0.2.2"
resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.2.1.tgz#1d0dc4ccf8be307ddd1e88fcfb6f94b9aa26f857" resolved "https://registry.yarnpkg.com/@project-serum/borsh/-/borsh-0.2.2.tgz#63e558f2d6eb6ab79086bf499dea94da3182498f"
integrity sha512-cE5z9iaYN5nC2L8ARslKdyA31EFV6hW2ROriLfNDBqwzbDCCx0uigUdNOBZ4FHEwE12B78vUEQGywVASWXzcKQ== integrity sha512-Ms+aWmGVW6bWd3b0+MWwoaYig2QD0F90h0uhr7AzY3dpCb5e2S6RsRW02vFTfa085pY2VLB7nTZNbFECQ1liTg==
dependencies: dependencies:
bn.js "^5.1.2" bn.js "^5.1.2"
buffer-layout "^1.2.0" buffer-layout "^1.2.0"