-
Notifications
You must be signed in to change notification settings - Fork 3
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
f958627
commit 5513cf2
Showing
24 changed files
with
615 additions
and
831 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,2 @@ | ||
node_modules/ | ||
dist/ |
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,16 @@ | ||
{ | ||
"name": "@casimir/ssv", | ||
"private": "true", | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"build": "esbuild src/index.ts --bundle --minify --sourcemap --platform=node --target=esnext --outfile=dist/index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
"ethers": "^5.7.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^17.0.38", | ||
"esbuild": "^0.15.9" | ||
} | ||
} |
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 @@ | ||
import { getCluster } from './providers/clusters' | ||
|
||
export { getCluster } |
7 changes: 3 additions & 4 deletions
7
services/dkg/src/interfaces/ClusterInput.ts → common/ssv/src/interfaces/ClusterInput.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 |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import { ISSVNetwork } from '@casimir/ethereum/build/artifacts/types' | ||
import { ethers } from 'ethers' | ||
|
||
export interface ClusterInput { | ||
/** SSV network contract */ | ||
ssv: ISSVNetwork & ethers.Contract | ||
/** SSV network address */ | ||
networkAddress: string | ||
/** Operator IDs */ | ||
operatorIds: number[] | ||
/** JSON RPC node provider */ | ||
provider: ethers.JsonRpcProvider | ||
provider: ethers.providers.JsonRpcProvider | ||
/** Withdrawal address */ | ||
withdrawalAddress: string | ||
} |
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,95 @@ | ||
import { ethers } from 'ethers' | ||
import { Cluster } from '@casimir/types' | ||
import ISSVNetworkJson from '@casimir/ethereum/build/artifacts/src/vendor/interfaces/ISSVNetwork.sol/ISSVNetwork.json' | ||
import { ClusterInput } from '../interfaces/ClusterInput' | ||
|
||
/** | ||
* Get cluster snapshot | ||
* @param {ClusterInput} input - Operator IDs and withdrawal address | ||
* @returns {Promise<Cluster>} Cluster snapshot | ||
*/ | ||
export async function getCluster(input: ClusterInput): Promise<Cluster> { | ||
const { provider, networkAddress, operatorIds, withdrawalAddress } = input | ||
|
||
const ssv = new ethers.Contract(networkAddress, ISSVNetworkJson.abi, provider) | ||
|
||
const DAY = 5400 | ||
const WEEK = DAY * 7 | ||
const MONTH = DAY * 30 | ||
const latestBlockNumber = await provider.getBlockNumber() | ||
let step = MONTH | ||
let cluster: Cluster | undefined | ||
let biggestBlockNumber = 0 | ||
|
||
const eventList = [ | ||
'ClusterDeposited', | ||
'ClusterWithdrawn', | ||
'ValidatorAdded', | ||
'ValidatorRemoved', | ||
'ClusterLiquidated', | ||
'ClusterReactivated' | ||
] | ||
|
||
// Todo create query filter for events | ||
|
||
let fromBlock = latestBlockNumber - step | ||
let toBlock = latestBlockNumber | ||
|
||
while (!cluster && fromBlock > 0) { | ||
try { | ||
const result = await ssv.queryFilter('*', fromBlock, toBlock) | ||
|
||
for (const item of result) { | ||
const { args, blockNumber, event } = item | ||
|
||
try { | ||
const checkClusterEvent = eventList.map(e => e.split('(')[0]).includes(event as string) | ||
if (!checkClusterEvent) continue | ||
const checkOwner = args?.owner === withdrawalAddress | ||
if (!checkOwner) continue | ||
const checkOperators = JSON.stringify(args?.operatorIds.map((value: string) => Number(value))) === JSON.stringify(operatorIds) | ||
if (!checkOperators) continue | ||
const checkCluster = args?.cluster !== undefined | ||
if (!checkCluster) continue | ||
|
||
if (blockNumber > biggestBlockNumber) { | ||
biggestBlockNumber = blockNumber | ||
const [ | ||
validatorCount, | ||
networkFeeIndex, | ||
index, | ||
balance, | ||
active | ||
] = args.cluster | ||
cluster = { | ||
validatorCount, | ||
networkFeeIndex, | ||
index, | ||
balance, | ||
active | ||
} | ||
} | ||
} catch (e) { | ||
console.error('ERROR FILTERING CLUSTER EVENTS', e) | ||
} | ||
} | ||
toBlock = fromBlock | ||
} catch (e) { | ||
console.error(e) | ||
if (step === MONTH) { | ||
step = WEEK | ||
} else if (step === WEEK) { | ||
step = DAY | ||
} | ||
} | ||
fromBlock = toBlock - step | ||
} | ||
|
||
return cluster || { | ||
validatorCount: 0, | ||
networkFeeIndex: 0, | ||
index: 0, | ||
balance: 0, | ||
active: true | ||
} | ||
} |
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,19 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"strict": true, | ||
"preserveConstEnums": true, | ||
"noEmit": true, | ||
"sourceMap": false, | ||
"module": "CommonJS", | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"isolatedModules": true, | ||
"resolveJsonModule": true | ||
}, | ||
"include": [ | ||
"./src/*" | ||
] | ||
} |
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,7 +1,9 @@ | ||
import { ethers } from 'ethers' | ||
|
||
export interface Cluster { | ||
validatorCount: number | ||
networkFeeIndex: number | ||
index: number | ||
balance: number | ||
validatorCount: number | ethers.BigNumber | ||
networkFeeIndex: number | ethers.BigNumber | ||
index: number | ethers.BigNumber | ||
balance: number | ethers.BigNumber | ||
active: boolean | ||
} |
Oops, something went wrong.