Skip to content

Commit

Permalink
🧹 Solana: Backtrack getEndpointConfigAddress changes [21/N] (LayerZer…
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Jul 26, 2024
1 parent 783461e commit 02b35af
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 105 deletions.
7 changes: 7 additions & 0 deletions .changeset/yellow-swans-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@layerzerolabs/ua-devtools-solana": patch
"@layerzerolabs/ua-devtools-evm": patch
"@layerzerolabs/ua-devtools": patch
---

Remove recently introduced getEndpointConfigAddress method on IOApp
9 changes: 0 additions & 9 deletions packages/ua-devtools-evm/src/oapp/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ export class OApp extends Ownable implements IOApp {
super(contract)
}

/**
* For EVM OApps, the config is stored under the OApp's address
*
* @returns {OmniAddress}
*/
getEndpointConfigAddress(): OmniAddress {
return this.contract.contract.address
}

@AsyncRetriable()
async getEndpointSDK(): Promise<IEndpointV2> {
this.logger.debug(`Getting EndpointV2 SDK`)
Expand Down
27 changes: 7 additions & 20 deletions packages/ua-devtools-solana/src/oft/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IOApp, OAppEnforcedOptionParam } from '@layerzerolabs/ua-devtools'
import { OftTools, OFT_SEED, EndpointProgram } from '@layerzerolabs/lz-solana-sdk-v2'
import { OftTools, EndpointProgram, OftProgram } from '@layerzerolabs/lz-solana-sdk-v2'
import {
type OmniAddress,
type OmniTransaction,
Expand Down Expand Up @@ -28,21 +28,12 @@ export class OFT extends OmniSDK implements IOApp {
connection: Connection,
point: OmniPoint,
userAccount: PublicKey,
public readonly mintAccount: PublicKey,
public readonly programId: PublicKey = OftProgram.OFT_DEFAULT_PROGRAM_ID,
logger?: Logger
) {
super(connection, point, userAccount, logger)
}

/**
* For Solana OFTs, the endpoint config is stored under the config account
*
* @returns {OmniAddress}
*/
getEndpointConfigAddress(): OmniAddress {
return this.configAccount.toBase58()
}

getOwner(): Promise<OmniAddress | undefined> {
throw new Error('Method not implemented.')
}
Expand All @@ -55,10 +46,6 @@ export class OFT extends OmniSDK implements IOApp {
throw new Error('Method not implemented.')
}

get configAccount(): PublicKey {
return PublicKey.findProgramAddressSync([Buffer.from(OFT_SEED), this.mintAccount.toBuffer()], this.publicKey)[0]
}

@AsyncRetriable()
async getEndpointSDK(): Promise<IEndpointV2> {
this.logger.debug(`Getting EndpointV2 SDK`)
Expand All @@ -76,7 +63,7 @@ export class OFT extends OmniSDK implements IOApp {

this.logger.debug(`Getting peer for ${eidLabel}`)
try {
const peer = await OftTools.getPeerAddress(this.connection, this.configAccount, eid, this.publicKey)
const peer = await OftTools.getPeerAddress(this.connection, this.publicKey, eid, this.programId)

// We run the hex string we got through a normalization/denormalization process
// that will ensure that zero addresses will get stripped
Expand Down Expand Up @@ -110,7 +97,7 @@ export class OFT extends OmniSDK implements IOApp {
this.logger.debug(`Setting peer for eid ${eid} (${eidLabel}) to address ${peerAsBytes32}`)

const transaction = new Transaction().add(
await OftTools.createSetPeerIx(this.userAccount, this.configAccount, eid, normalizedPeer, this.publicKey)
await OftTools.createSetPeerIx(this.userAccount, this.publicKey, eid, normalizedPeer, this.programId)
)

return {
Expand Down Expand Up @@ -148,7 +135,7 @@ export class OFT extends OmniSDK implements IOApp {
this.logger.verbose(`Getting enforced options for ${eidLabel} and message type ${msgType}`)

try {
const options = await OftTools.getEnforcedOptions(this.connection, this.configAccount, eid, this.publicKey)
const options = await OftTools.getEnforcedOptions(this.connection, this.publicKey, eid, this.programId)
const optionsForMsgType = msgType === MSG_TYPE_SEND ? options.send : options.sendAndCall

return toHex(optionsForMsgType)
Expand Down Expand Up @@ -176,11 +163,11 @@ export class OFT extends OmniSDK implements IOApp {

const instruction = await OftTools.createSetEnforcedOptionsIx(
this.userAccount, // your admin address
this.configAccount, // your OFT Config
this.publicKey, // your OFT Config
eid, // destination endpoint id for the options to apply to
sendOption,
sendAndCallOption,
this.publicKey
this.programId
)

transaction.add(instruction)
Expand Down
42 changes: 21 additions & 21 deletions packages/ua-devtools-solana/test/oft/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ describe('oft/sdk', () => {
//
// We need to run our own Solana node with the OFT account cloned
// so that we can isolate these tests
const point = { eid: EndpointId.SOLANA_V2_MAINNET, address: 'Ag28jYmND83RnwcSFq2vwWxThSya55etjWJwubd8tRXs' }
const programId = new PublicKey('Ag28jYmND83RnwcSFq2vwWxThSya55etjWJwubd8tRXs')
const point = { eid: EndpointId.SOLANA_V2_MAINNET, address: '8aFeCEhGLwbWHWiiezLAKanfD5Cn3BW3nP6PZ54K9LYC' }
const account = new PublicKey('6tzUZqC33igPgP7YyDnUxQg6eupMmZGRGKdVAksgRzvk')
const mintAccount = new PublicKey('Bq9wBU8fqFnUDkrWqLFXGRc7BvRMPjUkCM2SrJf6dBMv')

afterEach(() => {
createSetEnforcedOptionsIxMock.mockClear()
Expand All @@ -39,7 +39,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

expect(await sdk.getPeer(EndpointId.ETHEREUM_V2_TESTNET)).toBeUndefined()
})
Expand All @@ -48,7 +48,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

const peer = await sdk.getPeer(EndpointId.ETHEREUM_V2_MAINNET)
expect(peer).toEqual(expect.any(String))
Expand All @@ -67,7 +67,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

const omniTransaction = await sdk.setPeer(EndpointId.ETHEREUM_V2_MAINNET, makeBytes32())
expect(omniTransaction).toEqual({
Expand All @@ -83,7 +83,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

const omniTransaction = await sdk.setPeer(EndpointId.APTOS_MAINNET, makeBytes32())
expect(omniTransaction).toEqual({
Expand All @@ -99,7 +99,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

const omniTransaction = await sdk.setPeer(EndpointId.SOLANA_V2_MAINNET, point.address)
expect(omniTransaction).toEqual({
Expand All @@ -116,7 +116,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

await expect(sdk.getEnforcedOptions(EndpointId.ETHEREUM_V2_TESTNET, 3)).rejects.toMatchSnapshot()
})
Expand All @@ -125,7 +125,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

expect(await sdk.getEnforcedOptions(EndpointId.ETHEREUM_V2_TESTNET, 1)).toBe('0x')
})
Expand All @@ -134,7 +134,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

const sendOptionsHex = await sdk.getEnforcedOptions(EndpointId.ETHEREUM_V2_MAINNET, 1)
expect(sendOptionsHex).toEqual(expect.any(String))
Expand All @@ -155,7 +155,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

await expect(
sdk.setEnforcedOptions([
Expand All @@ -171,7 +171,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

const enforcedOptions = [
{
Expand All @@ -192,7 +192,7 @@ describe('oft/sdk', () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)

const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)

const enforcedOptions = [
// We'll set two options for ethereum
Expand Down Expand Up @@ -229,31 +229,31 @@ describe('oft/sdk', () => {
// Ethereum should have both msgType options set
expect(createSetEnforcedOptionsIxMock).toHaveBeenCalledWith(
sdk.userAccount,
sdk.configAccount,
sdk.publicKey,
EndpointId.ETHEREUM_V2_TESTNET,
Options.newOptions().addExecutorOrderedExecutionOption().toBytes(),
Options.newOptions().addExecutorLzReceiveOption(1, 1).toBytes(),
sdk.publicKey
programId
)

// Base should have one option set
expect(createSetEnforcedOptionsIxMock).toHaveBeenCalledWith(
sdk.userAccount,
sdk.configAccount,
sdk.publicKey,
EndpointId.BASE_V2_MAINNET,
Options.newOptions().addExecutorLzReceiveOption(4, 1).toBytes(),
Options.newOptions().toBytes(),
sdk.publicKey
programId
)

// Avalanche should use the latter option and ignore the first one
expect(createSetEnforcedOptionsIxMock).toHaveBeenCalledWith(
sdk.userAccount,
sdk.configAccount,
sdk.publicKey,
EndpointId.AVALANCHE_V2_MAINNET,
Options.newOptions().toBytes(),
Options.newOptions().addExecutorLzReceiveOption(3, 1).toBytes(),
sdk.publicKey
programId
)
})
})
Expand All @@ -262,14 +262,14 @@ describe('oft/sdk', () => {
it('should return an SDK with the correct eid and address', async () => {
const connectionFactory = createConnectionFactory(defaultRpcUrlFactory)
const connection = await connectionFactory(EndpointId.SOLANA_V2_MAINNET)
const sdk = new OFT(connection, point, account, mintAccount)
const sdk = new OFT(connection, point, account, programId)
const endpointSdk = await sdk.getEndpointSDK()

expect(endpointSdk.point).toEqual({ eid: sdk.point.eid, address: EndpointProgram.PROGRAM_ID.toBase58() })

// Run a random function on the SDK to check whether it works
expect(
await endpointSdk.isDefaultSendLibrary(sdk.configAccount.toBase58(), EndpointId.ETHEREUM_V2_MAINNET)
await endpointSdk.isDefaultSendLibrary(sdk.publicKey.toBase58(), EndpointId.ETHEREUM_V2_MAINNET)
).toBeFalsy()
})
})
Expand Down
Loading

0 comments on commit 02b35af

Please sign in to comment.