@reown/appkit-wagmi-react-native 0.0.0-develop-20250728153935 → 0.0.0-develop-20251030154825
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/adapter.js +183 -0
- package/lib/commonjs/adapter.js.map +1 -0
- package/lib/commonjs/connectors/UniversalConnector.js +230 -0
- package/lib/commonjs/connectors/UniversalConnector.js.map +1 -0
- package/lib/commonjs/index.js +8 -121
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/utils/helpers.js +16 -45
- package/lib/commonjs/utils/helpers.js.map +1 -1
- package/lib/module/adapter.js +178 -0
- package/lib/module/adapter.js.map +1 -0
- package/lib/module/connectors/UniversalConnector.js +226 -0
- package/lib/module/connectors/UniversalConnector.js.map +1 -0
- package/lib/module/index.js +4 -91
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/helpers.js +16 -41
- package/lib/module/utils/helpers.js.map +1 -1
- package/lib/typescript/adapter.d.ts +27 -0
- package/lib/typescript/adapter.d.ts.map +1 -0
- package/lib/typescript/connectors/UniversalConnector.d.ts +9 -0
- package/lib/typescript/connectors/UniversalConnector.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +3 -24
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/helpers.d.ts +3 -13
- package/lib/typescript/utils/helpers.d.ts.map +1 -1
- package/package.json +12 -28
- package/src/adapter.ts +227 -0
- package/src/connectors/UniversalConnector.ts +265 -0
- package/src/index.tsx +3 -121
- package/src/utils/helpers.ts +17 -57
- package/lib/commonjs/client.js +0 -516
- package/lib/commonjs/client.js.map +0 -1
- package/lib/commonjs/connectors/WalletConnectConnector.js +0 -355
- package/lib/commonjs/connectors/WalletConnectConnector.js.map +0 -1
- package/lib/commonjs/utils/defaultWagmiConfig.js +0 -46
- package/lib/commonjs/utils/defaultWagmiConfig.js.map +0 -1
- package/lib/module/client.js +0 -510
- package/lib/module/client.js.map +0 -1
- package/lib/module/connectors/WalletConnectConnector.js +0 -350
- package/lib/module/connectors/WalletConnectConnector.js.map +0 -1
- package/lib/module/utils/defaultWagmiConfig.js +0 -40
- package/lib/module/utils/defaultWagmiConfig.js.map +0 -1
- package/lib/typescript/client.d.ts +0 -40
- package/lib/typescript/client.d.ts.map +0 -1
- package/lib/typescript/connectors/WalletConnectConnector.d.ts +0 -74
- package/lib/typescript/connectors/WalletConnectConnector.d.ts.map +0 -1
- package/lib/typescript/utils/defaultWagmiConfig.d.ts +0 -11
- package/lib/typescript/utils/defaultWagmiConfig.d.ts.map +0 -1
- package/src/client.ts +0 -644
- package/src/connectors/WalletConnectConnector.ts +0 -472
- package/src/utils/defaultWagmiConfig.ts +0 -53
|
@@ -1,53 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
3
|
import { http } from 'viem';
|
|
4
|
-
|
|
5
|
-
if (!chain) {
|
|
6
|
-
return undefined;
|
|
7
|
-
}
|
|
8
|
-
return {
|
|
9
|
-
id: `${ConstantsUtil.EIP155}:${chain.id}`,
|
|
10
|
-
name: chain.name,
|
|
11
|
-
imageId: PresetsUtil.EIP155NetworkImageIds[chain.id]
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
export async function getWalletConnectCaipNetworks(connector) {
|
|
15
|
-
if (!connector) {
|
|
16
|
-
throw new Error('networkControllerClient:getApprovedCaipNetworks - connector is undefined');
|
|
17
|
-
}
|
|
18
|
-
const provider = await connector?.getProvider();
|
|
19
|
-
const ns = provider?.signer?.session?.namespaces;
|
|
20
|
-
const nsMethods = ns?.[ConstantsUtil.EIP155]?.methods;
|
|
21
|
-
const nsChains = ns?.[ConstantsUtil.EIP155]?.chains;
|
|
22
|
-
return {
|
|
23
|
-
supportsAllNetworks: Boolean(nsMethods?.includes(ConstantsUtil.ADD_CHAIN_METHOD)),
|
|
24
|
-
approvedCaipNetworkIds: nsChains
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
export function getAuthCaipNetworks() {
|
|
28
|
-
return {
|
|
29
|
-
supportsAllNetworks: false,
|
|
30
|
-
approvedCaipNetworkIds: PresetsUtil.RpcChainIds.map(id => `${ConstantsUtil.EIP155}:${id}`)
|
|
31
|
-
};
|
|
32
|
-
}
|
|
4
|
+
import { PresetsUtil, ConstantsUtil } from '@reown/appkit-common-react-native';
|
|
33
5
|
export function getTransport({
|
|
34
6
|
chainId,
|
|
35
7
|
projectId
|
|
36
8
|
}) {
|
|
37
|
-
const RPC_URL =
|
|
9
|
+
const RPC_URL = ConstantsUtil.BLOCKCHAIN_API_RPC_URL;
|
|
38
10
|
if (!PresetsUtil.RpcChainIds.includes(chainId)) {
|
|
39
11
|
return http();
|
|
40
12
|
}
|
|
41
13
|
return http(`${RPC_URL}/v1/?chainId=${ConstantsUtil.EIP155}:${chainId}&projectId=${projectId}`);
|
|
42
14
|
}
|
|
43
|
-
export function
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
15
|
+
export function formatNetwork(network) {
|
|
16
|
+
return {
|
|
17
|
+
...network,
|
|
18
|
+
rpcUrls: {
|
|
19
|
+
...network.rpcUrls
|
|
20
|
+
},
|
|
21
|
+
chainNamespace: 'eip155',
|
|
22
|
+
caipNetworkId: `eip155:${network.id}`
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export function formatNetworks(networks) {
|
|
26
|
+
return networks.map(formatNetwork);
|
|
52
27
|
}
|
|
53
28
|
//# sourceMappingURL=helpers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["http","PresetsUtil","ConstantsUtil","getTransport","chainId","projectId","RPC_URL","BLOCKCHAIN_API_RPC_URL","RpcChainIds","includes","EIP155","formatNetwork","network","rpcUrls","chainNamespace","caipNetworkId","id","formatNetworks","networks","map"],"sourceRoot":"../../../src","sources":["utils/helpers.ts"],"mappings":";;AAAA,SAASA,IAAI,QAAQ,MAAM;AAC3B,SACEC,WAAW,EACXC,aAAa,QAGR,mCAAmC;AAE1C,OAAO,SAASC,YAAYA,CAAC;EAAEC,OAAO;EAAEC;AAAkD,CAAC,EAAE;EAC3F,MAAMC,OAAO,GAAGJ,aAAa,CAACK,sBAAsB;EAEpD,IAAI,CAACN,WAAW,CAACO,WAAW,CAACC,QAAQ,CAACL,OAAO,CAAC,EAAE;IAC9C,OAAOJ,IAAI,CAAC,CAAC;EACf;EAEA,OAAOA,IAAI,CAAC,GAAGM,OAAO,gBAAgBJ,aAAa,CAACQ,MAAM,IAAIN,OAAO,cAAcC,SAAS,EAAE,CAAC;AACjG;AAEA,OAAO,SAASM,aAAaA,CAACC,OAAgB,EAAiB;EAC7D,OAAO;IACL,GAAGA,OAAO;IACVC,OAAO,EAAE;MAAE,GAAGD,OAAO,CAACC;IAAQ,CAAC;IAC/BC,cAAc,EAAE,QAAQ;IACxBC,aAAa,EAAE,UAAUH,OAAO,CAACI,EAAE;EACrC,CAAC;AACH;AAEA,OAAO,SAASC,cAAcA,CAACC,QAAmB,EAAmB;EACnE,OAAOA,QAAQ,CAACC,GAAG,CAACR,aAAa,CAAC;AACpC","ignoreList":[]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { EVMAdapter, type AppKitNetwork, type BlockchainAdapterInitParams, type CaipAddress, type ChainNamespace, type GetBalanceParams, type GetBalanceResponse } from '@reown/appkit-common-react-native';
|
|
2
|
+
import { type Config, type CreateConfigParameters, type Connector } from '@wagmi/core';
|
|
3
|
+
import type { Chain } from 'wagmi/chains';
|
|
4
|
+
type ConfigParams = Partial<CreateConfigParameters> & {
|
|
5
|
+
networks: readonly [Chain, ...Chain[]];
|
|
6
|
+
projectId: string;
|
|
7
|
+
connectors?: Connector[];
|
|
8
|
+
};
|
|
9
|
+
export declare class WagmiAdapter extends EVMAdapter {
|
|
10
|
+
private static supportedNamespace;
|
|
11
|
+
wagmiChains: readonly Chain[] | undefined;
|
|
12
|
+
wagmiConfig: Config;
|
|
13
|
+
private wagmiConfigConnector?;
|
|
14
|
+
private unsubscribeWatchAccount?;
|
|
15
|
+
constructor(configParams: ConfigParams);
|
|
16
|
+
private createWagmiInternalConfig;
|
|
17
|
+
switchNetwork(network: AppKitNetwork): Promise<void>;
|
|
18
|
+
getBalance(params: GetBalanceParams): Promise<GetBalanceResponse>;
|
|
19
|
+
getAccounts(): CaipAddress[] | undefined;
|
|
20
|
+
disconnect(): Promise<void>;
|
|
21
|
+
getSupportedNamespace(): ChainNamespace;
|
|
22
|
+
subscribeToEvents(): void;
|
|
23
|
+
init({ connector: _connector }: BlockchainAdapterInitParams): void;
|
|
24
|
+
setupWatchers(): void;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,KAAK,aAAa,EAClB,KAAK,2BAA2B,EAChC,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACxB,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EACL,KAAK,MAAM,EACX,KAAK,sBAAsB,EAM3B,KAAK,SAAS,EAEf,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAK1C,KAAK,YAAY,GAAG,OAAO,CAAC,sBAAsB,CAAC,GAAG;IACpD,QAAQ,EAAE,SAAS,CAAC,KAAK,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC;IACvC,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC;CAC1B,CAAC;AAEF,qBAAa,YAAa,SAAQ,UAAU;IAC1C,OAAO,CAAC,MAAM,CAAC,kBAAkB,CAA4B;IACtD,WAAW,EAAE,SAAS,KAAK,EAAE,GAAG,SAAS,CAAC;IAC1C,WAAW,EAAG,MAAM,CAAC;IAC5B,OAAO,CAAC,oBAAoB,CAAC,CAAY;IACzC,OAAO,CAAC,uBAAuB,CAAC,CAAa;gBAEjC,YAAY,EAAE,YAAY;IAStC,OAAO,CAAC,yBAAyB;IAoB3B,aAAa,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAWpD,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAuCvE,WAAW,IAAI,WAAW,EAAE,GAAG,SAAS;IAgBlC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAoBjC,qBAAqB,IAAI,cAAc;IAM9B,iBAAiB,IAAI,IAAI;IAIzB,IAAI,CAAC,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,2BAA2B,GAAG,IAAI;IAgC3E,aAAa;CA+Bd"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Provider, WalletConnector } from '@reown/appkit-common-react-native';
|
|
2
|
+
type Properties = {
|
|
3
|
+
onSessionDelete(data: {
|
|
4
|
+
topic: string;
|
|
5
|
+
}): void;
|
|
6
|
+
};
|
|
7
|
+
export declare function UniversalConnector(appKitProvidedConnector: WalletConnector): import("wagmi").CreateConnectorFn<Provider, Properties, Record<string, unknown>>;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=UniversalConnector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UniversalConnector.d.ts","sourceRoot":"","sources":["../../../src/connectors/UniversalConnector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AAqBnF,KAAK,UAAU,GAAG;IAChB,eAAe,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;CAChD,CAAC;AAEF,wBAAgB,kBAAkB,CAAC,uBAAuB,EAAE,eAAe,oFA+O1E"}
|
|
@@ -1,25 +1,4 @@
|
|
|
1
|
-
import '
|
|
2
|
-
export {
|
|
3
|
-
|
|
4
|
-
export { defaultWagmiConfig } from './utils/defaultWagmiConfig';
|
|
5
|
-
import { AppKit, type AppKitOptions } from './client';
|
|
6
|
-
export type { AppKitOptions } from './client';
|
|
7
|
-
type OpenOptions = Parameters<AppKit['open']>[0];
|
|
8
|
-
export declare function createAppKit(options: AppKitOptions): AppKit;
|
|
9
|
-
export declare function useAppKit(): {
|
|
10
|
-
open: (options?: OpenOptions) => Promise<void>;
|
|
11
|
-
close: () => Promise<void>;
|
|
12
|
-
};
|
|
13
|
-
export declare function useAppKitState(): {
|
|
14
|
-
selectedNetworkId: number | undefined;
|
|
15
|
-
open: boolean;
|
|
16
|
-
};
|
|
17
|
-
export declare function useWalletInfo(): {
|
|
18
|
-
walletInfo: import("@reown/appkit-scaffold-react-native").ConnectedWalletInfo;
|
|
19
|
-
};
|
|
20
|
-
export declare function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void): {
|
|
21
|
-
timestamp: number;
|
|
22
|
-
data: import("@reown/appkit-scaffold-react-native").Event;
|
|
23
|
-
};
|
|
24
|
-
export declare function useAppKitEventSubscription(event: EventName, callback: (newEvent: EventsControllerState) => void): void;
|
|
1
|
+
import { WagmiAdapter } from './adapter';
|
|
2
|
+
export { WagmiAdapter };
|
|
3
|
+
export { formatNetworks, formatNetwork } from './utils/helpers';
|
|
25
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import type { Connector } from '@wagmi/core';
|
|
3
|
-
import type { AppKitClientOptions } from '../client';
|
|
4
|
-
export declare function getCaipDefaultChain(chain?: AppKitClientOptions['defaultChain']): CaipNetwork | undefined;
|
|
5
|
-
export declare function getWalletConnectCaipNetworks(connector?: Connector): Promise<{
|
|
6
|
-
supportsAllNetworks: boolean;
|
|
7
|
-
approvedCaipNetworkIds: `${string}:${string}`[];
|
|
8
|
-
}>;
|
|
9
|
-
export declare function getAuthCaipNetworks(): {
|
|
10
|
-
supportsAllNetworks: boolean;
|
|
11
|
-
approvedCaipNetworkIds: `${string}:${string}`[];
|
|
12
|
-
};
|
|
1
|
+
import { type AppKitNetwork, type Network } from '@reown/appkit-common-react-native';
|
|
13
2
|
export declare function getTransport({ chainId, projectId }: {
|
|
14
3
|
chainId: number;
|
|
15
4
|
projectId: string;
|
|
16
5
|
}): import("viem").HttpTransport<undefined, false>;
|
|
17
|
-
export declare function
|
|
6
|
+
export declare function formatNetwork(network: Network): AppKitNetwork;
|
|
7
|
+
export declare function formatNetworks(networks: Network[]): AppKitNetwork[];
|
|
18
8
|
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/utils/helpers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/utils/helpers.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,OAAO,EACb,MAAM,mCAAmC,CAAC;AAE3C,wBAAgB,YAAY,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,kDAQ1F;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,aAAa,CAO7D;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,aAAa,EAAE,CAEnE"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reown/appkit-wagmi-react-native",
|
|
3
|
-
"version": "0.0.0-develop-
|
|
3
|
+
"version": "0.0.0-develop-20251030154825",
|
|
4
4
|
"main": "lib/commonjs/index.js",
|
|
5
5
|
"types": "lib/typescript/index.d.ts",
|
|
6
6
|
"module": "lib/module/index.js",
|
|
7
|
+
"react-native": "src/index.tsx",
|
|
7
8
|
"source": "src/index.tsx",
|
|
8
9
|
"scripts": {
|
|
9
10
|
"build": "bob build",
|
|
@@ -30,44 +31,27 @@
|
|
|
30
31
|
"repository": "https://github.com/reown-com/appkit-react-native",
|
|
31
32
|
"author": "Reown (https://discord.gg/reown)",
|
|
32
33
|
"homepage": "https://reown.com/appkit",
|
|
33
|
-
"license": "
|
|
34
|
+
"license": "SEE LICENSE IN LICENSE.md",
|
|
34
35
|
"bugs": {
|
|
35
36
|
"url": "https://github.com/reown-com/appkit-react-native/issues"
|
|
36
37
|
},
|
|
37
38
|
"publishConfig": {
|
|
38
39
|
"registry": "https://registry.npmjs.org/",
|
|
39
|
-
"access": "public"
|
|
40
|
+
"access": "public",
|
|
41
|
+
"provenance": true
|
|
40
42
|
},
|
|
41
43
|
"dependencies": {
|
|
42
|
-
"@reown/appkit-common-react-native": "0.0.0-develop-
|
|
43
|
-
"@reown/appkit-
|
|
44
|
-
"@reown/appkit-scaffold-utils-react-native": "0.0.0-develop-20250728153935",
|
|
45
|
-
"@reown/appkit-siwe-react-native": "0.0.0-develop-20250728153935"
|
|
44
|
+
"@reown/appkit-common-react-native": "0.0.0-develop-20251030154825",
|
|
45
|
+
"@reown/appkit-react-native": "0.0.0-develop-20251030154825"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@react-native-async-storage/async-storage": ">=1.17.0",
|
|
49
48
|
"@react-native-community/netinfo": "*",
|
|
50
|
-
"@walletconnect/react-native-compat": ">=2.
|
|
51
|
-
"react": ">=
|
|
52
|
-
"react-native": ">=0.
|
|
49
|
+
"@walletconnect/react-native-compat": ">=2.16.1",
|
|
50
|
+
"react": ">=18",
|
|
51
|
+
"react-native": ">=0.72",
|
|
53
52
|
"react-native-get-random-values": "*",
|
|
54
|
-
"viem": ">=2.
|
|
55
|
-
"wagmi": ">=2.
|
|
56
|
-
},
|
|
57
|
-
"react-native": "src/index.tsx",
|
|
58
|
-
"react-native-builder-bob": {
|
|
59
|
-
"source": "src",
|
|
60
|
-
"output": "lib",
|
|
61
|
-
"targets": [
|
|
62
|
-
"commonjs",
|
|
63
|
-
"module",
|
|
64
|
-
[
|
|
65
|
-
"typescript",
|
|
66
|
-
{
|
|
67
|
-
"tsc": "../../node_modules/.bin/tsc"
|
|
68
|
-
}
|
|
69
|
-
]
|
|
70
|
-
]
|
|
53
|
+
"viem": ">=2 <3.0.0",
|
|
54
|
+
"wagmi": ">=2 <3.0.0"
|
|
71
55
|
},
|
|
72
56
|
"eslintIgnore": [
|
|
73
57
|
"node_modules/",
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EVMAdapter,
|
|
3
|
+
type AppKitNetwork,
|
|
4
|
+
type BlockchainAdapterInitParams,
|
|
5
|
+
type CaipAddress,
|
|
6
|
+
type ChainNamespace,
|
|
7
|
+
type GetBalanceParams,
|
|
8
|
+
type GetBalanceResponse
|
|
9
|
+
} from '@reown/appkit-common-react-native';
|
|
10
|
+
import {
|
|
11
|
+
type Config,
|
|
12
|
+
type CreateConfigParameters,
|
|
13
|
+
createConfig,
|
|
14
|
+
getBalance as getBalanceWagmi,
|
|
15
|
+
switchChain as switchChainWagmi,
|
|
16
|
+
disconnect as disconnectWagmiCore,
|
|
17
|
+
connect as connectWagmi,
|
|
18
|
+
type Connector,
|
|
19
|
+
watchAccount
|
|
20
|
+
} from '@wagmi/core';
|
|
21
|
+
import type { Chain } from 'wagmi/chains';
|
|
22
|
+
import { getTransport } from './utils/helpers';
|
|
23
|
+
import { formatUnits, type Hex } from 'viem';
|
|
24
|
+
import { UniversalConnector } from './connectors/UniversalConnector';
|
|
25
|
+
|
|
26
|
+
type ConfigParams = Partial<CreateConfigParameters> & {
|
|
27
|
+
networks: readonly [Chain, ...Chain[]];
|
|
28
|
+
projectId: string;
|
|
29
|
+
connectors?: Connector[];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export class WagmiAdapter extends EVMAdapter {
|
|
33
|
+
private static supportedNamespace: ChainNamespace = 'eip155';
|
|
34
|
+
public wagmiChains: readonly Chain[] | undefined;
|
|
35
|
+
public wagmiConfig!: Config;
|
|
36
|
+
private wagmiConfigConnector?: Connector;
|
|
37
|
+
private unsubscribeWatchAccount?: () => void;
|
|
38
|
+
|
|
39
|
+
constructor(configParams: ConfigParams) {
|
|
40
|
+
super({
|
|
41
|
+
supportedNamespace: WagmiAdapter.supportedNamespace,
|
|
42
|
+
adapterType: 'wagmi'
|
|
43
|
+
});
|
|
44
|
+
this.wagmiChains = configParams.networks;
|
|
45
|
+
this.wagmiConfig = this.createWagmiInternalConfig(configParams);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private createWagmiInternalConfig(configParams: ConfigParams): Config {
|
|
49
|
+
// Connectors are typically added via wagmiConfig.connectors, but here AppKit manages the connection.
|
|
50
|
+
// We'll use the `connect` action with our dynamically created connector instance.
|
|
51
|
+
// So, the `connectors` array for createConfig can be empty and is added later.
|
|
52
|
+
const initialConnectors: (() => Connector)[] = [];
|
|
53
|
+
|
|
54
|
+
const transportsArr = configParams.networks.map(chain => [
|
|
55
|
+
chain.id,
|
|
56
|
+
getTransport({ chainId: chain.id, projectId: configParams.projectId })
|
|
57
|
+
]);
|
|
58
|
+
const transports = Object.fromEntries(transportsArr);
|
|
59
|
+
|
|
60
|
+
return createConfig({
|
|
61
|
+
chains: configParams.networks,
|
|
62
|
+
connectors: initialConnectors, // Empty, as we connect programmatically
|
|
63
|
+
transports,
|
|
64
|
+
multiInjectedProviderDiscovery: false
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async switchNetwork(network: AppKitNetwork): Promise<void> {
|
|
69
|
+
if (!this.wagmiConfigConnector) {
|
|
70
|
+
throw new Error('WagmiAdapter: AppKit connector not set or not connected via Wagmi.');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
await switchChainWagmi(this.wagmiConfig, {
|
|
74
|
+
chainId: network.id as number,
|
|
75
|
+
connector: this.wagmiConfigConnector
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async getBalance(params: GetBalanceParams): Promise<GetBalanceResponse> {
|
|
80
|
+
const { network, address, tokens } = params;
|
|
81
|
+
|
|
82
|
+
if (!this.connector) throw new Error('No active AppKit connector (EVMAdapter.connector)');
|
|
83
|
+
if (!network) throw new Error('No network provided');
|
|
84
|
+
|
|
85
|
+
if (!this.wagmiConfigConnector) {
|
|
86
|
+
throw new Error('WagmiAdapter: AppKit connector not properly configured with Wagmi.');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const balanceAddress =
|
|
90
|
+
address ||
|
|
91
|
+
this.getAccounts()?.find((acc: CaipAddress) => acc.includes(network.id.toString()));
|
|
92
|
+
|
|
93
|
+
if (!balanceAddress) {
|
|
94
|
+
return Promise.resolve({ amount: '0.00', symbol: network.nativeCurrency.symbol || 'ETH' });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const accountHex = balanceAddress.split(':')[2] as Hex;
|
|
98
|
+
|
|
99
|
+
const token = network?.caipNetworkId && (tokens?.[network.caipNetworkId]?.address as Hex);
|
|
100
|
+
|
|
101
|
+
const balance = await getBalanceWagmi(this.wagmiConfig, {
|
|
102
|
+
address: accountHex,
|
|
103
|
+
chainId: network.id as number,
|
|
104
|
+
token
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const formattedBalance = {
|
|
108
|
+
amount: formatUnits(balance.value, balance.decimals),
|
|
109
|
+
symbol: balance.symbol,
|
|
110
|
+
address: token ? (`${network.caipNetworkId}:${token}` as CaipAddress) : undefined
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
this.emit('balanceChanged', { address: balanceAddress, balance: formattedBalance });
|
|
114
|
+
|
|
115
|
+
return Promise.resolve(formattedBalance);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getAccounts(): CaipAddress[] | undefined {
|
|
119
|
+
if (!this.connector) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const namespaces = this.connector.getNamespaces();
|
|
124
|
+
if (!namespaces) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const supportedNamespaceKey = this.getSupportedNamespace();
|
|
129
|
+
const accountsForNamespace = namespaces[supportedNamespaceKey];
|
|
130
|
+
|
|
131
|
+
return accountsForNamespace?.accounts;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async disconnect(): Promise<void> {
|
|
135
|
+
if (this.unsubscribeWatchAccount) {
|
|
136
|
+
this.unsubscribeWatchAccount();
|
|
137
|
+
this.unsubscribeWatchAccount = undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (this.wagmiConfigConnector) {
|
|
141
|
+
await disconnectWagmiCore(this.wagmiConfig, { connector: this.wagmiConfigConnector });
|
|
142
|
+
this.wagmiConfigConnector = undefined;
|
|
143
|
+
} else if (this.connector) {
|
|
144
|
+
await this.connector.disconnect();
|
|
145
|
+
this.onDisconnect();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const evmAdapterInstance = this as any;
|
|
149
|
+
if ('connector' in evmAdapterInstance) {
|
|
150
|
+
evmAdapterInstance.connector = undefined;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
getSupportedNamespace(): ChainNamespace {
|
|
155
|
+
return WagmiAdapter.supportedNamespace;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Override subscribeToEvents to prevent double subscription
|
|
159
|
+
// Wagmi handles provider events through its connector system and watchAccount
|
|
160
|
+
override subscribeToEvents(): void {
|
|
161
|
+
// Do nothing - wagmi's watchAccount in setupWatchers handles all events
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
override init({ connector: _connector }: BlockchainAdapterInitParams): void {
|
|
165
|
+
super.init({ connector: _connector });
|
|
166
|
+
|
|
167
|
+
if (_connector && this.wagmiChains) {
|
|
168
|
+
if (!this.wagmiConfigConnector) {
|
|
169
|
+
// Manually add the connector to the wagmiConfig
|
|
170
|
+
const connectorInstance = this.wagmiConfig._internal.connectors.setup(
|
|
171
|
+
UniversalConnector(_connector)
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
this.wagmiConfig._internal.connectors.setState(prev => [...prev, connectorInstance]);
|
|
175
|
+
this.wagmiConfigConnector = connectorInstance;
|
|
176
|
+
|
|
177
|
+
connectorInstance.emitter.on('message', ({ type }: { type: string }) => {
|
|
178
|
+
if (type === 'externalDisconnect') {
|
|
179
|
+
this.onDisconnect();
|
|
180
|
+
|
|
181
|
+
this.wagmiConfigConnector = undefined;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
connectWagmi(this.wagmiConfig, { connector: connectorInstance });
|
|
187
|
+
} catch (error) {
|
|
188
|
+
this.wagmiConfigConnector = undefined;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
this.setupWatchers();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
setupWatchers() {
|
|
197
|
+
// Clean up existing subscription if any
|
|
198
|
+
this.unsubscribeWatchAccount?.();
|
|
199
|
+
|
|
200
|
+
this.unsubscribeWatchAccount = watchAccount(this.wagmiConfig, {
|
|
201
|
+
onChange: (accountData, prevAccountData) => {
|
|
202
|
+
if (!this.connector) return;
|
|
203
|
+
|
|
204
|
+
// Handle disconnect
|
|
205
|
+
if (accountData.status === 'disconnected' && prevAccountData.address) {
|
|
206
|
+
this.onDisconnect();
|
|
207
|
+
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Handle account address changes
|
|
212
|
+
if (
|
|
213
|
+
accountData?.addresses &&
|
|
214
|
+
accountData?.address &&
|
|
215
|
+
accountData.address !== prevAccountData?.address
|
|
216
|
+
) {
|
|
217
|
+
this.onAccountsChanged([...accountData.addresses]);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Handle chain changes
|
|
221
|
+
if (accountData?.chainId && accountData.chainId !== prevAccountData?.chainId) {
|
|
222
|
+
this.onChainChanged(accountData.chainId?.toString());
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|