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

Fix/cardano connect #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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.

7 changes: 0 additions & 7 deletions src/ada/types.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',
}
23 changes: 14 additions & 9 deletions src/ada/connect.ts → src/cardano/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error';
import { NoProviderAvailableError } from '@/errors/no-provider-available-error';
import { web3Window } from '@/types';
import { availableWallets } from './available-wallets';
import type { CardanoUsedAddress } from './types';
import type { CardanoUnusedAddresses } from './types';

export async function connect(wallet?: string): Promise<CardanoUsedAddress[]> {
export async function connect(wallet?: string): Promise<CardanoUnusedAddresses[]> {
if (!web3Window.cardano)
throw new NotInjectedError()

Expand All @@ -18,13 +18,18 @@ export async function connect(wallet?: string): Promise<CardanoUsedAddress[]> {
}

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()

await web3Window.cardano[injectedWallet].enable()

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

return usedAddresses
try {
const api = await web3Window.cardano[injectedWallet].enable()
const unusedAddresses: CardanoUnusedAddresses[] = await api.getUnusedAddresses()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to getUnusedAddresses method for enhanced privacy and avoid empty arrays from used Address throwing errors.

if (unusedAddresses.length === 0)
throw new NoAvailableAccountsError()
return unusedAddresses
}
catch (error) {
console.error(error)
return []
}
}
20 changes: 20 additions & 0 deletions src/cardano/getBalance.ts
Original file line number Diff line number Diff line change
@@ -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<Balance> {
if (address.length === 0)
throw new EmptyAddressError()

try {
const accountBalance = await api.getBalance(address)
return {
free: accountBalance,
frozen: 0, // asset staking never gets "frozen" or "locked" in Cardano Network
}
}
catch (error) {
throw new BalanceFetchError(error as Error)
}
}
File renamed without changes.
13 changes: 13 additions & 0 deletions src/cardano/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { CardanoWallet } from './available-wallets';

export type CardanoProviderProps = Record<'wallet', CardanoWallet>;

export type CardanoUsedAddress = string

export type CardanoUnusedAddresses = string

export interface ApiPromise {
getBalance: (address: string) => Promise<string>
getUsedAddresses: () => Promise<string[]>
getUnusedAddresses: () => Promise<string[]>
}
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'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected network name, ada is the coin

export * from './networks'
export * from './substrate/dot/index'
export * from './substrate/ksm/index'
Expand Down
4 changes: 2 additions & 2 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,7 +19,7 @@ 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
cardano?: 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 type { NetworkData, NetworkKey } from './networks';
Expand Down