Skip to content

Commit

Permalink
Update Smart Router with BNB Chain (#198) (#201)
Browse files Browse the repository at this point in the history
* Update Smart Router with BNB Chain (#198)

* Refactor SWAP_ROUTER_02_ADDRESSES to support BNB

* Rmeove unused address

* Create separate bsc integration suite

* Make BUSD the default gas token for BSC

* Bump UR sdk version to 1.3.8 to get BSC deployment

* Fix caching token provider

* Remove BUSD from usdTokens and fix integ tests to use new pairs

* Move USDT in front of USDC

---------

Co-authored-by: ILIA.eth <[email protected]>
  • Loading branch information
zhongeric and ilyamk authored Mar 1, 2023
1 parent 86fe325 commit 9245baf
Show file tree
Hide file tree
Showing 23 changed files with 371 additions and 49 deletions.
17 changes: 7 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [3.6.0](https://github.com/Uniswap/smart-order-router/compare/v3.5.0...v3.6.0) (2023-02-24)

### [3.0.1](https://github.com/Uniswap/smart-order-router/compare/v2.10.2...v3.0.1) (2022-11-20)

### [2.10.2](https://github.com/Uniswap/smart-order-router/compare/v2.10.1...v2.10.2) (2022-11-08)
Expand All @@ -10,10 +12,9 @@ All notable changes to this project will be documented in this file. See [standa

## [2.10.0](https://github.com/Uniswap/smart-order-router/compare/v2.9.3...v2.10.0) (2022-09-09)


### Features

* add network context to logs ([#163](https://github.com/Uniswap/smart-order-router/issues/163)) ([d686a14](https://github.com/Uniswap/smart-order-router/commit/d686a14a0d0cf248caf29bc1c0384c4d84719475))
- add network context to logs ([#163](https://github.com/Uniswap/smart-order-router/issues/163)) ([d686a14](https://github.com/Uniswap/smart-order-router/commit/d686a14a0d0cf248caf29bc1c0384c4d84719475))

### [2.9.3](https://github.com/Uniswap/smart-order-router/compare/v2.9.2...v2.9.3) (2022-08-17)

Expand All @@ -27,31 +28,27 @@ All notable changes to this project will be documented in this file. See [standa

## [2.8.0](https://github.com/Uniswap/smart-order-router/compare/v2.6.1...v2.8.0) (2022-07-20)


### Features

* add stale config ([#116](https://github.com/Uniswap/smart-order-router/issues/116)) ([3c4a32f](https://github.com/Uniswap/smart-order-router/commit/3c4a32fc671ee1b57c48f494090caede2d5f776f))
- add stale config ([#116](https://github.com/Uniswap/smart-order-router/issues/116)) ([3c4a32f](https://github.com/Uniswap/smart-order-router/commit/3c4a32fc671ee1b57c48f494090caede2d5f776f))

## [2.7.0](https://github.com/Uniswap/smart-order-router/compare/v2.6.1...v2.7.0) (2022-07-14)


### Features

* add stale config ([#116](https://github.com/Uniswap/smart-order-router/issues/116)) ([3c4a32f](https://github.com/Uniswap/smart-order-router/commit/3c4a32fc671ee1b57c48f494090caede2d5f776f))
- add stale config ([#116](https://github.com/Uniswap/smart-order-router/issues/116)) ([3c4a32f](https://github.com/Uniswap/smart-order-router/commit/3c4a32fc671ee1b57c48f494090caede2d5f776f))

### [2.6.1](https://github.com/Uniswap/smart-order-router/compare/v2.6.0...v2.6.1) (2022-07-12)

## [2.6.0](https://github.com/Uniswap/smart-order-router/compare/v2.5.32...v2.6.0) (2022-07-07)


### Features

* export TokenValidatorProvider ([#110](https://github.com/Uniswap/smart-order-router/issues/110)) ([53c6bef](https://github.com/Uniswap/smart-order-router/commit/53c6befc4a78f814a80d510896e4c15da527151c))

- export TokenValidatorProvider ([#110](https://github.com/Uniswap/smart-order-router/issues/110)) ([53c6bef](https://github.com/Uniswap/smart-order-router/commit/53c6befc4a78f814a80d510896e4c15da527151c))

### Bug Fixes

* update celo contract addresses ([#109](https://github.com/Uniswap/smart-order-router/issues/109)) ([121e9ba](https://github.com/Uniswap/smart-order-router/commit/121e9ba5fd37afaaeea65d0e92e9fdd599d7cbb2))
- update celo contract addresses ([#109](https://github.com/Uniswap/smart-order-router/issues/109)) ([121e9ba](https://github.com/Uniswap/smart-order-router/commit/121e9ba5fd37afaaeea65d0e92e9fdd599d7cbb2))

### [2.5.32](https://github.com/Uniswap/smart-order-router/compare/v2.5.31...v2.5.32) (2022-07-05)

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ JSON_RPC_PROVIDER_POLYGON = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_POLYGON_MUMBAI = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_CELO = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_CELO_ALFAJORES = '<JSON_RPC_PROVIDER>'
JSON_RPC_PROVIDER_BSC = '<JSON_RPC_PROVIDER>'
```

Then from the root directory you can execute the CLI.
Expand Down Expand Up @@ -195,6 +196,12 @@ Total ticks crossed: 7
./bin/cli quote --tokenIn CELO --tokenOut 0x765DE816845861e75A25fCA122bb6898B8B1282a --amount 5 --exactIn --minSplits 1 --protocols v3 --router alpha --chainId 42220
```

## BSC Mainnet

```
./bin/cli quote --tokenIn 0x55d398326f99059fF775485246999027B3197955 --tokenOut 0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d --amount 1 --exactIn --minSplits 1 --protocols v3 --router alpha --chainId 56
```

## Adding a new Chain

The main components to complete are:
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uniswap/smart-order-router",
"version": "3.5.0",
"version": "3.6.0",
"description": "Uniswap Smart Order Router",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down Expand Up @@ -37,7 +37,7 @@
"@uniswap/swap-router-contracts": "^1.3.0",
"@uniswap/token-lists": "^1.0.0-beta.25",
"@uniswap/universal-router": "^1.0.1",
"@uniswap/universal-router-sdk": "^1.3.0",
"@uniswap/universal-router-sdk": "^1.3.8",
"@uniswap/v2-sdk": "^3.0.1",
"@uniswap/v3-sdk": "^3.7.0",
"async-retry": "^1.3.1",
Expand Down
1 change: 1 addition & 0 deletions src/providers/caching-token-list-provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { Token } from '@uniswap/sdk-core';
import { TokenInfo, TokenList } from '@uniswap/token-lists';
import axios from 'axios';
Expand Down
15 changes: 15 additions & 0 deletions src/providers/caching-token-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { ChainId, log, WRAPPED_NATIVE_CURRENCY } from '../util';

import { ICache } from './cache';
import {
BTC_BSC,
BUSD_BSC,
CELO,
CELO_ALFAJORES,
CEUR_CELO,
CUSD_CELO,
CUSD_CELO_ALFAJORES,
DAI_ARBITRUM,
DAI_ARBITRUM_RINKEBY,
DAI_BSC,
DAI_CELO,
DAI_CELO_ALFAJORES,
DAI_MAINNET,
Expand All @@ -21,12 +24,14 @@ import {
DAI_POLYGON_MUMBAI,
DAI_RINKEBY_1,
DAI_RINKEBY_2,
ETH_BSC,
ITokenProvider,
TokenAccessor,
UNI_ARBITRUM_RINKEBY,
USDC_ARBITRUM,
USDC_ARBITRUM_GOERLI,
USDC_ARBITRUM_RINKEBY,
USDC_BSC,
USDC_ETHEREUM_GNOSIS,
USDC_MAINNET,
USDC_MOONBEAM,
Expand All @@ -35,6 +40,7 @@ import {
USDC_POLYGON,
USDT_ARBITRUM,
USDT_ARBITRUM_RINKEBY,
USDT_BSC,
USDT_MAINNET,
USDT_OPTIMISM,
USDT_OPTIMISTIC_KOVAN,
Expand Down Expand Up @@ -130,6 +136,15 @@ export const CACHE_SEED_TOKENS: {
WBTC: WBTC_MOONBEAM,
WGLMR: WRAPPED_NATIVE_CURRENCY[ChainId.MOONBEAM],
},
[ChainId.BSC]: {
USDC: USDC_BSC,
USDT: USDT_BSC,
BUSD: BUSD_BSC,
ETH: ETH_BSC,
DAI: DAI_BSC,
BTC: BTC_BSC,
WBNB: WRAPPED_NATIVE_CURRENCY[ChainId.BSC],
},

// Currently we do not have providers for Moonbeam mainnet or Gnosis testnet
};
Expand Down
11 changes: 8 additions & 3 deletions src/providers/simulation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { BigNumber } from 'ethers/lib/ethers';
import { SwapOptions, SwapRoute, SwapType } from '../routers';
import { Erc20__factory } from '../types/other/factories/Erc20__factory';
import { Permit2__factory } from '../types/other/factories/Permit2__factory';
import { ChainId, CurrencyAmount, log, SWAP_ROUTER_02_ADDRESS } from '../util';
import {
ChainId,
CurrencyAmount,
log,
SWAP_ROUTER_02_ADDRESSES,
} from '../util';

import { ProviderConfig } from './provider';
import { ArbitrumGasData, OptimismGasData } from './v3/gas-data-provider';
Expand Down Expand Up @@ -174,7 +179,7 @@ export abstract class Simulator {
await permit2Contract.allowance(
fromAddress,
inputAmount.currency.wrapped.address,
SWAP_ROUTER_02_ADDRESS
SWAP_ROUTER_02_ADDRESSES(this.chainId)
);

const nowTimestampS = Math.round(Date.now() / 1000);
Expand Down Expand Up @@ -211,7 +216,7 @@ export abstract class Simulator {

const allowance = await tokenContract.allowance(
fromAddress,
SWAP_ROUTER_02_ADDRESS
SWAP_ROUTER_02_ADDRESSES(this.chainId)
);
const hasAllowance = allowance.gte(
BigNumber.from(inputAmount.quotient.toString())
Expand Down
9 changes: 6 additions & 3 deletions src/providers/swap-router-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ApprovalTypes } from '@uniswap/router-sdk';
import { Currency, CurrencyAmount } from '@uniswap/sdk-core';

import { SwapRouter02__factory } from '../types/other/factories/SwapRouter02__factory';
import { log, SWAP_ROUTER_02_ADDRESS } from '../util';
import { ChainId, log, SWAP_ROUTER_02_ADDRESSES } from '../util';

import { IMulticallProvider } from './multicall-provider';

Expand Down Expand Up @@ -32,7 +32,10 @@ export interface ISwapRouterProvider {
}

export class SwapRouterProvider implements ISwapRouterProvider {
constructor(protected multicall2Provider: IMulticallProvider) {}
constructor(
protected multicall2Provider: IMulticallProvider,
protected chainId: ChainId
) {}

public async getApprovalType(
tokenInAmount: CurrencyAmount<Currency>,
Expand All @@ -54,7 +57,7 @@ export class SwapRouterProvider implements ISwapRouterProvider {
[string, string],
[ApprovalTypes]
>({
address: SWAP_ROUTER_02_ADDRESS,
address: SWAP_ROUTER_02_ADDRESSES(this.chainId),
contractInterface: SwapRouter02__factory.createInterface(),
functionName: 'getApprovalType',
functionParams,
Expand Down
4 changes: 2 additions & 2 deletions src/providers/tenderly-simulation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { BigNumber } from 'ethers/lib/ethers';
import { SwapOptions, SwapRoute, SwapType } from '../routers';
import { Erc20__factory } from '../types/other/factories/Erc20__factory';
import { Permit2__factory } from '../types/other/factories/Permit2__factory';
import { ChainId, log, MAX_UINT160, SWAP_ROUTER_02_ADDRESS } from '../util';
import { ChainId, log, MAX_UINT160, SWAP_ROUTER_02_ADDRESSES } from '../util';
import { APPROVE_TOKEN_FOR_TRANSFER } from '../util/callData';
import {
calculateGasUsed,
Expand Down Expand Up @@ -316,7 +316,7 @@ export class TenderlySimulator extends Simulator {
const swap = {
network_id: chainId,
input: calldata,
to: SWAP_ROUTER_02_ADDRESS,
to: SWAP_ROUTER_02_ADDRESSES(chainId),
gas_estimate: true,
value: currencyIn.isNative ? swapRoute.methodParameters.value : '0',
from: fromAddress,
Expand Down
55 changes: 55 additions & 0 deletions src/providers/token-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,55 @@ export const WETH_POLYGON_MUMBAI = new Token(
'Wrapped Ether'
);

// BNB chain Tokens
export const BTC_BSC = new Token(
ChainId.BSC,
'0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c',
18,
'BTCB',
'Binance BTC'
);

export const BUSD_BSC = new Token(
ChainId.BSC,
'0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56',
18,
'BUSD',
'BUSD'
);

export const DAI_BSC = new Token(
ChainId.BSC,
'0x1AF3F329e8BE154074D8769D1FFa4eE058B1DBc3',
18,
'DAI',
'DAI'
);

export const ETH_BSC = new Token(
ChainId.BSC,
'0x2170Ed0880ac9A755fd29B2688956BD959F933F8',
18,
'ETH',
'ETH'
);

export const USDC_BSC = new Token(
ChainId.BSC,
'0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d',
18,
'USDC',
'USDC'
);

export const USDT_BSC = new Token(
ChainId.BSC,
'0x55d398326f99059fF775485246999027B3197955',
18,
'USDT',
'USDT'
);

// Celo Tokens
export const CELO = new Token(
ChainId.CELO,
Expand Down Expand Up @@ -655,6 +704,8 @@ export const DAI_ON = (chainId: ChainId): Token => {
return DAI_CELO_ALFAJORES;
case ChainId.MOONBEAM:
return DAI_MOONBEAM;
case ChainId.BSC:
return DAI_BSC;
default:
throw new Error(`Chain id: ${chainId} not supported`);
}
Expand All @@ -680,6 +731,8 @@ export const USDT_ON = (chainId: ChainId): Token => {
return USDT_ARBITRUM;
case ChainId.ARBITRUM_RINKEBY:
return USDT_ARBITRUM_RINKEBY;
case ChainId.BSC:
return USDT_BSC;
default:
throw new Error(`Chain id: ${chainId} not supported`);
}
Expand Down Expand Up @@ -715,6 +768,8 @@ export const USDC_ON = (chainId: ChainId): Token => {
return USDC_ETHEREUM_GNOSIS;
case ChainId.MOONBEAM:
return USDC_MOONBEAM;
case ChainId.BSC:
return USDC_BSC;
default:
throw new Error(`Chain id: ${chainId} not supported`);
}
Expand Down
1 change: 1 addition & 0 deletions src/providers/v2/static-subgraph-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
[ChainId.CELO_ALFAJORES]: [],
[ChainId.GNOSIS]: [],
[ChainId.MOONBEAM]: [],
[ChainId.BSC]: [],
};

/**
Expand Down
Loading

0 comments on commit 9245baf

Please sign in to comment.