Skip to content

Commit a9372a9

Browse files
Feat/default component (#13)
* ✨ Modify macro to allow [bolt_account] macro without component_id * ✨ Update client SDK to use default component id * 🐛 Fix dependencies * 🐛 Fix dependencies * 🐛 Update yarn.lock * ✅ Simplify test
1 parent 4be0728 commit a9372a9

File tree

13 files changed

+208
-106
lines changed

13 files changed

+208
-106
lines changed

clients/bolt-sdk/lib/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export declare function FindEntityPda(
1616
export declare function FindComponentPda(
1717
componentProgramId: PublicKey,
1818
entity: PublicKey,
19-
componentId: string
19+
componentId?: string
2020
): PublicKey;
2121
//# sourceMappingURL=index.d.ts.map

clients/bolt-sdk/lib/index.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

clients/bolt-sdk/lib/index.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/bolt-sdk/lib/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/bolt-sdk/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{
22
"name": "bolt-sdk",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "Bolt typescript SDK",
55
"author": "[email protected]",
66
"license": "MIT",
77
"private": false,
8+
"dependencies": {
9+
"@metaplex-foundation/beet": "^0.7.1",
10+
"@metaplex-foundation/beet-solana": "^0.4.0"
11+
},
812
"devDependencies": {
913
"@metaplex-foundation/solita": "^0.20.1",
1014
"@typescript-eslint/eslint-plugin": "^6.14.0",

clients/bolt-sdk/src/generated/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function FindEntityPda(
5757
export function FindComponentPda(
5858
componentProgramId: PublicKey,
5959
entity: PublicKey,
60-
componentId: string
60+
componentId: string = ""
6161
) {
6262
return PublicKey.findProgramAddressSync(
6363
[Buffer.from(componentId), entity.toBytes()],

clients/bolt-sdk/yarn.lock

+11
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@
8787
bs58 "^5.0.0"
8888
debug "^4.3.4"
8989

90+
"@metaplex-foundation/beet-solana@^0.4.0":
91+
version "0.4.1"
92+
resolved "https://registry.yarnpkg.com/@metaplex-foundation/beet-solana/-/beet-solana-0.4.1.tgz#255747aa7feee1c20202146a752c057feca1948f"
93+
integrity sha512-/6o32FNUtwK8tjhotrvU/vorP7umBuRFvBZrC6XCk51aKidBHe5LPVPA5AjGPbV3oftMfRuXPNd9yAGeEqeCDQ==
94+
dependencies:
95+
"@metaplex-foundation/beet" ">=0.1.0"
96+
"@solana/web3.js" "^1.56.2"
97+
bs58 "^5.0.0"
98+
debug "^4.3.4"
99+
90100
"@metaplex-foundation/beet@>=0.1.0", "@metaplex-foundation/beet@^0.7.1":
91101
version "0.7.1"
92102
resolved "https://registry.npmjs.org/@metaplex-foundation/beet/-/beet-0.7.1.tgz"
@@ -2169,6 +2179,7 @@ spok@^1.4.3:
21692179
find-process "^1.4.7"
21702180

21712181
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0:
2182+
name string-width-cjs
21722183
version "4.2.3"
21732184
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
21742185
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==

crates/bolt-lang/attribute/account/src/lib.rs

+26-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use proc_macro::TokenStream;
22
use quote::quote;
3-
use syn::spanned::Spanned;
43
use syn::{parse_macro_input, parse_quote, Attribute, DeriveInput, Lit, Meta, NestedMeta};
54

65
/// This BoltAccount attribute is used to automatically generate the seed and size functions
@@ -9,7 +8,7 @@ use syn::{parse_macro_input, parse_quote, Attribute, DeriveInput, Lit, Meta, Nes
98
/// The macro also adds the InitSpace and Default derives to the struct.
109
///
1110
/// #[account]
12-
/// #[bolt_account(component_id = "bolt-position")]
11+
/// #[bolt_account]
1312
/// pub struct Position {
1413
/// pub x: i64,
1514
/// pub y: i64,
@@ -18,45 +17,41 @@ use syn::{parse_macro_input, parse_quote, Attribute, DeriveInput, Lit, Meta, Nes
1817
/// ```
1918
#[proc_macro_attribute]
2019
pub fn bolt_account(attr: TokenStream, item: TokenStream) -> TokenStream {
21-
let attr = parse_macro_input!(attr as Meta);
2220
let mut input = parse_macro_input!(item as DeriveInput);
21+
let mut component_id_value = None;
2322

24-
let component_id_value = match attr {
25-
Meta::NameValue(meta_name_value) if meta_name_value.path.is_ident("component_id") => {
26-
if let Lit::Str(lit) = meta_name_value.lit {
27-
Some(lit.value())
28-
} else {
29-
None
23+
if !attr.is_empty() {
24+
let attr_meta = parse_macro_input!(attr as Meta);
25+
26+
component_id_value = match attr_meta {
27+
Meta::Path(_) => None,
28+
Meta::NameValue(meta_name_value) if meta_name_value.path.is_ident("component_id") => {
29+
if let Lit::Str(lit) = meta_name_value.lit {
30+
Some(lit.value())
31+
} else {
32+
None
33+
}
3034
}
31-
}
32-
Meta::List(meta) => meta.nested.into_iter().find_map(|nested_meta| {
33-
if let NestedMeta::Meta(Meta::NameValue(meta_name_value)) = nested_meta {
34-
if meta_name_value.path.is_ident("component_id") {
35-
if let Lit::Str(lit) = meta_name_value.lit {
36-
Some(lit.value())
35+
Meta::List(meta) => meta.nested.into_iter().find_map(|nested_meta| {
36+
if let NestedMeta::Meta(Meta::NameValue(meta_name_value)) = nested_meta {
37+
if meta_name_value.path.is_ident("component_id") {
38+
if let Lit::Str(lit) = meta_name_value.lit {
39+
Some(lit.value())
40+
} else {
41+
None
42+
}
3743
} else {
3844
None
3945
}
4046
} else {
4147
None
4248
}
43-
} else {
44-
None
45-
}
46-
}),
47-
_ => {
48-
let error = syn::Error::new(attr.span(), "Missing required attribute `component_id`");
49-
return error.to_compile_error().into();
50-
}
51-
};
49+
}),
50+
_ => None,
51+
};
52+
}
5253

53-
let component_id_value = match component_id_value {
54-
Some(value) => value,
55-
None => {
56-
let error = syn::Error::new(input.span(), "The `component_id` attribute is required");
57-
return error.to_compile_error().into();
58-
}
59-
};
54+
let component_id_value = component_id_value.unwrap_or_else(|| "".to_string());
6055

6156
let additional_derives: Attribute = parse_quote! { #[derive(InitSpace, Default)] };
6257
input.attrs.push(additional_derives);

crates/bolt-lang/attribute/component/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syn::{
1919
/// }
2020
///
2121
/// #[account]
22-
/// #[bolt_account(component_id = "bolt-position")]
22+
/// #[bolt_account]
2323
/// pub struct Position {
2424
/// pub x: i64,
2525
/// pub y: i64,

examples/component-position/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub mod component_position {
99
}
1010

1111
#[account]
12-
#[bolt_account(component_id = "component-position")]
12+
#[bolt_account]
1313
#[derive(Copy)]
1414
pub struct Position {
1515
pub x: i64,

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"eslint": "^8.33.0",
2020
"eslint-config-prettier": "^8.6.0",
2121
"eslint-config-standard-with-typescript": "^34.0.0",
22+
"@metaplex-foundation/beet": "^0.7.1",
23+
"@metaplex-foundation/beet-solana": "^0.4.0",
2224
"eslint-plugin-import": "^2.25.3",
2325
"eslint-plugin-n": "^15.6.1",
2426
"eslint-plugin-prettier": "^4.2.1",

tests/bolt.ts

+31-60
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import { SystemApplyVelocity } from "../target/types/system_apply_velocity";
1010
import { World } from "../target/types/world";
1111
import { expect } from "chai";
1212
import BN from "bn.js";
13+
import {
14+
FindComponentPda,
15+
FindEntityPda,
16+
FindWorldPda,
17+
FindWorldRegistryPda,
18+
} from "../clients/bolt-sdk";
1319

1420
enum Direction {
1521
Left = "Left",
@@ -57,7 +63,7 @@ describe("bolt", () => {
5763
let componentVelocityEntity1: PublicKey;
5864

5965
it("InitializeWorldsRegistry", async () => {
60-
const registryPda = FindWorldRegistryPda(worldProgram);
66+
const registryPda = FindWorldRegistryPda(worldProgram.programId);
6167
await worldProgram.methods
6268
.initializeRegistry()
6369
.accounts({
@@ -68,9 +74,9 @@ describe("bolt", () => {
6874
});
6975

7076
it("InitializeNewWorld", async () => {
71-
const registryPda = FindWorldRegistryPda(worldProgram);
77+
const registryPda = FindWorldRegistryPda(worldProgram.programId);
7278

73-
const worldPda = FindWorldPda(worldProgram, new BN(0));
79+
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);
7480
await worldProgram.methods
7581
.initializeNewWorld()
7682
.accounts({
@@ -82,9 +88,9 @@ describe("bolt", () => {
8288
});
8389

8490
it("InitializeNewWorld 2", async () => {
85-
const registryPda = FindWorldRegistryPda(worldProgram);
91+
const registryPda = FindWorldRegistryPda(worldProgram.programId);
8692

87-
const worldPda = FindWorldPda(worldProgram, new BN(1));
93+
const worldPda = FindWorldPda(new BN(1), worldProgram.programId);
8894
await worldProgram.methods
8995
.initializeNewWorld()
9096
.accounts({
@@ -96,8 +102,8 @@ describe("bolt", () => {
96102
});
97103

98104
it("Add entity 1", async () => {
99-
const worldPda = FindWorldPda(worldProgram, new BN(0));
100-
entity1 = FindEntityPda(worldProgram, new BN(0), new BN(0));
105+
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);
106+
entity1 = FindEntityPda(new BN(0), new BN(0), null, worldProgram.programId);
101107
await worldProgram.methods
102108
.addEntity(null)
103109
.accounts({
@@ -109,9 +115,9 @@ describe("bolt", () => {
109115
});
110116

111117
it("Add entity 2", async () => {
112-
const worldPda = FindWorldPda(worldProgram, new BN(0));
118+
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);
113119

114-
entity2 = FindEntityPda(worldProgram, new BN(0), new BN(1));
120+
entity2 = FindEntityPda(new BN(0), new BN(1), null, worldProgram.programId);
115121
await worldProgram.methods
116122
.addEntity(null)
117123
.accounts({
@@ -123,9 +129,14 @@ describe("bolt", () => {
123129
});
124130

125131
it("Add entity 3", async () => {
126-
const worldPda = FindWorldPda(worldProgram, new BN(0));
132+
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);
127133

128-
const entityPda = FindEntityPda(worldProgram, new BN(0), new BN(2));
134+
const entityPda = FindEntityPda(
135+
new BN(0),
136+
new BN(2),
137+
null,
138+
worldProgram.programId
139+
);
129140
await worldProgram.methods
130141
.addEntity(null)
131142
.accounts({
@@ -137,9 +148,14 @@ describe("bolt", () => {
137148
});
138149

139150
it("Add entity 4 with extra seeds", async () => {
140-
const worldPda = FindWorldPda(worldProgram, new BN(0));
151+
const worldPda = FindWorldPda(new BN(0), worldProgram.programId);
141152
const seed = "extra-seed";
142-
let entity3 = FindEntityPda(worldProgram, new BN(0), new BN(3), seed);
153+
let entity3 = FindEntityPda(
154+
new BN(0),
155+
new BN(3),
156+
seed,
157+
worldProgram.programId
158+
);
143159

144160
await worldProgram.methods
145161
.addEntity(seed)
@@ -188,8 +204,7 @@ describe("bolt", () => {
188204
it("Initialize Position Component on Entity 1", async () => {
189205
componentPositionEntity1 = FindComponentPda(
190206
boltComponentPositionProgram.programId,
191-
entity1,
192-
"component-position"
207+
entity1
193208
);
194209

195210
console.log("Component Position E1: ", componentPositionEntity1.toBase58());
@@ -226,8 +241,7 @@ describe("bolt", () => {
226241
it("Initialize Position Component on Entity 2", async () => {
227242
componentPositionEntity2 = FindComponentPda(
228243
boltComponentPositionProgram.programId,
229-
entity2,
230-
"component-position"
244+
entity2
231245
);
232246

233247
await worldProgram.methods
@@ -464,47 +478,4 @@ describe("bolt", () => {
464478
console.log("| |");
465479
console.log("+-----------------------------+");
466480
});
467-
468-
// Utils
469-
470-
function FindWorldRegistryPda(program: Program<World>) {
471-
return PublicKey.findProgramAddressSync(
472-
[Buffer.from("registry")],
473-
program.programId
474-
)[0];
475-
}
476-
477-
function FindWorldPda(program: Program<World>, id: BN) {
478-
return PublicKey.findProgramAddressSync(
479-
[Buffer.from("world"), id.toBuffer("be", 8)],
480-
program.programId
481-
)[0];
482-
}
483-
484-
function FindEntityPda(
485-
program: Program<World>,
486-
worldId: BN,
487-
entityId: BN,
488-
extraSeed?: string
489-
) {
490-
let seeds = [Buffer.from("entity"), worldId.toBuffer("be", 8)];
491-
if (extraSeed) {
492-
seeds.push(Buffer.from(new Uint8Array(8)));
493-
seeds.push(Buffer.from(extraSeed));
494-
} else {
495-
seeds.push(entityId.toBuffer("be", 8));
496-
}
497-
return PublicKey.findProgramAddressSync(seeds, program.programId)[0];
498-
}
499-
500-
function FindComponentPda(
501-
program: PublicKey,
502-
entity: PublicKey,
503-
seed: string = "component"
504-
) {
505-
return PublicKey.findProgramAddressSync(
506-
[Buffer.from(seed), entity.toBytes()],
507-
program
508-
)[0];
509-
}
510481
});

0 commit comments

Comments
 (0)