Skip to content

Commit

Permalink
feat: migrate clover from legacy to hub
Browse files Browse the repository at this point in the history
  • Loading branch information
nikaaru committed Dec 1, 2024
1 parent 5438438 commit c1da199
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 51 deletions.
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"
}
}
}
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: 'Clover',
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 };
File renamed without changes.
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 };
49 changes: 49 additions & 0 deletions wallets/provider-clover/src/namespaces/solana.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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 { WALLET_ID } from '../constants.js';
import { solanaClover } from '../utils.js';

const connect = builders
.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;
})
.build();

const disconnect = commonBuilders.disconnect<SolanaActions>().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 };
70 changes: 70 additions & 0 deletions wallets/provider-clover/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { ProviderAPI as EvmProviderApi } from '@rango-dev/wallets-core/namespaces/evm';
import type { ProviderAPI as SolanaProviderApi } from '@rango-dev/wallets-core/namespaces/solana';
import type { ProviderConnectResult } from '@rango-dev/wallets-shared';

import { LegacyNetworks } from '@rango-dev/wallets-core/legacy';

type Provider = Map<string, any>;

export function clover() {
const { clover, clover_solana } = window;

if (!clover) {
return null;
}

const instances: Provider = new Map();

if (clover) {
instances.set(LegacyNetworks.ETHEREUM, clover);
}
if (clover_solana) {
instances.set(LegacyNetworks.SOLANA, clover_solana);
}

return instances;
}

export function evmClover(): EvmProviderApi {
const instances = clover();

const evmInstance = instances?.get(LegacyNetworks.ETHEREUM);

if (!evmInstance) {
throw new Error(
'Clover not injected or EVM not enabled. Please check your wallet.'
);
}

return evmInstance as EvmProviderApi;
}

export function solanaClover(): SolanaProviderApi {
const instance = clover();
const solanaInstance = instance?.get(LegacyNetworks.SOLANA);

if (!solanaInstance) {
throw new Error(
'Clover not injected or Solana not enabled. Please check your wallet.'
);
}

return solanaInstance;
}

export async function getSolanaAccounts(
instances: Provider
): Promise<ProviderConnectResult[]> {
const solanaInstance = instances.get(LegacyNetworks.SOLANA);
const results: ProviderConnectResult[] = [];
if (solanaInstance) {
const solanaAccounts = await solanaInstance.getAccount();

results.push({
accounts: [solanaAccounts],
chainId: LegacyNetworks.SOLANA,
});
}

return results;
}

0 comments on commit c1da199

Please sign in to comment.