Skip to content

Commit

Permalink
feat: cardano
Browse files Browse the repository at this point in the history
add correct getBalance implementation for cardano
add getBalance test
change provider-entity address parameter for getBalance since cardano always return full wallet balance
  • Loading branch information
lucasportella committed Mar 1, 2024
1 parent a932492 commit b0886a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
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('shoud be able to return mocked free balance', async () => {
const balance = await getBalance(cardanoMockedAPI)
expect(balance.free).toEqual('00')
})
})
9 changes: 3 additions & 6 deletions src/cardano/getBalance.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { BalanceFetchError } from '@/errors/balance-fetch-error';
import { EmptyAddressError } from '@/errors/empty-address-error';
import type { ApiPromise } from './types';
import type { Balance } from '@/types';

export async function getBalance(api: ApiPromise, address: string): Promise<Balance> {
if (address.length === 0)
throw new EmptyAddressError()
export async function getBalance(api: ApiPromise): Promise<Balance> {

try {
const accountBalance = await api.getBalance(address)
const accountBalance = await api.getBalance()
return {
free: accountBalance,
frozen: 0, // asset staking never gets "frozen" or "locked" in Cardano Network
frozen: '00', // asset staking never gets "frozen" or "locked" in Cardano Network
}
}
catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/cardano/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface CardanoProviderProps {
export type CardanoUsedAddress = string

export interface ApiPromise {
getBalance: (address: string) => Promise<string>
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

0 comments on commit b0886a3

Please sign in to comment.