npm package: @owen05/dodo-sdk
Description: An SDK for building applications on top of dodo
Author: owen05
Latest version: 0.1.18

# dodo-sdk

## npm

- link：https://www.npmjs.com/package/@owen05/dodo-sdk
- install: npm i @owen05/dodo-sdk@latest

## example

```
const Route = require("@owen05/dodo-sdk").Route;

main();

async function main() {
  /**
   * @notice 初始化构造参数 
   * @param  networkId  主网 1 Kovan 42  BSC 56
   * @param  DODOProxyV1      可选 0xa7bE5627aFF05e464f0F996111C21Bbd5535C509
   * @param  DODOApprove   可选 0x4eC851895d85bfa6835241b3157ae10FfFD3BebC
   */
  var dodoSdk = new Route(1);
  /**
   * @notice 获取token信息 
   * @param  tokenAddr    
   * @param  web3Provider          可选
   */
  var tokenAddr = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
  var tokenObj = await dodoSdk.GetTokenByAddr(tokenAddr);
  console.log("tokenObj", tokenObj);

  /**
   * @notice 获取默认tokenList
   */
  var tokenList = dodoSdk.GetTokenList();

  /**
   * @notice 获取订单历史
   * @param  userAddr
   * @param  fromTokenAddress  可选
   * @param  toTokenAddress    可选
   * @param  gapBlockNum       订单的区块高度区间（默认 30 * 6000, 30天） 可选
   * @param  web3Provider      可选
   */
  var userAddr = "0xaac153c1344cA14497A5dd22b1F70C28793625aa"
  var fromTokenAddress = "0xdAC17F958D2ee523a2206206994597C13D831ec7"
  var orders = await dodoSdk.GetUserOrderHistory(userAddr, fromTokenAddress)
  console.log("orders", orders);


  /**
   * @notice 查询路由
   * @param fromToken    {"symbol","name","decimals","address"}
   * @param toToken      {"symbol","name","decimals","address"}
   * @param swapAmount   (不考虑decimals)
   */
  var fromToken = tokenList['DODO']
  var toToken = tokenList['ETH']
  var swapAmount = 1
  var routeQuoteDetail = await dodoSdk.GetRoute(fromToken, toToken, swapAmount)
  console.log("routeQuoteDetail:", routeQuoteDetail);

  /**
   * @notice 构造路由请求至合约
   * @param fromToken    {"symbol","name","decimals","address"}
   * @param toToken      {"symbol","name","decimals","address"}
   * @param swapAmount   (不考虑decimals)
   * @param slippage     %
   * @param addr         账户地址
   * @param deadline     截止时间戳 单位 秒
   */
  var fromToken = tokenList['DODO']
  var toToken = tokenList['ETH']
  var swapAmount = 1
  var slippage = 5
  var addr = "0xaac153c1344cA14497A5dd22b1F70C28793625aa" //当前用户地址
  var deadline = Math.floor(new Date().getTime() / 1000 + 60 * 10)
  var source = routeQuoteDetail.useSource
  var routeDetail = await dodoSdk.MakeRoute(fromToken, toToken, swapAmount, slippage, addr, deadline, source)
  console.log("routeDetail:", routeDetail);


  /**
   * @notice 判断Swap的token是否需要Approve
   * @param  fromToken            {"symbol","name","decimals","address"}     
   * @param  accountAddr          用户地址 
   * @param  targetAddr           授权地址
   * @param  web3Provider         可选
   */
  var fromToken = tokenList['DODO'];
  var accountAddr = "0xaac153c1344cA14497A5dd22b1F70C28793625aa";
  var targetAddr = routeDetail.targetApproveAddr;
  var approveAmount = await dodoSdk.ApproveJudge(fromToken, accountAddr, targetAddr);
  console.log("curApproveAmount", approveAmount);

  /**
   * @notice ERC20 Token 交易授权 Data
   * @param  fromToken      {"symbol","name","decimals","address"}     
   * @param  targetAddr     授权合约地址
   * @param  approveAmount  授权数值
   * @param  web3Provider  可选
   */
  var fromToken = tokenList['USDC'];
  var targetAddr = routeDetail.targetApproveAddr;
  var approveAmount = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
  var approveData = await dodoSdk.ApproveHandle(fromToken, targetAddr, approveAmount);
  console.log("ApproveData", approveData);

  /**
   * @notice Legacy 已废弃
   * @param  fromToken    {"symbol","name","decimals","address"}
   * @param  toToken      {"symbol","name","decimals","address"}
   * @param  swapAmount   (不考虑decimals)
   * @param  slippage     {slippage}%
   * @param  accountAddr   账户地址
   * @param  deadline      expire时间戳 单位：秒
   * @param  web3Provider          可选
   */
  var fromToken = tokenList['USDC'];
  var toToken = tokenList['WETH'];
  var swapAmount = 1;
  var slippage = 1;
  var accountAddr = "0xaac153c1344cA14497A5dd22b1F70C28793625aa";
  var deadline = Math.floor(new Date().getTime() / 1000 + 60 * 10);
  var routeDetail = await dodoSdk.GetRouteDetail(fromToken, toToken, swapAmount, slippage, accountAddr, deadline);
  console.log("routeDetail", routeDetail);

}

```

### 有关ETH => WETH  WETH => ETH

- tokenList 中提供ETH，直接使用即可 （其中地址为：0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE）
- ETH => other token 不需要进行Approve判断，且routeDetail.targetApproveAddr 为空
- other token => ETH 正常进行Approve判断授权，构造交易

*注：返回内容格式参考*

![](http://projectowen.oss-cn-beijing.aliyuncs.com/2020-11-19-074922.png)