Skip to content

Commit

Permalink
✨ Add InitWorld example for js (jest and mocha)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco committed Jan 27, 2024
1 parent 1684d40 commit 58eda84
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,14 @@ fn init(
if solidity {
test.write_all(anchor_cli::solidity_template::jest(&project_name).as_bytes())?;
} else {
test.write_all(anchor_cli::rust_template::jest(&project_name).as_bytes())?;
test.write_all(rust_template::jest(&project_name).as_bytes())?;
}
} else {
let mut test = File::create(format!("tests/{}.js", &project_name))?;
if solidity {
test.write_all(anchor_cli::solidity_template::mocha(&project_name).as_bytes())?;
} else {
test.write_all(anchor_cli::rust_template::mocha(&project_name).as_bytes())?;
test.write_all(rust_template::mocha(&project_name).as_bytes())?;
}
}

Expand Down
82 changes: 82 additions & 0 deletions cli/src/rust_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,88 @@ pub fn ts_package_json(jest: bool) -> String {
}
}

pub fn mocha(name: &str) -> String {
format!(
r#"const anchor = require("@coral-xyz/anchor");
const boltSdk = require("bolt-sdk");
const {{
createInitializeNewWorldInstruction,
FindWorldPda,
FindWorldRegistryPda,
Registry,
World
}} = boltSdk;
describe("{}", () => {{
// Configure the client to use the local cluster.
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
it("InitializeNewWorld", async () => {{
const registry = await Registry.fromAccountAddress(provider.connection, registryPda);
worldId = new anchor.BN(registry.worlds);
worldPda = FindWorldPda(new anchor.BN(worldId))
const initializeWorldIx = createInitializeNewWorldInstruction(
{{
world: worldPda,
registry: registryPda,
payer: provider.wallet.publicKey,
}});
const tx = new anchor.web3.Transaction().add(initializeWorldIx);
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new world (ID=${{worldId}}). Initialization signature: ${{txSign}}`);
}});
}});
}});
"#,
name,
)
}

pub fn jest(name: &str) -> String {
format!(
r#"const anchor = require("@coral-xyz/anchor");
const boltSdk = require("bolt-sdk");
const {{
createInitializeNewWorldInstruction,
FindWorldPda,
FindWorldRegistryPda,
Registry,
World
}} = boltSdk;
describe("{}", () => {{
// Configure the client to use the local cluster.
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
// Constants used to test the program.
const registryPda = FindWorldRegistryPda();
let worldId: anchor.BN;
let worldPda: PublicKey;
it("InitializeNewWorld", async () => {{
const registry = await Registry.fromAccountAddress(provider.connection, registryPda);
worldId = new anchor.BN(registry.worlds);
worldPda = FindWorldPda(new anchor.BN(worldId))
const initializeWorldIx = createInitializeNewWorldInstruction(
{{
world: worldPda,
registry: registryPda,
payer: provider.wallet.publicKey,
}});
const tx = new anchor.web3.Transaction().add(initializeWorldIx);
const txSign = await provider.sendAndConfirm(tx);
console.log(`Initialized a new world (ID=${{worldId}}). Initialization signature: ${{txSign}}`);
}});
}});
"#,
name,
)
}

pub fn ts_mocha(name: &str) -> String {
format!(
r#"import * as anchor from "@coral-xyz/anchor";
Expand Down

0 comments on commit 58eda84

Please sign in to comment.