From 9d16b518ef8af9418313d2adef086a880a082ea8 Mon Sep 17 00:00:00 2001 From: DanL0 Date: Thu, 5 Sep 2024 16:42:59 +0200 Subject: [PATCH 1/2] add OmniCounter Solana initialize test --- examples/omnicounter-solana/.gitignore | 1 + examples/omnicounter-solana/Cargo.lock | 9 +++++ .../counter/src/instructions/init_count.rs | 4 +-- .../programs/endpoint-mock/Cargo.toml | 22 ++++++++++++ .../programs/endpoint-mock/Xargo.toml | 2 ++ .../endpoint-mock/src/instructions/mod.rs | 3 ++ .../src/instructions/oapp/mod.rs | 3 ++ .../src/instructions/oapp/register_oapp.rs | 35 +++++++++++++++++++ .../programs/endpoint-mock/src/lib.rs | 19 ++++++++++ .../endpoint-mock/src/state/endpoint.rs | 8 +++++ .../programs/endpoint-mock/src/state/mod.rs | 3 ++ .../test/anchor/omnicounter.test.ts | 34 ++++++++++++++++-- 12 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 examples/omnicounter-solana/programs/endpoint-mock/Cargo.toml create mode 100644 examples/omnicounter-solana/programs/endpoint-mock/Xargo.toml create mode 100644 examples/omnicounter-solana/programs/endpoint-mock/src/instructions/mod.rs create mode 100644 examples/omnicounter-solana/programs/endpoint-mock/src/instructions/oapp/mod.rs create mode 100644 examples/omnicounter-solana/programs/endpoint-mock/src/instructions/oapp/register_oapp.rs create mode 100644 examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs create mode 100644 examples/omnicounter-solana/programs/endpoint-mock/src/state/endpoint.rs create mode 100644 examples/omnicounter-solana/programs/endpoint-mock/src/state/mod.rs diff --git a/examples/omnicounter-solana/.gitignore b/examples/omnicounter-solana/.gitignore index 0f7da464c..1507b6945 100644 --- a/examples/omnicounter-solana/.gitignore +++ b/examples/omnicounter-solana/.gitignore @@ -6,6 +6,7 @@ coverage.json target typechain typechain-types +test-ledger # Hardhat files cache diff --git a/examples/omnicounter-solana/Cargo.lock b/examples/omnicounter-solana/Cargo.lock index d5d41e163..f941ae3b3 100644 --- a/examples/omnicounter-solana/Cargo.lock +++ b/examples/omnicounter-solana/Cargo.lock @@ -718,6 +718,15 @@ dependencies = [ "utils", ] +[[package]] +name = "endpoint-mock" +version = "0.1.0" +dependencies = [ + "anchor-lang", + "cpi-helper", + "solana-program", +] + [[package]] name = "feature-probe" version = "0.1.1" diff --git a/examples/omnicounter-solana/programs/counter/src/instructions/init_count.rs b/examples/omnicounter-solana/programs/counter/src/instructions/init_count.rs index e95f021ce..ff8f9d1b0 100644 --- a/examples/omnicounter-solana/programs/counter/src/instructions/init_count.rs +++ b/examples/omnicounter-solana/programs/counter/src/instructions/init_count.rs @@ -1,5 +1,5 @@ use crate::*; -use oapp::endpoint::{instructions::RegisterOAppParams, ID as ENDPOINT_ID}; +use oapp::endpoint::{instructions::RegisterOAppParams}; #[derive(Accounts)] #[instruction(params: InitCountParams)] @@ -47,7 +47,7 @@ impl InitCount<'_> { let register_params = RegisterOAppParams { delegate: ctx.accounts.count.admin }; let seeds: &[&[u8]] = &[COUNT_SEED, &[ctx.accounts.count.id], &[ctx.accounts.count.bump]]; oapp::endpoint_cpi::register_oapp( - ENDPOINT_ID, + params.endpoint, ctx.accounts.count.key(), ctx.remaining_accounts, seeds, diff --git a/examples/omnicounter-solana/programs/endpoint-mock/Cargo.toml b/examples/omnicounter-solana/programs/endpoint-mock/Cargo.toml new file mode 100644 index 000000000..d3b0a59f8 --- /dev/null +++ b/examples/omnicounter-solana/programs/endpoint-mock/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "endpoint-mock" +version = "0.1.0" +description = "Endpoint Mock" +edition = "2021" + +[lib] +crate-type = ["cdylib", "lib"] +name = "endpoint" + +[features] +no-entrypoint = [] +no-idl = [] +no-log-ix-name = [] +cpi = ["no-entrypoint"] +default = [] +idl-build = ["anchor-lang/idl-build"] + +[dependencies] +anchor-lang = { version = "0.29.0", features = ["event-cpi"] } +solana-program = "=1.17.31" +cpi-helper = { git = "https://github.com/LayerZero-Labs/LayerZero-v2.git", branch = "main" } diff --git a/examples/omnicounter-solana/programs/endpoint-mock/Xargo.toml b/examples/omnicounter-solana/programs/endpoint-mock/Xargo.toml new file mode 100644 index 000000000..475fb71ed --- /dev/null +++ b/examples/omnicounter-solana/programs/endpoint-mock/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] diff --git a/examples/omnicounter-solana/programs/endpoint-mock/src/instructions/mod.rs b/examples/omnicounter-solana/programs/endpoint-mock/src/instructions/mod.rs new file mode 100644 index 000000000..7e79a19da --- /dev/null +++ b/examples/omnicounter-solana/programs/endpoint-mock/src/instructions/mod.rs @@ -0,0 +1,3 @@ +pub mod oapp; + +pub use oapp::*; diff --git a/examples/omnicounter-solana/programs/endpoint-mock/src/instructions/oapp/mod.rs b/examples/omnicounter-solana/programs/endpoint-mock/src/instructions/oapp/mod.rs new file mode 100644 index 000000000..54d7fe213 --- /dev/null +++ b/examples/omnicounter-solana/programs/endpoint-mock/src/instructions/oapp/mod.rs @@ -0,0 +1,3 @@ +pub mod register_oapp; + +pub use register_oapp::*; diff --git a/examples/omnicounter-solana/programs/endpoint-mock/src/instructions/oapp/register_oapp.rs b/examples/omnicounter-solana/programs/endpoint-mock/src/instructions/oapp/register_oapp.rs new file mode 100644 index 000000000..5da8dfd3e --- /dev/null +++ b/examples/omnicounter-solana/programs/endpoint-mock/src/instructions/oapp/register_oapp.rs @@ -0,0 +1,35 @@ +use crate::*; + +use cpi_helper::CpiContext; + +#[event_cpi] +#[derive(CpiContext, Accounts)] +#[instruction(params: RegisterOAppParams)] +pub struct RegisterOApp<'info> { + #[account(mut)] + pub payer: Signer<'info>, + /// The PDA of the OApp + pub oapp: Signer<'info>, + #[account( + init, + payer = payer, + space = 8 + OAppRegistry::INIT_SPACE, + seeds = [OAPP_SEED, oapp.key.as_ref()], + bump + )] + pub oapp_registry: Account<'info, OAppRegistry>, + pub system_program: Program<'info, System>, +} + +impl RegisterOApp<'_> { + pub fn apply(ctx: &mut Context, params: &RegisterOAppParams) -> Result<()> { + ctx.accounts.oapp_registry.delegate = params.delegate; + ctx.accounts.oapp_registry.bump = ctx.bumps.oapp_registry; + Ok(()) + } +} + +#[derive(Clone, AnchorSerialize, AnchorDeserialize)] +pub struct RegisterOAppParams { + pub delegate: Pubkey, +} diff --git a/examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs b/examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs new file mode 100644 index 000000000..191775432 --- /dev/null +++ b/examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs @@ -0,0 +1,19 @@ +pub mod instructions; +pub mod state; + +use anchor_lang::prelude::*; +use instructions::*; +use state::*; + +declare_id!("C788vjjQLvMJFjRRYTQi2TyueH9XecBi5Ck45G57qWcC"); + +pub const OAPP_SEED: &[u8] = b"OApp"; + +#[program] +pub mod endpoint { + use super::*; + + pub fn register_oapp(mut ctx: Context, params: RegisterOAppParams) -> Result<()> { + RegisterOApp::apply(&mut ctx, ¶ms) + } +} diff --git a/examples/omnicounter-solana/programs/endpoint-mock/src/state/endpoint.rs b/examples/omnicounter-solana/programs/endpoint-mock/src/state/endpoint.rs new file mode 100644 index 000000000..42ff5cbd6 --- /dev/null +++ b/examples/omnicounter-solana/programs/endpoint-mock/src/state/endpoint.rs @@ -0,0 +1,8 @@ +use crate::*; + +#[account] +#[derive(InitSpace)] +pub struct OAppRegistry { + pub delegate: Pubkey, + pub bump: u8, +} diff --git a/examples/omnicounter-solana/programs/endpoint-mock/src/state/mod.rs b/examples/omnicounter-solana/programs/endpoint-mock/src/state/mod.rs new file mode 100644 index 000000000..86edb11f4 --- /dev/null +++ b/examples/omnicounter-solana/programs/endpoint-mock/src/state/mod.rs @@ -0,0 +1,3 @@ +pub mod endpoint; + +pub use endpoint::*; diff --git a/examples/omnicounter-solana/test/anchor/omnicounter.test.ts b/examples/omnicounter-solana/test/anchor/omnicounter.test.ts index 4c47ac18b..1d041990e 100644 --- a/examples/omnicounter-solana/test/anchor/omnicounter.test.ts +++ b/examples/omnicounter-solana/test/anchor/omnicounter.test.ts @@ -1,6 +1,36 @@ +import * as anchor from '@coral-xyz/anchor' +import { PublicKey } from '@solana/web3.js' + +import { EndpointProgram } from '@layerzerolabs/lz-solana-sdk-v2' + +import { OmniCounter } from '../../src/omnicounter' +import endpointIdl from '../../target/idl/endpoint.json' +import omniCounterIdl from '../../target/idl/omnicounter.json' + describe('OmniCounter', () => { + const provider = anchor.AnchorProvider.local(undefined, { + commitment: 'confirmed', + preflightCommitment: 'confirmed', + }) + const connection = provider.connection + const wallet = provider.wallet as anchor.Wallet + const OMNICOUNTER_PROGRAM_ID = new PublicKey(omniCounterIdl.metadata.address) + const ENDPOINT_PROGRAM_ID = new PublicKey(endpointIdl.metadata.address) + it('should initialize an OmniCounter', async () => { - console.log('OmniCounter not implemented') - // No other logic or checks here + const counter = new OmniCounter(OMNICOUNTER_PROGRAM_ID) + const endpoint = new EndpointProgram.Endpoint(ENDPOINT_PROGRAM_ID) + + const initInstruction = await counter.initCount(connection, wallet.publicKey, wallet.publicKey, endpoint) + + if (initInstruction === null) { + throw new Error('initInstruction is null') + } + + await provider.sendAndConfirm(new anchor.web3.Transaction().add(initInstruction), [wallet.payer]) + + const count = await counter.getCount(connection) + + expect(count?.count.toString()).toBe('0') }) }) From f0cae5fe3bb3f48ee5dee96d1632e30240bae9ed Mon Sep 17 00:00:00 2001 From: DanL0 Date: Tue, 10 Sep 2024 11:13:44 +0200 Subject: [PATCH 2/2] change solana endpoint program id in omnicounter --- examples/omnicounter-solana/Anchor.toml | 1 + .../programs/counter/src/instructions/init_count.rs | 4 ++-- examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/omnicounter-solana/Anchor.toml b/examples/omnicounter-solana/Anchor.toml index f29d291f5..d734bb966 100644 --- a/examples/omnicounter-solana/Anchor.toml +++ b/examples/omnicounter-solana/Anchor.toml @@ -7,6 +7,7 @@ skip-lint = false [programs.localnet] omnicounter = '2tLJfE12h5RY7vJqK6i41taeg8ejzigoFXduBanDV4Cu' +endpoint = '76y77prsiCMvXMjuoZ5VRrhG5qYBrUMYTE5WgHqgjEn6' [registry] url = "https://api.apr.dev" diff --git a/examples/omnicounter-solana/programs/counter/src/instructions/init_count.rs b/examples/omnicounter-solana/programs/counter/src/instructions/init_count.rs index ff8f9d1b0..e95f021ce 100644 --- a/examples/omnicounter-solana/programs/counter/src/instructions/init_count.rs +++ b/examples/omnicounter-solana/programs/counter/src/instructions/init_count.rs @@ -1,5 +1,5 @@ use crate::*; -use oapp::endpoint::{instructions::RegisterOAppParams}; +use oapp::endpoint::{instructions::RegisterOAppParams, ID as ENDPOINT_ID}; #[derive(Accounts)] #[instruction(params: InitCountParams)] @@ -47,7 +47,7 @@ impl InitCount<'_> { let register_params = RegisterOAppParams { delegate: ctx.accounts.count.admin }; let seeds: &[&[u8]] = &[COUNT_SEED, &[ctx.accounts.count.id], &[ctx.accounts.count.bump]]; oapp::endpoint_cpi::register_oapp( - params.endpoint, + ENDPOINT_ID, ctx.accounts.count.key(), ctx.remaining_accounts, seeds, diff --git a/examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs b/examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs index 191775432..522ccbdb5 100644 --- a/examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs +++ b/examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs @@ -5,7 +5,7 @@ use anchor_lang::prelude::*; use instructions::*; use state::*; -declare_id!("C788vjjQLvMJFjRRYTQi2TyueH9XecBi5Ck45G57qWcC"); +declare_id!("76y77prsiCMvXMjuoZ5VRrhG5qYBrUMYTE5WgHqgjEn6"); pub const OAPP_SEED: &[u8] = b"OApp";