-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🗞️ Adding getEnforcedOptions task (#369)
- Loading branch information
1 parent
092b4d6
commit b2670f8
Showing
12 changed files
with
680 additions
and
5 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/ua-devtools-evm-hardhat": patch | ||
"@layerzerolabs/ua-devtools": patch | ||
--- | ||
|
||
Adding getEnforcedOptions task |
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
120 changes: 120 additions & 0 deletions
120
packages/ua-devtools-evm-hardhat/src/tasks/oapp/enforced.options.get.ts
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,120 @@ | ||
import { ActionType } from 'hardhat/types' | ||
import { task } from 'hardhat/config' | ||
import { createLogger, printCrossTable, printRecord, setDefaultLogLevel } from '@layerzerolabs/io-devtools' | ||
import { TASK_LZ_OAPP_ENFORCED_OPTIONS_GET } from '@/constants/tasks' | ||
import { printLogo } from '@layerzerolabs/io-devtools/swag' | ||
import { EncodedOption, OAppOmniGraph } from '@layerzerolabs/ua-devtools' | ||
import { createConnectedContractFactory, types } from '@layerzerolabs/devtools-evm-hardhat' | ||
import { createOAppFactory } from '@layerzerolabs/ua-devtools-evm' | ||
import { checkOAppEnforcedOptions } from '@layerzerolabs/ua-devtools' | ||
import { validateAndTransformOappConfig } from '@/utils/taskHelpers' | ||
import { getNetworkNameForEid } from '@layerzerolabs/devtools-evm-hardhat' | ||
import { areVectorsEqual, isZero } from '@layerzerolabs/devtools' | ||
import { Options } from '@layerzerolabs/lz-v2-utilities' | ||
|
||
interface TaskArgs { | ||
oappConfig: string | ||
logLevel?: string | ||
} | ||
|
||
export const enforcedOptionsGet: ActionType<TaskArgs> = async ({ oappConfig: oappConfigPath, logLevel = 'info' }) => { | ||
printLogo() | ||
|
||
// We'll set the global logging level to get as much info as needed | ||
setDefaultLogLevel(logLevel) | ||
|
||
// And we'll create a logger for ourselves | ||
const logger = createLogger() | ||
const graph: OAppOmniGraph = await validateAndTransformOappConfig(oappConfigPath, logger) | ||
|
||
// need points for OApp Enforced Option Matrix | ||
const points = graph.contracts | ||
.map(({ point }) => point) | ||
.map((point) => ({ | ||
...point, | ||
networkName: getNetworkNameForEid(point.eid), | ||
})) | ||
|
||
// At this point we are ready read data from the OApp | ||
const contractFactory = createConnectedContractFactory() | ||
const oAppFactory = createOAppFactory(contractFactory) | ||
|
||
try { | ||
const enforcedOptions = await checkOAppEnforcedOptions(graph, oAppFactory) | ||
const enforcedOptsNetworkMatrix = points.map((row) => { | ||
/** | ||
* for each point in the network (referred to as 'row'), create a row in the matrix | ||
*/ | ||
const connectionsForCurrentRow = points.reduce((tableRow, column) => { | ||
/** | ||
* find a peer with a vector matching the connection from 'column' to 'row' | ||
*/ | ||
const connection = enforcedOptions.find((peer) => { | ||
return areVectorsEqual(peer.vector, { from: column, to: row }) | ||
}) | ||
/** | ||
* update the row with a key-value pair indicating the enforced option for the current column | ||
*/ | ||
const enforcedOptsByMsgType: Record<string, unknown> = {} | ||
if (connection?.enforcedOptions) { | ||
connection.enforcedOptions.forEach((encodedEnforcedOpts) => { | ||
if (!isZero(encodedEnforcedOpts.options)) { | ||
enforcedOptsByMsgType['msgType: ' + encodedEnforcedOpts.msgType] = | ||
decodeEnforcedOptions(encodedEnforcedOpts) | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
...tableRow, | ||
[column.networkName]: printRecord(enforcedOptsByMsgType), | ||
} | ||
}, {}) | ||
|
||
/** | ||
* return the row representing connections for the current 'row' | ||
*/ | ||
return connectionsForCurrentRow | ||
}) | ||
|
||
console.log( | ||
printCrossTable(enforcedOptsNetworkMatrix, ['from → to', ...points.map(({ networkName }) => networkName)]) | ||
) | ||
|
||
return enforcedOptions | ||
} catch (error) { | ||
throw new Error(`An error occurred while getting the OApp configuration: ${error}`) | ||
} | ||
} | ||
|
||
task(TASK_LZ_OAPP_ENFORCED_OPTIONS_GET, 'Outputs table of OApp enforced options using layerzero.config') | ||
.addParam('oappConfig', 'Path to your LayerZero OApp config', undefined, types.string) | ||
.addParam('logLevel', 'Logging level. One of: error, warn, info, verbose, debug, silly', 'info', types.logLevel) | ||
.setAction(enforcedOptionsGet) | ||
|
||
/** | ||
* Decodes enforced options from the provided encoded enforced options. | ||
* @param {EncodedOption} option - The encoded options. | ||
* @returns {Record<string, unknown>} - The decoded enforced options. | ||
*/ | ||
function decodeEnforcedOptions(option: EncodedOption): Record<string, unknown> { | ||
const fromOptions = Options.fromOptions(option.options) | ||
const lzReceiveOption = fromOptions.decodeExecutorLzReceiveOption() | ||
const lzNativeDropOption = fromOptions.decodeExecutorNativeDropOption() | ||
const lzComposeOption = fromOptions.decodeExecutorComposeOption() | ||
const lzOrderedExecutionOption = fromOptions.decodeExecutorOrderedExecutionOption() | ||
|
||
return { | ||
...(lzReceiveOption ? { lzReceiveOption } : {}), | ||
...(lzNativeDropOption.length ? { lzNativeDropOption: headOrEverything(lzNativeDropOption) } : {}), | ||
...(lzComposeOption.length ? { lzComposeOption: headOrEverything(lzComposeOption) } : {}), | ||
...(lzOrderedExecutionOption ? { lzOrderedExecutionOption } : {}), | ||
} | ||
} | ||
|
||
/** | ||
* This is used for better readability in print tables | ||
* If array has length 1, pop from array and return the object | ||
* Else return the array | ||
*/ | ||
const headOrEverything = <T>(array: T[]): T | T[] => (array.length === 1 ? array[0]! : array) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.