Skip to content

Commit

Permalink
Merge pull request #23 from uniswap-integration/feature/support-abili…
Browse files Browse the repository at this point in the history
…ty-to-overriden-uniswap-contract-addresses

Feature/support ability to override uniswap contract addresses
  • Loading branch information
joshstevens19 authored Oct 18, 2021
2 parents c696464 + 15582f6 commit 8bebd9a
Show file tree
Hide file tree
Showing 21 changed files with 274 additions and 60 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,45 @@ export interface GasSettings {
getGasPrice: () => Promise<number>;
}

export interface CloneUniswapContractDetailsV2 {
routerAddress: string;
factoryAddress: string;
pairAddress: string;
}

export interface CloneUniswapContractDetailsV3 {
routerAddress: string;
factoryAddress: string;
quoterAddress: string;
}

export interface CloneUniswapContractDetails {
v2Override?: CloneUniswapContractDetailsV2 | undefined;
v3Override?: CloneUniswapContractDetailsV3 | undefined;
}

export class UniswapPairSettings {
slippage: number;
deadlineMinutes: number;
disableMultihops: boolean;
uniswapVersions: UniswapVersion[] = [UniswapVersion.v2, UniswapVersion.v3];
gasSettings?: GasSettings = undefined;
// can be used to pass in a fork of uniswap contract details
cloneUniswapContractDetails?: CloneUniswapContractDetails = undefined;

constructor(settings?: {
slippage?: number | undefined;
deadlineMinutes?: number | undefined;
disableMultihops?: boolean | undefined;
uniswapVersions?: UniswapVersion[] | undefined;
gasSettings?: GasSettings | undefined;
cloneUniswapContractDetails?: CloneUniswapContractDetails | undefined;
}) {
this.slippage = settings?.slippage || 0.005;
this.deadlineMinutes = settings?.deadlineMinutes || 20;
this.disableMultihops = settings?.disableMultihops || false;
this.gasSettings = settings?.gasSettings;
this.cloneUniswapContractDetails = settings?.cloneUniswapContractDetails;

if (
Array.isArray(settings?.uniswapVersions) &&
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-uniswap-sdk",
"version": "3.4.2",
"version": "3.5.0",
"description": "Simple easy to understand SDK for uniswap which looks over best v2 and v3 to find you the best swap quote",
"main": "dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
16 changes: 16 additions & 0 deletions src/factories/pair/models/clone-uniswap-contract-details.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface CloneUniswapContractDetailsV2 {
routerAddress: string;
factoryAddress: string;
pairAddress: string;
}

export interface CloneUniswapContractDetailsV3 {
routerAddress: string;
factoryAddress: string;
quoterAddress: string;
}

export interface CloneUniswapContractDetails {
v2Override?: CloneUniswapContractDetailsV2 | undefined;
v3Override?: CloneUniswapContractDetailsV3 | undefined;
}
4 changes: 4 additions & 0 deletions src/factories/pair/models/uniswap-pair-settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ErrorCodes } from '../../../common/errors/error-codes';
import { UniswapError } from '../../../common/errors/uniswap-error';
import { UniswapVersion } from '../../../enums/uniswap-version';
import { CloneUniswapContractDetails } from './clone-uniswap-contract-details';
import { GasSettings } from './gas-settings';

export class UniswapPairSettings {
Expand All @@ -9,18 +10,21 @@ export class UniswapPairSettings {
disableMultihops: boolean;
uniswapVersions: UniswapVersion[] = [UniswapVersion.v2, UniswapVersion.v3];
gasSettings?: GasSettings = undefined;
cloneUniswapContractDetails?: CloneUniswapContractDetails = undefined;

constructor(settings?: {
slippage?: number | undefined;
deadlineMinutes?: number | undefined;
disableMultihops?: boolean | undefined;
uniswapVersions?: UniswapVersion[] | undefined;
gasSettings?: GasSettings | undefined;
cloneUniswapContractDetails?: CloneUniswapContractDetails | undefined;
}) {
this.slippage = settings?.slippage || 0.005;
this.deadlineMinutes = settings?.deadlineMinutes || 20;
this.disableMultihops = settings?.disableMultihops || false;
this.gasSettings = settings?.gasSettings;
this.cloneUniswapContractDetails = settings?.cloneUniswapContractDetails;

if (
Array.isArray(settings?.uniswapVersions) &&
Expand Down
14 changes: 9 additions & 5 deletions src/factories/pair/uniswap-pair.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { deepClone } from '../../common/utils/deep-clone';
import { getTradePath } from '../../common/utils/trade-path';
import { TradePath } from '../../enums/trade-path';
import { UniswapVersion } from '../../enums/uniswap-version';
import { UniswapContractContextV2 } from '../../uniswap-contract-context/uniswap-contract-context-v2';
import { UniswapContractContextV3 } from '../../uniswap-contract-context/uniswap-contract-context-v3';
import { uniswapContracts } from '../../uniswap-contract-context/get-uniswap-contracts';
import { AllPossibleRoutes } from '../router/models/all-possible-routes';
import { BestRouteQuotes } from '../router/models/best-route-quotes';
import { RouteQuote } from '../router/models/route-quote';
Expand All @@ -30,7 +29,8 @@ import { UniswapPairFactoryContext } from './models/uniswap-pair-factory-context
export class UniswapPairFactory {
private _fromTokenFactory = new TokenFactory(
this._uniswapPairFactoryContext.fromToken.contractAddress,
this._uniswapPairFactoryContext.ethersProvider
this._uniswapPairFactoryContext.ethersProvider,
this._uniswapPairFactoryContext.settings.cloneUniswapContractDetails
);

private _toTokenFactory = new TokenFactory(
Expand Down Expand Up @@ -260,8 +260,12 @@ export class UniswapPairFactory {

const data = this._fromTokenFactory.generateApproveAllowanceData(
uniswapVersion === UniswapVersion.v2
? UniswapContractContextV2.routerAddress
: UniswapContractContextV3.routerAddress,
? uniswapContracts.v2.getRouterAddress(
this._uniswapPairFactoryContext.settings.cloneUniswapContractDetails
)
: uniswapContracts.v3.getRouterAddress(
this._uniswapPairFactoryContext.settings.cloneUniswapContractDetails
),
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import {
EthereumProvider,
EthersProvider,
} from '../../../ethers-provider';
import { UniswapContractContextV2 } from '../../../uniswap-contract-context/uniswap-contract-context-v2';
import { UniswapPairContractFactoryV2 } from './uniswap-pair-contract.factory.v2';

export class UniswapPairContractFactoryPublicV2 extends UniswapPairContractFactoryV2 {
constructor(providerContext: ChainIdAndProvider | EthereumProvider) {
super(new EthersProvider(providerContext));
constructor(
providerContext: ChainIdAndProvider | EthereumProvider,
pairAddress: string = UniswapContractContextV2.pairAddress
) {
super(new EthersProvider(providerContext), pairAddress);
}
}
14 changes: 9 additions & 5 deletions src/factories/pair/v2/uniswap-pair-contract.factory.v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import { EthersProvider } from '../../../ethers-provider';
import { UniswapContractContextV2 } from '../../../uniswap-contract-context/uniswap-contract-context-v2';

export class UniswapPairContractFactoryV2 {
private _uniswapPairFactory = this._ethersProvider.getContract<PairContractContext>(
JSON.stringify(UniswapContractContextV2.pairAbi),
UniswapContractContextV2.pairAddress
);
private _uniswapPairFactory =
this._ethersProvider.getContract<PairContractContext>(
JSON.stringify(UniswapContractContextV2.pairAbi),
this._pairAddress
);

constructor(private _ethersProvider: EthersProvider) {}
constructor(
private _ethersProvider: EthersProvider,
private _pairAddress: string = UniswapContractContextV2.pairAddress
) {}

public async allPairs(parameter0: BigNumberish): Promise<string> {
return await this._uniswapPairFactory.allPairs(parameter0);
Expand Down
48 changes: 37 additions & 11 deletions src/factories/router/uniswap-router.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { ChainId } from '../../enums/chain-id';
import { TradePath } from '../../enums/trade-path';
import { UniswapVersion } from '../../enums/uniswap-version';
import { EthersProvider } from '../../ethers-provider';
import { uniswapContracts } from '../../uniswap-contract-context/get-uniswap-contracts';
import { UniswapContractContextV2 } from '../../uniswap-contract-context/uniswap-contract-context-v2';
import { UniswapContractContextV3 } from '../../uniswap-contract-context/uniswap-contract-context-v3';
import { TradeDirection } from '../pair/models/trade-direction';
Expand Down Expand Up @@ -66,14 +67,23 @@ export class UniswapRouterFactory {
});

private _uniswapRouterContractFactoryV2 = new UniswapRouterContractFactoryV2(
this._ethersProvider
this._ethersProvider,
uniswapContracts.v2.getRouterAddress(
this._settings.cloneUniswapContractDetails
)
);

private _uniswapRouterContractFactoryV3 = new UniswapRouterContractFactoryV3(
this._ethersProvider
this._ethersProvider,
uniswapContracts.v3.getRouterAddress(
this._settings.cloneUniswapContractDetails
)
);

private _tokensFactory = new TokensFactory(this._ethersProvider);
private _tokensFactory = new TokensFactory(
this._ethersProvider,
this._settings.cloneUniswapContractDetails
);

private readonly LIQUIDITY_PROVIDER_FEE_V2 = 0.003;

Expand Down Expand Up @@ -117,7 +127,9 @@ export class UniswapRouterFactory {
if (this._settings.uniswapVersions.includes(UniswapVersion.v2)) {
contractCallContext.push({
reference: UniswapVersion.v2,
contractAddress: UniswapContractContextV2.pairAddress,
contractAddress: uniswapContracts.v2.getPairAddress(
this._settings.cloneUniswapContractDetails
),
abi: UniswapContractContextV2.pairAbi,
calls: [],
});
Expand Down Expand Up @@ -147,7 +159,9 @@ export class UniswapRouterFactory {
if (this._settings.uniswapVersions.includes(UniswapVersion.v3)) {
contractCallContext.push({
reference: UniswapVersion.v3,
contractAddress: UniswapContractContextV3.factoryAddress,
contractAddress: uniswapContracts.v3.getFactoryAddress(
this._settings.cloneUniswapContractDetails
),
abi: UniswapContractContextV3.factoryAbi,
calls: [
{
Expand Down Expand Up @@ -303,7 +317,9 @@ export class UniswapRouterFactory {
if (this._settings.uniswapVersions.includes(UniswapVersion.v2)) {
contractCallContext.push({
reference: UniswapVersion.v2,
contractAddress: UniswapContractContextV2.routerAddress,
contractAddress: uniswapContracts.v2.getRouterAddress(
this._settings.cloneUniswapContractDetails
),
abi: UniswapContractContextV2.routerAbi,
calls: [],
context: routes.v2,
Expand All @@ -328,7 +344,9 @@ export class UniswapRouterFactory {
if (this._settings.uniswapVersions.includes(UniswapVersion.v3)) {
contractCallContext.push({
reference: UniswapVersion.v3,
contractAddress: UniswapContractContextV3.quoterAddress,
contractAddress: uniswapContracts.v3.getQuoterAddress(
this._settings.cloneUniswapContractDetails
),
abi: UniswapContractContextV3.quoterAbi,
calls: [],
context: routes.v3,
Expand Down Expand Up @@ -824,8 +842,12 @@ export class UniswapRouterFactory {
return {
to:
uniswapVersion === UniswapVersion.v2
? UniswapContractContextV2.routerAddress
: UniswapContractContextV3.routerAddress,
? uniswapContracts.v2.getRouterAddress(
this._settings.cloneUniswapContractDetails
)
: uniswapContracts.v3.getRouterAddress(
this._settings.cloneUniswapContractDetails
),
from: this._ethereumAddress,
data,
value: Constants.EMPTY_HEX_STRING,
Expand All @@ -845,8 +867,12 @@ export class UniswapRouterFactory {
return {
to:
uniswapVersion === UniswapVersion.v2
? UniswapContractContextV2.routerAddress
: UniswapContractContextV3.routerAddress,
? uniswapContracts.v2.getRouterAddress(
this._settings.cloneUniswapContractDetails
)
: uniswapContracts.v3.getRouterAddress(
this._settings.cloneUniswapContractDetails
),
from: this._ethereumAddress,
data,
value: toEthersBigNumber(parseEther(ethValue)).toHexString(),
Expand Down
14 changes: 9 additions & 5 deletions src/factories/router/v2/uniswap-router-contract.factory.v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import { EthersProvider } from '../../../ethers-provider';
import { UniswapContractContextV2 } from '../../../uniswap-contract-context/uniswap-contract-context-v2';

export class UniswapRouterContractFactoryV2 {
private _uniswapRouterContract = this._ethersProvider.getContract<RouterContractContext>(
JSON.stringify(UniswapContractContextV2.routerAbi),
UniswapContractContextV2.routerAddress
);
private _uniswapRouterContract =
this._ethersProvider.getContract<RouterContractContext>(
JSON.stringify(UniswapContractContextV2.routerAbi),
this._routerAddress
);

constructor(private _ethersProvider: EthersProvider) {}
constructor(
private _ethersProvider: EthersProvider,
private _routerAddress: string = UniswapContractContextV2.routerAddress
) {}

public addLiquidity(
tokenA: string,
Expand Down
7 changes: 5 additions & 2 deletions src/factories/router/v3/uniswap-router-contract.factory.v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ export class UniswapRouterContractFactoryV3 {
private _uniswapRouterContract =
this._ethersProvider.getContract<RouterContractContext>(
JSON.stringify(UniswapContractContextV3.routerAbi),
UniswapContractContextV3.routerAddress
this._routerAddress
);

constructor(private _ethersProvider: EthersProvider) {}
constructor(
private _ethersProvider: EthersProvider,
private _routerAddress: string = UniswapContractContextV3.routerAddress
) {}

/**
* Exact input single
Expand Down
2 changes: 1 addition & 1 deletion src/factories/token/token.factory.public.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe('TokenFactoryPublic', () => {
describe('erc20', () => {
it('totalSupply', async () => {
const result = await tokenFactory.totalSupply();
expect(result).toEqual('0x0f43f0ad89c30bb6');
expect(result).toEqual('0x0f4229ebe353e7b6');
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/factories/token/token.factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('TokenFactory', () => {
describe('erc20', () => {
it('totalSupply', async () => {
const result = await tokenFactory.totalSupply();
expect(result).toEqual('0x0f43f0ad89c30bb6');
expect(result).toEqual('0x0f4229ebe353e7b6');
});
});

Expand Down
25 changes: 18 additions & 7 deletions src/factories/token/token.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { isTokenOverrideInfo } from '../../common/tokens/overrides';
import { getAddress } from '../../common/utils/get-address';
import { UniswapVersion } from '../../enums/uniswap-version';
import { EthersProvider } from '../../ethers-provider';
import { UniswapContractContextV2 } from '../../uniswap-contract-context/uniswap-contract-context-v2';
import { UniswapContractContextV3 } from '../../uniswap-contract-context/uniswap-contract-context-v3';
import { uniswapContracts } from '../../uniswap-contract-context/get-uniswap-contracts';
import { CloneUniswapContractDetails } from '../pair/models/clone-uniswap-contract-details';
import { AllowanceAndBalanceOf } from './models/allowance-balance-of';
import { Token } from './models/token';

Expand All @@ -26,7 +26,10 @@ export class TokenFactory {

constructor(
private _tokenContractAddress: string,
private _ethersProvider: EthersProvider
private _ethersProvider: EthersProvider,
private _cloneUniswapContractDetails?:
| CloneUniswapContractDetails
| undefined
) {}

/**
Expand Down Expand Up @@ -100,8 +103,12 @@ export class TokenFactory {
const allowance = await this._erc20TokenContract.allowance(
ethereumAddress,
uniswapVersion === UniswapVersion.v2
? UniswapContractContextV2.routerAddress
: UniswapContractContextV3.routerAddress
? uniswapContracts.v2.getRouterAddress(
this._cloneUniswapContractDetails
)
: uniswapContracts.v3.getRouterAddress(
this._cloneUniswapContractDetails
)
);

return allowance.toHexString();
Expand Down Expand Up @@ -216,8 +223,12 @@ export class TokenFactory {
methodParameters: [
ethereumAddress,
uniswapVersion === UniswapVersion.v2
? UniswapContractContextV2.routerAddress
: UniswapContractContextV3.routerAddress,
? uniswapContracts.v2.getRouterAddress(
this._cloneUniswapContractDetails
)
: uniswapContracts.v3.getRouterAddress(
this._cloneUniswapContractDetails
),
],
},
{
Expand Down
Loading

0 comments on commit 8bebd9a

Please sign in to comment.