-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path.cursorrules
More file actions
92 lines (75 loc) · 2.63 KB
/
.cursorrules
File metadata and controls
92 lines (75 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Helium Program Library - Cursor Rules
## Anchor Account Initialization
When initializing new Anchor accounts in instruction handlers, always use the `set_inner()` syntax instead of setting fields individually.
**Preferred:**
```rust
pub fn handler(ctx: Context<InitializeV0>, args: InitializeArgsV0) -> Result<()> {
let account = &mut ctx.accounts.account;
account.set_inner(AccountV0 {
field1: value1,
field2: value2,
// ... all fields
});
Ok(())
}
```
**Not preferred:**
```rust
pub fn handler(ctx: Context<InitializeV0>, args: InitializeArgsV0) -> Result<()> {
let account = &mut ctx.accounts.account;
account.field1 = value1;
account.field2 = value2;
// ... setting fields individually
Ok(())
}
```
This pattern is cleaner, ensures all fields are explicitly initialized, and makes it easier to see the complete state of the account at initialization time.
## Anchor Method Account Resolution
When calling Anchor program methods in TypeScript/JavaScript, always use `.accounts()` instead of `.accountsPartial()` or `.accountsStrict()`. Anchor's account resolvers will automatically derive the remaining accounts.
**Preferred:**
```typescript
await program.methods.someInstruction(args)
.accounts({
payer: authority,
authority,
someAccount: myAccount,
})
.instruction()
```
**Not preferred:**
```typescript
// Don't use accountsPartial
await program.methods.someInstruction(args)
.accountsPartial({
payer: authority,
authority,
someAccount: myAccount,
})
.instruction()
// Don't use accountsStrict
await program.methods.someInstruction(args)
.accountsStrict({
payer: authority,
authority,
someAccount: myAccount,
// ... all accounts manually specified
})
.instruction()
```
This approach is cleaner and leverages Anchor's automatic account resolution, reducing boilerplate and potential errors.
## Test Assertions
When writing tests, always use precise assertions with exact values rather than lazy comparisons like `.greaterThan()` or `.lessThan()` whenever the exact expected value can be calculated.
**Preferred:**
```typescript
const balance = await getTokenBalance(account);
expect(balance.toString()).to.equal(expectedAmount.toString()); // Exact value
```
**Not preferred:**
```typescript
const balance = await getTokenBalance(account);
expect(balance).to.be.greaterThan(0); // Lazy assertion
```
Only use comparison assertions when:
- The exact value is truly non-deterministic (e.g., timestamps, randomness)
- Testing boundary conditions where you explicitly need to verify > or < behavior
- The calculation is complex enough that approximation is acceptable (document why)