forked from coral-xyz/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6eacad4
commit 6f9f7d9
Showing
26 changed files
with
587 additions
and
641 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("C1ient1nteractions1111111111111111111111111"); | ||
|
||
#[program] | ||
pub mod client_interactions { | ||
use super::*; | ||
|
||
pub fn int(ctx: Context<Int>, i8: i8, i16: i16, i32: i32, i64: i64, i128: i128) -> Result<()> { | ||
ctx.accounts.account.i8 = i8; | ||
ctx.accounts.account.i16 = i16; | ||
ctx.accounts.account.i32 = i32; | ||
ctx.accounts.account.i64 = i64; | ||
ctx.accounts.account.i128 = i128; | ||
Ok(()) | ||
} | ||
|
||
pub fn uint( | ||
ctx: Context<UnsignedInt>, | ||
u8: u8, | ||
u16: u16, | ||
u32: u32, | ||
u64: u64, | ||
u128: u128, | ||
) -> Result<()> { | ||
ctx.accounts.account.u8 = u8; | ||
ctx.accounts.account.u16 = u16; | ||
ctx.accounts.account.u32 = u32; | ||
ctx.accounts.account.u64 = u64; | ||
ctx.accounts.account.u128 = u128; | ||
Ok(()) | ||
} | ||
|
||
pub fn enm(ctx: Context<Enum>, enum_arg: MyEnum) -> Result<()> { | ||
ctx.accounts.account.enum_field = enum_arg; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Int<'info> { | ||
#[account(zero)] | ||
pub account: Account<'info, IntAccount>, | ||
} | ||
|
||
#[account] | ||
pub struct IntAccount { | ||
pub i8: i8, | ||
pub i16: i16, | ||
pub i32: i32, | ||
pub i64: i64, | ||
pub i128: i128, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct UnsignedInt<'info> { | ||
#[account(zero)] | ||
pub account: Account<'info, UnsignedIntAccount>, | ||
} | ||
|
||
#[account] | ||
pub struct UnsignedIntAccount { | ||
pub u8: u8, | ||
pub u16: u16, | ||
pub u32: u32, | ||
pub u64: u64, | ||
pub u128: u128, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct Enum<'info> { | ||
#[account(zero)] | ||
pub account: Account<'info, EnumAccount>, | ||
} | ||
|
||
#[account] | ||
pub struct EnumAccount { | ||
pub enum_field: MyEnum, | ||
} | ||
|
||
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug, Eq, PartialEq)] | ||
pub enum MyEnum { | ||
Unit, | ||
Named { x: u64, y: u64 }, | ||
Unnamed(u8, u8, u16, u16), | ||
UnnamedStruct(MyStruct), | ||
} | ||
|
||
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug, Eq, PartialEq)] | ||
pub struct MyStruct { | ||
pub u8: u8, | ||
pub u16: u16, | ||
pub u32: u32, | ||
pub u64: u64, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "docs" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
rust-version = "1.60" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "docs" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import * as anchor from "@coral-xyz/anchor"; | ||
import { assert } from "chai"; | ||
|
||
import { ClientInteractions } from "../target/types/client_interactions"; | ||
|
||
describe("Client interactions", () => { | ||
anchor.setProvider(anchor.AnchorProvider.env()); | ||
const program = anchor.workspace | ||
.clientInteractions as anchor.Program<ClientInteractions>; | ||
|
||
it("Can use integers", async () => { | ||
const kp = anchor.web3.Keypair.generate(); | ||
|
||
const i8 = -3; | ||
const i16 = 1; | ||
const i32 = -5555551; | ||
const i64 = new anchor.BN("384535471"); | ||
const i128 = new anchor.BN(-8342491); | ||
|
||
await program.methods | ||
.int(i8, i16, i32, i64, i128) | ||
.accounts({ account: kp.publicKey }) | ||
.signers([kp]) | ||
.preInstructions([await program.account.intAccount.createInstruction(kp)]) | ||
.rpc(); | ||
|
||
const account = await program.account.intAccount.fetch(kp.publicKey); | ||
assert.strictEqual(account.i8, i8); | ||
assert.strictEqual(account.i16, i16); | ||
assert.strictEqual(account.i32, i32); | ||
assert(account.i64.eq(i64)); | ||
assert(account.i128.eq(i128)); | ||
}); | ||
|
||
it("Can use unsigned integers", async () => { | ||
const kp = anchor.web3.Keypair.generate(); | ||
|
||
const u8 = 123; | ||
const u16 = 7888; | ||
const u32 = 5555551; | ||
const u64 = new anchor.BN("384535471"); | ||
const u128 = new anchor.BN(8888888); | ||
|
||
await program.methods | ||
.uint(u8, u16, u32, u64, u128) | ||
.accounts({ account: kp.publicKey }) | ||
.signers([kp]) | ||
.preInstructions([ | ||
await program.account.unsignedIntAccount.createInstruction(kp), | ||
]) | ||
.rpc(); | ||
|
||
const account = await program.account.unsignedIntAccount.fetch( | ||
kp.publicKey | ||
); | ||
assert.strictEqual(account.u8, u8); | ||
assert.strictEqual(account.u16, u16); | ||
assert.strictEqual(account.u32, u32); | ||
assert(account.u64.eq(u64)); | ||
assert(account.u128.eq(u128)); | ||
}); | ||
|
||
it("Can use enum", async () => { | ||
const testAccountEnum = async ( | ||
...args: Parameters<typeof program["methods"]["enm"]> | ||
) => { | ||
const kp = anchor.web3.Keypair.generate(); | ||
await program.methods | ||
.enm(...(args as any)) | ||
.accounts({ account: kp.publicKey }) | ||
.signers([kp]) | ||
.preInstructions([ | ||
await program.account.enumAccount.createInstruction(kp), | ||
]) | ||
.rpc(); | ||
return await program.account.enumAccount.fetch(kp.publicKey); | ||
}; | ||
|
||
// Unit | ||
const unit = await testAccountEnum({ unit: {} }); | ||
assert.deepEqual(unit.enumField.unit, {}); | ||
|
||
// Named | ||
const x = new anchor.BN(1); | ||
const y = new anchor.BN(2); | ||
const named = await testAccountEnum({ named: { x, y } }); | ||
assert(named.enumField.named.x.eq(x)); | ||
assert(named.enumField.named.y.eq(y)); | ||
|
||
// Unnamed | ||
const tupleArg = [1, 2, 3, 4] as const; | ||
const unnamed = await testAccountEnum({ unnamed: tupleArg }); | ||
assert.strictEqual(unnamed.enumField.unnamed[0], tupleArg[0]); | ||
assert.strictEqual(unnamed.enumField.unnamed[1], tupleArg[1]); | ||
assert.strictEqual(unnamed.enumField.unnamed[2], tupleArg[2]); | ||
assert.strictEqual(unnamed.enumField.unnamed[3], tupleArg[3]); | ||
|
||
// Unnamed struct | ||
const tupleStructArg = [ | ||
{ u8: 1, u16: 11, u32: 111, u64: new anchor.BN(1111) }, | ||
] as const; | ||
const unnamedStruct = await testAccountEnum({ | ||
unnamedStruct: tupleStructArg, | ||
}); | ||
assert.strictEqual( | ||
unnamedStruct.enumField.unnamedStruct[0].u8, | ||
tupleStructArg[0].u8 | ||
); | ||
assert.strictEqual( | ||
unnamedStruct.enumField.unnamedStruct[0].u16, | ||
tupleStructArg[0].u16 | ||
); | ||
assert.strictEqual( | ||
unnamedStruct.enumField.unnamedStruct[0].u32, | ||
tupleStructArg[0].u32 | ||
); | ||
assert( | ||
unnamedStruct.enumField.unnamedStruct[0].u64.eq(tupleStructArg[0].u64) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as anchor from "@coral-xyz/anchor"; | ||
import { Program } from "@coral-xyz/anchor"; | ||
import { assert } from "chai"; | ||
|
||
import { Docs } from "../target/types/docs"; | ||
|
||
describe("Docs", () => { | ||
anchor.setProvider(anchor.AnchorProvider.env()); | ||
const program = anchor.workspace.docs as Program<Docs>; | ||
|
||
const instruction = program.idl.instructions.find( | ||
(i) => i.name === "testIdlDocParse" | ||
); | ||
|
||
it("includes instruction doc comment", () => { | ||
assert.deepEqual(instruction.docs, [ | ||
"This instruction doc should appear in the IDL", | ||
]); | ||
}); | ||
|
||
it("includes account doc comment", () => { | ||
const act = instruction.accounts.find((i) => i.name === "act"); | ||
assert.deepEqual(act.docs, [ | ||
"This account doc comment should appear in the IDL", | ||
"This is a multi-line comment", | ||
]); | ||
}); | ||
|
||
const dataWithDoc = program.idl.accounts.find( | ||
// @ts-expect-error | ||
(acc) => acc.name === "DataWithDoc" | ||
); | ||
|
||
it("includes accounts doc comment", () => { | ||
assert.deepEqual(dataWithDoc.docs, [ | ||
"Custom account doc comment should appear in the IDL", | ||
]); | ||
}); | ||
|
||
it("includes account attribute doc comment", () => { | ||
const dataField = dataWithDoc.type.fields.find((i) => i.name === "data"); | ||
assert.deepEqual(dataField.docs, [ | ||
"Account attribute doc comment should appear in the IDL", | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.