-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dac0da
commit 7bd271e
Showing
14 changed files
with
186 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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,7 @@ | ||
--- | ||
"@layerzerolabs/ua-devtools-evm-hardhat-test": patch | ||
"@layerzerolabs/protocol-devtools-evm": patch | ||
"@layerzerolabs/protocol-devtools": patch | ||
--- | ||
|
||
Add PriceFeed SDK |
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './endpoint' | ||
export * from './priceFeed' | ||
export * from './uln302' |
This file contains 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,15 @@ | ||
import pMemoize from 'p-memoize' | ||
import type { OmniContractFactory } from '@layerzerolabs/devtools-evm' | ||
import type { PriceFeedFactory } from '@layerzerolabs/protocol-devtools' | ||
import { PriceFeed } from './sdk' | ||
|
||
/** | ||
* Syntactic sugar that creates an instance of EVM `PriceFeed` SDK | ||
* based on an `OmniPoint` with help of an `OmniContractFactory` | ||
* | ||
* @param {OmniContractFactory} contractFactory | ||
* @returns {PriceFeedFactory<PriceFeed>} | ||
*/ | ||
export const createPriceFeedFactory = <TOmniPoint = never>( | ||
contractFactory: OmniContractFactory<TOmniPoint> | ||
): PriceFeedFactory<PriceFeed, TOmniPoint> => pMemoize(async (point) => new PriceFeed(await contractFactory(point))) |
This file contains 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,3 @@ | ||
export * from './factory' | ||
export * from './schema' | ||
export * from './sdk' |
This file contains 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,13 @@ | ||
import { BigNumberishBigintSchema } from '@layerzerolabs/devtools-evm' | ||
import { PriceData } from '@layerzerolabs/protocol-devtools' | ||
import { PriceDataSchema as PriceDataSchemaBase } from '@layerzerolabs/protocol-devtools' | ||
import { z } from 'zod' | ||
|
||
/** | ||
* Schema for parsing an ethers-specific PriceData into a common format | ||
*/ | ||
export const PriceDataSchema = PriceDataSchemaBase.extend({ | ||
priceRatio: BigNumberishBigintSchema, | ||
gasPriceInUnit: BigNumberishBigintSchema, | ||
gasPerByte: BigNumberishBigintSchema, | ||
}) satisfies z.ZodSchema<PriceData, z.ZodTypeDef, unknown> |
This file contains 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,34 @@ | ||
import type { EndpointId } from '@layerzerolabs/lz-definitions' | ||
import type { IPriceFeed, PriceData } from '@layerzerolabs/protocol-devtools' | ||
import { formatEid, type OmniTransaction } from '@layerzerolabs/devtools' | ||
import { OmniSDK } from '@layerzerolabs/devtools-evm' | ||
import { printRecord } from '@layerzerolabs/io-devtools' | ||
import { PriceDataSchema } from './schema' | ||
|
||
export class PriceFeed extends OmniSDK implements IPriceFeed { | ||
async getPrice(eid: EndpointId): Promise<PriceData> { | ||
const config = await this.contract.contract['getPrice(uint32)'](eid) | ||
|
||
// Now we convert the ethers-specific object into the common structure | ||
// | ||
// Here we need to spread the config into an object because what ethers gives us | ||
// is actually an array with extra properties | ||
return PriceDataSchema.parse({ ...config }) | ||
} | ||
|
||
async setPrice(eid: EndpointId, priceData: PriceData): Promise<OmniTransaction> { | ||
const data = this.contract.contract.interface.encodeFunctionData('setPrice', [ | ||
[ | ||
{ | ||
eid, | ||
price: priceData, | ||
}, | ||
], | ||
]) | ||
|
||
return { | ||
...this.createTransaction(data), | ||
description: `Setting price for ${formatEid(eid)}: ${printRecord(priceData)}`, | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './endpoint' | ||
export * from './priceFeed' | ||
export * from './uln302' |
This file contains 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,25 @@ | ||
import { flattenTransactions, isDeepEqual, type OmniTransaction } from '@layerzerolabs/devtools' | ||
import type { PriceFeedFactory, PriceFeedOmniGraph } from './types' | ||
|
||
export type PriceFeedConfigurator = ( | ||
graph: PriceFeedOmniGraph, | ||
createSdk: PriceFeedFactory | ||
) => Promise<OmniTransaction[]> | ||
|
||
export const configurePriceFeed: PriceFeedConfigurator = async (graph, createSdk) => | ||
flattenTransactions([await configurePriceFeedPriceData(graph, createSdk)]) | ||
|
||
export const configurePriceFeedPriceData: PriceFeedConfigurator = async (graph, createSdk) => | ||
flattenTransactions( | ||
await Promise.all( | ||
graph.connections.map(async ({ vector: { from, to }, config }): Promise<OmniTransaction[]> => { | ||
const sdk = await createSdk(from) | ||
const priceData = await sdk.getPrice(to.eid) | ||
|
||
// TODO Normalize the config values using a schema before comparing them | ||
if (isDeepEqual(priceData, config.priceData)) return [] | ||
|
||
return [await sdk.setPrice(to.eid, config.priceData)] | ||
}) | ||
) | ||
) |
This file contains 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,3 @@ | ||
export * from './config' | ||
export * from './schema' | ||
export * from './types' |
This file contains 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,9 @@ | ||
import { z } from 'zod' | ||
import type { PriceData } from './types' | ||
import { UIntSchema } from '@layerzerolabs/devtools' | ||
|
||
export const PriceDataSchema = z.object({ | ||
priceRatio: UIntSchema, | ||
gasPriceInUnit: UIntSchema, | ||
gasPerByte: UIntSchema, | ||
}) satisfies z.ZodSchema<PriceData, z.ZodTypeDef, unknown> |
This file contains 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,24 @@ | ||
import type { Factory, IOmniSDK, OmniGraph, OmniPoint, OmniTransaction } from '@layerzerolabs/devtools' | ||
import type { EndpointId } from '@layerzerolabs/lz-definitions' | ||
|
||
export interface IPriceFeed extends IOmniSDK { | ||
getPrice(eid: EndpointId): Promise<PriceData> | ||
setPrice(eid: EndpointId, priceData: PriceData): Promise<OmniTransaction> | ||
} | ||
|
||
export interface PriceData { | ||
priceRatio: bigint | string | number | ||
gasPriceInUnit: bigint | string | number | ||
gasPerByte: bigint | string | number | ||
} | ||
|
||
export interface PriceFeedEdgeConfig { | ||
priceData: PriceData | ||
} | ||
|
||
export type PriceFeedOmniGraph = OmniGraph<unknown, PriceFeedEdgeConfig> | ||
|
||
export type PriceFeedFactory<TPriceFeed extends IPriceFeed = IPriceFeed, TOmniPoint = OmniPoint> = Factory< | ||
[TOmniPoint], | ||
TPriceFeed | ||
> |
This file contains 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
This file contains 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