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

Feat/rf 1863 migrate clover to hub #957

Open
wants to merge 3 commits into
base: next
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 wallets/core/src/hub/namespaces/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type {
Subscriber,
SubscriberCleanUp,
State,
RegisteredActions as ActionsMap,
Context,
Expand Down
5 changes: 5 additions & 0 deletions wallets/core/src/namespaces/common/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ export type {
Accounts,
AccountsWithActiveChain,
} from '../../types/accounts.js';

export type {
Subscriber,
SubscriberCleanUp,
} from '../../hub/namespaces/mod.js';
4 changes: 2 additions & 2 deletions wallets/provider-all/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as argentx from '@rango-dev/provider-argentx';
import * as bitget from '@rango-dev/provider-bitget';
import * as braavos from '@rango-dev/provider-braavos';
import * as brave from '@rango-dev/provider-brave';
import * as clover from '@rango-dev/provider-clover';
import { versions as clover } from '@rango-dev/provider-clover';
import * as coin98 from '@rango-dev/provider-coin98';
import * as coinbase from '@rango-dev/provider-coinbase';
import * as cosmostation from '@rango-dev/provider-cosmostation';
Expand Down Expand Up @@ -119,7 +119,7 @@ export const allProviders = (options?: Options): VersionedProviders[] => {
legacyProviderImportsToVersionsInterface(bitget),
legacyProviderImportsToVersionsInterface(enkrypt),
legacyProviderImportsToVersionsInterface(xdefi),
legacyProviderImportsToVersionsInterface(clover),
clover,
legacyProviderImportsToVersionsInterface(safepal),
legacyProviderImportsToVersionsInterface(brave),
legacyProviderImportsToVersionsInterface(coin98),
Expand Down
12 changes: 6 additions & 6 deletions wallets/provider-clover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
"version": "0.41.1-next.0",
"license": "MIT",
"type": "module",
"source": "./src/index.ts",
"main": "./dist/index.js",
"source": "./src/mod.ts",
"main": "./dist/mod.js",
"exports": {
".": "./dist/index.js"
".": "./dist/mod.js"
},
"typings": "dist/index.d.ts",
"typings": "dist/mod.d.ts",
"files": [
"dist",
"src"
],
"scripts": {
"build": "node ../../scripts/build/command.mjs --path wallets/provider-clover",
"build": "node ../../scripts/build/command.mjs --path wallets/provider-clover --inputs src/mod.ts",
"ts-check": "tsc --declaration --emitDeclarationOnly -p ./tsconfig.json",
"clean": "rimraf dist",
"format": "prettier --write '{.,src}/**/*.{ts,tsx}'",
Expand All @@ -30,4 +30,4 @@
"publishConfig": {
"access": "public"
}
}
}
69 changes: 69 additions & 0 deletions wallets/provider-clover/src/actions/solana.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type {
Subscriber,
SubscriberCleanUp,
} from '@rango-dev/wallets-core/namespaces/common';

import {
CAIP_NAMESPACE,
CAIP_SOLANA_CHAIN_ID,
type SolanaActions,
} from '@rango-dev/wallets-core/namespaces/solana';
import { AccountId } from 'caip';

import { evmClover, solanaClover } from '../utils.js';

/*
* The EVM instance is used to listen for the accountsChanged event,
* because Clover itself did not have a chain change event for the Solana namespace.
*/
export function changeAccountSubscriberAction(): [
yeager-eren marked this conversation as resolved.
Show resolved Hide resolved
Subscriber<SolanaActions>,
SubscriberCleanUp<SolanaActions>
] {
let eventCallback: () => void;

// 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.
return [
(context, err) => {
const solanaInstance = solanaClover();
const evmInstance = evmClover();
if (!solanaInstance) {
throw new Error(
'Trying to subscribe to your Solana wallet, but seems its instance is not available.'
);
}

const [, setState] = context.state();

eventCallback = async () => {
const solanaInstance = solanaClover();
const solanaAccount = await solanaInstance.getAccount();

setState('accounts', [
AccountId.format({
address: solanaAccount,
chainId: {
namespace: CAIP_NAMESPACE,
reference: CAIP_SOLANA_CHAIN_ID,
},
}),
]);
};
evmInstance.on('accountsChanged', eventCallback);

if (err instanceof Error) {
throw err;
}
},
(_context, err) => {
const evmInstance = evmClover();
if (eventCallback && evmInstance) {
evmInstance.removeListener('accountsChanged', eventCallback);
}

if (err instanceof Error) {
throw err;
}
},
];
}
22 changes: 22 additions & 0 deletions wallets/provider-clover/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type ProviderInfo } from '@rango-dev/wallets-core';

export const WALLET_ID = 'clover';

export const info: ProviderInfo = {
name: 'CLV',
icon: 'https://raw.githubusercontent.com/rango-exchange/assets/main/wallets/clover/icon.svg',
extensions: {
chrome:
'https://chrome.google.com/webstore/detail/clover-wallet/nhnkbkgjikgcigadomkphalanndcapjk',
brave:
'https://chrome.google.com/webstore/detail/clover-wallet/nhnkbkgjikgcigadomkphalanndcapjk',
homepage: 'https://wallet.clover.finance',
},
properties: [
{
name: 'detached',
// if you are adding a new namespace, don't forget to also update `getWalletInfo`
value: ['solana', 'evm'],
},
],
};
41 changes: 0 additions & 41 deletions wallets/provider-clover/src/helpers.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LegacyProviderInterface } from '@rango-dev/wallets-core/legacy';
import type {
CanEagerConnect,
CanSwitchNetwork,
Expand All @@ -20,7 +21,8 @@ import {
} from '@rango-dev/wallets-shared';
import { evmBlockchains, isEvmBlockchain, solanaBlockchain } from 'rango-types';

import { clover as clover_instance, getNonEvmAccounts } from './helpers.js';
import { clover as clover_instance, getSolanaAccounts } from '../utils.js';

import signer from './signer.js';

const WALLET = WalletTypes.CLOVER;
Expand All @@ -44,7 +46,7 @@ export const connect: Connect = async ({ instance, meta }) => {
});
}

const nonEvmResults = await getNonEvmAccounts(instance);
const nonEvmResults = await getSolanaAccounts(instance);
results = [...results, ...nonEvmResults];

return results;
Expand Down Expand Up @@ -118,3 +120,17 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
supportedChains: [...evms, ...solana],
};
};

const legacyProvider: LegacyProviderInterface = {
config,
getInstance,
connect,
subscribe,
canSwitchNetworkTo,
getSigners,
getWalletInfo,
canEagerConnect,
switchNetwork,
};

export { legacyProvider };
11 changes: 11 additions & 0 deletions wallets/provider-clover/src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineVersions } from '@rango-dev/wallets-core/utils';

import { legacyProvider } from './legacy/index.js';
import { provider } from './provider.js';

const versions = defineVersions()
.version('0.0.0', legacyProvider)
.version('1.0.0', provider)
.build();

export { versions };
36 changes: 36 additions & 0 deletions wallets/provider-clover/src/namespaces/evm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { EvmActions } from '@rango-dev/wallets-core/namespaces/evm';

import { NamespaceBuilder } from '@rango-dev/wallets-core';
import { builders as commonBuilders } from '@rango-dev/wallets-core/namespaces/common';
import { actions, builders } from '@rango-dev/wallets-core/namespaces/evm';

import { WALLET_ID } from '../constants.js';
import { evmClover } from '../utils.js';

const [changeAccountSubscriber, changeAccountCleanup] =
actions.changeAccountSubscriber(evmClover);

const [changeChainSubscriber, changeChainCleanup] =
actions.changeChainSubscriber(evmClover);

const connect = builders
.connect()
.action(actions.connect(evmClover))
.before(changeAccountSubscriber)
.before(changeChainSubscriber)
.or(changeAccountCleanup)
.or(changeChainCleanup)
.build();

const disconnect = commonBuilders
.disconnect<EvmActions>()
.after(changeAccountCleanup)
.after(changeChainCleanup)
.build();

const evm = new NamespaceBuilder<EvmActions>('EVM', WALLET_ID)
.action(connect)
.action(disconnect)
.build();

export { evm };
58 changes: 58 additions & 0 deletions wallets/provider-clover/src/namespaces/solana.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { CaipAccount } from '@rango-dev/wallets-core/namespaces/common';
import type { SolanaActions } from '@rango-dev/wallets-core/namespaces/solana';

import { NamespaceBuilder } from '@rango-dev/wallets-core';
import { LegacyNetworks } from '@rango-dev/wallets-core/legacy';
import { builders as commonBuilders } from '@rango-dev/wallets-core/namespaces/common';
import {
builders,
CAIP_NAMESPACE,
CAIP_SOLANA_CHAIN_ID,
} from '@rango-dev/wallets-core/namespaces/solana';
import { CAIP } from '@rango-dev/wallets-core/utils';

import { changeAccountSubscriberAction } from '../actions/solana.js';
import { WALLET_ID } from '../constants.js';
import { solanaClover } from '../utils.js';

const [changeAccountSubscriber, changeAccountCleanup] =
changeAccountSubscriberAction();

const connect = builders
yeager-eren marked this conversation as resolved.
Show resolved Hide resolved
.connect()
.action(async function () {
const solanaInstance = solanaClover();
const solanaAccounts = await solanaInstance.getAccount();
const result = {
accounts: [solanaAccounts],
chainId: LegacyNetworks.SOLANA,
};

const formatAccounts = result.accounts.map(
(account) =>
CAIP.AccountId.format({
address: account,
chainId: {
namespace: CAIP_NAMESPACE,
reference: CAIP_SOLANA_CHAIN_ID,
},
}) as CaipAccount
);

return formatAccounts;
})
.before(changeAccountSubscriber)
.or(changeAccountCleanup)
.build();

const disconnect = commonBuilders
.disconnect<SolanaActions>()
.after(changeAccountCleanup)
.build();

const solana = new NamespaceBuilder<SolanaActions>('Solana', WALLET_ID)
.action(connect)
.action(disconnect)
.build();

export { solana };
22 changes: 22 additions & 0 deletions wallets/provider-clover/src/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ProviderBuilder } from '@rango-dev/wallets-core';

import { info, WALLET_ID } from './constants.js';
import { evm } from './namespaces/evm.js';
import { solana } from './namespaces/solana.js';
import { clover as cloverInstance } from './utils.js';

const provider = new ProviderBuilder(WALLET_ID)
.init(function (context) {
const [, setState] = context.state();

if (cloverInstance()) {
setState('installed', true);
console.debug('[clover] instance detected.', context);
}
})
.config('info', info)
.add('solana', solana)
.add('evm', evm)
.build();

export { provider };
Loading
Loading