Skip to content

Commit

Permalink
Type event handler input
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejearley committed Sep 17, 2023
1 parent 6a4c2c1 commit c0ee173
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 72 deletions.
2 changes: 1 addition & 1 deletion common/env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 5 additions & 5 deletions common/events/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<void>((resolve) => {
Expand Down
1 change: 0 additions & 1 deletion contracts/ethereum/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}()
2 changes: 1 addition & 1 deletion contracts/ethereum/test/fixtures/shared.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion services/functions/Functions-request-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions services/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -35,7 +35,7 @@ const eventsIterable = getEventsIterable({
startBlock
})

const handlers: Record<string, (input: HandlerInput) => Promise<void>> = {}
const handlers: Record<string, (input: ethers.utils.Result) => Promise<void>> = {}
for (const contractName in contracts) {
const contract = contracts[contractName as keyof typeof contracts]
for (const [event, handler] of Object.entries(contract.events)) {
Expand All @@ -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)
}
Expand Down
12 changes: 0 additions & 12 deletions services/functions/src/interfaces/HandlerInput.ts

This file was deleted.

38 changes: 17 additions & 21 deletions services/functions/src/providers/handlers.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -9,8 +8,8 @@ import { updateExecutionLog } from '@casimir/logs'

const config = getConfig()

export async function fulfillRequestHandler(input: HandlerInput): Promise<void> {
const { requestId, data } = input.args
export async function fulfillRequestHandler(input: ethers.utils.Result): Promise<void> {
const { requestId, data } = input
if (!requestId) throw new Error('No request id provided')
if (!data) throw new Error('No data provided')

Expand All @@ -23,25 +22,22 @@ export async function fulfillRequestHandler(input: HandlerInput): Promise<void>
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)
}
Expand Down
10 changes: 5 additions & 5 deletions services/oracle/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
// reportForcedExitsHandler,
reportCompletedExitsHandler
} from './providers/handlers'
import { HandlerInput } from './interfaces/HandlerInput'
import { ethers } from 'ethers'

const config = getConfig()

Expand Down Expand Up @@ -48,7 +48,7 @@ const eventsIterable = getEventsIterable({
startBlock
})

const handlers: Record<string, (input: HandlerInput) => Promise<void>> = {}
const handlers: Record<string, (input: ethers.utils.Result) => Promise<void>> = {}
for (const contractName in contracts) {
const contract = contracts[contractName as keyof typeof contracts]
for (const [event, handler] of Object.entries(contract.events)) {
Expand All @@ -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)
}
Expand Down
7 changes: 0 additions & 7 deletions services/oracle/src/interfaces/HandlerInput.ts

This file was deleted.

23 changes: 11 additions & 12 deletions services/oracle/src/providers/handlers.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
})

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit c0ee173

Please sign in to comment.