UNPKG

@okcontract/sdk

Version:

One-stop-shop permissionless SDK for building any blockchain frontend

142 lines (141 loc) 6.37 kB
import type { TransactionReceipt } from "viem"; import { type AnyCell, type CellArray, type MapCell, Sheet, SheetProxy, type ValueCell } from "@okcontract/cells"; import { type CacheDataFromType, type CacheQuery, type CachedData, type ChainQueryType, type TypeFromCacheQuery, type WritableDataType } from "@okcontract/coredata"; import { Environment } from "@okcontract/lambdascript"; import { type Address, type Chain, type ChainType, LocalRPCSubscriber, MultiChainRPC, type Network, type StringAddress } from "@okcontract/multichain"; import { type ForeignAccount, type Role, type User, type UserAuth } from "@scv/auth"; import { type APICacheInterface, LocalSubscriber, QueryCache } from "@scv/cache"; import { type AppID, type LogState } from "./constants"; import { DefaultContracts } from "./defaultContract"; import { Lighthouse } from "./lighthouse"; import type { SentTransaction } from "./tx"; import { type OKConnector, type WalletConnector, type WalletDetail } from "./wallet"; export declare const OKCoreKey: unique symbol; export type CoreOptions = { id: AppID; connector?: (proxy: SheetProxy, core: OKCore) => OKConnector<Network>; chain?: ChainType; sheet: Sheet; endpoint: string; fail: (err: Error) => Error; lic?: boolean; overrideConnectors?: (proxy: SheetProxy, chains: AnyCell<Record<ChainType, Chain>>, detected: CellArray<OKConnector<Network>>) => CellArray<OKConnector<Network>>; }; export declare const singleConnector: (proxy: SheetProxy, conn: OKConnector<Network>) => CellArray<OKConnector<Network>>; export declare const baseOptions: (id: AppID, chain?: ChainType, endpoint?: string, sheet?: Sheet) => CoreOptions; /** * Core is the main OKcontract class. There should be a single instance of OKCore * for a given app. */ export declare class OKCore { readonly AppID: string; readonly Sheet: Sheet; readonly Api: APICacheInterface; readonly IsSwitchingChain: AnyCell<boolean>; readonly IsSwitchingAccount: AnyCell<boolean>; /** list of connected wallets */ readonly ConnectedWallets: AnyCell<Record<string, WalletDetail>>; readonly IsVerified: MapCell<boolean, false>; readonly IsAuth: MapCell<boolean, false>; /** the current walletID */ readonly WalletID: AnyCell<Address<Network>>; /** the current wallet account */ /** wallet name if any */ readonly WalletName: AnyCell<string | null>; readonly Cache?: QueryCache; readonly CacheOnce: <Q extends CacheQuery>(q: Q) => Promise<CachedData<TypeFromCacheQuery<Q>>["data"]>; readonly MultiRPC: MultiChainRPC; readonly DetectedConnectors: AnyCell<OKConnector<Network>[]>; readonly CurrentConnector: ValueCell<number>; readonly Connector: MapCell<OKConnector<Network>, true>; readonly ConnectorAccount: AnyCell<StringAddress<Network>>; readonly CurrentChain: MapCell<ChainType, true>; readonly Chain: AnyCell<Chain>; readonly IsAdmin?: MapCell<boolean, true>; readonly IsGuest?: MapCell<boolean, true>; readonly Role: MapCell<Role, boolean>; /** * SentTransactions is a an array of all the sent txs * @todo persist */ readonly SentTransactions: ValueCell<SentTransaction[]>; /** * Receipts is an array of all receipts */ readonly Receipts: ValueCell<TransactionReceipt[]>; readonly Settings: Environment; readonly UserID: MapCell<string, false>; readonly User: MapCell<User, false>; readonly WantedWalletAccount: ValueCell<StringAddress<Network>>; readonly IsConnectWallet: MapCell<boolean, boolean>; readonly IsNoWallet: MapCell<boolean, boolean>; readonly Chains: AnyCell<Record<ChainQueryType, AnyCell<Chain>>>; readonly Auth: AnyCell<UserAuth | null>; readonly AllForeign: AnyCell<string[], false>; readonly Code: ValueCell<string>; readonly LogState: ValueCell<LogState>; readonly DefaultContracts: DefaultContracts; readonly Lighthouse: Lighthouse; readonly Master: MapCell<boolean, true>; readonly options: CoreOptions; readonly _rpc: LocalRPCSubscriber; private readonly _proxy; private readonly _apiUser; private readonly _parsedJWT; private readonly _local; private readonly _global; private readonly _chains; private readonly _syncAuth; private readonly _Token; private readonly _firstConnectedAccount; constructor(options: CoreOptions); private _afterAuth; private _verify2; LocalRPC: (onDestroy?: (fn: () => unknown) => void, proxy?: SheetProxy, _name?: string) => LocalRPCSubscriber; /** * Retrieves the current connector, or the connector specified * by its index. * @todo replace list with ConnectorID keys? */ private _getConnector; /** * Connects a connector and listen for its events. * @note It does not change the WantedAccount. * @param connector * @returns */ Connect: (connectorID?: WalletConnector) => Promise<boolean>; Disconnect: (connectorID?: WalletConnector) => Promise<boolean>; /** * Local creates a new LocalSubscriber, with an optional onDestroy * which must be used when instantiated in Svelte components. * @returns * @todo take the local proxy or create a local proxy... */ Local: (onDestroy?: (fn: () => unknown) => void, proxy?: SheetProxy, name?: string) => LocalSubscriber<CacheQuery>; WalletChange: (detail: WalletDetail) => Promise<void>; /** * switchChain switches the current chain, possibly adding the chain if not present. * @param ch * @see https://docs.metamask.io/guide/rpc-api.html#unrestricted-methods */ SwitchChain: (ch: ChainType) => Promise<ChainType>; Drop: () => Promise<boolean>; PairForeignAccount: (usr: User, fa: ForeignAccount) => Promise<boolean>; /** * Write a new OKcontract data. * @todo should we keep in the SDK? * @todo do not navigate from here */ Write: <T extends WritableDataType>(ty: T, data: CacheDataFromType<T>, options?: { connectorID?: WalletConnector; callback?: (cd: CachedData<T>) => void | Promise<void>; auth?: string; }) => Promise<Error>; /** * Add a UserForeign data. */ AddForeign: (addr: AnyCell<Address<Network>>, options?: { connectorID?: WalletConnector; }) => Promise<void>; }