Skip to content

Commit 6bb9abc

Browse files
committed
chore: add custom action for changeAccountSubscriber
1 parent c1da199 commit 6bb9abc

File tree

6 files changed

+83
-4
lines changed

6 files changed

+83
-4
lines changed

wallets/core/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
"./namespaces/solana": {
3131
"types": "./dist/namespaces/solana/mod.d.ts",
3232
"default": "./dist/namespaces/solana/mod.js"
33+
},
34+
"./hub": {
35+
"types": "./dist/hub/mod.d.ts",
36+
"default": "./dist/hub/mod.js"
3337
}
3438
},
3539
"files": [
@@ -38,7 +42,7 @@
3842
"legacy"
3943
],
4044
"scripts": {
41-
"build": "node ../../scripts/build/command.mjs --path wallets/core --inputs src/mod.ts,src/utils/mod.ts,src/legacy/mod.ts,src/namespaces/evm/mod.ts,src/namespaces/solana/mod.ts,src/namespaces/common/mod.ts",
45+
"build": "node ../../scripts/build/command.mjs --path wallets/core --inputs src/mod.ts,src/utils/mod.ts,src/legacy/mod.ts,src/namespaces/evm/mod.ts,src/namespaces/solana/mod.ts,src/namespaces/common/mod.ts,src/hub/mod.ts",
4246
"ts-check": "tsc --declaration --emitDeclarationOnly -p ./tsconfig.json",
4347
"clean": "rimraf dist",
4448
"format": "prettier --write '{.,src}/**/*.{ts,tsx}'",

wallets/core/src/hub/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { Namespace } from './namespaces/mod.js';
2-
2+
export type { Subscriber, SubscriberCleanUp } from './namespaces/mod.js';
33
export { Provider } from './provider/mod.js';
44
export type { CommonNamespaces, CommonNamespaceKeys } from './provider/mod.js';
55

wallets/core/src/hub/namespaces/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export type {
22
Subscriber,
3+
SubscriberCleanUp,
34
State,
45
RegisteredActions as ActionsMap,
56
Context,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type {
2+
Subscriber,
3+
SubscriberCleanUp,
4+
} from '@rango-dev/wallets-core/hub';
5+
6+
import {
7+
CAIP_NAMESPACE,
8+
CAIP_SOLANA_CHAIN_ID,
9+
type SolanaActions,
10+
} from '@rango-dev/wallets-core/namespaces/solana';
11+
import { AccountId } from 'caip';
12+
13+
import { evmClover, solanaClover } from '../utils.js';
14+
15+
export function changeAccountSubscriberAction(): [
16+
Subscriber<SolanaActions>,
17+
SubscriberCleanUp<SolanaActions>
18+
] {
19+
let eventCallback: () => void;
20+
21+
// subscriber can be passed to `or`, it will get the error and should rethrow error to pass the error to next `or` or throw error.
22+
return [
23+
(context, err) => {
24+
const solanaInstance = solanaClover();
25+
const evmInstance = evmClover();
26+
if (!solanaInstance) {
27+
throw new Error(
28+
'Trying to subscribe to your Solana wallet, but seems its instance is not available.'
29+
);
30+
}
31+
32+
const [, setState] = context.state();
33+
34+
eventCallback = async () => {
35+
const solanaInstance = solanaClover();
36+
const solanaAccount = await solanaInstance.getAccount();
37+
38+
setState('accounts', [
39+
AccountId.format({
40+
address: solanaAccount,
41+
chainId: {
42+
namespace: CAIP_NAMESPACE,
43+
reference: CAIP_SOLANA_CHAIN_ID,
44+
},
45+
}),
46+
]);
47+
};
48+
evmInstance.on('accountsChanged', eventCallback);
49+
50+
if (err instanceof Error) {
51+
throw err;
52+
}
53+
},
54+
(_context, err) => {
55+
const evmInstance = evmClover();
56+
if (eventCallback && evmInstance) {
57+
evmInstance.removeListener('accountsChanged', eventCallback);
58+
}
59+
60+
if (err instanceof Error) {
61+
throw err;
62+
}
63+
},
64+
];
65+
}

wallets/provider-clover/src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type ProviderInfo } from '@rango-dev/wallets-core';
33
export const WALLET_ID = 'clover';
44

55
export const info: ProviderInfo = {
6-
name: 'Clover',
6+
name: 'CLV',
77
icon: 'https://raw.githubusercontent.com/rango-exchange/assets/main/wallets/clover/icon.svg',
88
extensions: {
99
chrome:

wallets/provider-clover/src/namespaces/solana.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import {
1111
} from '@rango-dev/wallets-core/namespaces/solana';
1212
import { CAIP } from '@rango-dev/wallets-core/utils';
1313

14+
import { changeAccountSubscriberAction } from '../actions/solana.js';
1415
import { WALLET_ID } from '../constants.js';
1516
import { solanaClover } from '../utils.js';
1617

18+
const [changeAccountSubscriber, changeAccountCleanup] =
19+
changeAccountSubscriberAction();
20+
1721
const connect = builders
1822
.connect()
1923
.action(async function () {
@@ -37,9 +41,14 @@ const connect = builders
3741

3842
return formatAccounts;
3943
})
44+
.before(changeAccountSubscriber)
45+
.or(changeAccountCleanup)
4046
.build();
4147

42-
const disconnect = commonBuilders.disconnect<SolanaActions>().build();
48+
const disconnect = commonBuilders
49+
.disconnect<SolanaActions>()
50+
.after(changeAccountCleanup)
51+
.build();
4352

4453
const solana = new NamespaceBuilder<SolanaActions>('Solana', WALLET_ID)
4554
.action(connect)

0 commit comments

Comments
 (0)