From c0ee173932868c4c85810343def0d2779b85c269 Mon Sep 17 00:00:00 2001 From: Shane Earley Date: Sun, 17 Sep 2023 11:28:10 -0400 Subject: [PATCH] Type event handler input --- common/env/src/index.ts | 2 +- common/events/src/index.ts | 10 ++--- contracts/ethereum/scripts/dev.ts | 1 - contracts/ethereum/test/fixtures/shared.ts | 2 +- .../functions/Functions-request-config.js | 2 +- services/functions/src/index.ts | 12 +++--- .../functions/src/interfaces/HandlerInput.ts | 12 ------ services/functions/src/providers/handlers.ts | 38 +++++++++---------- services/oracle/src/index.ts | 10 ++--- .../oracle/src/interfaces/HandlerInput.ts | 7 ---- services/oracle/src/providers/handlers.ts | 23 ++++++----- 11 files changed, 47 insertions(+), 72 deletions(-) delete mode 100644 services/functions/src/interfaces/HandlerInput.ts delete mode 100644 services/oracle/src/interfaces/HandlerInput.ts diff --git a/common/env/src/index.ts b/common/env/src/index.ts index 2d7e74aa9..f6617d7ce 100644 --- a/common/env/src/index.ts +++ b/common/env/src/index.ts @@ -15,9 +15,9 @@ const ETHEREUM_CONTRACTS = { BEACON_DEPOSIT_ADDRESS: '0x07b39F4fDE4A38bACe212b546dAc87C58DfE3fDC', DAO_ORACLE_ADDRESS: '', - LINK_ETH_FEED_ADDRESS: '0xb4c4a493AB6356497713A78FFA6c60FB53517c63', KEEPER_REGISTRAR_ADDRESS: '0x57A4a13b35d25EE78e084168aBaC5ad360252467', KEEPER_REGISTRY_ADDRESS: '0xE16Df59B887e3Caa439E0b29B42bA2e7976FD8b2', + LINK_ETH_FEED_ADDRESS: '0xb4c4a493AB6356497713A78FFA6c60FB53517c63', LINK_TOKEN_ADDRESS: '0x326C977E6efc84E512bB9C30f76E30c160eD06FB', SSV_NETWORK_ADDRESS: '0xC3CD9A0aE89Fff83b71b58b6512D43F8a41f363D', SSV_VIEWS_ADDRESS: '0xAE2C84c48272F5a1746150ef333D5E5B51F68763', diff --git a/common/events/src/index.ts b/common/events/src/index.ts index de34f40f7..d5df5e8bc 100644 --- a/common/events/src/index.ts +++ b/common/events/src/index.ts @@ -13,8 +13,8 @@ export function getEventsIterable(input: { const provider = input.provider || new ethers.providers.JsonRpcProvider(input.ethereumUrl) return (async function* () { - const queue: any[][] = [] - const listener = (...args: any[]) => queue.push(args) + const queue: ethers.Event[][] = [] + const enqueue = (...args: ethers.Event[]) => queue.push(args) for (const filter of input.contractFilters) { const contract = new ethers.Contract( filter.address, @@ -28,13 +28,13 @@ export function getEventsIterable(input: { input.startBlock, 'latest' ) - for (const eventObj of historicalEvents) { - queue.push([eventObj]) + for (const historicalEvent of historicalEvents) { + enqueue(historicalEvent) } } } for (const event of filter.events) { - contract.on(event, listener) + contract.on(event, enqueue) while (true) { if (queue.length === 0) { await new Promise((resolve) => { diff --git a/contracts/ethereum/scripts/dev.ts b/contracts/ethereum/scripts/dev.ts index 80d945db5..1c0dda8d5 100644 --- a/contracts/ethereum/scripts/dev.ts +++ b/contracts/ethereum/scripts/dev.ts @@ -242,5 +242,4 @@ void async function () { process.env.FUNCTIONS_BILLING_REGISTRY_ADDRESS = functionsBillingRegistry.address process.env.FUNCTIONS_ORACLE_ADDRESS = functionsOracle.address run('npm run dev --workspace @casimir/oracle') - run('npm run dev --workspace @casimir/functions') }() diff --git a/contracts/ethereum/test/fixtures/shared.ts b/contracts/ethereum/test/fixtures/shared.ts index 48684ea0e..232b43439 100644 --- a/contracts/ethereum/test/fixtures/shared.ts +++ b/contracts/ethereum/test/fixtures/shared.ts @@ -1,6 +1,6 @@ import { ethers, network, upgrades } from 'hardhat' import { loadFixture, time, setBalance } from '@nomicfoundation/hardhat-network-helpers' -import { CasimirManager, CasimirRegistry, CasimirUpkeep, CasimirViews, FunctionsBillingRegistry, FunctionsOracle, FunctionsOracleFactory, IBeaconUpgradeable, ISSVViews } from '../../build/@types' +import { CasimirManager, CasimirRegistry, CasimirUpkeep, CasimirViews, FunctionsBillingRegistry, FunctionsOracle, FunctionsOracleFactory, ISSVViews } from '../../build/@types' import { fulfillReport, runUpkeep } from '../../helpers/upkeep' import { depositFunctionsBalanceHandler, depositUpkeepBalanceHandler, initiateDepositHandler, reportCompletedExitsHandler } from '../../helpers/oracle' import { round } from '../../helpers/math' diff --git a/services/functions/Functions-request-config.js b/services/functions/Functions-request-config.js index aa7c7832e..b30148f98 100644 --- a/services/functions/Functions-request-config.js +++ b/services/functions/Functions-request-config.js @@ -43,7 +43,7 @@ const requestConfig = { // Args (string only array) can be accessed within the source code with `args[index]` (ie: args[0]). args: [ "1616508000", // genesisTimestamp - "0x552804Cf1fbFfa1E539bEBeF7117d5E8a1E4F32D", // viewsAddress + "0xc9F69bD5F43153FB485cBF1DB907EE1eb28c9B29", // viewsAddress "0x0812a9fe", // getCompoundablePoolIds(uint256,uint256) "0x5d1e0780", // getDepositedPoolCount() "0xdcf25c1d", // getDepositedPoolPublicKeys(uint256,uint256) diff --git a/services/functions/src/index.ts b/services/functions/src/index.ts index 3f8f281d1..c26703b01 100644 --- a/services/functions/src/index.ts +++ b/services/functions/src/index.ts @@ -2,7 +2,7 @@ import { getConfig } from './providers/config' import { getEventsIterable } from '@casimir/events' import { getStartBlock, updateErrorLog, updateStartBlock } from '@casimir/logs' import { fulfillRequestHandler } from './providers/handlers' -import { HandlerInput } from './interfaces/HandlerInput' +import { ethers } from 'ethers' const config = getConfig() @@ -35,7 +35,7 @@ const eventsIterable = getEventsIterable({ startBlock }) -const handlers: Record Promise> = {} +const handlers: Record Promise> = {} for (const contractName in contracts) { const contract = contracts[contractName as keyof typeof contracts] for (const [event, handler] of Object.entries(contract.events)) { @@ -46,11 +46,11 @@ for (const contractName in contracts) { void async function () { try { for await (const event of eventsIterable) { - const details = event?.[event.length - 1] - const { args } = details - const handler = handlers[details.event] + const details = event?.[event.length - 1] as ethers.Event + const input = details.args as ethers.utils.Result + const handler = handlers[details.event as string] if (!handler) throw new Error(`No handler found for event ${details.event}`) - await handler({ args }) + await handler(input) if (process.env.USE_LOGS === 'true') { updateStartBlock('.log/block.log', details.blockNumber) } diff --git a/services/functions/src/interfaces/HandlerInput.ts b/services/functions/src/interfaces/HandlerInput.ts deleted file mode 100644 index 18f30df33..000000000 --- a/services/functions/src/interfaces/HandlerInput.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { ethers } from 'ethers' - -export interface HandlerInput { - args: { - requestId?: string - requestingContract?: string - requestInitiator?: string - subscriptionId?: ethers.BigNumber - subscriptionOwner?: string - data?: string - } -} \ No newline at end of file diff --git a/services/functions/src/providers/handlers.ts b/services/functions/src/providers/handlers.ts index 12f07235c..47db4e33c 100644 --- a/services/functions/src/providers/handlers.ts +++ b/services/functions/src/providers/handlers.ts @@ -1,6 +1,5 @@ import { ethers } from 'ethers' import { decodeDietCBOR } from './format' -import { HandlerInput } from '../interfaces/HandlerInput' import requestConfig from '@casimir/functions/Functions-request-config' import { simulateRequest } from '../../FunctionsSandboxLibrary' import { getConfig } from './config' @@ -9,8 +8,8 @@ import { updateExecutionLog } from '@casimir/logs' const config = getConfig() -export async function fulfillRequestHandler(input: HandlerInput): Promise { - const { requestId, data } = input.args +export async function fulfillRequestHandler(input: ethers.utils.Result): Promise { + const { requestId, data } = input if (!requestId) throw new Error('No request id provided') if (!data) throw new Error('No data provided') @@ -23,25 +22,22 @@ export async function fulfillRequestHandler(input: HandlerInput): Promise args } const { result, resultLog, success } = await simulateRequest(currentRequestConfig) - console.log('Execution result', result) - console.log('Execution result log', resultLog) - console.log('Execution success', success) if (success) { - // const dummySigners = Array(31).fill(signer.address) - // const fulfillAndBill = await functionsBillingRegistry.fulfillAndBill( - // requestId, - // result, - // '0x', - // signer.address, - // dummySigners, - // 4, - // 100_000, - // 500_000, - // { - // gasLimit: 500_000, - // } - // ) - // await fulfillAndBill.wait() + const dummySigners = Array(31).fill(signer.address) + const fulfillAndBill = await functionsBillingRegistry.fulfillAndBill( + requestId, + result, + '0x', + signer.address, + dummySigners, + 4, + 100_000, + 500_000, + { + gasLimit: 500_000, + } + ) + await fulfillAndBill.wait() if (process.env.USE_LOGS === 'true') { updateExecutionLog('.log/execution.log', resultLog) } diff --git a/services/oracle/src/index.ts b/services/oracle/src/index.ts index 6ca441c1c..72562e323 100644 --- a/services/oracle/src/index.ts +++ b/services/oracle/src/index.ts @@ -10,7 +10,7 @@ import { // reportForcedExitsHandler, reportCompletedExitsHandler } from './providers/handlers' -import { HandlerInput } from './interfaces/HandlerInput' +import { ethers } from 'ethers' const config = getConfig() @@ -48,7 +48,7 @@ const eventsIterable = getEventsIterable({ startBlock }) -const handlers: Record Promise> = {} +const handlers: Record Promise> = {} for (const contractName in contracts) { const contract = contracts[contractName as keyof typeof contracts] for (const [event, handler] of Object.entries(contract.events)) { @@ -59,13 +59,13 @@ for (const contractName in contracts) { void async function () { try { for await (const event of eventsIterable) { - const details = event?.[event.length - 1] - const { args } = details + const details = event?.[event.length - 1] as ethers.Event + const input = details.args as ethers.utils.Result const handler = handlers[details.event as keyof typeof handlers] if (!handler) throw new Error(`No handler found for event ${details.event}`) await depositFunctionsBalanceHandler() await depositUpkeepBalanceHandler() - await handler({ args }) + await handler(input) if (process.env.USE_LOGS === 'true') { updateStartBlock('.log/block.log', details.blockNumber) } diff --git a/services/oracle/src/interfaces/HandlerInput.ts b/services/oracle/src/interfaces/HandlerInput.ts deleted file mode 100644 index 386633ad4..000000000 --- a/services/oracle/src/interfaces/HandlerInput.ts +++ /dev/null @@ -1,7 +0,0 @@ -export interface HandlerInput { - args: { - poolId?: number - operatorId?: number - count?: number - } -} \ No newline at end of file diff --git a/services/oracle/src/providers/handlers.ts b/services/oracle/src/providers/handlers.ts index e4c204255..a2c1941d0 100644 --- a/services/oracle/src/providers/handlers.ts +++ b/services/oracle/src/providers/handlers.ts @@ -1,5 +1,4 @@ import { ethers } from 'ethers' -import { HandlerInput } from '../interfaces/HandlerInput' import { CasimirManager, CasimirRegistry, CasimirViews, IFunctionsBillingRegistry, IAutomationRegistry } from '@casimir/ethereum/build/@types' import CasimirManagerAbi from '@casimir/ethereum/build/abi/CasimirManager.json' import CasimirViewsAbi from '@casimir/ethereum/build/abi/CasimirViews.json' @@ -103,8 +102,8 @@ export async function depositUpkeepBalanceHandler() { } } -export async function initiateDepositHandler(input: HandlerInput) { - const { poolId } = input.args +export async function initiateDepositHandler(input: ethers.utils.Result) { + const { poolId } = input if (!poolId) throw new Error('No pool id provided') const provider = new ethers.providers.JsonRpcProvider(config.ethereumUrl) @@ -199,8 +198,8 @@ export async function initiateDepositHandler(input: HandlerInput) { await initiateDeposit.wait() } -export async function initiateResharesHandler(input: HandlerInput) { - const { operatorId } = input.args +export async function initiateResharesHandler(input: ethers.utils.Result) { + const { operatorId } = input if (!operatorId) throw new Error('No operator id provided') const provider = new ethers.providers.JsonRpcProvider(config.ethereumUrl) @@ -240,7 +239,7 @@ export async function initiateResharesHandler(input: HandlerInput) { if (newOperatorId && poolDetails.reshares.toNumber() > 1) { const newOperatorIds = oldOperatorIds.map((operatorId) => { - if (operatorId === input.args.operatorId) return newOperatorId + if (operatorId === input.operatorId) return newOperatorId return operatorId }) @@ -314,8 +313,8 @@ export async function initiateResharesHandler(input: HandlerInput) { } } -export async function initiateExitsHandler(input: HandlerInput) { - const { poolId } = input.args +export async function initiateExitsHandler(input: ethers.utils.Result) { + const { poolId } = input if (!poolId) throw new Error('No pool id provided') const provider = new ethers.providers.JsonRpcProvider(config.ethereumUrl) @@ -328,8 +327,8 @@ export async function initiateExitsHandler(input: HandlerInput) { // Get operators to sign exit } -export async function reportForcedExitsHandler(input: HandlerInput) { - const { count } = input.args +export async function reportForcedExitsHandler(input: ethers.utils.Result) { + const { count } = input if (!count) throw new Error('No count provided') const provider = new ethers.providers.JsonRpcProvider(config.ethereumUrl) @@ -354,8 +353,8 @@ export async function reportForcedExitsHandler(input: HandlerInput) { } } -export async function reportCompletedExitsHandler(input: HandlerInput) { - const { count } = input.args +export async function reportCompletedExitsHandler(input: ethers.utils.Result) { + const { count } = input if (!count) throw new Error('No count provided') const provider = new ethers.providers.JsonRpcProvider(config.ethereumUrl)