Skip to content

Commit

Permalink
🪲 make initConfig idempotent (#1114)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Goulding <[email protected]>
  • Loading branch information
ryandgoulding authored Dec 7, 2024
1 parent dab81ac commit cf47228
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
29 changes: 22 additions & 7 deletions packages/ua-devtools-solana/src/oft/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
OmniSDKFactory,
OmniPoint,
} from '@layerzerolabs/devtools'
import { createModuleLogger } from '@layerzerolabs/io-devtools'
import { createModuleLogger, createWithAsyncLogger } from '@layerzerolabs/io-devtools'
import { isOmniPointOnSolana } from '@layerzerolabs/devtools-solana'
import type { IOApp, OAppConfigurator, OAppOmniGraph } from '@layerzerolabs/ua-devtools'
import { OFT } from './sdk'

const createOFTLogger = () => createModuleLogger('OFT')
const withOFTLogger = createWithAsyncLogger(createOFTLogger)

/**
* Helper function that checks whether a vector originates from a Solana network
Expand Down Expand Up @@ -44,12 +45,26 @@ const onlyEdgesFromSolana = (
}

export const initConfig: OAppConfigurator = createConfigureEdges(
onlyEdgesFromSolana(async ({ vector: { to } }, sdk) => {
if (typeof sdk.initConfig !== 'function') {
return createOFTLogger().warn(`Could not find initConfig() method on OFT SDK, skipping`), undefined
}
return sdk.initConfig(to.eid)
})
onlyEdgesFromSolana(
withOFTLogger(async ({ vector: { to } }, sdk) => {
const logger = createOFTLogger()
if (typeof sdk.sendConfigIsInitialized !== 'function') {
return logger.warn(`Could not find sendConfigIsInitialized() method on OFT SDK, skipping`), undefined
}
if (typeof sdk.initConfig !== 'function') {
return logger.warn(`Could not find initConfig() method on OFT SDK, skipping`), undefined
}

logger.verbose(`Checking if the sendConfig for ${to.eid} ${to.address} is initialized`)

const isInitialized = await sdk.sendConfigIsInitialized(to.eid)
if (isInitialized) {
return logger.verbose(`sendConfig for ${to.eid} ${to.address} is already initialized`), undefined
}
logger.verbose(`Initializing sendConfig for ${to.eid} ${to.address}`)
return sdk.initConfig(to.eid)
})
)
)

export const initOFTAccounts = createConfigureMultiple(initConfig)
9 changes: 8 additions & 1 deletion packages/ua-devtools-solana/src/oft/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import { mplToolbox } from '@metaplex-foundation/mpl-toolbox'
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
import { fromWeb3JsPublicKey } from '@metaplex-foundation/umi-web3js-adapters'
import { EndpointProgram, UlnProgram } from '@layerzerolabs/lz-solana-sdk-v2'
import { EndpointProgram, MessageLibPDADeriver, UlnProgram } from '@layerzerolabs/lz-solana-sdk-v2'

// TODO: Use exported interfaces when they are available
interface SetPeerAddressParam {
Expand Down Expand Up @@ -408,6 +408,13 @@ export class OFT extends OmniSDK implements IOApp {
throw new TypeError(`getCallerBpsCap() not implemented on Solana OFT SDK`)
}

public async sendConfigIsInitialized(_eid: EndpointId): Promise<boolean> {
const deriver = new MessageLibPDADeriver(UlnProgram.PROGRAM_ID)
const [sendConfig] = deriver.sendConfig(_eid, new PublicKey(this.point.address))
const accountInfo = await this.connection.getAccountInfo(sendConfig)
return accountInfo != null
}

public async initConfig(eid: EndpointId): Promise<OmniTransaction | undefined> {
return {
...(await this.createTransaction(
Expand Down

0 comments on commit cf47228

Please sign in to comment.