Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BNB chain/network (BSC) to the Uniswap connector #326

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@types/uuid": "^8.3.4",
"@uniswap/sdk": "3.0.2",
"@uniswap/sdk-core": "^3.0.0",
"@uniswap/smart-order-router": "^2.5.26",
"@uniswap/smart-order-router": "^3.28.7",
"@uniswap/v3-core": "^1.0.0",
"@uniswap/v3-periphery": "^1.1.1",
"@uniswap/v3-sdk": "^3.7.0",
Expand Down
8 changes: 8 additions & 0 deletions src/chains/binance-smart-chain/binance-smart-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { SushiswapConfig } from '../../connectors/sushiswap/sushiswap.config';
import { ConfigManagerV2 } from '../../services/config-manager-v2';
import { OpenoceanConfig } from '../../connectors/openocean/openocean.config';
import { EVMController } from '../ethereum/evm.controllers';
import {UniswapConfig} from "../../connectors/uniswap/uniswap.config";

export class BinanceSmartChain extends EthereumBase implements Ethereumish {
private static _instances: { [name: string]: BinanceSmartChain };
Expand Down Expand Up @@ -113,6 +114,13 @@ export class BinanceSmartChain extends EthereumBase implements Ethereumish {
'binance-smart-chain',
this._chain
);
} else if (reqSpender === 'uniswap') {
spender = UniswapConfig.config.uniswapV3SmartOrderRouterAddress(
'binance-smart-chain',
this._chain
);
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress('binance-smart-chain', this._chain);
} else {
spender = reqSpender;
}
Expand Down
5 changes: 4 additions & 1 deletion src/chains/ethereum/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class Ethereum extends EthereumBase implements Ethereumish {
let spender: string;
if (reqSpender === 'uniswap') {
spender = UniswapConfig.config.uniswapV3SmartOrderRouterAddress(
this.chainName,
this._chain,
);
} else if (reqSpender === 'pancakeswap') {
Expand All @@ -196,7 +197,9 @@ export class Ethereum extends EthereumBase implements Ethereumish {
this._chain,
);
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress(this._chain);
spender = UniswapConfig.config.uniswapV3NftManagerAddress(
this.chainName,
this._chain);
} else if (reqSpender === 'carbonamm') {
spender = CarbonConfig.config.carbonContractsConfig(
'ethereum',
Expand Down
3 changes: 2 additions & 1 deletion src/chains/polygon/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ export class Polygon extends EthereumBase implements Ethereumish {
let spender: string;
if (reqSpender === 'uniswap') {
spender = UniswapConfig.config.uniswapV3SmartOrderRouterAddress(
'polygon',
this._chain
);
} else if (reqSpender === 'uniswapLP') {
spender = UniswapConfig.config.uniswapV3NftManagerAddress(this._chain);
spender = UniswapConfig.config.uniswapV3NftManagerAddress('polygon', this._chain);
} else if (reqSpender === 'quickswap') {
spender = QuickswapConfig.config.routerAddress(this._chain);
} else if (reqSpender === 'sushiswap') {
Expand Down
65 changes: 38 additions & 27 deletions src/connectors/uniswap/uniswap.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ export namespace UniswapConfig {
gasLimitEstimate: number;
ttl: number;
maximumHops: number;
uniswapV3SmartOrderRouterAddress: (network: string) => string;
uniswapV3NftManagerAddress: (network: string) => string;
uniswapV3SmartOrderRouterAddress: (chain: string, network: string) => string;
uniswapV3NftManagerAddress: (chain: string, network: string) => string;
uniswapV3FactoryAddress: (chain: string, network: string) => string;
tradingTypes: (type: string) => Array<string>;
chainType: string;
availableNetworks: Array<AvailableNetworks>;
useRouter?: boolean;
feeTier?: string;
quoterContractAddress: (network: string) => string;
quoterContractAddress: (chain: string, network: string) => string;
}

export const config: NetworkConfig = {
Expand All @@ -25,13 +26,37 @@ export namespace UniswapConfig {
),
ttl: ConfigManagerV2.getInstance().get(`uniswap.ttl`),
maximumHops: ConfigManagerV2.getInstance().get(`uniswap.maximumHops`),
uniswapV3SmartOrderRouterAddress: (network: string) =>
uniswapV3SmartOrderRouterAddress: (chain: string, network: string) =>
ConfigManagerV2.getInstance().get(
`uniswap.contractAddresses.${network}.uniswapV3SmartOrderRouterAddress`
'uniswap.contractAddresses.' +
chain +
'.' +
network +
'.uniswapV3SmartOrderRouterAddress'
),
uniswapV3NftManagerAddress: (network: string) =>
uniswapV3NftManagerAddress: (chain: string, network: string) =>
ConfigManagerV2.getInstance().get(
`uniswap.contractAddresses.${network}.uniswapV3NftManagerAddress`
'uniswap.contractAddresses.' +
chain +
'.' +
network +
'.uniswapV3NftManagerAddress'
),
uniswapV3FactoryAddress: (chain: string, network: string) =>
ConfigManagerV2.getInstance().get(
'uniswap.contractAddresses.' +
chain +
'.' +
network +
'.uniswapV3FactoryAddress'
),
quoterContractAddress: (chain: string, network: string) =>
ConfigManagerV2.getInstance().get(
'uniswap.contractAddresses.' +
chain +
'.' +
network +
'.uniswapV3QuoterV2ContractAddress'
),
tradingTypes: (type: string) => {
return type === 'swap' ? ['AMM'] : ['AMM_LP'];
Expand All @@ -40,30 +65,16 @@ export namespace UniswapConfig {
availableNetworks: [
{
chain: 'ethereum',
networks: Object.keys(
ConfigManagerV2.getInstance().get('uniswap.contractAddresses')
).filter((network) =>
Object.keys(
ConfigManagerV2.getInstance().get('ethereum.networks')
).includes(network)
),
networks: ['mainnet', 'goerli', 'arbitrum', 'optimism'],
},
{
chain: 'polygon',
networks: Object.keys(
ConfigManagerV2.getInstance().get('uniswap.contractAddresses')
).filter((network) =>
Object.keys(
ConfigManagerV2.getInstance().get('polygon.networks')
).includes(network)
),
{ chain: 'polygon',
networks: ['mainnet', 'mumbai']
},
{ chain: 'binance-smart-chain',
networks: ['mainnet']
},
],
useRouter: ConfigManagerV2.getInstance().get(`uniswap.useRouter`),
feeTier: ConfigManagerV2.getInstance().get(`uniswap.feeTier`),
quoterContractAddress: (network: string) =>
ConfigManagerV2.getInstance().get(
`uniswap.contractAddresses.${network}.uniswapV3QuoterV2ContractAddress`
),
};
}
4 changes: 2 additions & 2 deletions src/connectors/uniswap/uniswap.lp.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export class UniswapLPHelper {
provider: this.ethereum.provider,
});
this._router =
UniswapConfig.config.uniswapV3SmartOrderRouterAddress(network);
this._nftManager = UniswapConfig.config.uniswapV3NftManagerAddress(network);
UniswapConfig.config.uniswapV3SmartOrderRouterAddress(chain, network);
this._nftManager = UniswapConfig.config.uniswapV3NftManagerAddress(chain, network);
this._ttl = UniswapConfig.config.ttl;
this._routerAbi =
require('@uniswap/v3-periphery/artifacts/contracts/SwapRouter.sol/SwapRouter.json').abi;
Expand Down
16 changes: 10 additions & 6 deletions src/connectors/uniswap/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
SwapQuoter,
Trade as UniswapV3Trade,
Route,
FACTORY_ADDRESS,
} from '@uniswap/v3-sdk';
import { abi as IUniswapV3PoolABI } from '@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json';
import { abi as IUniswapV3FactoryABI } from '@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Factory.sol/IUniswapV3Factory.json';
Expand All @@ -38,15 +37,17 @@ import { logger } from '../../services/logger';
import { percentRegexp } from '../../services/config-manager-v2';
import { Ethereum } from '../../chains/ethereum/ethereum';
import { Polygon } from '../../chains/polygon/polygon';
import { BinanceSmartChain } from "../../chains/binance-smart-chain/binance-smart-chain";
import { ExpectedTrade, Uniswapish } from '../../services/common-interfaces';
import { getAddress } from 'ethers/lib/utils';

export class Uniswap implements Uniswapish {
private static _instances: { [name: string]: Uniswap };
private chain: Ethereum | Polygon;
private chain: Ethereum | Polygon | BinanceSmartChain;
private _alphaRouter: AlphaRouter;
private _router: string;
private _routerAbi: ContractInterface;
private _v3Factory: string;
private _gasLimitEstimate: number;
private _ttl: number;
private _maximumHops: number;
Expand All @@ -61,8 +62,10 @@ export class Uniswap implements Uniswapish {
const config = UniswapConfig.config;
if (chain === 'ethereum') {
this.chain = Ethereum.getInstance(network);
} else {
} else if (chain === 'polygon') {
this.chain = Polygon.getInstance(network);
} else {
this.chain = BinanceSmartChain.getInstance(network);
}
this.chainId = this.chain.chainId;
this._ttl = UniswapConfig.config.ttl;
Expand All @@ -73,7 +76,8 @@ export class Uniswap implements Uniswapish {
});
this._routerAbi = routerAbi.abi;
this._gasLimitEstimate = UniswapConfig.config.gasLimitEstimate;
this._router = config.uniswapV3SmartOrderRouterAddress(network);
this._router = config.uniswapV3SmartOrderRouterAddress(chain, network);
this._v3Factory = config.uniswapV3FactoryAddress(chain, network);

if (config.useRouter === false && config.feeTier == null) {
throw new Error('Must specify fee tier if not using router');
Expand All @@ -87,7 +91,7 @@ export class Uniswap implements Uniswapish {
this._feeTier = config.feeTier
? FeeAmount[config.feeTier as keyof typeof FeeAmount]
: FeeAmount.MEDIUM;
this._quoterContractAddress = config.quoterContractAddress(network);
this._quoterContractAddress = config.quoterContractAddress(chain, network);
}

public static getInstance(chain: string, network: string): Uniswap {
Expand Down Expand Up @@ -428,7 +432,7 @@ export class Uniswap implements Uniswapish {
poolId?: string
): Promise<Pool | null> {
const uniswapFactory = new Contract(
FACTORY_ADDRESS,
this._v3Factory,
IUniswapV3FactoryABI,
this.chain.provider
);
Expand Down
4 changes: 2 additions & 2 deletions src/services/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ export async function getConnector<T>(
let connectorInstance: ConnectorUnion;

if (
(chain === 'ethereum' || chain === 'polygon') &&
(chain === 'ethereum' || chain === 'polygon' || chain === 'binance-smart-chain') &&
connector === 'uniswap'
) {
connectorInstance = Uniswap.getInstance(chain, network);
} else if (chain === 'polygon' && connector === 'quickswap') {
connectorInstance = Quickswap.getInstance(chain, network);
} else if (
(chain === 'ethereum' || chain === 'polygon') &&
(chain === 'ethereum' || chain === 'polygon' || chain === 'binance-smart-chain') &&
connector === 'uniswapLP'
) {
connectorInstance = UniswapLP.getInstance(chain, network);
Expand Down
25 changes: 16 additions & 9 deletions src/services/schema/uniswap-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@
"contractAddresses": {
"type": "object",
"patternProperties": {
"^\\w+$": {
"[\\w-]+$": {
"type": "object",
"properties": {
"uniswapV3SmartOrderRouterAddress": { "type": "string" },
"uniswapV3NftManagerAddress": { "type": "string" },
"uniswapV3QuoterV2ContractAddress": { "type": "string" }
"patternProperties": {
"^\\w+$": {
"type": "object",
"properties": {
"uniswapV3SmartOrderRouterAddress": { "type": "string" },
"uniswapV3NftManagerAddress": { "type": "string" },
"uniswapV3QuoterV2ContractAddress": { "type": "string" },
"uniswapV3FactoryAddress": { "type": "string" }
},
"required": [
"uniswapV3SmartOrderRouterAddress",
"uniswapV3NftManagerAddress"
],
"additionalProperties": false
}
},
"required": [
"uniswapV3SmartOrderRouterAddress",
"uniswapV3NftManagerAddress"
],
"additionalProperties": false
}
},
Expand Down
58 changes: 38 additions & 20 deletions src/templates/uniswap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,41 @@ useRouter: false
feeTier: 'MEDIUM'

contractAddresses:
mainnet:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
goerli:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
arbitrum:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
optimism:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
mumbai:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
ethereum:
mainnet:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
uniswapV3FactoryAddress: '0x1F98431c8aD98523631AE4a59f267346ea31F984'
goerli:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
uniswapV3FactoryAddress: '0x1F98431c8aD98523631AE4a59f267346ea31F984'
arbitrum:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
uniswapV3FactoryAddress: '0x1F98431c8aD98523631AE4a59f267346ea31F984'
optimism:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
uniswapV3FactoryAddress: '0x1F98431c8aD98523631AE4a59f267346ea31F984'
polygon:
mainnet:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
uniswapV3FactoryAddress: '0x1F98431c8aD98523631AE4a59f267346ea31F984'
mumbai:
uniswapV3SmartOrderRouterAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45'
uniswapV3NftManagerAddress: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
uniswapV3QuoterV2ContractAddress: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e'
uniswapV3FactoryAddress: '0x1F98431c8aD98523631AE4a59f267346ea31F984'
binance-smart-chain:
mainnet:
uniswapV3SmartOrderRouterAddress: '0xB971eF87ede563556b2ED4b1C0b0019111Dd85d2'
uniswapV3NftManagerAddress: '0x7b8A01B39D58278b5DE7e48c8449c9f4F5170613'
uniswapV3QuoterV2ContractAddress: '0x78D78E420Da98ad378D7799bE8f4AF69033EB077'
uniswapV3FactoryAddress: '0xdB1d10011AD0Ff90774D0C6Bb92e5C5c8b4461F7'
Loading
Loading