Skip to content

Commit

Permalink
feat(connect): support add chain method (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
greyy-nguyen authored Dec 4, 2024
1 parent be97d77 commit 9dff789
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/connect/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sky-mavis/tanto-connect",
"version": "0.0.12",
"version": "0.0.13",
"description": "Tanto Connect",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
Expand Down
5 changes: 5 additions & 0 deletions packages/connect/src/common/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IChainInfo {
blockExplorerUrl?: string;
chainName: string;
iconUrl?: string;
rpcUrls?: string[];
nativeCurrency: {
name: string;
symbol: string;
Expand All @@ -25,6 +26,7 @@ export const CHAINS_CONFIG: IChainsConfig = {
blockExplorerUrl: 'https://app.roninchain.com',
chainName: 'Ronin Mainnet',
iconUrl: 'https://cdn.skymavis.com/explorer-cdn/asset/favicon/apple-touch-icon.png',
rpcUrls: ['https://api.roninchain.com/rpc'],
nativeCurrency: {
name: 'Ronin',
symbol: 'RON',
Expand All @@ -36,6 +38,7 @@ export const CHAINS_CONFIG: IChainsConfig = {
blockExplorerUrl: 'https://saigon-app.roninchain.com',
chainName: 'Saigon Testnet',
iconUrl: 'https://cdn.skymavis.com/explorer-cdn/asset/favicon/apple-touch-icon.png',
rpcUrls: ['https://saigon-testnet.roninchain.com/rpc'],
nativeCurrency: {
name: 'tRonin',
symbol: 'tRON',
Expand All @@ -46,6 +49,7 @@ export const CHAINS_CONFIG: IChainsConfig = {
chainId: ChainIds.Ethereum,
blockExplorerUrl: 'https://etherscan.io',
chainName: 'Ethereum',
rpcUrls: [],
nativeCurrency: {
name: 'Ethereum',
symbol: 'ETH',
Expand All @@ -56,6 +60,7 @@ export const CHAINS_CONFIG: IChainsConfig = {
chainId: ChainIds.Goerli,
blockExplorerUrl: 'https://goerli.etherscan.io/',
chainName: 'Goerli',
rpcUrls: [],
nativeCurrency: {
name: 'GoerliETH',
symbol: 'GTH',
Expand Down
1 change: 1 addition & 0 deletions packages/connect/src/connectors/base/BaseConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export abstract class BaseConnector<ProviderType = IEIP1193Provider>
abstract getAccounts(): Promise<readonly string[]>;
abstract getChainId(): Promise<number>;
abstract switchChain(chain: number): Promise<void>;
abstract addChain(chain: number): Promise<void>;
abstract requestAccounts(): Promise<readonly string[]>;

protected abstract requestProvider(): Promise<ProviderType>;
Expand Down
23 changes: 23 additions & 0 deletions packages/connect/src/connectors/injected/InjectedConnector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CHAINS_CONFIG } from '../../common/chain';
import { ReconnectStorage } from '../../common/storage';
import { IConnectorConfigs } from '../../types/connector';
import { ConnectorError, ConnectorErrorType } from '../../types/connector-error';
Expand Down Expand Up @@ -68,6 +69,28 @@ export class InjectedConnector extends BaseConnector {
});
}

async addChain(chain: number): Promise<void> {
const provider = await this.getProvider();

const { iconUrl, blockExplorerUrl, ...chainConfig } = CHAINS_CONFIG[chain] ?? {};

if (!chainConfig) {
throw new ConnectorError(ConnectorErrorType.ADD_CHAIN_NOT_SUPPORTED);
}

return provider.request<void>({
method: 'wallet_addEthereumChain',
params: [
{
...chainConfig,
chainId: numberToHex(chain),
iconUrls: [iconUrl],
blockExplorerUrls: [blockExplorerUrl],
},
],
});
}

async getChainId() {
const provider = await this.getProvider();
const chainId = await provider?.request<number | string>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { WC_SUPPORTED_CHAIN_IDS } from '../../common/constant';
import { ReconnectStorage } from '../../common/storage';
import { requestRoninWalletConnectProvider } from '../../providers';
import { IConnectorConfigs } from '../../types/connector';
import { ConnectorError, ConnectorErrorType } from '../../types/connector-error';
import { ConnectorEvent, WCEvent } from '../../types/connector-event';
import { EIP1193Event } from '../../types/eip1193';
import { numberToHex } from '../../utils';
Expand Down Expand Up @@ -93,6 +94,10 @@ export class RoninWalletConnectConnector extends BaseConnector<EthereumProvider>
});
}

async addChain(): Promise<void> {
throw new ConnectorError(ConnectorErrorType.ADD_CHAIN_NOT_SUPPORTED);
}

async getChainId() {
const provider = await this.getProvider();
const chainId = await provider?.request<string>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DEFAULT_CONNECTORS_CONFIG } from '../../common/connectors';
import { ReconnectStorage } from '../../common/storage';
import { requestRoninProvider } from '../../providers';
import { IConnectorConfigs } from '../../types/connector';
import { ConnectorError, ConnectorErrorType } from '../../types/connector-error';
import { EIP1193Event, IEIP1193Provider } from '../../types/eip1193';
import { numberToHex } from '../../utils';
import { BaseConnector } from '../base/BaseConnector';
Expand Down Expand Up @@ -66,6 +67,10 @@ export class RoninWalletConnector extends BaseConnector {
});
}

async addChain(): Promise<void> {
throw new ConnectorError(ConnectorErrorType.ADD_CHAIN_NOT_SUPPORTED);
}

async getChainId() {
const provider = await this.getProvider();
const chainId = await provider?.request<number | string>({
Expand Down
5 changes: 4 additions & 1 deletion packages/connect/src/connectors/safe/SafeConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export class SafeConnector extends BaseConnector {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async switchChain(chain: number) {
// TODO: Add a Connector Error for not allowing switch chain
throw new ConnectorError(ConnectorErrorType.SWITCH_CHAIN_NOT_SUPPORTED);
}

Expand All @@ -77,6 +76,10 @@ export class SafeConnector extends BaseConnector {
});
}

async addChain(): Promise<void> {
throw new ConnectorError(ConnectorErrorType.ADD_CHAIN_NOT_SUPPORTED);
}

async requestProvider() {
return await requestSafeProvider();
}
Expand Down
4 changes: 4 additions & 0 deletions packages/connect/src/connectors/waypoint/WaypointConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export class WaypointConnector extends BaseConnector<WaypointProvider> {
throw new ConnectorError(ConnectorErrorType.SWITCH_CHAIN_NOT_SUPPORTED);
}

async addChain(): Promise<void> {
throw new ConnectorError(ConnectorErrorType.ADD_CHAIN_NOT_SUPPORTED);
}

async getChainId() {
const provider = await this.getProvider();
const chainId = await provider?.request<number | string>({
Expand Down
1 change: 1 addition & 0 deletions packages/connect/src/types/connector-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum ConnectorErrorType {
CONNECT_FAILED = 'ConnectFailed',
NOT_INSTALLED = 'NotInstalled',
SWITCH_CHAIN_NOT_SUPPORTED = 'SwitchChainNotSupported',
ADD_CHAIN_NOT_SUPPORTED = 'AddChainNotSupported',
}

export class ConnectorError extends Error {
Expand Down

0 comments on commit 9dff789

Please sign in to comment.