2.0.0 • Published 1 year ago

@dodoex/token v2.0.0

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
1 year ago

@dodoex/token

独立 Token 相关的逻辑,和 wallet 项目解耦。

初始化

import {
  getCurrentChainId,
  fetchTokensByAddress,
  getLatestBlockNumber,
  getCurrentAccount,
  loadEthBalanceV2,
} from '@dodoex/wallet';
import { createStore, combineReducers, compose, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import mixpanel from 'mixpanel-browser';
import * as Sentry from '@sentry/browser';
import { enUS as walletEnUS, zhCN as walletZhCN } from 'dodo-wallet';

mixpanel.init('8b3e8750fd267914f7911af086a5e5d7');

// ... 处理多语言

const store = createStore(
  combineReducers(WalletReducer),
  undefined,
  compose(applyMiddleware(thunkMiddleware)),
);

const isEmitLazyLoad = useLazyLoad();
const { erc20List, filterErc20List } = useGetErc20List({
    skip: !isEmitLazyLoad,
    chainId,
  });

const chainId = useSelector(getCurrentChainId);
const account = useSelector(getCurrentAccount);
const blockNumber = useSelector(getLatestBlockNumber);

useInitToken({
  projectStore: store,
  projectSentry: Sentry,
  projectMixpanel: mixpanel,
  tokens: filterErc20List,
  fullTokens: erc20List,
  chainId,
  account,
  loadTokenList: true,
  fetchTokensByAddress,
  loadEthBalance: loadEthBalanceV2,
  blockNumber,
});

useInitToken 初始化一些环境变量级别的配置和项目本身的 Store。以及 wallet 项目加载 token 的方法。都是可选参数,传入才会执行对应的操作。

初始化 token

只有当初始化时传入 fetchTokensByAddress 才会初始化 token 数据,除了校验 token list 之外,会记录 token 的 balanceallowance 到 redux.

默认会在每个 blockNumber 之间刷新 初始化时传入的 tokens,及 redux 中存的 checkTokenAddresses(只要调用了 TokenLogo,就会写入 checkTokenAddresses)。

组件

暴露了 TokenLogo, TokenPicker, TokenPickerModal 三个组件。

  • TokenLogo 相对通用;
  • 另外两个需要在初始化时传入 tokens 才会有数据;
  • 传入 fetchTokensByAddress 才能加载自定义 token; -传入 loadTokenList 才会加载 token list;

API

获取 token 数据

  • useFetchTokensDetail (批量定时刷新token 信息,并将 balanceallowance 写入到 redux)
  • fetchTokenDetail (批量获取 token 信息,并将 balanceallowance 写入到 redux)
  • fetchCustomToken (获取单个 token 信息)
  • useGetAccountToken (返回 getAllowance、getBalance 获取详情)
  • useGetAccountChainToken (指定链返回 getBalance 用于获取 token 余额)