Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/cardano get balance #4

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 0 additions & 8 deletions src/ada/available-wallets.ts

This file was deleted.

16 changes: 16 additions & 0 deletions src/cardano/available-wallets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// TODO: Add klever mobile app source name
export const availableWallets = ['begin', 'eternl', 'flint', 'gerowallet', 'lace', 'nami', 'nufi', 'raywallet', 'yoroi'] as const

export type AvailableWallet = typeof availableWallets[number]

export enum CardanoWallet {
BEGIN = 'begin',
ETERNL = 'eternl',
FLINT = 'flint',
GERO_WALLET = 'gerowallet',
LACE = 'lace',
NAMI = 'nami',
NUFI = 'nufi',
RAY_WALLET = 'raywallet',
YOROI = 'yoroi',
}
4 changes: 2 additions & 2 deletions src/ada/connect.ts → src/cardano/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export async function connect(wallet?: string): Promise<CardanoUsedAddress[]> {
if (typeof injectedWallet === 'undefined')
throw new NoProviderAvailableError()

await web3Window.cardano[injectedWallet].enable()
const api = await web3Window.cardano[injectedWallet].enable()

const usedAddresses: CardanoUsedAddress[] = await web3Window.cardano.getUsedAddresses()
const usedAddresses: CardanoUsedAddress[] = await api.getUsedAddresses()
if (usedAddresses.length === 0)
throw new NoAvailableAccountsError()

Expand Down
28 changes: 28 additions & 0 deletions src/cardano/get-balance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { BalanceFetchError } from '@/errors/balance-fetch-error';
import { describe, expect, it, vi } from 'vitest';
import { getBalance } from './getBalance';

describe('Get balance use case', () => {
const cardanoMockedAPI = {
getBalance: vi.fn().mockResolvedValue('00'),
getUsedAddresses: vi.fn(),
getUnusedAddresses: vi.fn(),
}

const cardanoMockedInvalidAPI = {
getBalance: async () => {
throw new Error('unknown error')
},
getUsedAddresses: vi.fn(),
getUnusedAddresses: vi.fn(),
}

it('should be able to throw error when api throws error', async () => {
await expect(getBalance(cardanoMockedInvalidAPI)).rejects.toThrow(BalanceFetchError)
})

it('should be able to return mocked free balance', async () => {
const balance = await getBalance(cardanoMockedAPI)
expect(balance.free).toEqual('00')
})
})
16 changes: 16 additions & 0 deletions src/cardano/getBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BalanceFetchError } from '@/errors/balance-fetch-error';
import type { ApiPromise } from './types';
import type { Balance } from '@/types';

export async function getBalance(api: ApiPromise): Promise<Balance> {
try {
const accountBalance = await api.getBalance()
return {
free: accountBalance,
frozen: '00', // asset staking never gets "frozen" or "locked" in Cardano Network
}
}
catch (error) {
throw new BalanceFetchError(error as Error)
}
}
File renamed without changes.
6 changes: 6 additions & 0 deletions src/ada/types.ts → src/cardano/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ export interface CardanoProviderProps {
}

export type CardanoUsedAddress = string

export interface ApiPromise {
getBalance: () => Promise<string>
getUsedAddresses: () => Promise<string[]>
getUnusedAddresses: () => Promise<string[]>
}
4 changes: 2 additions & 2 deletions src/entities/provider-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { Account, Address, Balance, Hash } from '@/types'

export interface ProviderEntity {
connect(): Promise<Account[]>
getBalance(address: Address): Promise<Balance>
signMessage(address: Address, message: string): Promise<string>
getBalance(address?: string): Promise<Balance>
signMessage(address: string, message: string): Promise<string>
signatureVerify(message: string, signature: string, address: Address): boolean
joinPool(address: Address, poolId: number, amount: number): Promise<Hash>
bondExtra(address: Address, amount: number): Promise<Hash>
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './ada/index'
export * from './cardano/index'
export * from './icp/index'
export * from './networks'
export * from './substrate/dot/index'
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CardanoProviderProps } from './ada/types'
import type { CardanoProviderProps } from './cardano/types'
import type { NetworkKey } from './networks'
import type { SubstrateProviderProps } from './substrate/types'

Expand All @@ -19,9 +19,9 @@ export type ProviderBuilderProps<T extends NetworkKey> = T extends 'dot' | 'ksm'

// TODO: Add type and functions of injected providers
export type Web3Window = {
injectedWeb3: any
cardano: any
ic: any
injectedWeb3?: any
cardano?: any
ic?: any
} & Window & typeof globalThis

export const web3Window = (window as Web3Window)
4 changes: 2 additions & 2 deletions src/web3-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CardanoProvider } from './ada';
import type { CardanoProviderProps } from './ada/types';
import { CardanoProvider } from './cardano';
import type { CardanoProviderProps } from './cardano/types';
import type { ProviderEntity } from './entities/provider-entity';
import { InvalidNetworkError } from './errors/invalid-network-error';
import { InternetComputerProvider } from './icp';
Expand Down
Loading