Skip to content

Commit

Permalink
fix: smart register operator fn
Browse files Browse the repository at this point in the history
  • Loading branch information
sumbat-ssvlabs committed Jan 2, 2025
1 parent 6374109 commit ac7905b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
23 changes: 16 additions & 7 deletions src/__tests__/contract-interaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { globals } from '@/config'
import { mockFetchedOperators } from '@/mock'
import { createMockConfig } from '@/mock/config'
import { SSVSDK } from '@/sdk'
import { decodeOperatorPublicKey, roundOperatorFee } from '@/utils'
import hre from 'hardhat'
import { CONFIG, initializeContract } from 'hardhat/contract-helpers'
import { initializeContract } from 'hardhat/contract-helpers'
import { getAddress, parseEther } from 'viem'
import { describe, expect, it } from 'vitest'

Expand All @@ -24,15 +27,19 @@ describe('SSV Keys', async () => {
)

it('can write to the SSVNetwork contract', async () => {
const yearlyFee = parseEther('1')
const blockFee = roundOperatorFee(yearlyFee / globals.BLOCKS_PER_YEAR)

const receipt = await sdk.operators
.registerOperator({
args: {
publicKey: '0x10',
fee: CONFIG.minimalOperatorFee,
setPrivate: true,
publicKey: mockFetchedOperators[0].publicKey, // LS0tLS1CRUdJTiBSU0EgUFVCTElDIE....
yearlyFee, // 1 ssv
isPrivate: true,
},
})
.then((tx) => tx.wait())

expect(receipt).toBeDefined()

const operatorAddedEvent = receipt.events.find(
Expand All @@ -44,9 +51,11 @@ describe('SSV Keys', async () => {
} => event.eventName === 'OperatorAdded',
)
expect(operatorAddedEvent).toBeDefined()
expect(operatorAddedEvent?.args.operatorId).toBe(1n)
expect(operatorAddedEvent?.args.publicKey).toBe('0x10')
expect(operatorAddedEvent?.args.fee).toBe(CONFIG.minimalOperatorFee)
expect(operatorAddedEvent!.args.operatorId).toBe(1n)
expect(decodeOperatorPublicKey(operatorAddedEvent!.args.publicKey)).toBe(
mockFetchedOperators[0].publicKey,
)
expect(operatorAddedEvent?.args.fee).toBe(blockFee)
expect(operatorAddedEvent?.args.owner).toBe(
getAddress(sdk.config.walletClient.account!.address),
)
Expand Down
4 changes: 2 additions & 2 deletions src/libs/operator/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ConfigReturnType } from '@/config/create'
import { setOperatorWhitelists, withdraw } from '@/libs/operator/methods'
import { registerOperator, setOperatorWhitelists, withdraw } from '@/libs/operator/methods'
import type { RemoveConfigArg } from '@/types/methods'

export const createOperatorManager = (config: ConfigReturnType) => ({
registerOperator: config.contract.ssv.write.registerOperator,
registerOperator: registerOperator.bind(null, config) as RemoveConfigArg<typeof registerOperator>,
removeOperator: config.contract.ssv.write.removeOperator,
withdraw: withdraw.bind(null, config) as RemoveConfigArg<typeof withdraw>,
setOperatorWhitelists: config.contract.ssv.write.setOperatorsWhitelists,
Expand Down
24 changes: 23 additions & 1 deletion src/libs/operator/methods.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { getOperator } from '@/api/subgraph'
import { globals } from '@/config'
import type { ConfigReturnType } from '@/config/create'
import type { SmartFnWriteOptions } from '@/contract-interactions/types'
import { roundOperatorFee } from '@/utils'
import type { Address } from 'abitype'
import { isAddressEqual, zeroAddress } from 'viem'
import { encodeAbiParameters, isAddressEqual, parseAbiParameters, zeroAddress } from 'viem'

type WithdrawArgs = SmartFnWriteOptions<{
operatorId: string
Expand Down Expand Up @@ -36,6 +38,26 @@ export const withdraw = async (
})
}

type RegisterOperatorArgs = SmartFnWriteOptions<{
isPrivate: boolean
yearlyFee: bigint
publicKey: string
}>

export const registerOperator = async (
config: ConfigReturnType,
{ args: { isPrivate, yearlyFee, publicKey }, ...writeOptions }: RegisterOperatorArgs,
) => {
return config.contract.ssv.write.registerOperator({
args: {
publicKey: encodeAbiParameters(parseAbiParameters('string'), [publicKey]),
fee: roundOperatorFee(yearlyFee / globals.BLOCKS_PER_YEAR),
setPrivate: isPrivate,
},
...writeOptions,
})
}

type SetOperatorWhitelistsArgs = SmartFnWriteOptions<{
operatorIds: string[]
contractAddress: Address
Expand Down

0 comments on commit ac7905b

Please sign in to comment.