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

[Issue 3248] Add support for dedot #3251

Open
wants to merge 1 commit into
base: subwallet-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/extension-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"browser-passworder": "^2.0.3",
"buffer": "^6.0.3",
"cross-fetch": "^3.1.5",
"dedot": "0.0.1-alpha.36",
"dexie": "^3.2.2",
"dexie-export-import": "^4.0.7",
"eth-simple-keyring": "^4.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
// SPDX-License-Identifier: Apache-2.0

import '@polkadot/types-augment';

import { options as acalaOptions } from '@acala-network/api';
import { GearApi } from '@gear-js/api';
import { rpc as oakRpc, types as oakTypes } from '@oak-foundation/types';
import { MetadataItem } from '@subwallet/extension-base/background/KoniTypes';
import { _API_OPTIONS_CHAIN_GROUP, API_AUTO_CONNECT_MS, API_CONNECT_TIMEOUT } from '@subwallet/extension-base/services/chain-service/constants';
import { getSubstrateConnectProvider } from '@subwallet/extension-base/services/chain-service/handler/light-client';
import { DEFAULT_AUX } from '@subwallet/extension-base/services/chain-service/handler/SubstrateChainHandler';
import { _ApiOptions } from '@subwallet/extension-base/services/chain-service/handler/types';
import { _ApiOptions, _SubstrateApiMode } from '@subwallet/extension-base/services/chain-service/handler/types';
import { _ChainConnectionStatus, _SubstrateApi, _SubstrateDefaultFormatBalance } from '@subwallet/extension-base/services/chain-service/types';
import { createPromiseHandler, PromiseHandler } from '@subwallet/extension-base/utils/promise';
import { goldbergRpc, goldbergTypes, spec as availSpec } from 'avail-js-sdk';
import { DedotClient, WsProvider as DedotWsProvider } from 'dedot';
import { BehaviorSubject } from 'rxjs';

import { ApiPromise, WsProvider } from '@polkadot/api';
Expand All @@ -28,7 +27,7 @@

export class SubstrateApi implements _SubstrateApi {
chainSlug: string;
api: ApiPromise;
_api: ApiPromise | DedotClient;
providerName?: string;
provider: ProviderInterface;
apiUrl: string;
Expand All @@ -41,6 +40,11 @@
private handleApiReady: PromiseHandler<_SubstrateApi>;
public readonly isApiConnectedSubject = new BehaviorSubject(false);
public readonly connectionStatusSubject = new BehaviorSubject(_ChainConnectionStatus.DISCONNECTED);

get api (): ApiPromise {
return this._api as ApiPromise;
}

get isApiConnected (): boolean {
return this.isApiConnectedSubject.getValue();
}
Expand Down Expand Up @@ -86,7 +90,7 @@
}
}

private createApi (provider: ProviderInterface, externalApiPromise?: ApiPromise): ApiPromise {
private createApiPromise (provider: ProviderInterface, externalApiPromise?: ApiPromise): ApiPromise {
const apiOption: ApiOptions = {
provider,
typesBundle,
Expand Down Expand Up @@ -150,14 +154,41 @@
return api;
}

constructor (chainSlug: string, apiUrl: string, { externalApiPromise, metadata, providerName }: _ApiOptions = {}) {
private createDedotClient (providerUrl: string): DedotClient {
const wsProvider = new DedotWsProvider(providerUrl);
const dedot = new DedotClient(wsProvider);

dedot.on('ready', this.onReady.bind(this));
dedot.on('connected', this.onConnect.bind(this));
dedot.on('disconnected', this.onDisconnect.bind(this));
dedot.on('error', this.onError.bind(this));

return dedot;
}

private createApiByMode (mode: _SubstrateApiMode, apiUrl: string, externalApiPromise?: ApiPromise) {

Check failure on line 169 in packages/extension-base/src/services/chain-service/handler/SubstrateApi.ts

View workflow job for this annotation

GitHub Actions / Build Development Preview

'createApiByMode' is declared but its value is never read.
switch (mode) {
case _SubstrateApiMode.DEDOT: {
this._api = this.createDedotClient(apiUrl);

break;
}

default: {
this.provider = this.createProvider(apiUrl);
this._api = this.createApiPromise(this.provider, externalApiPromise);
}
}
}

constructor (chainSlug: string, apiUrl: string, { externalApiPromise, metadata, mode, providerName }: _ApiOptions = {}) {
this.chainSlug = chainSlug;
this.apiUrl = apiUrl;
this.providerName = providerName;
this.registry = new TypeRegistry();
this.metadata = metadata;
this.provider = this.createProvider(apiUrl);
this.api = this.createApi(this.provider, externalApiPromise);
this._api = this.createApiPromise(this.provider, externalApiPromise);

this.handleApiReady = createPromiseHandler<_SubstrateApi>();
}
Expand All @@ -182,7 +213,7 @@
// Create new provider and api
this.apiUrl = apiUrl;
this.provider = this.createProvider(apiUrl);
this.api = this.createApi(this.provider);
this._api = this.createApiPromise(this.provider);
}

connect (): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ export interface _ApiOptions {
providerName?: string,
metadata?: MetadataItem,
onUpdateStatus?: (status: _ChainConnectionStatus) => void,
externalApiPromise?: ApiPromise
externalApiPromise?: ApiPromise,
mode?: _SubstrateApiMode
}

export enum _SubstrateApiMode {
POLKADOT_JS = 'POLKADOT_JS',
DEDOT = 'DEDOT',
PAPI = 'PAPI'
}

export enum _CHAIN_VALIDATION_ERROR {
Expand Down
2 changes: 2 additions & 0 deletions packages/extension-base/src/services/chain-service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { _AssetRef, _AssetType, _ChainAsset, _ChainInfo, _CrowdloanFund } from '@subwallet/chain-list/types';
import { _CHAIN_VALIDATION_ERROR } from '@subwallet/extension-base/services/chain-service/handler/types';
import { DedotClient } from 'dedot';
import { BehaviorSubject } from 'rxjs';
import Web3 from 'web3';

Expand Down Expand Up @@ -81,6 +82,7 @@ export interface _SubstrateApiState {
}

export interface _SubstrateApi extends _SubstrateApiState, _ChainBaseApi {
_api: ApiPromise | DedotClient;
api: ApiPromise;
isReady: Promise<_SubstrateApi>;

Expand Down
Loading
Loading