From b0886a34bd6f9747f9a01b29abbf3e6f76597c05 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Fri, 1 Mar 2024 11:10:42 -0300 Subject: [PATCH] feat: cardano add correct getBalance implementation for cardano add getBalance test change provider-entity address parameter for getBalance since cardano always return full wallet balance --- src/cardano/get-balance.spec.ts | 28 ++++++++++++++++++++++++++++ src/cardano/getBalance.ts | 9 +++------ src/cardano/types.ts | 2 +- src/entities/provider-entity.ts | 4 ++-- 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 src/cardano/get-balance.spec.ts diff --git a/src/cardano/get-balance.spec.ts b/src/cardano/get-balance.spec.ts new file mode 100644 index 0000000..05c4e4a --- /dev/null +++ b/src/cardano/get-balance.spec.ts @@ -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') + }) +}) diff --git a/src/cardano/getBalance.ts b/src/cardano/getBalance.ts index c587e5b..6f29edc 100644 --- a/src/cardano/getBalance.ts +++ b/src/cardano/getBalance.ts @@ -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 { - if (address.length === 0) - throw new EmptyAddressError() +export async function getBalance(api: ApiPromise): Promise { 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) { diff --git a/src/cardano/types.ts b/src/cardano/types.ts index 9b352a5..2e6deac 100644 --- a/src/cardano/types.ts +++ b/src/cardano/types.ts @@ -7,7 +7,7 @@ export interface CardanoProviderProps { export type CardanoUsedAddress = string export interface ApiPromise { - getBalance: (address: string) => Promise + getBalance: () => Promise getUsedAddresses: () => Promise getUnusedAddresses: () => Promise } diff --git a/src/entities/provider-entity.ts b/src/entities/provider-entity.ts index 0c0c85f..428c191 100644 --- a/src/entities/provider-entity.ts +++ b/src/entities/provider-entity.ts @@ -2,8 +2,8 @@ import type { Account, Address, Balance, Hash } from '@/types' export interface ProviderEntity { connect(): Promise - getBalance(address: Address): Promise - signMessage(address: Address, message: string): Promise + getBalance(address?: string): Promise + signMessage(address: string, message: string): Promise signatureVerify(message: string, signature: string, address: Address): boolean joinPool(address: Address, poolId: number, amount: number): Promise bondExtra(address: Address, amount: number): Promise