Skip to content

Commit

Permalink
Merge pull request paraswap#95 from paraswap/fix/dex-key-special
Browse files Browse the repository at this point in the history
Handle special dexkey for payload encoding
  • Loading branch information
shresthagrawal authored Apr 21, 2022
2 parents f2a7501 + 6c84d1a commit e0d6b44
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paraswap/dex-lib",
"version": "2.0.19",
"version": "2.0.20",
"main": "build/index.js",
"types": "build/index.d.ts",
"repository": "https://github.com/paraswap/paraswap-dex-lib",
Expand Down
52 changes: 32 additions & 20 deletions src/dex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,22 @@ export class DexAdapterService {
this.dexHelper,
);

const sellAdapters = (
const sellAdaptersDex = (
this.dexInstances[_key] as IDex<any, any, any>
).getAdapters(SwapSide.SELL);
if (sellAdapters)
this.sellAdapters[_key] = sellAdapters.map(({ name, index }) => ({
adapter: AdapterNameAddressMap[network][name],
index,
}));
if (sellAdaptersDex)
this.sellAdapters[_key] = sellAdaptersDex.map(
({ name, index }) => ({
adapter: AdapterNameAddressMap[network][name],
index,
}),
);

const buyAdapters = (
const buyAdaptersDex = (
this.dexInstances[_key] as IDex<any, any, any>
).getAdapters(SwapSide.BUY);
if (buyAdapters)
this.buyAdapters[_key] = buyAdapters.map(({ name, index }) => ({
if (buyAdaptersDex)
this.buyAdapters[_key] = buyAdaptersDex.map(({ name, index }) => ({
adapter: AdapterNameAddressMap[network][name],
index,
}));
Expand All @@ -197,17 +199,7 @@ export class DexAdapterService {
}

getTxBuilderDexByKey(dexKey: string): IDexTxBuilder<any, any> {
let _dexKey = dexKey.toLowerCase();

if (/^paraswappool(.*)/i.test(_dexKey)) _dexKey = 'zerox';

if ('uniswapforkoptimized' === _dexKey) {
if (!this.uniswapV2Alias)
throw new Error(
`${dexKey} dex is not supported for network(${this.network})!`,
);
_dexKey = this.uniswapV2Alias;
}
let _dexKey = this.getDexKeySpecial(dexKey);

if (!this.dexInstances[_dexKey]) {
const DexAdapter = this.dexToKeyMap[_dexKey];
Expand Down Expand Up @@ -245,4 +237,24 @@ export class DexAdapterService {
getAllDexAdapters(side: SwapSide = SwapSide.SELL) {
return side === SwapSide.SELL ? this.sellAdapters : this.buyAdapters;
}

getDexKeySpecial(dexKey: string) {
dexKey = dexKey.toLowerCase();
if (/^paraswappool(.*)/i.test(dexKey)) return 'zerox';
else if ('uniswapforkoptimized' === dexKey) {
if (!this.uniswapV2Alias)
throw new Error(
`${dexKey} dex is not supported for network(${this.network})!`,
);
return this.uniswapV2Alias;
}
return dexKey;
}

getAdapter(dexKey: string, side: SwapSide) {
const specialDexKey = this.getDexKeySpecial(dexKey);
return side === SwapSide.SELL
? this.sellAdapters[specialDexKey]
: this.buyAdapters[specialDexKey];
}
}
4 changes: 2 additions & 2 deletions src/router/buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class Buy extends PayloadEncoder implements IRouter<BuyParam> {
paraswapInterface: Interface;
contractMethodName: string;

constructor(dexAdapterService: DexAdapterService, adapters: Adapters) {
super(dexAdapterService, adapters);
constructor(dexAdapterService: DexAdapterService) {
super(dexAdapterService);
this.paraswapInterface = new Interface(IParaswapABI);
this.contractMethodName = 'buy';
}
Expand Down
8 changes: 1 addition & 7 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@ export class RouterService {
directSwapRouter: DirectSwap<any>;

constructor(private dexAdapterService: DexAdapterService) {
const adapters = dexAdapterService.getAllDexAdapters(SwapSide.SELL);
const buyAdapters = dexAdapterService.getAllDexAdapters(SwapSide.BUY);

this.hybridRouterMap = this.hybridRouters.reduce<{
[contractMethod: string]: IRouter<any>;
}>((acc, Router) => {
const router = new Router(
this.dexAdapterService,
Router.isBuy ? buyAdapters : adapters,
);
const router = new Router(this.dexAdapterService);
acc[router.getContractMethodName().toLowerCase()] = router;
return acc;
}, {});
Expand Down
4 changes: 2 additions & 2 deletions src/router/megaswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class MegaSwap extends PayloadEncoder implements IRouter<MegaSwapParam> {
paraswapInterface: Interface;
contractMethodName: string;

constructor(dexAdapterService: DexAdapterService, adapters: Adapters) {
super(dexAdapterService, adapters);
constructor(dexAdapterService: DexAdapterService) {
super(dexAdapterService);
this.paraswapInterface = new Interface(IParaswapABI);
this.contractMethodName = 'megaSwap';
}
Expand Down
4 changes: 2 additions & 2 deletions src/router/multiswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class MultiSwap
paraswapInterface: Interface;
contractMethodName: string;

constructor(dexAdapterService: DexAdapterService, adapters: Adapters) {
super(dexAdapterService, adapters);
constructor(dexAdapterService: DexAdapterService) {
super(dexAdapterService);
this.paraswapInterface = new Interface(IParaswapABI);
this.contractMethodName = 'multiSwap';
}
Expand Down
16 changes: 6 additions & 10 deletions src/router/payload-encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ export function encodeFeePercent(
// This class can be used commonly by all the router
// that will use the adapters.
export class PayloadEncoder {
constructor(
protected dexAdapterService: DexAdapterService,
protected adapters: Adapters,
) {}
constructor(protected dexAdapterService: DexAdapterService) {}
// Should have function for optimally choosing the Adapters
getContractPathsWithNetworkFee(swaps: OptimalSwap[]): {
paths: ContractPath[];
Expand Down Expand Up @@ -193,7 +190,7 @@ export class PayloadEncoder {

const adapterPoints: { [adapter: string]: number } = {};
swapExchanges.forEach(se => {
const adapters = this.adapters[se.exchange.toLowerCase()];
const adapters = this.dexAdapterService.getAdapter(se.exchange, side);
if (!adapters.length)
throw new Error(`No adapter found for ${se.exchange}`);
adapters.forEach(a => {
Expand All @@ -213,12 +210,11 @@ export class PayloadEncoder {
const leftSwapExchange: OptimalSwapExchange<any>[] = [];

swapExchanges.forEach(se => {
const exchangeKey = se.exchange.toLowerCase();
const adapterConfig = this.adapters[exchangeKey].find(
({ adapter }) => adapter.toLowerCase() === bestAdapter,
);
const adapterConfig = this.dexAdapterService
.getAdapter(se.exchange, side)
.find(({ adapter }) => adapter.toLowerCase() === bestAdapter);
if (adapterConfig) {
optimalAdapters[exchangeKey] = [
optimalAdapters[se.exchange.toLowerCase()] = [
adapterConfig.adapter,
adapterConfig.index,
];
Expand Down
9 changes: 4 additions & 5 deletions src/router/simpleswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ abstract class SimpleRouter implements IRouter<SimpleSwapParam> {

constructor(
protected dexAdapterService: DexAdapterService,
adapters: Adapters,
protected side: SwapSide,

// prepare mapping: network -> wrapped exchange key
Expand Down Expand Up @@ -261,14 +260,14 @@ abstract class SimpleRouter implements IRouter<SimpleSwapParam> {

export class SimpleSwap extends SimpleRouter {
static isBuy = false;
constructor(dexAdapterService: DexAdapterService, adapters: Adapters) {
super(dexAdapterService, adapters, SwapSide.SELL);
constructor(dexAdapterService: DexAdapterService) {
super(dexAdapterService, SwapSide.SELL);
}
}

export class SimpleBuy extends SimpleRouter {
static isBuy = true;
constructor(dexAdapterService: DexAdapterService, adapters: Adapters) {
super(dexAdapterService, adapters, SwapSide.BUY);
constructor(dexAdapterService: DexAdapterService) {
super(dexAdapterService, SwapSide.BUY);
}
}

0 comments on commit e0d6b44

Please sign in to comment.