Skip to content

Commit

Permalink
refactor: account interfaces (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien authored Sep 26, 2024
1 parent 444d164 commit e5dd504
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 38 deletions.
33 changes: 13 additions & 20 deletions packages/core/src/actions/get-accounts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MaybeAsync, PolkadotAccount } from "../types.js";
import type { MaybeAsync } from "../types.js";
import { toObservable } from "../utils.js";
import type { WalletAccount } from "../wallets/account.js";
import type { Wallet } from "../wallets/wallet.js";
import type { ChainSpecData } from "@polkadot-api/substrate-client";
import { AccountId } from "polkadot-api";
Expand All @@ -21,15 +22,18 @@ export function getAccounts(
const ss58Format =
typeof maybeSs58Format === "number" ? maybeSs58Format : undefined;

const ss58AccountId =
ss58Format === undefined ? undefined : AccountId(ss58Format);
const ss58AccountId = AccountId(ss58Format);

return combineLatest(
wallets.map((wallet) =>
wallet.accounts$.pipe(
map((accounts) =>
accounts.map(
(account): PolkadotAccount => ({ ...account, wallet }),
(account): WalletAccount => ({
...account,
address: ss58AccountId.dec(account.polkadotSigner.publicKey),
wallet,
}),
),
),
),
Expand All @@ -40,22 +44,11 @@ export function getAccounts(
chainSpec === undefined
? (accounts) => accounts
: (accounts) =>
accounts
.filter(
(account) =>
!account.genesisHash ||
chainSpec.genesisHash.includes(account.genesisHash),
)
.map((account) =>
ss58AccountId === undefined
? account
: {
...account,
address: ss58AccountId.dec(
ss58AccountId.enc(account.address),
),
},
),
accounts.filter(
(account) =>
!account.genesisHash ||
chainSpec.genesisHash.includes(account.genesisHash),
),
),
);
}),
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ export { MutationError, QueryError, ReactiveDotError } from "./errors.js";
export { Query } from "./query-builder.js";
export { PrefixedStorage } from "./storage.js";
export { idle, pending } from "./symbols.js";
export type { PolkadotAccount } from "./types.js";
4 changes: 0 additions & 4 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { Wallet } from "./wallets/wallet.js";
import type { InjectedPolkadotAccount } from "polkadot-api/pjs-signer";
import type { Observable } from "rxjs";

export type Gettable<T> = T | PromiseLike<T> | (() => T | PromiseLike<T>);

export type MaybeAsync<T> = T | Promise<T> | Observable<T>;

export type PolkadotAccount = InjectedPolkadotAccount & { wallet: Wallet };
16 changes: 16 additions & 0 deletions packages/core/src/wallets/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Wallet } from "./wallet.js";
import type { InjectedPolkadotAccount } from "polkadot-api/pjs-signer";

export type PolkadotSignerAccount = {
polkadotSigner: InjectedPolkadotAccount["polkadotSigner"];
genesisHash?: InjectedPolkadotAccount["genesisHash"];
};

export type PolkadotAccount = PolkadotSignerAccount & {
address: string;
};

export type WalletAccount = PolkadotAccount & {
name?: string;
wallet: Wallet;
};
5 changes: 5 additions & 0 deletions packages/core/src/wallets/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export type {
PolkadotAccount,
PolkadotSignerAccount,
WalletAccount,
} from "./account.js";
export { WalletAggregator } from "./aggregator.js";
export { DeepLinkWallet } from "./deep-link-wallet.js";
export { initializeWallets } from "./initialize-wallets.js";
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/wallets/injected/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ReactiveDotError } from "../../errors.js";
import type { PolkadotSignerAccount } from "../account.js";
import { Wallet, type WalletOptions } from "../wallet.js";
import {
connectInjectedExtension,
type InjectedExtension,
type InjectedPolkadotAccount,
} from "polkadot-api/pjs-signer";
import { BehaviorSubject, Observable } from "rxjs";
import { map, switchMap } from "rxjs/operators";
Expand Down Expand Up @@ -50,7 +50,7 @@ export class InjectedWallet extends Wallet {
override readonly accounts$ = this.#extension$.pipe(
switchMap(
(extension) =>
new Observable<InjectedPolkadotAccount[]>((subscriber) => {
new Observable<PolkadotSignerAccount[]>((subscriber) => {
if (extension === undefined) {
subscriber.next([]);
} else {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/wallets/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type PrefixedStorage, defaultStorage } from "../storage.js";
import type { InjectedPolkadotAccount } from "polkadot-api/pjs-signer";
import type { PolkadotSignerAccount } from "./account.js";
import type { Observable } from "rxjs";

export type WalletOptions = {
Expand Down Expand Up @@ -29,9 +29,9 @@ export abstract class Wallet {

abstract disconnect(): void | Promise<void>;

abstract readonly accounts$: Observable<InjectedPolkadotAccount[]>;
abstract readonly accounts$: Observable<PolkadotSignerAccount[]>;

abstract getAccounts():
| InjectedPolkadotAccount[]
| Promise<InjectedPolkadotAccount[]>;
| PolkadotSignerAccount[]
| Promise<PolkadotSignerAccount[]>;
}
10 changes: 3 additions & 7 deletions packages/react/src/stores/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { withAtomFamilyErrorCatcher } from "../utils/jotai.js";
import { chainSpecDataAtomFamily } from "./client.js";
import { walletsAtom } from "./wallets.js";
import {
type Config,
getAccounts,
type ChainId,
type PolkadotAccount,
} from "@reactive-dot/core";
import { getAccounts, type ChainId, type Config } from "@reactive-dot/core";
import type { WalletAccount } from "@reactive-dot/core/wallets.js";
import type { Atom } from "jotai";
import { atomFamily, atomWithObservable } from "jotai/utils";

export const accountsAtom = atomFamily(
(param: {
config: Config;
chainId: ChainId | undefined;
}): Atom<PolkadotAccount[] | Promise<PolkadotAccount[]>> =>
}): Atom<WalletAccount[] | Promise<WalletAccount[]>> =>
withAtomFamilyErrorCatcher(
accountsAtom,
param,
Expand Down

0 comments on commit e5dd504

Please sign in to comment.