1.2.68 • Published 2 years ago

@dodoex-io/wallet v1.2.68

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

dodo-web 项目抽出来的钱包模块,意图独立钱包相关的逻辑,方便维护升级,也希望可以在将来的其他项目中直接引用,目前是 Init 版本,仅规范了一些用法和一部分监听逻辑

使用(可参照 example)

初始化

import { initWallet, WalletReducer } from 'dodo-wallet';
import { createStore, combineReducers, compose, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import mixpanel from 'mixpanel-browser';
import { enUS as walletEnUS, zhCN as walletZhCN } from 'dodo-wallet';

mixpanel.init('8b3e8750fd267914f7911af086a5e5d7');

// ... 处理多语言

const store = createStore(
  combineReducers(WalletReducer),
  undefined,
  compose(applyMiddleware(thunkMiddleware)),
);
initWallet({
  walletConfig: {
    ETH_NETWORK: 'mainnet',
    PORTIS_ID: 'portis',
  },
  customChainConfig: {},
  projectStore: store,
  projectMixpanel: mixpanel,
});

initWallet 初始化一些环境变量级别的配置和项目本身的 Store,参数说明:

walletConfig: {
  ETH_NETWORK, // 初始化web3Modal以及指定默认ChainId
  PORTIS_ID    // 初始化Portis
}
customChainConfig // 选填,自定义配置,目前支持multicallAddr字段,用于打包RPC查询请求
store // 项目的Store,WalletReducer在项目初始化store的时候要放到Reducers里,```state.dodoWallet```就是钱包的reducer

API

Hooks

useDODOWallet(); // 自动连接浏览器插件钱包,监听TokenList变化和ProxyAddress变化加载TokenList的Balance和Allowance
useBlockNumberTimer(); // 轮询区块高度,每13秒查询一次
useBalancesTimer(); // 轮询ETH余额,每13秒查询一次

Actions,举例两个特殊的 Action,setTokens setProxyAddress,他们除了更新 Reducer 数据外,还会触发一些其他的逻辑。使用useDODOWallet()时,初始 Store 里的 Tokens 和 ProxyAddress 是undefined,当 Tokens 和 ProxyAddress 发生改变时会去查询 Balance 和 Allowance,意味着只要有 TokenList,并且连接钱包成功获取到钱包地址,那么就会去加载一次全部 Token 的 Balance,同时如果存在 ProxyAddress,那么就会加载一次全部的 Allowance。这里没有轮询的逻辑,如果需要更新数据则需要手动执行loadTokenBalancesloadTokenAllowances

Selectors,如果是一次性的查询,可以直接用getCurrentAccount()方法获取到当前连接到的钱包地址,如果需要 Hooks 监听状态变化,则可以使用useSelector(getCurrentAccount)

web3,定义了一些 web3 方法,如connectToWallet连接钱包。也可以通过getWeb3()来直接获取一个 web3 实例。

开发流程

根目录执行yarn安装依赖和本地打包 package 文件

建立本地 package 软链接

yarn link

进入到example/路径或 dodo-web 项目下

alias 不能使用不同依赖的包

yarn link @dodoex-io/wallet 后,dodo-wallet 下的文件会使用最近一层的 node_modules,外部的文件使用自身项目的 node_modules 。导致两者不唯一。使用 alias ,确保使用的是同一个包.

// config-overrides.js (for create-react-app)
const path = require('path');
const {
  override,
  addWebpackAlias,
} = require('customize-cra');

const webpackAlias = {
  src: path.resolve(__dirname, 'src'),
};
if (process.env.NODE_ENV !== 'production') {
  // avoid Link leading to depends on errors
  const reactLinkPackages = [
    'react',
    'react-dom',
    'react-router-dom',
    'react-redux',
    'styled-components',
    'react-i18next',
    'redux',
  ];
  reactLinkPackages.forEach((pkg) => {
    webpackAlias[pkg] = path.resolve(__dirname, `node_modules/${pkg}`);
  });
}

const overrides = [
  addWebpackAlias(webpackAlias)
];

module.exports = override(...overrides);

执行

yarn
yarn link @dodoex-io/wallet
yarn run start

运行 example,默认写了 3000 端口,浏览器打开页面进行调试

Add new chain

  • chainConfig.ts
  • wallet.ts registerArbitrumNetworkWithMetamask()
  • src/components/images/networks-logo/CHAIN.svg
  • NetworkMismatchDialog.ts registerMap
  • WalletsPage.ts Network networkKeyMap networkMatchChainNameMap chainLogoMap list handleChange
  • utils/wallet.ts getChainIdWithNetworkName getNetworkNameWithChainId isETHOrBSCChain ChainType getChainType validNetworkList

新增配置

新增 chain

  1. 配置 chainConfigs.ts;
  2. 配置 injects/chain/types 下的 ChainType;
  3. 配置 injects/chain/constant.ts;
  4. 如果可代码切换 chain,在 NetworkMismatchDialogwallet.ts 下配置相关切换代码;

新增 wallet

  1. 配置 injects/wallet/types 下的 WalletType (如果是 inject ,则无需配置);
  2. 配置 injects/wallet/constant.ts;

单元测试

监听 __tests__ 文件下的 [jt]sx?,使用 js-dom 环境执行

测试步骤

yarn test:unit

setup

默认执行 testUtils/setup.ts 文件,初始化 wallet ,并简单 mock metamask 环境

hooks

使用 @testing-library/react-hooks 测试

import { renderHook } from '@testing-library/react-hooks/dom';

it('should use counter', () => {
  const { result } = renderHook(() => useCounter())

  expect(result.current.count).toBe(0)
  expect(typeof result.current.increment).toBe('function')
});

组件渲染

封装 @testing-library/react 渲染,注入项目需要的一些 配置

import { renderProvider, store } from '../../../testUtils/init';

// PC
renderProvider(
  <div>children render</div>
)

// mobile
renderProvider(
  <div>children render</div>
, true)

多语言

import i18n from '../../../testUtils/i18n';
import { screen } from '@testing-library/react';

// ...render

it(('title is exist') => {
  const title = i18n.t('wallet.dialog.network-mismatch.title1');
  expect(screen.queryByText(title)).toBeTruthy();
})

redux

import { renderProvider, store } from '../../../testUtils/init';

// ...render

it(('set redux') => {
  store.dispatch(setChainId(1));
  expect(store.getState().dodoWallet.get('chainId')).toBe(1);
})

e2e 测试

目录结构

├── e2e
│   ├── setup.ts // .env 赋值
│   ├── chrome-data // chrome 生成的用户数据
│   ├── commands // 测试站点的封装
│   │   ├── metamask.ts
│   │   └── wallet.ts
│   ├── extension
│   │   └── metamask-chrome-10.0.1
│   ├── integration // 测试
│   │   └── connectWallet.ts
│   └── support
│       ├── constants.ts // 常量的配置。包括测试地址,钱包助记词等(可直接在 .env 配置)
│       └── helpers.ts
├── src
│   └── dom.ts // test-id 配置,供定位 dom 使用
├── .env // 常量配置
├── jest.config.ts // 单元测试配置
├── jest-e2e.config.js // e2e 测试配置
├── jest-e2e.sequencer.js // test-sequencer 配置
└── jest-puppeteer.config.js // puppeteer 启动浏览器配置

测试步骤

  1. 配置 .env (可选)。对应 e2e/support/constants.ts 文件
TEST_URL=http://localhost:3000
WALLET_KEY=jelly interest enjoy grace energy battle brief private duck rent alarm call
WALLET_PWD=12345678
  1. 运行示例站点。这里以 example 为例
yarn link

cd example
yarn link @dodoex-io/wallet
yarn start # server localhost:3000
  1. 执行测试
# dodo-wallet 根目录
yarn test:e2e

相关文档

上链相关

状态监听

useRequestStatus 方法返回当前未完全结束的上链记录

字段
  1. 记录正在上链中的记录: runningIdMap;
  2. 记录需要等待后端数据库同步,当前正处于上链中的条数: needSyncRunningCount;
  3. 记录需要等待后端数据库同步,当前已上链成功的记录: needSyncSuccessList;
记录需要等待后端数据同步的交易记录

在调用 submission.execute 时,传入 options 字段

{
  requestOptions: {
    /** 需要等待后台数据同步,前端才能看到 */
      needSync?: boolean;
      /** 附加信息给判断交易记录对应关系使用 */
      extraData?: any;
  }
}

Tips

  1. peerDependencies 的包直接使用 yarn add -P @owen05/dodo-sdk-v2 安装后再调用 yarn 安装之后该包会被删掉,导致 build 报错。参考 How to handle peer dependencies when developing modules 使用 install-peers-cli 和 prepare 脚本来在 yarn 之后执行 peers 的安装,而 prepare 脚本在包被其他项目安装时不会被执行,避免了安装重复的包。

  2. 修改打包配置,默认包为 commonjs 模块的包可以在 nodejs 环境例如 nextjs 中使用,es 模块的包会被 webpack 自动识别在浏览器环境下使用。

1.2.68

2 years ago

1.2.67-0

2 years ago

1.2.67

2 years ago

1.2.60

2 years ago

1.2.63

2 years ago

1.2.64

2 years ago

1.2.61

2 years ago

1.2.62

2 years ago

1.2.65

2 years ago

1.2.66

2 years ago

1.2.60-1

2 years ago

1.2.60-0

2 years ago

1.2.58-0

2 years ago

1.2.46

2 years ago

1.2.49

2 years ago

1.2.47

2 years ago

1.2.48

2 years ago

1.2.52

2 years ago

1.2.53

2 years ago

1.2.50

2 years ago

1.2.51

2 years ago

1.2.56

2 years ago

1.2.57

2 years ago

1.2.54

2 years ago

1.2.55

2 years ago

1.2.58

2 years ago

1.2.59

2 years ago

1.2.41

2 years ago

1.2.42

2 years ago

1.2.40

2 years ago

1.2.45

2 years ago

1.2.43

2 years ago

1.2.44

2 years ago

1.2.39-2

2 years ago

1.2.39-1

2 years ago

1.2.39-0

2 years ago

1.2.34

2 years ago

1.2.35

2 years ago

1.2.33

2 years ago

1.2.38

2 years ago

1.2.39

2 years ago

1.2.36

2 years ago

1.2.37

2 years ago

1.2.31

3 years ago

1.2.32

3 years ago

1.2.27

3 years ago

1.2.28

3 years ago

1.2.29

3 years ago

1.2.30

3 years ago

1.2.25

3 years ago

1.2.26

3 years ago

1.2.23

3 years ago

1.2.24

3 years ago

1.2.22

3 years ago

1.2.21

3 years ago

1.2.18

3 years ago

1.2.19

3 years ago

1.2.20

3 years ago

1.2.16

3 years ago

1.2.17

3 years ago

1.2.15

3 years ago

1.2.12

3 years ago

1.2.13

3 years ago

1.2.14

3 years ago

1.2.10

3 years ago

1.2.11

3 years ago

1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.56

3 years ago

1.1.55

3 years ago

1.1.54

3 years ago

1.1.52

3 years ago

1.1.53

3 years ago

1.1.51

3 years ago

1.1.50

3 years ago

1.1.49

3 years ago

1.1.47

3 years ago

1.1.46

3 years ago

1.1.45

3 years ago

1.1.44

3 years ago

1.1.43

3 years ago

1.1.39

3 years ago

1.1.41

3 years ago

1.1.40

3 years ago

1.1.42

3 years ago

1.1.38

3 years ago

1.1.37

3 years ago

1.1.36

3 years ago

1.1.34

3 years ago

1.1.35

3 years ago

1.1.29

3 years ago

1.1.30

3 years ago

1.1.33

3 years ago

1.1.32

3 years ago

1.1.31

3 years ago

1.1.28

3 years ago

1.1.27

3 years ago

1.1.26

3 years ago

1.1.25

3 years ago

1.1.24

3 years ago

1.1.23

3 years ago

1.1.22

3 years ago

1.1.21

3 years ago

1.1.20

3 years ago

1.1.19

3 years ago

1.1.18

3 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago