-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate clover from legacy to hub
- Loading branch information
Showing
12 changed files
with
236 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}, | ||
], | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |