|
| 1 | +import type { EvmActions } from '@rango-dev/wallets-core/namespaces/evm'; |
| 2 | + |
| 3 | +import { NamespaceBuilder } from '@rango-dev/wallets-core'; |
| 4 | +import { |
| 5 | + builders as commonBuilders, |
| 6 | + standardizeAndThrowError, |
| 7 | +} from '@rango-dev/wallets-core/namespaces/common'; |
| 8 | +import { |
| 9 | + actions, |
| 10 | + builders, |
| 11 | + utils, |
| 12 | +} from '@rango-dev/wallets-core/namespaces/evm'; |
| 13 | + |
| 14 | +import { WALLET_ID } from '../constants.js'; |
| 15 | +import { evmBinance, getEvmAccounts } from '../utils.js'; |
| 16 | + |
| 17 | +const [changeAccountSubscriber, changeAccountCleanup] = builders |
| 18 | + .changeAccountSubscriber(evmBinance) |
| 19 | + /* |
| 20 | + * Binance returns an array of connected accounts with the active one first. |
| 21 | + * Since we only need the active account, we take the first element as a workaround. |
| 22 | + */ |
| 23 | + .format(async (instance, accounts) => { |
| 24 | + const chainId = await instance.request({ method: 'eth_chainId' }); |
| 25 | + return utils.formatAccountsToCAIP([accounts[0]], chainId); |
| 26 | + }) |
| 27 | + .build(); |
| 28 | + |
| 29 | +const connect = builders |
| 30 | + .connect() |
| 31 | + .action( |
| 32 | + actions.connect(evmBinance, { |
| 33 | + getAccounts: getEvmAccounts, |
| 34 | + }) |
| 35 | + ) |
| 36 | + .before(changeAccountSubscriber) |
| 37 | + .or(changeAccountCleanup) |
| 38 | + .or(standardizeAndThrowError) |
| 39 | + .build(); |
| 40 | + |
| 41 | +const disconnect = commonBuilders |
| 42 | + .disconnect<EvmActions>() |
| 43 | + .after(changeAccountCleanup) |
| 44 | + .build(); |
| 45 | + |
| 46 | +const canSwitchNetwork = builders |
| 47 | + .canSwitchNetwork() |
| 48 | + .action(actions.canSwitchNetwork()) |
| 49 | + .build(); |
| 50 | + |
| 51 | +const getChainId = builders |
| 52 | + .getChainId() |
| 53 | + .action(actions.getChainId(evmBinance)) |
| 54 | + .build(); |
| 55 | + |
| 56 | +const evm = new NamespaceBuilder<EvmActions>('EVM', WALLET_ID) |
| 57 | + .action(connect) |
| 58 | + .action(disconnect) |
| 59 | + .action(canSwitchNetwork) |
| 60 | + .action(getChainId) |
| 61 | + .build(); |
| 62 | + |
| 63 | +export { evm }; |
0 commit comments