Skip to content

Commit

Permalink
♻️ Refactor & Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco committed Jan 26, 2024
1 parent fe8a41f commit 17e1374
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 33 deletions.
30 changes: 15 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ path = "src/bin/main.rs"
dev = []

[dependencies]
anchor-cli = { git = "https://github.com/coral-xyz/anchor.git"}
anchor-cli = { git = "https://github.com/coral-xyz/anchor.git" }
anchor-client = { git = "https://github.com/coral-xyz/anchor.git" }
anyhow = "1.0.32"
heck = "0.4.0"
Expand Down
13 changes: 6 additions & 7 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,16 @@ pub struct InitCommand {

#[derive(Debug, Parser)]
pub struct ComponentCommand {
#[clap(short, long, help = "Name of the component")]
pub name: String,
}

#[derive(Debug, Parser)]
pub struct SystemCommand {
#[clap(short, long, help = "Name of the system")]
pub name: String,
}

#[derive(Debug, Parser)]
#[clap(version = VERSION)]
pub struct Opts {
#[clap(flatten)]
pub cfg_override: ConfigOverride,
Expand Down Expand Up @@ -135,7 +134,7 @@ fn init(
fs::create_dir(&project_name)?;
}
std::env::set_current_dir(&project_name)?;
fs::create_dir("app")?;
fs::create_dir_all("app")?;

let mut cfg = Config::default();
if jest {
Expand Down Expand Up @@ -239,7 +238,7 @@ fn init(
// Initialize .prettierignore file
fs::write(
".prettierignore",
anchor_cli::rust_template::prettier_ignore(),
rust_template::prettier_ignore(),
)?;

// Remove the default programs if `--force` is passed
Expand All @@ -256,7 +255,7 @@ fn init(
)?;
}

// Build the program.
//Build the program.
if solidity {
anchor_cli::solidity_template::create_program(&project_name)?;
} else {
Expand All @@ -266,9 +265,9 @@ fn init(
}

// Build the test suite.
fs::create_dir("tests")?;
fs::create_dir_all("tests")?;
// Build the migrations directory.
fs::create_dir("migrations")?;
fs::create_dir_all("migrations")?;

if javascript {
// Build javascript config
Expand Down
87 changes: 77 additions & 10 deletions cli/src/rust_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::ANCHOR_VERSION;
use crate::VERSION;
use anchor_cli::Files;
use anyhow::Result;
use heck::{ToKebabCase, ToSnakeCase, ToUpperCamelCase};
use heck::{ToSnakeCase, ToUpperCamelCase};
use std::path::{Path, PathBuf};

/// Create a component from the given name.
Expand Down Expand Up @@ -53,7 +53,7 @@ pub mod {} {{
}}
#[account]
#[bolt_account(component_id = "{}")]
#[bolt_account(component_id = "")]
pub struct {} {{
pub x: i64,
pub y: i64,
Expand All @@ -65,7 +65,6 @@ pub struct {} {{
anchor_cli::rust_template::get_or_create_program_id(name),
name.to_upper_camel_case(),
name.to_snake_case(),
name.to_kebab_case(),
name.to_upper_camel_case(),
),
)]
Expand Down Expand Up @@ -164,7 +163,10 @@ pub fn package_json(jest: bool) -> String {
"devDependencies": {{
"chai": "^4.3.4",
"mocha": "^9.0.3",
"prettier": "^2.6.2"
"prettier": "^2.6.2",
"@metaplex-foundation/beet": "^0.7.1",
"@metaplex-foundation/beet-solana": "^0.4.0",
"bolt-sdk": "latest"
}}
}}
"#
Expand Down Expand Up @@ -231,7 +233,8 @@ pub fn ts_mocha(name: &str) -> String {
r#"import * as anchor from "@coral-xyz/anchor";
import {{ Program }} from "@coral-xyz/anchor";
import {{ PublicKey }} from "@solana/web3.js";
import {{ {} }} from "../target/types/{}";
import {{ Position }} from "../target/types/position";
import {{ Movement }} from "../target/types/movement";
import {{
createInitializeNewWorldInstruction,
FindWorldPda,
Expand All @@ -243,6 +246,7 @@ import {{
createInitializeComponentInstruction,
FindComponentPda, createApplyInstruction
}} from "bolt-sdk"
import {{expect}} from "chai";
describe("{}", () => {{
// Configure the client to use the local cluster.
Expand All @@ -255,7 +259,8 @@ describe("{}", () => {{
let worldPda: PublicKey;
let entityPda: PublicKey;
const program = anchor.workspace.{} as Program<{}>;
const positionComponent = anchor.workspace.Position as Program<Position>;
const systemMovement = anchor.workspace.Movement as Program<Movement>;
it("InitializeNewWorld", async () => {{
const registry = await Registry.fromAccountAddress(provider.connection, registryPda);
Expand All @@ -272,13 +277,62 @@ describe("{}", () => {{
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new world (ID=${{worldId}}). Initialization signature: ${{txSign}}`);
}});
it("Add an entity", async () => {{
const world = await World.fromAccountAddress(provider.connection, worldPda);
const entityId = new anchor.BN(world.entities);
entityPda = FindEntityPda(worldId, entityId);
let createEntityIx = createAddEntityInstruction({{
world: worldPda,
payer: provider.wallet.publicKey,
entity: entityPda,
}});
const tx = new anchor.web3.Transaction().add(createEntityIx);
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new Entity (ID=${{worldId}}). Initialization signature: ${{txSign}}`);
}});
it("Add a component", async () => {{
const positionComponentPda = FindComponentPda(positionComponent.programId, entityPda, "");
let initComponentIx = createInitializeComponentInstruction({{
payer: provider.wallet.publicKey,
entity: entityPda,
data: positionComponentPda,
componentProgram: positionComponent.programId,
}});
const tx = new anchor.web3.Transaction().add(initComponentIx);
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new component. Initialization signature: ${{txSign}}`);
}});
it("Apply a system", async () => {{
const positionComponentPda = FindComponentPda(positionComponent.programId, entityPda, "");
// Check that the component has been initialized and x is 0
let positionData = await positionComponent.account.position.fetch(
positionComponentPda
);
expect(positionData.x.toNumber()).to.eq(0);
let applySystemIx = createApplyInstruction({{
componentProgram: positionComponent.programId,
boltSystem: systemMovement.programId,
boltComponent: positionComponentPda,
}}, {{args: new Uint8Array()}});
const tx = new anchor.web3.Transaction().add(applySystemIx);
await provider.sendAndConfirm(tx);
// Check that the system has been applied and x is > 0
positionData = await positionComponent.account.position.fetch(
positionComponentPda
);
expect(positionData.x.toNumber()).to.gt(0);
}});
}});
"#,
name.to_upper_camel_case(),
name.to_snake_case(),
name,
name.to_upper_camel_case(),
name.to_upper_camel_case(),
)
}

Expand Down Expand Up @@ -329,3 +383,16 @@ test-ledger
.yarn
"#
}

pub fn prettier_ignore() -> &'static str {
r#"
.anchor
.bolt
.DS_Store
target
node_modules
dist
build
test-ledger
"#
}

0 comments on commit 17e1374

Please sign in to comment.