From bc57ae4f1f489b04e6db1e18aa35d0d8fef59a96 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Tue, 20 Feb 2024 16:43:36 -0300 Subject: [PATCH 1/6] feat: add getBalance method --- src/ada/getBalance.ts | 20 ++++++++++++++++++++ src/ada/types.ts | 6 ++++++ 2 files changed, 26 insertions(+) create mode 100644 src/ada/getBalance.ts diff --git a/src/ada/getBalance.ts b/src/ada/getBalance.ts new file mode 100644 index 0000000..75850e4 --- /dev/null +++ b/src/ada/getBalance.ts @@ -0,0 +1,20 @@ +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() + + try { + const accountBalance = await api.getBalance(address) + return { + free: accountBalance, + frozen: 0, // asset staking never gets "locked" in Cardano Network + } + } + catch (error) { + throw new BalanceFetchError(error as Error) + } +} diff --git a/src/ada/types.ts b/src/ada/types.ts index dc7ed8b..9b352a5 100644 --- a/src/ada/types.ts +++ b/src/ada/types.ts @@ -5,3 +5,9 @@ export interface CardanoProviderProps { } export type CardanoUsedAddress = string + +export interface ApiPromise { + getBalance: (address: string) => Promise + getUsedAddresses: () => Promise + getUnusedAddresses: () => Promise +} From c70000e80a227ff17ef4dd58ad7152032bd52469 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Tue, 20 Feb 2024 16:44:22 -0300 Subject: [PATCH 2/6] feat: add more available wallets --- src/ada/available-wallets.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ada/available-wallets.ts b/src/ada/available-wallets.ts index 283e492..7d06a74 100644 --- a/src/ada/available-wallets.ts +++ b/src/ada/available-wallets.ts @@ -1,8 +1,16 @@ // TODO: Add klever mobile app source name -export const availableWallets = ['nami'] as const +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', } From 1a8bd822611fb2bcd7d114bec79b78b50cebc9bc Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Wed, 28 Feb 2024 16:29:45 -0300 Subject: [PATCH 3/6] refactor: cardano replace ada folder to cardano since its the correct fullname of the network --- src/{ada => cardano}/available-wallets.ts | 2 +- src/{ada => cardano}/connect.ts | 4 ++-- src/{ada => cardano}/getBalance.ts | 2 +- src/{ada => cardano}/index.ts | 0 src/{ada => cardano}/types.ts | 4 +--- src/index.ts | 2 +- src/types.ts | 4 ++-- src/web3-provider.ts | 4 ++-- 8 files changed, 10 insertions(+), 12 deletions(-) rename src/{ada => cardano}/available-wallets.ts (71%) rename src/{ada => cardano}/connect.ts (86%) rename src/{ada => cardano}/getBalance.ts (86%) rename src/{ada => cardano}/index.ts (100%) rename src/{ada => cardano}/types.ts (80%) diff --git a/src/ada/available-wallets.ts b/src/cardano/available-wallets.ts similarity index 71% rename from src/ada/available-wallets.ts rename to src/cardano/available-wallets.ts index 7d06a74..f0bad71 100644 --- a/src/ada/available-wallets.ts +++ b/src/cardano/available-wallets.ts @@ -1,5 +1,5 @@ // TODO: Add klever mobile app source name -export const availableWallets = ['begin', 'eternl', 'flint ', 'gerowallet ', 'lace', 'nami', 'nufi', 'raywallet', 'yoroi'] as const +export const availableWallets = ['begin', 'eternl', 'flint', 'gerowallet', 'lace', 'nami', 'nufi', 'raywallet', 'yoroi'] as const export type AvailableWallet = typeof availableWallets[number] diff --git a/src/ada/connect.ts b/src/cardano/connect.ts similarity index 86% rename from src/ada/connect.ts rename to src/cardano/connect.ts index de58df6..83e222a 100644 --- a/src/ada/connect.ts +++ b/src/cardano/connect.ts @@ -20,9 +20,9 @@ export async function connect(wallet?: string): Promise { 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() diff --git a/src/ada/getBalance.ts b/src/cardano/getBalance.ts similarity index 86% rename from src/ada/getBalance.ts rename to src/cardano/getBalance.ts index 75850e4..c587e5b 100644 --- a/src/ada/getBalance.ts +++ b/src/cardano/getBalance.ts @@ -11,7 +11,7 @@ export async function getBalance(api: ApiPromise, address: string): Promise; export type CardanoUsedAddress = string diff --git a/src/index.ts b/src/index.ts index 8fa2d24..4a2c188 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -export * from './ada/index' +export * from './cardano/index' export * from './networks' export * from './substrate/dot/index' export * from './substrate/ksm/index' diff --git a/src/types.ts b/src/types.ts index 2c4bbb1..27d4012 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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' @@ -19,7 +19,7 @@ export type ProviderBuilderProps = T extends 'dot' | 'ksm' // TODO: Add type and functions of injected providers export type Web3Window = { injectedWeb3: any - cardano: any + cardano?: any } & Window & typeof globalThis export const web3Window = (window as Web3Window) diff --git a/src/web3-provider.ts b/src/web3-provider.ts index 7ea3a8a..4c0c0df 100644 --- a/src/web3-provider.ts +++ b/src/web3-provider.ts @@ -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 type { NetworkData, NetworkKey } from './networks'; From 31f63278c1ad96ed141f7c7d45d329e524dc1515 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Wed, 28 Feb 2024 16:32:33 -0300 Subject: [PATCH 4/6] cardano connect connect method have some issues, try fixing and debug some through catch block --- src/cardano/connect.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/cardano/connect.ts b/src/cardano/connect.ts index 83e222a..f9c3eea 100644 --- a/src/cardano/connect.ts +++ b/src/cardano/connect.ts @@ -18,13 +18,18 @@ export async function connect(wallet?: string): Promise { } if (typeof injectedWallet === 'undefined') + // maybe impossible to hit this branch since Cardano API demands a wallet in the browser to be injected. throw new NoProviderAvailableError() - const api = await web3Window.cardano[injectedWallet].enable() - - const usedAddresses: CardanoUsedAddress[] = await api.getUsedAddresses() - if (usedAddresses.length === 0) - throw new NoAvailableAccountsError() - - return usedAddresses + try { + const api = await web3Window.cardano[injectedWallet].enable() + const usedAddresses: CardanoUsedAddress[] = await api.getUsedAddresses() + if (usedAddresses.length === 0) + throw new NoAvailableAccountsError() + return usedAddresses + } + catch (error) { + console.log(error) + return [] + } } From 30fb9b3097c1176246c6cb443d66348a041b10c1 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Wed, 28 Feb 2024 16:33:26 -0300 Subject: [PATCH 5/6] cardano connect test add test for some error cases --- src/cardano/connect.spec.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/cardano/connect.spec.ts diff --git a/src/cardano/connect.spec.ts b/src/cardano/connect.spec.ts new file mode 100644 index 0000000..110efb5 --- /dev/null +++ b/src/cardano/connect.spec.ts @@ -0,0 +1,31 @@ +import { NotInjectedError } from '@/errors'; +import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error'; +import { NoProviderAvailableError } from '@/errors/no-provider-available-error'; +import { web3Window } from '@/types'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { connect } from './connect'; + +describe('Connect wallet use case', () => { + const cardanoWebMock = { + yoroi: { + icon: 'data:image/svg', + name: 'yoroi', + apiVersion: '0.3.0', + enable: vi.fn(), + isEnabled: vi.fn(), + }, + } + beforeEach(() => { + delete web3Window.cardano + web3Window.cardano = cardanoWebMock + }) + it('should be able to throw error when window dont have Cardano object', async () => { + delete web3Window.cardano + await expect(connect()).rejects.toThrow(NotInjectedError) + }) + + it('should be able to throw error when have no providers', async () => { + web3Window.cardano = {} + await expect(connect()).rejects.toThrow(NoProviderAvailableError) + }) +}) From e452faa360f523431d4b7410a2b2a1a58dba6945 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Wed, 28 Feb 2024 16:41:52 -0300 Subject: [PATCH 6/6] refactor: cardano-connect no provider error use delete command for consistency --- src/cardano/connect.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cardano/connect.spec.ts b/src/cardano/connect.spec.ts index 110efb5..c79901f 100644 --- a/src/cardano/connect.spec.ts +++ b/src/cardano/connect.spec.ts @@ -25,7 +25,7 @@ describe('Connect wallet use case', () => { }) it('should be able to throw error when have no providers', async () => { - web3Window.cardano = {} + delete web3Window.cardano.yoroi await expect(connect()).rejects.toThrow(NoProviderAvailableError) }) })