From 58eda84430816e5b7eaeb5d42ed884692f0d9aa6 Mon Sep 17 00:00:00 2001 From: Gabriele Picco Date: Sat, 27 Jan 2024 11:30:19 +0100 Subject: [PATCH] :sparkles: Add InitWorld example for js (jest and mocha) --- cli/src/lib.rs | 4 +- cli/src/rust_template.rs | 82 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index db67705..8c3c4fc 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -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())?; } } diff --git a/cli/src/rust_template.rs b/cli/src/rust_template.rs index 7e8119e..4ab80cc 100644 --- a/cli/src/rust_template.rs +++ b/cli/src/rust_template.rs @@ -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";