Skip to content

Commit

Permalink
feat: ✨ kleverHub provider and types
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnata committed Feb 27, 2024
1 parent 7fdf92b commit bb1725b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
41 changes: 28 additions & 13 deletions src/klv/connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,47 @@ import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error';
import { web3Window } from '@/types';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { connect } from './connect';
import type { KleverHub } from './types';

const defaultKleverHub: Required<KleverHub> = {
account: {
address: 'my_address',
chain: 1,
name: 'My wallet',
provider: 'kleverWeb',
},
accountChangeListeners: [],
blockChainChangeListeners: [],
communication: { waitingResponse: false },
isConnected: true,
isKleverHub: true,
name: 'kleverHub',
initialize: vi.fn(),
}

describe('Connect wallet use case', () => {
beforeEach(() => {
vi.resetAllMocks()

web3Window.kleverWeb = {
initialize: vi.fn(),
getWalletAddress: vi.fn(() => 'mockedAddress'),
}
web3Window.kleverHub = defaultKleverHub
})

it('should be able to throw error when window dont have kleverWeb object', async () => {
delete web3Window.kleverWeb
it('should be able to return account', async () => {
const account = await connect();

await expect(connect()).rejects.toThrow(NotInjectedError)
expect(account.address).toEqual(defaultKleverHub.account.address);
expect(account.name).toEqual(defaultKleverHub.account.name);
})

it('should be able to throw error when have no accounts', async () => {
vi.mocked(web3Window.kleverWeb.getWalletAddress).mockImplementation(() => undefined)
it('should be able to throw error when window dont have kleverHub object', async () => {
delete web3Window.kleverHub

await expect(connect()).rejects.toThrow(NoAvailableAccountsError)
await expect(connect()).rejects.toThrow(NotInjectedError)
})

it('should be able to return account', async () => {
const account = await connect();
it('should be able to throw error when have no accounts', async () => {
web3Window.kleverHub = { ...defaultKleverHub, account: undefined }

expect(account).toEqual('mockedAddress');
await expect(connect()).rejects.toThrow(NoAvailableAccountsError)
})
})
14 changes: 6 additions & 8 deletions src/klv/connect.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { NotInjectedError } from '@/errors';
import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error';
import { web3Window } from '@/types';
import type { KleverAddress } from './types';
import type { KleverAccount } from './types';

export async function connect(): Promise<KleverAddress> {
if (!web3Window.kleverWeb)
export async function connect(): Promise<KleverAccount> {
if (!web3Window.kleverHub)
throw new NotInjectedError()

await web3Window.kleverWeb.initialize();
await web3Window.kleverHub.initialize();

const address = web3Window.kleverWeb.getWalletAddress();

if (!address)
if (!web3Window.kleverHub.account)
throw new NoAvailableAccountsError()

return address
return web3Window.kleverHub.account
}
4 changes: 2 additions & 2 deletions src/klv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { connect } from './connect';

export class KleverProvider implements ProviderEntity {
async connect(): Promise<Account[]> {
const principalAddress = await connect()
const account = await connect()

return [{ name: 'Principal Account', address: principalAddress }]
return [{ name: account.name, address: account.address }]
}

getBalance(address: string): Promise<Balance> {
Expand Down
28 changes: 27 additions & 1 deletion src/klv/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
export type KleverAddress = string
export enum Chain {
KLV = 1,
ETH = 4,
TRX = 2,
}

export interface KleverAccount {
address: string
chain: number
name: string
provider: string
}

interface Communication {
waitingResponse: boolean
}

export interface KleverHub {
name: string
communication: Communication
accountChangeListeners: any[]
blockChainChangeListeners: any[]
isConnected: boolean
isKleverHub: boolean
account?: KleverAccount
initialize: () => Promise<void>
}
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CardanoProviderProps } from './ada/types'
import type { KleverHub } from './klv/types'
import type { NetworkKey } from './networks'
import type { SubstrateProviderProps } from './substrate/types'

Expand All @@ -22,7 +23,7 @@ export type Web3Window = {
injectedWeb3: any
cardano: any
ic: any
kleverWeb: any
kleverHub?: KleverHub
} & Window & typeof globalThis

export const web3Window = (window as Web3Window)

0 comments on commit bb1725b

Please sign in to comment.