Skip to content

Commit

Permalink
feat: cardano
Browse files Browse the repository at this point in the history
implement connect
  • Loading branch information
lucasportella committed Feb 14, 2024
1 parent dc82e58 commit b67db0e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ export const Networks = {
name: 'Kusama',
decimals: 12,
},
ada: {
id: 3,
name: 'Cardano',
decimals: 15, // review
}
}

export enum Network {
POLKADOT = 'dot',
KUSAMA = 'ksm',
CARDANO = 'ada'
}

export type NetworkKey = keyof typeof Networks
Expand Down
48 changes: 48 additions & 0 deletions src/substrate/ada/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { NotInjectedError } from '@/errors';
import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error';
import { NoProviderAvailableError } from '@/errors/no-provider-available-error';
import { Account } from '@/types';

interface CardanoWeb extends Window {
cardano: {
[key: string]: CardanoExtension;
};
}

interface CardanoInitializedExtension extends CardanoWeb {
getBalance: () => Promise<string>
getUsedAddresses: () => Promise<string[]>
getUnusedAddresses: () => Promise<string[]>
}

interface CardanoExtension {
apiVersion: string;
enable: () => Promise<CardanoInitializedExtension>;
isEnabled: () => Promise<boolean>;
icon: string;
name: string;
}


const CardanoWindow: CardanoWeb = (window as any)

export async function connect(appName: string): Promise<Account[]> {
if (!(window as any).cardano)
throw new NotInjectedError()

const isInjected = CardanoWindow.cardano[appName].name === appName
if (!isInjected)
throw new NoProviderAvailableError()

const api: CardanoInitializedExtension = await CardanoWindow.cardano[appName].enable()

const rawAccounts = await api.getUsedAddresses()
if (rawAccounts.length === 0)
throw new NoAvailableAccountsError()

const accounts = rawAccounts.map((account) => ({
address: account
}))

return accounts
}
13 changes: 13 additions & 0 deletions src/substrate/ada/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MissingPropsError } from '@/errors/missing-props-error';
import { Network } from '@/networks';
import { SubstrateProvider } from '@/substrate';
import type { SubstrateProviderProps } from '@/substrate/types';

export class CardanoProvider extends SubstrateProvider {
constructor({ appName, rpcProvider }: SubstrateProviderProps) {
if (!appName || appName.length === 0 || !rpcProvider || rpcProvider.length === 0)
throw new MissingPropsError()

super(Network.CARDANO, appName, rpcProvider)
}
}

0 comments on commit b67db0e

Please sign in to comment.