Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
mwamedacen committed Jul 30, 2021
1 parent b312497 commit 4cb67a1
Show file tree
Hide file tree
Showing 20 changed files with 492 additions and 354 deletions.
6 changes: 2 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export {
SwapSide,
} from 'paraswap-core';
export { SwapSide } from 'paraswap-core';

export const ETHER_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';

export const BUY_NOT_SUPPORTED_ERRROR = `Buy not supported`
export const BUY_NOT_SUPPORTED_ERRROR = `Buy not supported`;
147 changes: 85 additions & 62 deletions src/dex/curve-v2.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Interface, JsonFragment } from "@ethersproject/abi";
import { SwapSide } from "../constants";
import { AdapterExchangeParam, Address, NumberAsString, SimpleExchangeParam } from "../types";
import { IDex } from "./idex";
import { SimpleExchange } from "./simple-exchange";
import { Interface, JsonFragment } from '@ethersproject/abi';
import { SwapSide } from '../constants';
import {
AdapterExchangeParam,
Address,
NumberAsString,
SimpleExchangeParam,
} from '../types';
import { IDex } from './idex';
import { SimpleExchange } from './simple-exchange';
import CurveV2ABI from '../abi/CurveV2.json';
import { BUY_NOT_SUPPORTED_ERRROR } from "../constants";
import { isETHAddress } from "../utils";
import type {CurveData} from './curve'
import { BUY_NOT_SUPPORTED_ERRROR } from '../constants';
import { isETHAddress } from '../utils';
import type { CurveData } from './curve';

type CurveV2Data = Omit<CurveData, 'deadline' | 'v3'>;

Expand All @@ -15,69 +20,87 @@ type CurveV2Param = [
j: NumberAsString,
dx: NumberAsString,
min_dy: NumberAsString,
ethDeposit?: boolean
]
ethDeposit?: boolean,
];

enum CurveSwapFunctions {
exchange= 'exchange',
exchange_underlying = 'exchange_underlying'
exchange = 'exchange',
exchange_underlying = 'exchange_underlying',
}

export class CurveV2 extends SimpleExchange
implements IDex<CurveV2Data, CurveV2Param> {
protected dexKey = [
'curvev2'
];
exchangeRouterInterface: Interface;
minConversionRate = '1';
export class CurveV2
extends SimpleExchange
implements IDex<CurveV2Data, CurveV2Param>
{
protected dexKey = ['curvev2'];
exchangeRouterInterface: Interface;
minConversionRate = '1';

constructor(augustusAddress: Address){
super(augustusAddress);
this.exchangeRouterInterface = new Interface(CurveV2ABI as JsonFragment[]);
}
constructor(augustusAddress: Address) {
super(augustusAddress);
this.exchangeRouterInterface = new Interface(CurveV2ABI as JsonFragment[]);
}

getAdapterParam(srcToken: string, destToken: string, srcAmount: string, destAmount: string, data: CurveV2Data, side: SwapSide): AdapterExchangeParam {
if(side !== SwapSide.BUY) throw BUY_NOT_SUPPORTED_ERRROR
getAdapterParam(
srcToken: string,
destToken: string,
srcAmount: string,
destAmount: string,
data: CurveV2Data,
side: SwapSide,
): AdapterExchangeParam {
if (side !== SwapSide.BUY) throw BUY_NOT_SUPPORTED_ERRROR;

let payload

let payload;

const { i, j, underlyingSwap } = data;
payload = this.abiCoder.encodeParameter(
{
ParentStruct: {
i: 'uint256',
j: 'uint256',
underlyingSwap: 'bool',
},
},
{ i, j, underlyingSwap },
);
const { i, j, underlyingSwap } = data;
payload = this.abiCoder.encodeParameter(
{
ParentStruct: {
i: 'uint256',
j: 'uint256',
underlyingSwap: 'bool',
},
},
{ i, j, underlyingSwap },
);

return {
targetExchange: data.exchange,
payload,
networkFee: '0',
};
}
return {
targetExchange: data.exchange,
payload,
networkFee: '0',
};
}

getSimpleParam(srcToken: string, destToken: string, srcAmount: string, destAmount: string, data: CurveV2Data, side: SwapSide): SimpleExchangeParam {
if(side !== SwapSide.BUY) throw BUY_NOT_SUPPORTED_ERRROR
getSimpleParam(
srcToken: string,
destToken: string,
srcAmount: string,
destAmount: string,
data: CurveV2Data,
side: SwapSide,
): SimpleExchangeParam {
if (side !== SwapSide.BUY) throw BUY_NOT_SUPPORTED_ERRROR;

const { exchange, i, j, underlyingSwap } = data;
const defaultArgs: CurveV2Param = [i, j, srcAmount, this.minConversionRate];
// Only non underlyingSwaps in mainnet have an option to directly deposit ETH
if (!underlyingSwap && isETHAddress(srcToken)) defaultArgs.push(true);
const swapMethod = underlyingSwap ? CurveSwapFunctions.exchange_underlying : CurveSwapFunctions.exchange;
const swapData = this.exchangeRouterInterface.encodeFunctionData(swapMethod, defaultArgs);
const { exchange, i, j, underlyingSwap } = data;
const defaultArgs: CurveV2Param = [i, j, srcAmount, this.minConversionRate];
// Only non underlyingSwaps in mainnet have an option to directly deposit ETH
if (!underlyingSwap && isETHAddress(srcToken)) defaultArgs.push(true);
const swapMethod = underlyingSwap
? CurveSwapFunctions.exchange_underlying
: CurveSwapFunctions.exchange;
const swapData = this.exchangeRouterInterface.encodeFunctionData(
swapMethod,
defaultArgs,
);

return this.buildSimpleParamWithoutWETHConversion(
srcToken,
srcAmount,
destToken,
destAmount,
swapData,
exchange,
);
}
}
return this.buildSimpleParamWithoutWETHConversion(
srcToken,
srcAmount,
destToken,
destAmount,
swapData,
exchange,
);
}
}
154 changes: 89 additions & 65 deletions src/dex/curve.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Interface, JsonFragment } from "@ethersproject/abi";
import { SwapSide } from "../constants";
import { AdapterExchangeParam, Address, NumberAsString, SimpleExchangeParam } from "../types";
import { IDex } from "./idex";
import { SimpleExchange } from "./simple-exchange";
import { Interface, JsonFragment } from '@ethersproject/abi';
import { SwapSide } from '../constants';
import {
AdapterExchangeParam,
Address,
NumberAsString,
SimpleExchangeParam,
} from '../types';
import { IDex } from './idex';
import { SimpleExchange } from './simple-exchange';
import CurveABI from '../abi/Curve.json';
import { BUY_NOT_SUPPORTED_ERRROR } from "../constants";
import { BUY_NOT_SUPPORTED_ERRROR } from '../constants';

export type CurveData = {
exchange: string;
Expand All @@ -19,75 +24,94 @@ type CurveParam = [
i: NumberAsString,
j: NumberAsString,
dx: NumberAsString,
min_dy: NumberAsString
min_dy: NumberAsString,
];

export enum CurveSwapFunctions {
exchange= 'exchange',
exchange_underlying = 'exchange_underlying'
exchange = 'exchange',
exchange_underlying = 'exchange_underlying',
}

export class Curve extends SimpleExchange
implements IDex<CurveData, CurveParam> {
protected dexKey = [
'curve',
'curve3',
'swerve',
'acryptos',
'beltfi',
'ellipsis'
];
exchangeRouterInterface: Interface;
minConversionRate = '1';
export class Curve
extends SimpleExchange
implements IDex<CurveData, CurveParam>
{
protected dexKey = [
'curve',
'curve3',
'swerve',
'acryptos',
'beltfi',
'ellipsis',
];
exchangeRouterInterface: Interface;
minConversionRate = '1';

constructor(augustusAddress: Address){
super(augustusAddress);
this.exchangeRouterInterface = new Interface(CurveABI as JsonFragment[]);
}
constructor(augustusAddress: Address) {
super(augustusAddress);
this.exchangeRouterInterface = new Interface(CurveABI as JsonFragment[]);
}

getAdapterParam(srcToken: string, destToken: string, srcAmount: string, destAmount: string, data: CurveData, side: SwapSide): AdapterExchangeParam {
if(side !== SwapSide.BUY) throw BUY_NOT_SUPPORTED_ERRROR
getAdapterParam(
srcToken: string,
destToken: string,
srcAmount: string,
destAmount: string,
data: CurveData,
side: SwapSide,
): AdapterExchangeParam {
if (side !== SwapSide.BUY) throw BUY_NOT_SUPPORTED_ERRROR;

let payload

let payload;

const { i, j, deadline, underlyingSwap, v3 } = data;
payload = this.abiCoder.encodeParameter(
{
ParentStruct: {
i: 'int128',
j: 'int128',
deadline: 'uint256',
underlyingSwap: 'bool',
v3: 'bool',
},
},
{ i, j, deadline, underlyingSwap, v3 },
);

const { i, j, deadline, underlyingSwap, v3 } = data;
payload = this.abiCoder.encodeParameter(
{
ParentStruct: {
i: 'int128',
j: 'int128',
deadline: 'uint256',
underlyingSwap: 'bool',
v3: 'bool',
},
},
{ i, j, deadline, underlyingSwap, v3 },
);

return {
targetExchange: data.exchange,
payload,
networkFee: '0',
};
}
return {
targetExchange: data.exchange,
payload,
networkFee: '0',
};
}

getSimpleParam(srcToken: string, destToken: string, srcAmount: string, destAmount: string, data: CurveData, side: SwapSide): SimpleExchangeParam {
if(side !== SwapSide.BUY) throw BUY_NOT_SUPPORTED_ERRROR
getSimpleParam(
srcToken: string,
destToken: string,
srcAmount: string,
destAmount: string,
data: CurveData,
side: SwapSide,
): SimpleExchangeParam {
if (side !== SwapSide.BUY) throw BUY_NOT_SUPPORTED_ERRROR;

const { exchange, i, j, underlyingSwap } = data;
const defaultArgs = [i, j, srcAmount, this.minConversionRate];
const swapMethod = underlyingSwap ? CurveSwapFunctions.exchange_underlying : CurveSwapFunctions.exchange;
const swapData = this.exchangeRouterInterface.encodeFunctionData(swapMethod, defaultArgs);
const { exchange, i, j, underlyingSwap } = data;
const defaultArgs = [i, j, srcAmount, this.minConversionRate];
const swapMethod = underlyingSwap
? CurveSwapFunctions.exchange_underlying
: CurveSwapFunctions.exchange;
const swapData = this.exchangeRouterInterface.encodeFunctionData(
swapMethod,
defaultArgs,
);

return this.buildSimpleParamWithoutWETHConversion(
srcToken,
srcAmount,
destToken,
destAmount,
swapData,
exchange,
);
}
}
return this.buildSimpleParamWithoutWETHConversion(
srcToken,
srcAmount,
destToken,
destAmount,
swapData,
exchange,
);
}
}
13 changes: 4 additions & 9 deletions src/dex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ import { StablePool } from './stable-pool';
import { UniswapV2 } from './uniswap-v2';
import { ZeroX } from './zerox';

const dexes = [
UniswapV2,
Curve,
CurveV2,
StablePool,
ZeroX
];
const dexes = [UniswapV2, Curve, CurveV2, StablePool, ZeroX];

export function getDexMap(
augustusAddress: Address,
Expand All @@ -30,9 +24,10 @@ export function getDexMap(
) => IDex<any, any>,
) => {
const dexObj = new dex(augustusAddress, network, provider);
dexObj.getDEXKey().forEach(dexKey => { // temp: move to findDexByKey instead
dexObj.getDEXKey().forEach(dexKey => {
// temp: move to findDexByKey instead
acc[dexKey] = dexObj;
})
});
return acc;
},
{},
Expand Down
Loading

0 comments on commit 4cb67a1

Please sign in to comment.