Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ssv-labs/bapps-sdk",
"version": "0.0.9",
"version": "0.0.10",
"author": "SSV.Labs",
"description": "ssv labs based apps sdk",
"keywords": [
Expand Down
13 changes: 8 additions & 5 deletions src/__tests__/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { getParticipantWeights, getValidatorsBalance } from '@/api/based-apps-api'
import type { GetValidatorBalancesResponse } from '@/api/beacon-chain-api'
import { parseEther, type Address } from 'viem'
import type { ContractInteractions } from '@/contract-interactions/types'
import { createBasedAppsAPI } from '@/main'
import { BasedAppsSDK } from '@/sdk'
import type { GraphQLClient } from 'graphql-request'
import { parseEther } from 'viem'
import type { PublicClient, WalletClient, Address } from 'viem'
import { describe, expect, it } from 'vitest'
import {
mockAPIs,
Expand All @@ -16,10 +21,6 @@ import {
mockGetValidatorBalances,
mockGetValidatorsByAccount,
} from './mock-api'
import { BasedAppsSDK } from '@/sdk'
import { createBasedAppsAPI } from '@/main'
import type { GraphQLClient } from 'graphql-request'
import type { ContractInteractions } from '@/contract-interactions/types'

// Mock dependencies

Expand All @@ -35,6 +36,8 @@ const sdk = new BasedAppsSDK({
endpoint: '',
},
},
publicClient: {} as PublicClient,
walletClient: {} as WalletClient,
})

describe('Based Apps API Tests', () => {
Expand Down
57 changes: 57 additions & 0 deletions src/__tests__/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, test, expect, vi, beforeEach } from 'vitest'
import { createPublicClient, createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { bam_paid_graph_endpoints, chains } from '@/config'
import { defineChain } from 'viem'

const chain = chains.hoodi
const transport = http()
Expand Down Expand Up @@ -184,4 +185,60 @@ describe('BasedAppsSDK', () => {
}),
).toThrow('Public and wallet client chains must be the same')
})

test('should work with a custom chain when necessary config is provided', () => {
// Create a custom chain using defineChain
const customChain = defineChain({
id: 123456789,
name: 'Custom Test Chain',
nativeCurrency: {
name: 'Test Ether',
symbol: 'ETH',
decimals: 18,
},
rpcUrls: {
default: {
http: ['https://custom-rpc.example.com'],
},
},
testnet: true,
})

// Custom contract address for the chain
const customContractAddress = '0xabcdef1234567890abcdef1234567890abcdef12'

// Custom subgraph URL for the chain
const customSubgraphUrl = 'https://custom-subgraph.example.com/graphql'

// Create clients with the custom chain
const customPublicClient = createPublicClient({
chain: customChain,
transport,
})

const customWalletClient = createWalletClient({
account,
chain: customChain,
transport,
})

// Create the SDK with the custom chain and required config
const sdk = new BasedAppsSDK({
beaconchainUrl: 'https://example.com/beacon',
publicClient: customPublicClient,
walletClient: customWalletClient,
extendedConfig: {
// Provide a contract address since it won't be in the default contracts map
contract: customContractAddress,
// Provide a subgraph URL since it won't be in the default endpoints map
subgraph: {
url: customSubgraphUrl,
},
},
})

expect(sdk).toBeInstanceOf(BasedAppsSDK)
expect(sdk.core.publicClient.chain?.id).toBe(customChain.id)
expect(sdk.core.walletClient.chain?.id).toBe(customChain.id)
})
})
2 changes: 2 additions & 0 deletions src/config/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export const chains = {

export const chainIds = [hoodi.id] as const
export type ChainId = (typeof chainIds)[number]

export const networks = ['hoodi'] as const
export type Network = Lowercase<(typeof networks)[number]>

export const bam_graph_endpoints: Record<ChainId, string> = {
[hoodi.id]: 'https://api.studio.thegraph.com/query/71118/ssv-network-hoodi/version/latest/',
}
Expand Down
5 changes: 5 additions & 0 deletions src/config/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import type { ConfigArgs } from '@/utils/zod/config'
import { configArgsSchema } from '@/utils/zod/config'
import { GraphQLClient } from 'graphql-request'
import type { PublicClient, WalletClient } from 'viem'

export type ConfigReturnType = {
apis: APIs
Expand All @@ -26,6 +27,8 @@ export type ConfigReturnType = {
endpoint: string
}
}
publicClient: PublicClient
walletClient: WalletClient
}

export const isConfig = (props: unknown): props is ConfigReturnType => {
Expand Down Expand Up @@ -91,5 +94,7 @@ export const createConfig = (props: ConfigArgs): ConfigReturnType => {
endpoint: bapEndpoint,
},
},
publicClient: publicClient,
walletClient: walletClient,
} satisfies ConfigReturnType
}
17 changes: 0 additions & 17 deletions src/utils/zod/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { chainIds, networks } from '@/config'
import type { Address, PublicClient, WalletClient } from 'viem'
import { z } from 'zod'

Expand All @@ -23,14 +22,6 @@ export const configArgsSchema = z
return false
}

if (!chainIds.includes(client.chain?.id as (typeof chainIds)[number])) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Public client chain must be one of [${networks.join(', ')}]`,
})
return false
}

return true
}),
walletClient: z.custom().superRefine((val, ctx) => {
Expand All @@ -51,14 +42,6 @@ export const configArgsSchema = z
return false
}

if (!chainIds.includes(client.chain?.id as (typeof chainIds)[number])) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Wallet client chain must be one of [${networks.join(', ')}]`,
})
return false
}

return true
}),
extendedConfig: z
Expand Down