Skip to content

Commit ac1838b

Browse files
committed
refactor: wallet-connect to use reown/appkit
1 parent 3a747f7 commit ac1838b

File tree

13 files changed

+4754
-9166
lines changed

13 files changed

+4754
-9166
lines changed

packages/wallets/wallet-base/src/base.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import type {
55
} from '@injectivelabs/ts-types'
66
import type {
77
WalletMetadata,
8+
StrategyEmitter,
89
WalletEventListener,
910
ConcreteWalletStrategyArgs,
1011
ConcreteEvmWalletStrategyArgs,
12+
WalletStrategyEmitterEventType,
1113
ConcreteCosmosWalletStrategyArgs,
1214
} from './types/index.js'
1315

@@ -20,6 +22,12 @@ export default abstract class BaseConcreteStrategy {
2022

2123
public metadata?: WalletMetadata
2224

25+
/**
26+
* Optional emitter passed from parent WalletStrategy.
27+
* When provided, strategies emit events directly on the parent's emitter.
28+
*/
29+
protected emitter?: StrategyEmitter
30+
2331
public constructor(
2432
args:
2533
| ConcreteWalletStrategyArgs
@@ -32,9 +40,21 @@ export default abstract class BaseConcreteStrategy {
3240
: undefined
3341
this.chainId = args.chainId
3442
this.metadata = args.metadata
43+
this.emitter = args.emitter
3544
}
3645

3746
public setMetadata(metadata?: WalletMetadata) {
3847
this.metadata = metadata
3948
}
49+
50+
/**
51+
* Emit an event from this strategy.
52+
* If emitter was provided from parent, events go directly to parent.
53+
*/
54+
protected emit(
55+
event: WalletStrategyEmitterEventType,
56+
data?: Record<string, any>,
57+
) {
58+
this.emitter?.emit(event, data)
59+
}
4060
}

packages/wallets/wallet-base/src/types/enums.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { WalletErrorActionModule } from '@injectivelabs/exceptions'
22

3+
export * from './events.js'
4+
35
export const BroadcastMode = {
46
Block: 'block',
57
Sync: 'sync',
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export const StrategyEventType = {
2+
ConnectionEnd: 'connection-end',
3+
ConnectionStart: 'connection-start',
4+
WalletSigningEnd: 'wallet-signing-end',
5+
WalletSigningStart: 'wallet-signing-start',
6+
} as const
7+
8+
export const WalletConnectStrategyEventType = {
9+
WalletConnectSigningWithTxTimeout: 'signing-with-tx-timeout',
10+
} as const
11+
12+
export const WalletStrategyEmitterEventType = {
13+
TransactionFail: 'transaction-fail',
14+
TransactionSigned: 'transaction-signed',
15+
TransactionSignStart: 'transaction-sign-start',
16+
TransactionBroadcastEnd: 'transaction-broadcast-end',
17+
TransactionBroadcastStart: 'transaction-broadcast-start',
18+
TransactionPreparationEnd: 'transaction-preparation-end',
19+
TransactionPreparationStart: 'transaction-preparation-start',
20+
WalletStrategyDisconnect: 'wallet-strategy-disconnect',
21+
} as const
22+
23+
export type StrategyEventType =
24+
| (typeof StrategyEventType)[keyof typeof StrategyEventType]
25+
| (typeof WalletConnectStrategyEventType)[keyof typeof WalletConnectStrategyEventType]
26+
27+
export type WalletStrategyEmitterEventType =
28+
| (typeof WalletStrategyEmitterEventType)[keyof typeof WalletStrategyEmitterEventType]
29+
| StrategyEventType
30+
31+
export type WalletStrategyEmitterEvents = {
32+
[K in WalletStrategyEmitterEventType]: Record<string, any>
33+
}
34+
35+
export interface WalletStrategyEmitter {
36+
on(
37+
event: WalletStrategyEmitterEventType,
38+
listener: (...args: any[]) => void,
39+
): this
40+
off(
41+
event: WalletStrategyEmitterEventType,
42+
listener: (...args: any[]) => void,
43+
): this
44+
emit(event: WalletStrategyEmitterEventType, ...args: any[]): boolean
45+
}

packages/wallets/wallet-base/src/types/strategy.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import type {
1313
} from '@injectivelabs/sdk-ts/types'
1414
import type { Wallet, WalletDeviceType } from './enums.js'
1515

16+
export interface StrategyEmitter {
17+
emit(event: string, data?: Record<string, any>): boolean
18+
}
19+
1620
export type onAccountChangeCallback = (account: string | string[]) => void
1721
export type onChainIdChangeCallback = () => void
1822

@@ -104,6 +108,7 @@ export interface WalletMetadata {
104108
export interface ConcreteWalletStrategyArgs {
105109
chainId: ChainId
106110
metadata?: WalletMetadata
111+
emitter?: StrategyEmitter
107112
}
108113

109114
export interface ConcreteEvmWalletStrategyArgs extends ConcreteWalletStrategyArgs {
@@ -279,6 +284,8 @@ export interface ConcreteWalletStrategy extends Omit<
279284

280285
onChainIdChange?(callback: onChainIdChangeCallback): Promise<void> | void
281286

287+
initStrategy?(): Promise<void> | void
288+
282289
disconnect?(): Promise<void> | void
283290

284291
getCosmosWallet?(chainId: ChainId): CosmosWalletAbstraction

packages/wallets/wallet-base/src/utils/viem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const getViemPublicClient = (
5959
[EvmChainId.Mainnet, EvmChainId.Sepolia] as EvmChainId[]
6060
).includes(chainId as EvmChainId)
6161

62-
if (isEthereumNetwork && rpcUrl && !rpcUrl.includes('alchemyapi')) {
62+
if (isEthereumNetwork && rpcUrl && !rpcUrl.includes('alchemy')) {
6363
throw new Error(
6464
`An Alchemy RPC URL must be provided for EvmChainId ${chainId}`,
6565
)

packages/wallets/wallet-core/src/broadcaster/MsgBroadcaster.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
isCosmosAminoOnlyWallet,
5050
getEthereumSignerAddress,
5151
getInjectiveSignerAddress,
52+
WalletStrategyEmitterEventType,
5253
} from '@injectivelabs/wallet-base'
5354
import {
5455
TxGrpcApi,
@@ -65,7 +66,6 @@ import {
6566
createTransactionWithSigners,
6667
} from '@injectivelabs/sdk-ts/core/tx'
6768
import { checkIfTxRunOutOfGas } from '../utils/index.js'
68-
import { WalletStrategyEmitterEventType } from './types.js'
6969
import type { NetworkEndpoints } from '@injectivelabs/networks'
7070
import type { ThrownException } from '@injectivelabs/exceptions'
7171
import type { CosmosTxV1Beta1TxPb } from '@injectivelabs/sdk-ts'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './types.js'
1+
export type * from './types.js'
22
export * from './MsgBroadcaster.js'
33
export * from './Web3Broadcaster.js'
Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { EventEmitter } from 'eventemitter3'
21
import type { Msgs } from '@injectivelabs/sdk-ts/core/modules'
32
import type { ChainId, EvmChainId } from '@injectivelabs/ts-types'
43
import type { Network, NetworkEndpoints } from '@injectivelabs/networks'
@@ -35,39 +34,3 @@ export interface MsgBroadcasterOptions {
3534
gasBufferCoefficient?: number
3635
httpHeaders?: Record<string, string>
3736
}
38-
39-
export const WalletStrategyEmitterEventType = {
40-
TransactionFail: 'transaction-fail',
41-
TransactionSigned: 'transaction-signed',
42-
TransactionSignStart: 'transaction-sign-start',
43-
TransactionBroadcastEnd: 'transaction-broadcast-end',
44-
WalletStrategyDisconnect: 'wallet-strategy-disconnect',
45-
TransactionBroadcastStart: 'transaction-broadcast-start',
46-
TransactionPreparationEnd: 'transaction-preparation-end',
47-
TransactionPreparationStart: 'transaction-preparation-start',
48-
} as const
49-
50-
export type WalletStrategyEmitterEventType =
51-
(typeof WalletStrategyEmitterEventType)[keyof typeof WalletStrategyEmitterEventType]
52-
53-
export type WalletStrategyEmitterEvents = {
54-
[WalletStrategyEmitterEventType.TransactionFail]: Record<string, any>
55-
[WalletStrategyEmitterEventType.TransactionSigned]: Record<string, any>
56-
[WalletStrategyEmitterEventType.TransactionSignStart]: Record<string, any>
57-
[WalletStrategyEmitterEventType.TransactionBroadcastEnd]: Record<string, any>
58-
[WalletStrategyEmitterEventType.WalletStrategyDisconnect]: Record<string, any>
59-
[WalletStrategyEmitterEventType.TransactionBroadcastStart]: Record<
60-
string,
61-
any
62-
>
63-
[WalletStrategyEmitterEventType.TransactionPreparationEnd]: Record<
64-
string,
65-
any
66-
>
67-
[WalletStrategyEmitterEventType.TransactionPreparationStart]: Record<
68-
string,
69-
any
70-
>
71-
}
72-
73-
export type WalletStrategyEmitter = EventEmitter<WalletStrategyEmitterEvents>

packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import {
44
Wallet,
55
isEvmWallet,
66
isCosmosWallet,
7-
type WalletMetadata,
7+
WalletStrategyEmitterEventType,
88
} from '@injectivelabs/wallet-base'
9-
import { WalletStrategyEmitterEventType } from '../broadcaster/types.js'
109
import type { StdSignDoc } from '@keplr-wallet/types'
1110
import type { OfflineSigner } from '@cosmjs/proto-signing'
1211
import type { AccountAddress } from '@injectivelabs/ts-types'
@@ -17,6 +16,11 @@ import type {
1716
AminoSignResponse,
1817
DirectSignResponse,
1918
} from '@injectivelabs/sdk-ts/types'
19+
import type {
20+
WalletMetadata,
21+
WalletStrategyEmitter,
22+
WalletStrategyEmitterEvents,
23+
} from '@injectivelabs/wallet-base'
2024
import type {
2125
Eip1193Provider,
2226
WalletDeviceType,
@@ -29,10 +33,6 @@ import type {
2933
CosmosWalletAbstraction,
3034
WalletStrategy as WalletStrategyInterface,
3135
} from '@injectivelabs/wallet-base'
32-
import type {
33-
WalletStrategyEmitter,
34-
WalletStrategyEmitterEvents,
35-
} from '../broadcaster/types.js'
3636

3737
const getInitialWallet = (args: WalletStrategyArguments): Wallet => {
3838
if (args.wallet) {
@@ -84,14 +84,23 @@ export default class BaseWalletStrategy implements WalletStrategyInterface {
8484
this.emit = this.emitter.emit.bind(this.emitter)
8585
}
8686

87+
/**
88+
* Get the emitter instance.
89+
* Used to pass to strategies so they can emit events directly.
90+
*/
91+
public getEmitter(): WalletStrategyEmitter {
92+
return this.emitter
93+
}
94+
8795
public getWallet(): Wallet {
8896
return this.wallet
8997
}
9098

9199
public async setWallet(wallet: Wallet): Promise<void> {
92100
this.wallet = wallet
93101

94-
this.getStrategy()
102+
const strategy = this.getStrategy()
103+
await strategy?.initStrategy?.()
95104
}
96105

97106
public setMetadata(metadata?: WalletMetadata) {

packages/wallets/wallet-strategy/src/strategy/index.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { GeneralException } from '@injectivelabs/exceptions'
22
import { BaseWalletStrategy } from '@injectivelabs/wallet-core'
3-
import {
4-
Wallet,
5-
isEvmWallet,
6-
type WalletMetadata,
7-
} from '@injectivelabs/wallet-base'
3+
import { Wallet, isEvmWallet } from '@injectivelabs/wallet-base'
84
import {
95
loadEvmStrategy,
106
loadMagicStrategy,
@@ -16,6 +12,10 @@ import {
1612
loadWalletConnectStrategy,
1713
} from './loaders.js'
1814
import type { Wallet as WalletType } from '@injectivelabs/wallet-base'
15+
import type {
16+
WalletMetadata,
17+
StrategyEmitter,
18+
} from '@injectivelabs/wallet-base'
1919
import type {
2020
ConcreteStrategiesArg,
2121
ConcreteWalletStrategy,
@@ -43,9 +43,11 @@ const ethereumWalletsDisabled = (args: WalletStrategyArguments) => {
4343
const createStrategy = async ({
4444
args,
4545
wallet,
46+
emitter,
4647
}: {
4748
wallet: Wallet
4849
args: WalletStrategyArguments
50+
emitter?: StrategyEmitter
4951
}): Promise<ConcreteWalletStrategy | undefined> => {
5052
/**
5153
* If we only want to use Cosmos Native Wallets
@@ -63,8 +65,14 @@ const createStrategy = async ({
6365
...args,
6466
chainId: args.chainId,
6567
evmOptions: args.evmOptions as WalletStrategyEvmOptions,
68+
emitter,
6669
} as ConcreteEvmWalletStrategyArgs
6770

71+
const cosmosWalletArgs = {
72+
...args,
73+
emitter,
74+
}
75+
6876
switch (wallet) {
6977
case Wallet.Metamask:
7078
case Wallet.TrustWallet:
@@ -88,7 +96,7 @@ const createStrategy = async ({
8896
case Wallet.Cosmostation: {
8997
const CosmosWalletStrategy = await loadCosmosStrategy()
9098

91-
return new CosmosWalletStrategy({ ...args, wallet })
99+
return new CosmosWalletStrategy({ ...cosmosWalletArgs, wallet })
92100
}
93101

94102
case Wallet.Ledger: {
@@ -136,7 +144,7 @@ const createStrategy = async ({
136144
}
137145
const MagicStrategy = await loadMagicStrategy()
138146

139-
return new MagicStrategy(args)
147+
return new MagicStrategy(cosmosWalletArgs)
140148
}
141149

142150
case Wallet.WalletConnect: {
@@ -179,7 +187,8 @@ export class WalletStrategy extends BaseWalletStrategy {
179187
this.wallet = wallet
180188

181189
// Preload the strategy for the new wallet
182-
await this.loadStrategy(wallet)
190+
const strategy = await this.loadStrategy(wallet)
191+
await strategy?.initStrategy?.()
183192
}
184193

185194
/**
@@ -221,6 +230,7 @@ export class WalletStrategy extends BaseWalletStrategy {
221230
metadata: { ...this.args.metadata, ...metadata },
222231
},
223232
wallet: walletEnum,
233+
emitter: this.getEmitter(),
224234
})
225235

226236
continue
@@ -240,7 +250,9 @@ export class WalletStrategy extends BaseWalletStrategy {
240250
*/
241251
public getStrategy(): ConcreteWalletStrategy {
242252
if (this.strategies[this.wallet]) {
243-
return this.strategies[this.wallet] as ConcreteWalletStrategy
253+
const strategy = this.strategies[this.wallet] as ConcreteWalletStrategy
254+
255+
return strategy
244256
}
245257

246258
throw new GeneralException(
@@ -275,6 +287,7 @@ export class WalletStrategy extends BaseWalletStrategy {
275287
const loadPromise = createStrategy({
276288
args: this.args,
277289
wallet,
290+
emitter: this.getEmitter(),
278291
})
279292

280293
this.loadingStrategies.set(wallet, loadPromise)

0 commit comments

Comments
 (0)