Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🗞️ Solana: add OmniCounter tests #885

Open
wants to merge 2 commits into
base: feat/omnicounter-solana
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/omnicounter-solana/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ coverage.json
target
typechain
typechain-types
test-ledger

# Hardhat files
cache
Expand Down
1 change: 1 addition & 0 deletions examples/omnicounter-solana/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ skip-lint = false

[programs.localnet]
omnicounter = '2tLJfE12h5RY7vJqK6i41taeg8ejzigoFXduBanDV4Cu'
endpoint = '76y77prsiCMvXMjuoZ5VRrhG5qYBrUMYTE5WgHqgjEn6'

[registry]
url = "https://api.apr.dev"
Expand Down
9 changes: 9 additions & 0 deletions examples/omnicounter-solana/Cargo.lock

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

22 changes: 22 additions & 0 deletions examples/omnicounter-solana/programs/endpoint-mock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
2 changes: 2 additions & 0 deletions examples/omnicounter-solana/programs/endpoint-mock/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod oapp;

pub use oapp::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod register_oapp;

pub use register_oapp::*;
Original file line number Diff line number Diff line change
@@ -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<RegisterOApp>, 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,
}
19 changes: 19 additions & 0 deletions examples/omnicounter-solana/programs/endpoint-mock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub mod instructions;
pub mod state;

use anchor_lang::prelude::*;
use instructions::*;
use state::*;

declare_id!("76y77prsiCMvXMjuoZ5VRrhG5qYBrUMYTE5WgHqgjEn6");

pub const OAPP_SEED: &[u8] = b"OApp";

#[program]
pub mod endpoint {
use super::*;

pub fn register_oapp(mut ctx: Context<RegisterOApp>, params: RegisterOAppParams) -> Result<()> {
RegisterOApp::apply(&mut ctx, &params)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::*;

#[account]
#[derive(InitSpace)]
pub struct OAppRegistry {
pub delegate: Pubkey,
pub bump: u8,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod endpoint;

pub use endpoint::*;
34 changes: 32 additions & 2 deletions examples/omnicounter-solana/test/anchor/omnicounter.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
Loading