Skip to content

Commit

Permalink
fix: changed function names
Browse files Browse the repository at this point in the history
  • Loading branch information
sumbat-ssvlabs committed Dec 26, 2024
1 parent 0a200b4 commit 6374109
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 45 deletions.
82 changes: 60 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,30 @@ yarn add ssv-sdk ssv-keys viem
### Initialize the SDK

```typescript
import { SSVSDK } from 'ssv-sdk'
import { SSVSDK, chains } from 'ssv-sdk'
import { createPublicClient, createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

// Initialize with basic configuration
// Setup viem clients
const chain = chains.mainnet // or chains.holesky
const transport = http()

const publicClient = createPublicClient({
chain,
transport,
})

const account = privateKeyToAccount('0x...')
const walletClient = createWalletClient({
account,
chain,
transport,
})

// Initialize SDK with viem clients
const sdk = new SSVSDK({
chain: 'mainnet', // or holesky
private_key: '0x.......',
publicClient,
walletClient,
})
```

Expand All @@ -42,7 +60,7 @@ const operators = await sdk.api.getOperators({
})

const nonce = await sdk.api.getOwnerNonce({
owner: '0x',
owner: 'your_wallet_address',
})
```

Expand All @@ -51,29 +69,49 @@ const nonce = await sdk.api.getOwnerNonce({
```typescript
import { parseEther } from 'viem'

await sdk.clusters.deposit({
id: '...',
amount: parseEther('1.5'),
options: {
approve: true, // Automatically triggers token approval transaction if the allowance is lower than the deposit amount
await sdk.clusters.deposit(
{
id: 'your_cluster_id',
amount: parseEther('30'), // (30 SSV token)
},
})
{
approve: true, // Automatically triggers token approval transaction if the allowance is lower than the deposit amount
},
)
```

### Environment Setup for Testing

To run tests, you'll need to set up your environment variables. Create a `.env` file in the root directory of your project using the provided `.env.example` as a template:
### Register Validators

```bash
# Copy the example env file
cp .env.example .env
```
To register validators, you'll need to:

The `.env` file should contain the following variables:
1. Create shares from your keyshares JSON file
2. Register the validator using the created shares

```typescript
PRIVATE_KEY = your_private_key_here
OWNER_ADDRESS = your_owner_address_here
import { parseEther } from 'viem'

// Your keyshares JSON file containing the validator's data
import keyshares from 'path/to/keyshares.json'

// First, validate and create shares from your keyshares
try {
const result = await sdk.utils.validateSharesPreRegistration({
operatorIds: ['220', '221', '223', '224'],
keyshares,
})

// Register validators using the clusters API
const receipt = await sdk.clusters
.registerValidators({
args: {
keyshares: result.available,
depositAmount: parseEther('2'),
},
})
.then((tx) => tx.wait())
} catch (e) {
// something went wrong
}
```

Make sure to never commit your actual `.env` file to version control. The `.env.example` file serves as a template showing which variables are required.
The `keyshares` JSON contains the validator's public key and encrypted shares for each operator. The `validateSharesPreRegistration` method will validate the keyshares and return the available validators that can be registered.
28 changes: 14 additions & 14 deletions src/__tests__/keyshares.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ConfigReturnType } from '@/config'
import { createShares } from '@/libs/utils/methods'
import { validateSharesPreRegistration } from '@/libs/utils/methods'
import { mockFetchedOperators, operators as mockOperators } from '@/mock'
import { createMockConfig } from '@/mock/config'
import inconsistent_operator_ids_keyshares from '@/mock/keyshares/inconsistent_operator_ids_keyshares.json'
Expand Down Expand Up @@ -70,9 +70,9 @@ describe('Keyshares', async () => {
})

it('can validate valid keyshares', async () => {
const { createShares } = await import('../libs/utils/methods')
const { validateSharesPreRegistration } = await import('../libs/utils/methods')

const result = await createShares(mockConfig, {
const result = await validateSharesPreRegistration(mockConfig, {
operatorIds: mockOperators.ids.map(String),
keyshares: valid_keyshares,
})
Expand All @@ -83,7 +83,7 @@ describe('Keyshares', async () => {
})

it('should throw for invalid nonce', async () => {
const { createShares } = await import('../libs/utils/methods')
const { validateSharesPreRegistration } = await import('../libs/utils/methods')

const invalidNonceConfig = merge({}, mockConfig, {
api: {
Expand All @@ -93,15 +93,15 @@ describe('Keyshares', async () => {
} satisfies Partial<ConfigReturnType>)

await expect(
createShares(invalidNonceConfig, {
validateSharesPreRegistration(invalidNonceConfig, {
operatorIds: mockOperators.ids.map(String),
keyshares: valid_keyshares,
}),
).rejects.toThrowError()
})

it('should throw for no nonce', async () => {
const { createShares } = await import('../libs/utils/methods')
const { validateSharesPreRegistration } = await import('../libs/utils/methods')

const noNonceConfig = merge({}, mockConfig, {
api: {
Expand All @@ -111,15 +111,15 @@ describe('Keyshares', async () => {
} satisfies Partial<ConfigReturnType>)

await expect(
createShares(noNonceConfig, {
validateSharesPreRegistration(noNonceConfig, {
operatorIds: mockOperators.ids.map(String),
keyshares: valid_keyshares,
}),
).rejects.toThrowError('Failed to get owner nonce')
})

it('should take into account registered validators', async () => {
const { createShares } = await import('../libs/utils/methods')
const { validateSharesPreRegistration } = await import('../libs/utils/methods')

const registeredValidatorsConfig = merge({}, mockConfig, {
api: {
Expand All @@ -132,7 +132,7 @@ describe('Keyshares', async () => {
},
} satisfies Partial<ConfigReturnType>)

const result = await createShares(registeredValidatorsConfig, {
const result = await validateSharesPreRegistration(registeredValidatorsConfig, {
operatorIds: mockOperators.ids.map(String),
keyshares: valid_keyshares,
})
Expand All @@ -143,7 +143,7 @@ describe('Keyshares', async () => {
})

it('should throw if every validator is registered', async () => {
const { createShares } = await import('../libs/utils/methods')
const { validateSharesPreRegistration } = await import('../libs/utils/methods')

const registeredValidatorsConfig = merge({}, mockConfig, {
api: {
Expand All @@ -154,7 +154,7 @@ describe('Keyshares', async () => {
} satisfies Partial<ConfigReturnType>)

await expect(
createShares(registeredValidatorsConfig, {
validateSharesPreRegistration(registeredValidatorsConfig, {
operatorIds: mockOperators.ids.map(String),
keyshares: valid_keyshares,
}),
Expand Down Expand Up @@ -205,7 +205,7 @@ describe('Keyshares', async () => {
})

it('should throw when operator is private and account is not whitelisted', async () => {
const { createShares } = await import('../libs/utils/methods')
const { validateSharesPreRegistration } = await import('../libs/utils/methods')

const privateOperatorConfig = merge({}, mockConfig, {
api: {
Expand All @@ -227,7 +227,7 @@ describe('Keyshares', async () => {
} satisfies Partial<ConfigReturnType>)

await expect(
createShares(privateOperatorConfig, {
validateSharesPreRegistration(privateOperatorConfig, {
operatorIds: mockOperators.ids.map(String),
keyshares: valid_keyshares,
}),
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('Keyshares', async () => {
} satisfies Partial<ConfigReturnType>)

await expect(
createShares(maxedOutOperators, {
validateSharesPreRegistration(maxedOutOperators, {
operatorIds: mockOperators.ids.map(String),
keyshares: valid_keyshares,
}),
Expand Down
4 changes: 2 additions & 2 deletions src/libs/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { ConfigReturnType } from '@/config/create'
import { getClusterBalance, getOperatorCapacity } from '@/libs/utils/methods'
import { createShares, validateKeysharesJSON } from '@/libs/utils/methods/keyshares'
import { validateKeysharesJSON, validateSharesPreRegistration } from '@/libs/utils/methods/keyshares'
import { generateKeyShares } from '@/libs/utils/methods/keystores'
import type { RemoveConfigArg } from '@/types/methods'

export const createUtils = (config: ConfigReturnType) => ({
generateKeyShares,
validateKeysharesJSON,
createShares: createShares.bind(null, config) as RemoveConfigArg<typeof createShares>,
validateSharesPreRegistration: validateSharesPreRegistration.bind(null, config) as RemoveConfigArg<typeof validateSharesPreRegistration>,
getOperatorCapacity: getOperatorCapacity.bind(null, config) as RemoveConfigArg<
typeof getOperatorCapacity
>,
Expand Down
14 changes: 7 additions & 7 deletions src/libs/utils/methods/keyshares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { canAccountUseOperator } from '@/libs/operator/methods'
import type { MainnetEvent, ValidatorAddedEvent } from '@/types/contract-interactions'
import type { Operator } from '@/types/operator'
import {
KeysharesValidationError,
KeysharesValidationErrors,
ensureNoKeysharesErrors,
ensureValidatorsUniqueness,
validateConsistentOperatorIds,
validateConsistentOperatorPublicKeys,
KeysharesValidationError,
KeysharesValidationErrors,
ensureNoKeysharesErrors,
ensureValidatorsUniqueness,
validateConsistentOperatorIds,
validateConsistentOperatorPublicKeys,
} from '@/utils/keyshares'
import { sortNumbers } from '@/utils/number'
import { isEqual } from 'lodash-es'
Expand All @@ -22,7 +22,7 @@ type ValidatedKeysharesArgs = {
operatorIds: string[]
}

export const createShares = async (
export const validateSharesPreRegistration = async (
config: ConfigReturnType,
{ keyshares, operatorIds }: ValidatedKeysharesArgs,
) => {
Expand Down
1 change: 1 addition & 0 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export class SSVSDK {
this.utils = createUtils(this.config)
}
}

0 comments on commit 6374109

Please sign in to comment.