Skip to content

Commit

Permalink
chore: Update ULN getter for Solana
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Jul 31, 2024
1 parent 5b9938a commit 63d7785
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 17 deletions.
3 changes: 3 additions & 0 deletions packages/devtools-solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
"@solana/web3.js": "~1.95.0",
"@swc/core": "^1.4.0",
"@swc/jest": "^0.2.36",
"@types/bn.js": "~5.1.5",
"@types/jest": "^29.5.12",
"bn.js": "^5.2.0",
"fast-check": "^3.15.1",
"fp-ts": "^2.16.2",
"jest": "^29.7.0",
Expand All @@ -66,6 +68,7 @@
"@layerzerolabs/io-devtools": "~0.1.11",
"@layerzerolabs/lz-definitions": "^2.3.3",
"@solana/web3.js": "~1.95.0",
"bn.js": "^5.2.0",
"fp-ts": "^2.16.2",
"zod": "^3.22.4"
},
Expand Down
1 change: 1 addition & 0 deletions packages/devtools-solana/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './schema'
export * from './types'
9 changes: 9 additions & 0 deletions packages/devtools-solana/src/common/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from 'zod'
import BN from 'bn.js'
import { PublicKey } from '@solana/web3.js'

export const BNBigIntSchema = z.instanceof(BN).transform((bn) => BigInt(bn.toString()))

export const PublicKeySchema = z.instanceof(PublicKey)

export const PublicKeyBase58Schema = PublicKeySchema.transform((key) => key.toBase58())
47 changes: 47 additions & 0 deletions packages/devtools-solana/test/common/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { BNBigIntSchema, PublicKeySchema } from '@/common/schema'
import { keypairArbitrary } from '@layerzerolabs/test-devtools-solana'
import BN from 'bn.js'
import fc from 'fast-check'

describe('common/schema', () => {
describe('BNBigIntSchema', () => {
const bnArbitrary: fc.Arbitrary<BN> = fc.bigInt().map((value) => new BN(value.toString()))

it('should parse a BN instance', () => {
fc.assert(
fc.property(bnArbitrary, (bn) => {
expect(BNBigIntSchema.parse(bn)).toBe(BigInt(bn.toString()))
})
)
})

it('should not parse anything else', () => {
fc.assert(
fc.property(fc.anything(), (value) => {
expect(() => BNBigIntSchema.parse(value)).toThrow(/Input not instance of BN/)
})
)
})
})

describe('PublicKeySchema', () => {
it('should parse a PublicKey instance', () => {
fc.assert(
fc.property(
keypairArbitrary.map((keypair) => keypair.publicKey),
(publicKey) => {
expect(PublicKeySchema.parse(publicKey)).toBe(publicKey)
}
)
)
})

it('should not parse anything else', () => {
fc.assert(
fc.property(fc.anything(), (value) => {
expect(() => PublicKeySchema.parse(value)).toThrow(/Input not instance of PublicKey/)
})
)
})
})
})
18 changes: 18 additions & 0 deletions packages/protocol-devtools-solana/src/uln302/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { UIntBigIntSchema, UIntNumberSchema } from '@layerzerolabs/devtools'
import { BNBigIntSchema, PublicKeyBase58Schema } from '@layerzerolabs/devtools-solana'
import { Uln302UlnConfig } from '@layerzerolabs/protocol-devtools'
import { z } from 'zod'

export const Uln302UlnConfigInputSchema: z.ZodSchema<Uln302UlnConfig, z.ZodTypeDef, unknown> = z
.object({
confirmations: z.union([UIntBigIntSchema, BNBigIntSchema]),
optionalDvnThreshold: UIntNumberSchema,
requiredDvns: z.array(PublicKeyBase58Schema),
optionalDvns: z.array(PublicKeyBase58Schema),
})
.transform(({ confirmations, optionalDvnThreshold, requiredDvns, optionalDvns }) => ({
confirmations,
optionalDVNThreshold: optionalDvnThreshold,
requiredDVNs: requiredDvns,
optionalDVNs: optionalDvns,
}))
33 changes: 16 additions & 17 deletions packages/protocol-devtools-solana/src/uln302/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import type {
import {
IUln302,
Uln302ConfigType,
Uln302ExecutorConfig,
Expand All @@ -21,6 +21,7 @@ import { OmniSDK } from '@layerzerolabs/devtools-solana'
import { UlnProgram } from '@layerzerolabs/lz-solana-sdk-v2'
import { Connection, PublicKey } from '@solana/web3.js'
import assert from 'assert'
import { Uln302UlnConfigInputSchema } from './schema'

export class Uln302 extends OmniSDK implements IUln302 {
public readonly program: UlnProgram.Uln
Expand Down Expand Up @@ -55,15 +56,22 @@ export class Uln302 extends OmniSDK implements IUln302 {
this.logger.debug(`Getting App ULN ${type} config for eid ${eid} (${eidLabel}) and address ${address}`)

const config = await mapError(
async () => {
async (): Promise<UlnProgram.UlnConfig> => {
const publicKey = new PublicKey(address)
const config =
type === Uln302ConfigType.Receive
? await this.program.getReceiveConfigState(this.connection, publicKey, eid)
: await this.program.getSendConfigState(this.connection, publicKey, eid)

return (
(await this.program.getReceiveConfigState(this.connection, publicKey, eid)) ??
(this.logger.warn(
`Got an empty App ULN ${type} config for OApp ${address} and ${eidLabel}, getting the default one`
),
await this.program.getDefaultReceiveConfigState(this.connection, eid))
config?.uln ?? {
confirmations: 0,
optionalDvnThreshold: 0,
requiredDvns: [],
optionalDvns: [],
requiredDvnCount: 0,
optionalDvnCount: 0,
}
)
},
(error) =>
Expand All @@ -77,16 +85,7 @@ export class Uln302 extends OmniSDK implements IUln302 {
`Could not get App ULN ${type} config for ${this.label} and OApp ${address} and ${eidLabel}: Neither OApp nor default configs have been specified`
)

return {
confirmations: BigInt(
typeof config.uln.confirmations === 'number'
? config.uln.confirmations
: config.uln.confirmations.toString(10)
),
optionalDVNThreshold: config.uln.optionalDvnThreshold,
requiredDVNs: config.uln.requiredDvns.map((key) => key.toBase58()),
optionalDVNs: config.uln.optionalDvns.map((key) => key.toBase58()),
}
return Uln302UlnConfigInputSchema.parse(config)
}

/**
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 63d7785

Please sign in to comment.