-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from Eigen-Explorer/feature/switch-operator-sh…
…ares-to-stakers Reinclude operator shares, seed with impacted staker data
- Loading branch information
Showing
4 changed files
with
207 additions
and
72 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
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,78 +1,183 @@ | ||
import { parseAbiItem } from 'viem' | ||
import { getEigenContracts } from './data/address' | ||
import { getViemClient } from './utils/viemClient' | ||
import { getPrismaClient } from './utils/prismaClient' | ||
import { bulkUpdateDbTransactions, IMap } from './utils/seeder' | ||
import { | ||
baseBlock, | ||
bulkUpdateDbTransactions, | ||
fetchLastSyncBlock, | ||
IMap, | ||
loopThroughBlocks, | ||
saveLastSyncBlock | ||
} from './utils/seeder' | ||
|
||
export async function seedOperatorShares(operatorAddresses: string[]) { | ||
const blockSyncKey = 'lastSyncedBlock_operatorShares' | ||
|
||
export async function seedOperatorShares(toBlock?: bigint, fromBlock?: bigint) { | ||
console.log('Seeding operator shares ...') | ||
|
||
const viemClient = getViemClient() | ||
const prismaClient = getPrismaClient() | ||
const operatorShares: IMap< | ||
string, | ||
{ shares: bigint; strategyAddress: string }[] | ||
> = new Map() | ||
|
||
let currentIndex = 0 | ||
let nextIndex = 0 | ||
const totalOperators = await prismaClient.operator.count({ | ||
where: { address: { in: operatorAddresses } } | ||
}) | ||
const firstBlock = fromBlock | ||
? fromBlock | ||
: await fetchLastSyncBlock(blockSyncKey) | ||
const lastBlock = toBlock ? toBlock : await viemClient.getBlockNumber() | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const dbTransactions: any[] = [] | ||
if (firstBlock === baseBlock) { | ||
await prismaClient.operatorStrategyShares.deleteMany() | ||
} | ||
|
||
// Loop through evm logs | ||
await loopThroughBlocks(firstBlock, lastBlock, async (fromBlock, toBlock) => { | ||
const logs = await viemClient.getLogs({ | ||
address: getEigenContracts().DelegationManager, | ||
events: [ | ||
parseAbiItem( | ||
'event OperatorSharesIncreased(address indexed operator, address staker, address strategy, uint256 shares)' | ||
), | ||
parseAbiItem( | ||
'event OperatorSharesDecreased(address indexed operator, address staker, address strategy, uint256 shares)' | ||
) | ||
], | ||
fromBlock, | ||
toBlock | ||
}) | ||
|
||
while (nextIndex < totalOperators) { | ||
nextIndex = currentIndex + 12 | ||
// Operators list | ||
const operatorAddresses = logs.map((l) => | ||
String(l.args.operator).toLowerCase() | ||
) | ||
|
||
const operators = await prismaClient.operator.findMany({ | ||
const operatorInit = await prismaClient.operator.findMany({ | ||
where: { address: { in: operatorAddresses } }, | ||
skip: currentIndex, | ||
take: 12, | ||
include: { | ||
stakers: { | ||
include: { | ||
shares: true | ||
} | ||
} | ||
shares: true | ||
} | ||
}) | ||
|
||
operators.map((operator) => { | ||
const sharesMap: IMap<string, string> = new Map() | ||
for (const l in logs) { | ||
const log = logs[l] | ||
|
||
const operatorAddress = String(log.args.operator).toLowerCase() | ||
const strategyAddress = String(log.args.strategy).toLowerCase() | ||
const shares = log.args.shares | ||
if (!shares) continue | ||
|
||
// Load existing staker shares data | ||
if (!operatorShares.has(operatorAddress)) { | ||
const foundOperatorInit = operatorInit.find( | ||
(o) => o.address.toLowerCase() === operatorAddress.toLowerCase() | ||
) | ||
if (foundOperatorInit) { | ||
operatorShares.set( | ||
operatorAddress, | ||
foundOperatorInit.shares.map((o) => ({ | ||
...o, | ||
shares: BigInt(o.shares) | ||
})) | ||
) | ||
} else { | ||
operatorShares.set(operatorAddress, []) | ||
} | ||
} | ||
|
||
let foundSharesIndex = operatorShares | ||
.get(operatorAddress) | ||
.findIndex( | ||
(os) => | ||
os.strategyAddress.toLowerCase() === strategyAddress.toLowerCase() | ||
) | ||
|
||
operator.stakers.map((staker) => { | ||
staker.shares.map((s) => { | ||
if (!sharesMap.has(s.strategyAddress)) { | ||
sharesMap.set(s.strategyAddress, '0') | ||
} | ||
if (foundSharesIndex !== undefined && foundSharesIndex === -1) { | ||
operatorShares | ||
.get(operatorAddress) | ||
.push({ shares: 0n, strategyAddress: strategyAddress }) | ||
|
||
sharesMap.set( | ||
s.strategyAddress, | ||
( | ||
BigInt(sharesMap.get(s.strategyAddress)) + BigInt(s.shares) | ||
).toString() | ||
foundSharesIndex = operatorShares | ||
.get(operatorAddress) | ||
.findIndex( | ||
(os) => | ||
os.strategyAddress.toLowerCase() === strategyAddress.toLowerCase() | ||
) | ||
} | ||
|
||
if (log.eventName === 'OperatorSharesIncreased') { | ||
operatorShares.get(operatorAddress)[foundSharesIndex].shares = | ||
operatorShares.get(operatorAddress)[foundSharesIndex].shares + shares | ||
} else if (log.eventName === 'OperatorSharesDecreased') { | ||
operatorShares.get(operatorAddress)[foundSharesIndex].shares = | ||
operatorShares.get(operatorAddress)[foundSharesIndex].shares - shares | ||
} | ||
} | ||
|
||
console.log( | ||
`Operator shares updated between blocks ${fromBlock} ${toBlock}: ${logs.length}` | ||
) | ||
}) | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const dbTransactions: any[] = [] | ||
|
||
if (firstBlock === baseBlock) { | ||
// Clear existing table | ||
dbTransactions.push(prismaClient.operatorStrategyShares.deleteMany()) | ||
|
||
const newOperatorShares: { | ||
operatorAddress: string | ||
strategyAddress: string | ||
shares: string | ||
}[] = [] | ||
|
||
for (const [operatorAddress, shares] of operatorShares) { | ||
shares.map((share) => { | ||
newOperatorShares.push({ | ||
operatorAddress, | ||
strategyAddress: share.strategyAddress, | ||
shares: share.shares.toString() | ||
}) | ||
}) | ||
} | ||
|
||
for (const [strategyAddress, shares] of sharesMap) { | ||
dbTransactions.push( | ||
prismaClient.operatorStrategyShares.createMany({ | ||
data: newOperatorShares, | ||
skipDuplicates: true | ||
}) | ||
) | ||
} else { | ||
for (const [operatorAddress, shares] of operatorShares) { | ||
shares.map((share) => { | ||
dbTransactions.push( | ||
prismaClient.operatorStrategyShares.upsert({ | ||
where: { | ||
operatorAddress_strategyAddress: { | ||
operatorAddress: operator.address, | ||
strategyAddress | ||
operatorAddress, | ||
strategyAddress: share.strategyAddress | ||
} | ||
}, | ||
create: { | ||
operatorAddress: operator.address, | ||
strategyAddress, | ||
shares: shares.toString() | ||
operatorAddress, | ||
strategyAddress: share.strategyAddress, | ||
shares: share.shares.toString() | ||
}, | ||
update: { | ||
shares: shares.toString() | ||
shares: share.shares.toString() | ||
} | ||
}) | ||
) | ||
} | ||
}) | ||
|
||
currentIndex = nextIndex | ||
}) | ||
} | ||
} | ||
|
||
await bulkUpdateDbTransactions(dbTransactions) | ||
|
||
console.log('Seeded operator shares: ', totalOperators) | ||
// Storing last sycned block | ||
await saveLastSyncBlock(blockSyncKey, lastBlock) | ||
|
||
console.log('Seeded operator shares:', operatorShares.size) | ||
} |
Oops, something went wrong.