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.8",
"version": "0.0.9",
"author": "SSV.Labs",
"description": "ssv labs based apps sdk",
"keywords": [
Expand Down
99 changes: 84 additions & 15 deletions src/__tests__/read.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { BasedAppsSDK } from '@/sdk'
import { describe, test, vi, beforeEach, expect } from 'vitest'
import { describe, test, expect } from 'vitest'

import { createPublicClient, createWalletClient, http, isAddress } from 'viem'
import { createPublicClient, createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { chains } from '@/config'

describe('Read Contract', () => {
beforeEach(() => {
// Reset the import.meta.env before each test
vi.stubGlobal('import.meta', { env: {} })
})

test('can communicate with holesky contract', async () => {
const chain = chains.holesky // or chains.holesky
describe('Communications', () => {
test('can communicate with hoodi contract', async () => {
const chain = chains.hoodi // or chains.mainnet
const transport = http()

const publicClient = createPublicClient({
Expand All @@ -35,12 +30,12 @@ describe('Read Contract', () => {
walletClient,
})

const result = await sdk.core.contracts.bapp.read.ETH_ADDRESS()
const result = await sdk.core.contracts.bapp.read.UPGRADE_INTERFACE_VERSION()

expect(isAddress(result)).toBe(true)
expect(result).toBeTypeOf('string')
})
test('can communicate with hoodi contract', async () => {
const chain = chains.hoodi // or chains.holesky
const chain = chains.hoodi // or chains.mainnet
const transport = http()

const publicClient = createPublicClient({
Expand All @@ -63,8 +58,82 @@ describe('Read Contract', () => {
walletClient,
})

const result = await sdk.core.contracts.bapp.read.ETH_ADDRESS()
const result = await sdk.core.contracts.bapp.read.UPGRADE_INTERFACE_VERSION()

expect(isAddress(result)).toBe(true)
expect(result).toBeTypeOf('string')
})

test('can communicate with the subgraph', async () => {
const chain = chains.hoodi // or chains.mainnet
const transport = http()

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

const account = privateKeyToAccount(
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', // Hardhat Account,
)
const walletClient = createWalletClient({
account,
chain,
transport,
})

const sdk = new BasedAppsSDK({
beaconchainUrl: 'https://example.com/beacon',
publicClient,
walletClient,
})

const result = await sdk.api.getValidatorsBalance({
account: '0x0000000000000000000000000000000000000000',
})

expect(result).toHaveProperty('account')
expect(result).toHaveProperty('validators')
expect(result).toHaveProperty('balance')
})

test.skipIf(!import.meta.env.VITE_SUBGRAPH_API_KEY)(
'can communicate with the subgraph(apikey)',
async () => {
const chain = chains.hoodi // or chains.mainnet
const transport = http()

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

const account = privateKeyToAccount(
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', // Hardhat Account,
)
const walletClient = createWalletClient({
account,
chain,
transport,
})

const sdk = new BasedAppsSDK({
beaconchainUrl: 'https://example.com/beacon',
publicClient,
walletClient,
extendedConfig: {
subgraph: {
apiKey: import.meta.env.VITE_SUBGRAPH_API_KEY,
},
},
})

const result = await sdk.api.getValidatorsBalance({
account: '0x0000000000000000000000000000000000000000',
})

expect(result).toHaveProperty('account')
expect(result).toHaveProperty('validators')
expect(result).toHaveProperty('balance')
},
)
})
72 changes: 60 additions & 12 deletions src/__tests__/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { describe, test, expect, vi, beforeEach } from 'vitest'

import { createPublicClient, createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { chains } from '@/config'
import { bam_paid_graph_endpoints, chains } from '@/config'

const chain = chains.holesky // or chains.holesky
const chain = chains.hoodi
const transport = http()

const publicClient = createPublicClient({
Expand Down Expand Up @@ -68,8 +68,10 @@ describe('BasedAppsSDK', () => {
beaconchainUrl: 'https://example.com/beacon',
publicClient,
walletClient,
_: {
subgraphUrl: customEndpoint,
extendedConfig: {
subgraph: {
url: customEndpoint,
},
},
})

Expand All @@ -82,14 +84,60 @@ describe('BasedAppsSDK', () => {
beaconchainUrl: 'https://example.com/beacon',
publicClient,
walletClient,
_: {
contractAddress: customAddress,
extendedConfig: {
contract: customAddress,
},
})

expect(sdk.core.contracts.bapp.address).toBe(customAddress)
})

test('should use paid graph when api key is provided', () => {
const apiKey = '1234567890abcdef'
const sdk = new BasedAppsSDK({
beaconchainUrl: 'https://example.com/beacon',
publicClient,
walletClient,
extendedConfig: {
subgraph: {
apiKey,
},
},
})
const requestHeaders = sdk.core.graphs.bam.client.requestConfig.headers as Record<
string,
string
>

expect(sdk.core.graphs.bam.endpoint).toBe(bam_paid_graph_endpoints[chain.id])
expect(requestHeaders['Authorization']).toBe(`Bearer ${apiKey}`)
})

test('should use custom graph endpoint and api key when both are provided', () => {
const customEndpoint = 'https://custom.endpoint/graphql'
const apiKey = '1234567890abcdef'

const sdk = new BasedAppsSDK({
beaconchainUrl: 'https://example.com/beacon',
publicClient,
walletClient,
extendedConfig: {
subgraph: {
url: customEndpoint,
apiKey,
},
},
})

const requestHeaders = sdk.core.graphs.bam.client.requestConfig.headers as Record<
string,
string
>

expect(sdk.core.graphs.bam.endpoint).toBe(customEndpoint)
expect(requestHeaders['Authorization']).toBe(`Bearer ${apiKey}`)
})

test('should have contract.bapp.read and contract.bapp.write functionality', () => {
const sdk = new BasedAppsSDK({
beaconchainUrl: 'https://example.com/beacon',
Expand All @@ -106,16 +154,16 @@ describe('BasedAppsSDK', () => {
})

test('should throw error when public client and wallet client have different chains', () => {
// Create a public client with the holesky chain
const holeskyPublicClient = createPublicClient({
chain: chains.holesky,
// Create a public client with the hoodi chain
const hoodiPublicClient = createPublicClient({
chain: chains.hoodi,
transport,
})

// Create another chain for test (simulating a different chain)
const differentChain = {
...chains.holesky,
id: chains.holesky.id + 1, // Use a different chain ID
...chains.hoodi,
id: chains.hoodi.id + 1, // Use a different chain ID
name: 'Different Chain',
}

Expand All @@ -131,7 +179,7 @@ describe('BasedAppsSDK', () => {
() =>
new BasedAppsSDK({
beaconchainUrl: 'https://example.com/beacon',
publicClient: holeskyPublicClient,
publicClient: hoodiPublicClient,
walletClient: differentChainWalletClient,
}),
).toThrow('Public and wallet client chains must be the same')
Expand Down
Loading