-
Notifications
You must be signed in to change notification settings - Fork 273
IP-1286 test(oft-solana): add solana tests #1897
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
Open
nazreen
wants to merge
26
commits into
main
Choose a base branch
from
init-solana-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
84c4da3
Add OFT Solana example tests
nazreen 5840ef1
Ignore oft-solana test ledger
nazreen 1da29da
test(oft-solana): expand suite coverage
nazreen 937d1eb
move to test/anchor
nazreen 368391d
gen lockfiles
nazreen 5d6920e
update lockfile
nazreen a9a73d8
lint
nazreen aa13ba0
fix lint warnings
nazreen d8cae3f
fix runtime error
nazreen 74832cc
fix
nazreen 6c50bc3
fix
nazreen 8e7243c
fix
nazreen c7f5471
fix
nazreen 5fa115c
DEVREL-1077 migrate oft-solana anchor tests from solana-test-validato…
nazreen 2ae55ab
Merge branch 'main' into init-solana-tests
nazreen 10995bb
rm unused
nazreen 3af6dd1
lockfile
nazreen 25c7dc5
lockfiles:generate
nazreen b9340cf
Merge branch 'main' into init-solana-tests
nazreen da045f3
Merge branch 'main' into init-solana-tests
nazreen 39e2e94
PR feedbacks
nazreen 406b5a7
fix keypair path issue
nazreen fb12e7a
bump solidity version
nazreen a482c21
changeset
nazreen 8415e95
bump
nazreen 39dadc2
specify evmVersion
nazreen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@layerzerolabs/oft-solana-example": patch | ||
| --- | ||
|
|
||
| Add OFT Solana test coverage for peer/config validation and fee withdrawal guards, plus a got shim for test runs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ node_modules | |
| coverage | ||
| coverage.json | ||
| target | ||
| test-ledger | ||
| typechain | ||
| typechain-types | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import fs from 'fs' | ||
| import path from 'path' | ||
| import { $ } from 'zx' | ||
|
|
||
| interface FeatureInfo { | ||
| description: string | ||
| id: string | ||
| status: string | ||
| } | ||
|
|
||
| interface FeaturesResponse { | ||
| features: FeatureInfo[] | ||
| } | ||
|
|
||
| async function generateFeatures(): Promise<void> { | ||
| console.log('Retrieving mainnet feature flags...') | ||
|
|
||
| try { | ||
| const rpcEndpoints = [ | ||
| 'https://api.mainnet-beta.solana.com', | ||
| 'https://solana-rpc.publicnode.com', | ||
| 'https://rpc.ankr.com/solana', | ||
| ] | ||
|
|
||
| let features: FeaturesResponse | null = null | ||
| let lastError: unknown = null | ||
|
|
||
| for (const rpc of rpcEndpoints) { | ||
| try { | ||
| console.log(` Trying ${rpc}...`) | ||
| features = (await $`solana feature status -u ${rpc} --display-all --output json-compact`).json() | ||
| break | ||
| } catch (error) { | ||
| lastError = error | ||
| console.log(` Failed with ${rpc}, trying next...`) | ||
| } | ||
| } | ||
|
|
||
| if (!features) { | ||
| throw lastError || new Error('All RPC endpoints failed') | ||
| } | ||
|
|
||
| const inactiveFeatures = features.features.filter((feature) => feature.status === 'inactive') | ||
|
|
||
| console.log(`Found ${inactiveFeatures.length} inactive features`) | ||
|
|
||
| const targetDir = path.join(__dirname, '../target/programs') | ||
| const featuresFile = path.join(targetDir, 'features.json') | ||
|
|
||
| if (!fs.existsSync(targetDir)) { | ||
| fs.mkdirSync(targetDir, { recursive: true }) | ||
| } | ||
|
|
||
| const featuresData = { | ||
| timestamp: new Date().toISOString(), | ||
| source: 'https://solana-rpc.publicnode.com', | ||
|
nazreen marked this conversation as resolved.
Outdated
|
||
| totalFeatures: features.features.length, | ||
| inactiveFeatures, | ||
| inactiveCount: inactiveFeatures.length, | ||
| } | ||
|
|
||
| fs.writeFileSync(featuresFile, JSON.stringify(featuresData, null, 2)) | ||
|
|
||
| console.log(`Features data saved to ${featuresFile}`) | ||
| console.log(`Cached ${inactiveFeatures.length} inactive features for faster test startup`) | ||
| } catch (error) { | ||
| console.error('Failed to retrieve features:', error) | ||
| process.exit(1) | ||
| } | ||
| } | ||
|
|
||
| ;(async (): Promise<void> => { | ||
| await generateFeatures() | ||
| })().catch((err: unknown) => { | ||
| console.error(err) | ||
| process.exit(1) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { publicKey } from '@metaplex-foundation/umi' | ||
| import { utils } from '@noble/secp256k1' | ||
| import { UMI } from '@layerzerolabs/lz-solana-sdk-v2' | ||
|
|
||
| export const SRC_EID = 50168 | ||
| export const DST_EID = 50125 | ||
| export const INVALID_EID = 999999 // Non-existent EID for testing | ||
| export const TON_EID = 50343 | ||
|
|
||
| export const OFT_PROGRAM_ID = publicKey('9UovNrJD8pQyBLheeHNayuG1wJSEAoxkmM14vw5gcsTT') | ||
|
|
||
| export const DVN_SIGNERS = new Array(4).fill(0).map(() => utils.randomPrivateKey()) | ||
|
|
||
| export const OFT_DECIMALS = 6 | ||
|
|
||
| export const defaultMultiplierBps = 12500 // 125% | ||
|
|
||
| export const simpleMessageLib: UMI.SimpleMessageLibProgram.SimpleMessageLib = | ||
| new UMI.SimpleMessageLibProgram.SimpleMessageLib(UMI.SimpleMessageLibProgram.SIMPLE_MESSAGELIB_PROGRAM_ID) | ||
|
|
||
| export const endpoint: UMI.EndpointProgram.Endpoint = new UMI.EndpointProgram.Endpoint( | ||
| UMI.EndpointProgram.ENDPOINT_PROGRAM_ID | ||
| ) | ||
| export const uln: UMI.UlnProgram.Uln = new UMI.UlnProgram.Uln(UMI.UlnProgram.ULN_PROGRAM_ID) | ||
| export const executor: UMI.ExecutorProgram.Executor = new UMI.ExecutorProgram.Executor( | ||
| UMI.ExecutorProgram.EXECUTOR_PROGRAM_ID | ||
| ) | ||
| export const priceFeed: UMI.PriceFeedProgram.PriceFeed = new UMI.PriceFeedProgram.PriceFeed( | ||
| UMI.PriceFeedProgram.PRICEFEED_PROGRAM_ID | ||
| ) | ||
|
|
||
| export const dvns = [publicKey('HtEYV4xB4wvsj5fgTkcfuChYpvGYzgzwvNhgDZQNh7wW')] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| const Module = require('module') | ||
|
|
||
| const originalLoad = Module._load | ||
|
|
||
| const gotStub = { | ||
| default: Object.assign(async () => { | ||
| throw new Error('got stub: network client not available in tests') | ||
| }, { | ||
| get: async () => { | ||
| throw new Error('got stub: network client not available in tests') | ||
| }, | ||
| post: async () => { | ||
| throw new Error('got stub: network client not available in tests') | ||
| }, | ||
| }), | ||
| } | ||
|
|
||
| Module._load = function (request, parent, isMain) { | ||
| if (request === 'got') { | ||
| return gotStub | ||
| } | ||
| return originalLoad.call(this, request, parent, isMain) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { Program, ProgramError } from '@metaplex-foundation/umi' | ||
| import assert from 'assert' | ||
| import { oft } from '@layerzerolabs/oft-v2-solana-sdk' | ||
|
|
||
| export async function expectOftError<T extends ProgramError>( | ||
| operation: () => Promise<unknown>, | ||
| expectedErrorClass: new (...args: unknown[]) => T, | ||
| program: Program, | ||
| customMessage?: string | ||
| ): Promise<void> { | ||
| try { | ||
| await operation() | ||
| assert.fail(`Expected ${expectedErrorClass.name} to be thrown, but operation succeeded`) | ||
| } catch (error: unknown) { | ||
| const errorMessage = customMessage || `Expected ${expectedErrorClass.name}` | ||
| assertOftError(error, expectedErrorClass, program, errorMessage) | ||
| } | ||
| } | ||
|
|
||
| export function assertOftError<T extends ProgramError>( | ||
| error: unknown, | ||
| expectedErrorClass: new (...args: unknown[]) => T, | ||
| program: Program, | ||
| message: string | ||
| ): void { | ||
| const isMatch = isOftError(error, expectedErrorClass, program) | ||
|
|
||
| if (!isMatch) { | ||
| const actualInfo = getErrorInfo(error) | ||
| assert.fail(`${message}, but got: ${actualInfo}\nExpected: ${expectedErrorClass.name}`) | ||
| } | ||
| } | ||
|
|
||
| function isOftError<T extends ProgramError>( | ||
| error: unknown, | ||
| expectedErrorClass: new (...args: unknown[]) => T, | ||
| program: Program | ||
| ): boolean { | ||
| const instance = new expectedErrorClass(program) | ||
| if (!('code' in instance) || typeof instance.code !== 'number') { | ||
| return false | ||
| } | ||
| const expectedMessage = `Error Code: ${instance.name}. Error Number: ${instance.code.toString(10)}` | ||
|
|
||
| if ( | ||
| typeof error === 'object' && | ||
| 'message' in error && | ||
| typeof error.message === 'string' && | ||
| error.message.startsWith('Simulate Fail:') | ||
| ) { | ||
| const msg = error.message.split('Simulate Fail:')[1] | ||
| return JSON.stringify(msg).includes(expectedMessage) | ||
| } | ||
|
|
||
| if ( | ||
| typeof error !== 'object' || | ||
| !('transactionError' in error) || | ||
| typeof error.transactionError !== 'object' || | ||
| !('logs' in error.transactionError) | ||
| ) { | ||
| return false | ||
| } | ||
|
|
||
| const logs = error.transactionError.logs | ||
| return JSON.stringify(logs).includes(expectedMessage) | ||
| } | ||
|
|
||
| function getErrorInfo(error: unknown): string { | ||
| const err = error as { message?: string; code?: string; name?: string; logs?: string[] } | ||
| const parts = [] | ||
|
|
||
| if (err.message) { | ||
| parts.push(`Message: "${err.message}"`) | ||
| } | ||
|
|
||
| if (err.code) { | ||
| parts.push(`Code: ${err.code}`) | ||
| } | ||
|
|
||
| if (err.name) { | ||
| parts.push(`Name: ${err.name}`) | ||
| } | ||
|
|
||
| if (err.logs) { | ||
| const errorLogs = err.logs.filter((log: string) => log.includes('Error Code:') || log.includes('Error Number:')) | ||
| if (errorLogs.length > 0) { | ||
| parts.push(`Logs: [${errorLogs.slice(0, 2).join(', ')}]`) | ||
| } | ||
| } | ||
|
|
||
| return parts.length > 0 ? parts.join(', ') : 'Unknown error format' | ||
| } | ||
|
|
||
| export const OftErrors = oft.errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export { initOft, send } from './oft-layerzero-simulation' | ||
| export { quoteSend, quoteOft } from './oft-quotes' | ||
| export { createOftKeys, createOftKeySets, createKeypairFromSeed, createSignerFromSeed } from './test-keys' | ||
| export { expectOftError, assertOftError, OftErrors } from './error-assertions' |
107 changes: 107 additions & 0 deletions
107
examples/oft-solana/tests/helpers/oft-layerzero-simulation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { KeypairSigner, PublicKey, Umi, publicKeyBytes } from '@metaplex-foundation/umi' | ||
| import { base58 } from '@metaplex-foundation/umi/serializers' | ||
| import { fromWeb3JsPublicKey, toWeb3JsPublicKey } from '@metaplex-foundation/umi-web3js-adapters' | ||
| import { Options } from '@layerzerolabs/lz-v2-utilities' | ||
| import { UMI } from '@layerzerolabs/lz-solana-sdk-v2' | ||
| import { TOKEN_PROGRAM_ID } from '@solana/spl-token' | ||
|
|
||
| import { endpoint, OFT_PROGRAM_ID } from '../constants' | ||
| import { TestContext, OftKeys, PacketSentEvent } from '../types' | ||
| import { sendAndConfirm } from '../utils' | ||
| import { oft, OFT_DECIMALS } from '@layerzerolabs/oft-v2-solana-sdk' | ||
|
|
||
| const SIMULATION_EXECUTION_OPTIONS = Options.newOptions().addExecutorLzReceiveOption(200000, 200000 * 5) | ||
| const SIMULATION_COMPUTE_UNITS = 400000 | ||
|
|
||
| export async function initOft( | ||
| umi: Umi, | ||
| keys: OftKeys, | ||
| oftType: oft.types.OFTType, | ||
| sharedDecimals = OFT_DECIMALS | ||
| ): Promise<void> { | ||
| const ix = oft.initOft( | ||
| { | ||
| payer: keys.oappAdmin, | ||
| admin: keys.oappAdmin.publicKey, | ||
| mint: keys.mint.publicKey, | ||
| escrow: keys.escrow, | ||
| }, | ||
| oftType, | ||
| sharedDecimals, | ||
| { | ||
| oft: OFT_PROGRAM_ID, | ||
| endpoint: endpoint.programId, | ||
| } | ||
| ) | ||
|
|
||
| await sendAndConfirm(umi, ix, keys.oappAdmin) | ||
| } | ||
|
|
||
| export async function send( | ||
| context: TestContext, | ||
| oftKeys: OftKeys, | ||
| source: KeypairSigner, | ||
| sourceTokenAccount: PublicKey, | ||
| dest: PublicKey, | ||
| dstEid: number, | ||
| sendAmount: bigint, | ||
| fee: UMI.EndpointProgram.types.MessagingFee, | ||
| composeMsg?: Uint8Array, | ||
| minAmountLd: bigint = 0n | ||
| ): Promise<PacketSentEvent> { | ||
| const { umi } = context | ||
|
|
||
| const ix = await oft.send( | ||
| umi.rpc, | ||
| { | ||
| payer: source, | ||
| tokenMint: oftKeys.mint.publicKey, | ||
| tokenEscrow: oftKeys.escrow.publicKey, | ||
| tokenSource: sourceTokenAccount, | ||
| }, | ||
| { | ||
| dstEid, | ||
| to: publicKeyBytes(dest), | ||
| amountLd: sendAmount, | ||
| minAmountLd, | ||
| options: SIMULATION_EXECUTION_OPTIONS.toBytes(), | ||
| composeMsg, | ||
| nativeFee: fee.nativeFee, | ||
| lzTokenFee: fee.lzTokenFee, | ||
| }, | ||
| { | ||
| oft: context.program.publicKey, | ||
| endpoint: endpoint.programId, | ||
| token: fromWeb3JsPublicKey(TOKEN_PROGRAM_ID), | ||
| } | ||
| ) | ||
|
|
||
| const { signature } = await sendAndConfirm( | ||
| umi, | ||
| ix, | ||
| source, | ||
| SIMULATION_COMPUTE_UNITS, | ||
| context.lookupTable === undefined ? undefined : [context.lookupTable] | ||
| ) | ||
|
|
||
| return extractPacketSentEvent(context, signature) | ||
| } | ||
|
|
||
| async function extractPacketSentEvent(context: TestContext, signature: Uint8Array): Promise<PacketSentEvent> { | ||
| const signatureBase58 = base58.deserialize(signature)[0] | ||
| const maxAttempts = 10 | ||
| for (let attempt = 0; attempt < maxAttempts; attempt++) { | ||
| const events = await UMI.extractEventFromTransactionSignature( | ||
| context.connection, | ||
| toWeb3JsPublicKey(endpoint.programId), | ||
| signatureBase58, | ||
| UMI.EndpointProgram.events.getPacketSentEventSerializer(), | ||
| { commitment: 'confirmed', maxSupportedTransactionVersion: 0 } | ||
| ) | ||
| if (events && events.length > 0) { | ||
| return events[0] | ||
| } | ||
| await new Promise((resolve) => setTimeout(resolve, 500)) | ||
| } | ||
| throw new Error(`PacketSent event not found for signature ${signatureBase58}`) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.