Skip to content

Commit

Permalink
Merge branch 'master' into feat/BACK-840
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis-Amas committed Dec 16, 2022
2 parents b592f0b + f36fb59 commit a5c5bba
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 25 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.9.29-erc20tracker.0",
"version": "2.9.29",
"main": "build/index.js",
"types": "build/index.d.ts",
"repository": "https://github.com/paraswap/paraswap-dex-lib",
Expand Down
8 changes: 4 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
privateHttpProvider: process.env.HTTP_PROVIDER_56,
adapterAddresses: {
BscAdapter01: '0x27eb327B7255a2bF666EBB4D60AB4752dA4611b9',
BscAdapter02: '0x5e09f0F5A1d1dE32b56ab8D16A6F687ed763e0E8',
BscAdapter02: '0xBaff776E0bD50c617167Ae360448E2Ae060B99B1',
BscBuyAdapter: '0xF52523B9d788F4E2Dd256dc5077879Af0448c37A',
},
uniswapV2ExchangeRouterAddress:
Expand All @@ -160,7 +160,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
privateHttpProvider: process.env.HTTP_PROVIDER_137,
adapterAddresses: {
PolygonAdapter01: '0xE44769f42E1e9592f86B82f206407a8f7C84b4ed',
PolygonAdapter02: '0x97768fD3A529ee29B433cbb5E45E63F3bFFf0A93',
PolygonAdapter02: '0xD64d4CD829013438e1A158455801ae2f7543C75f',
PolygonBuyAdapter: '0x40e11AE88A9402A34208D05bFB7E88171d2f58a0',
},
uniswapV2ExchangeRouterAddress:
Expand Down Expand Up @@ -202,7 +202,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
multicallV2Address: '0xdC6E2b14260F972ad4e5a31c68294Fba7E720701',
privateHttpProvider: process.env.HTTP_PROVIDER_250,
adapterAddresses: {
FantomAdapter01: '0xe5993623FF3ecD1f550124059252dDff804b3879',
FantomAdapter01: '0xD9026Ca611EEC89dd4030ae86953F2d8D899535c',
FantomBuyAdapter: '0x27eb327B7255a2bF666EBB4D60AB4752dA4611b9',
},
uniswapV2ExchangeRouterAddress:
Expand Down Expand Up @@ -244,7 +244,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
multicallV2Address: '0x2DC0E2aa608532Da689e89e237dF582B783E552C',
privateHttpProvider: process.env.HTTP_PROVIDER_10,
adapterAddresses: {
OptimismAdapter01: '0x0aA8b0ef37c482ff80f9D214F9E09B2Aef089265',
OptimismAdapter01: '0xaacA165FD5abb6564098e8595BC836470f19e209',
OptimismBuyAdapter: '0xeef30844023B355408C44224B9d4031609D316d4',
},
uniswapV2ExchangeRouterAddress:
Expand Down
113 changes: 94 additions & 19 deletions src/dex/solidly/solidly.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { UniswapV2 } from '../uniswap-v2/uniswap-v2';
import { Network, NULL_ADDRESS, SUBGRAPH_TIMEOUT } from '../../constants';
import {
Network,
NULL_ADDRESS,
SUBGRAPH_TIMEOUT,
DEST_TOKEN_PARASWAP_TRANSFERS,
SRC_TOKEN_PARASWAP_TRANSFERS,
} from '../../constants';
import {
AdapterExchangeParam,
Address,
ExchangePrices,
PoolLiquidity,
SimpleExchangeParam,
Token,
TransferFeeParams,
} from '../../types';
import { IDexHelper } from '../../dex-helper';
import erc20ABI from '../../abi/erc20.json';
Expand All @@ -19,19 +26,41 @@ import { NumberAsString, SwapSide } from '@paraswap/core';
import { Interface, AbiCoder } from '@ethersproject/abi';
import { SolidlyStablePool } from './solidly-stable-pool';
import { Uniswapv2ConstantProductPool } from '../uniswap-v2/uniswap-v2-constant-product-pool';
import { PoolState, SolidlyPair, SolidlyPoolOrderedParams } from './types';
import {
PoolState,
SolidlyData,
SolidlyPair,
SolidlyPool,
SolidlyPoolOrderedParams,
} from './types';
import { SolidlyConfig, Adapters } from './config';
import { applyTransferFee } from '../../lib/token-transfer-fee';

const erc20Iface = new Interface(erc20ABI);
const solidlyPairIface = new Interface(solidlyPair);
const defaultAbiCoder = new AbiCoder();

function encodePools(
pools: SolidlyPool[],
feeFactor: number,
): NumberAsString[] {
return pools.map(({ fee, direction, address }) => {
return (
(BigInt(feeFactor - fee) << 161n) +
((direction ? 0n : 1n) << 160n) +
BigInt(address)
).toString();
});
}

export class Solidly extends UniswapV2 {
pairs: { [key: string]: SolidlyPair } = {};
stableFee?: number;
volatileFee?: number;

readonly isFeeOnTransferSupported = false;
readonly isFeeOnTransferSupported: boolean = true;
readonly SRC_TOKEN_DEX_TRANSFERS = 1;
readonly DEST_TOKEN_DEX_TRANSFERS = 1;

public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
getDexKeysWithNetwork(
Expand Down Expand Up @@ -246,6 +275,12 @@ export class Solidly extends UniswapV2 {
blockNumber: number,
// list of pool identifiers to use for pricing, if undefined use all pools
limitPools?: string[],
transferFees: TransferFeeParams = {
srcFee: 0,
destFee: 0,
srcDexFee: 0,
destDexFee: 0,
},
): Promise<ExchangePrices<UniswapV2Data> | null> {
try {
if (side === SwapSide.BUY) return null; // Buy side not implemented yet
Expand All @@ -266,17 +301,27 @@ export class Solidly extends UniswapV2 {
await this.batchCatchUpPairs([[from, to]], blockNumber);

const resultPromises = [false, true].map(async stable => {
// We don't support fee on transfer for stable pools yet
if (
stable &&
(transferFees.srcFee !== 0 || transferFees.srcDexFee !== 0)
) {
return null;
}

const poolIdentifier =
`${this.dexKey}_${tokenAddress}` + this.poolPostfix(stable);

if (limitPools && limitPools.every(p => p !== poolIdentifier))
return null;

const isSell = side === SwapSide.SELL;
const pairParam = await this.getSolidlyPairOrderedParams(
from,
to,
blockNumber,
stable,
transferFees.srcDexFee,
);

if (!pairParam) return null;
Expand All @@ -285,39 +330,58 @@ export class Solidly extends UniswapV2 {
// @ts-expect-error Buy side is not implemented yet
side === SwapSide.BUY ? to.decimals : from.decimals,
);

const [unitVolumeWithFee, ...amountsWithFee] = applyTransferFee(
[unitAmount, ...amounts],
side,
isSell ? transferFees.srcFee : transferFees.destFee,
isSell ? SRC_TOKEN_PARASWAP_TRANSFERS : DEST_TOKEN_PARASWAP_TRANSFERS,
);

const unit =
// @ts-expect-error Buy side is not implemented yet
side === SwapSide.BUY
? await this.getBuyPricePath(unitAmount, [pairParam])
: await this.getSellPricePath(unitAmount, [pairParam]);
? await this.getBuyPricePath(unitVolumeWithFee, [pairParam])
: await this.getSellPricePath(unitVolumeWithFee, [pairParam]);

const prices =
// @ts-expect-error Buy side is not implemented yet
side === SwapSide.BUY
? await Promise.all(
amounts.map(amount =>
amountsWithFee.map(amount =>
amount === 0n
? 0n
: this.getBuyPricePath(amount, [pairParam]),
),
)
: await Promise.all(
amounts.map(amount =>
amountsWithFee.map(amount =>
amount === 0n
? 0n
: this.getSellPricePath(amount, [pairParam]),
),
);

const [unitOutWithFee, ...outputsWithFee] = applyTransferFee(
[unit, ...prices],
side,
// This part is confusing, because we treat differently SELL and BUY fees
// If Buy, we should apply transfer fee on srcToken on top of dexFee applied earlier
// But for Sell we should apply only one dexFee
isSell ? transferFees.destDexFee : transferFees.srcFee,
isSell ? this.DEST_TOKEN_DEX_TRANSFERS : SRC_TOKEN_PARASWAP_TRANSFERS,
);

return {
prices: prices,
unit: unit,
prices: outputsWithFee,
unit: unitOutWithFee,
data: {
router: this.router,
path: [from.address.toLowerCase(), to.address.toLowerCase()],
factory: this.factoryAddress,
initCode: this.initCode,
feeFactor: this.feeFactor,
isFeeTokenInRoute: Object.values(transferFees).some(f => f !== 0),
pools: [
{
address: pairParam.exchange,
Expand Down Expand Up @@ -436,6 +500,7 @@ export class Solidly extends UniswapV2 {
to: Token,
blockNumber: number,
stable: boolean,
tokenDexTransferFee: number,
): Promise<SolidlyPoolOrderedParams | null> {
const pair = await this.findSolidlyPair(from, to, stable);
if (!(pair && pair.pool && pair.exchange)) return null;
Expand All @@ -449,6 +514,7 @@ export class Solidly extends UniswapV2 {
return null;
}

const fee = (pairState.feeCode + tokenDexTransferFee).toString();
const pairReversed =
pair.token1.address.toLowerCase() === from.address.toLowerCase();
if (pairReversed) {
Expand All @@ -457,7 +523,7 @@ export class Solidly extends UniswapV2 {
tokenOut: to.address,
reservesIn: pairState.reserves1,
reservesOut: pairState.reserves0,
fee: pairState.feeCode.toString(),
fee,
direction: false,
exchange: pair.exchange,
decimalsIn: from.decimals,
Expand All @@ -470,7 +536,7 @@ export class Solidly extends UniswapV2 {
tokenOut: to.address,
reservesIn: pairState.reserves0,
reservesOut: pairState.reserves1,
fee: pairState.feeCode.toString(),
fee,
direction: true,
exchange: pair.exchange,
decimalsIn: from.decimals,
Expand Down Expand Up @@ -525,17 +591,26 @@ export class Solidly extends UniswapV2 {
destToken: Address,
srcAmount: NumberAsString,
toAmount: NumberAsString, // required for buy case
data: UniswapData,
data: SolidlyData,
side: SwapSide,
): AdapterExchangeParam {
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
return super.getAdapterParam(
srcToken,
destToken,
srcAmount,
toAmount,
data,
side,
const pools = encodePools(data.pools, this.feeFactor);
const weth = this.getWETHAddress(srcToken, destToken, data.wethAddress);
const payload = this.abiCoder.encodeParameter(
{
ParentStruct: {
weth: 'address',
pools: 'uint256[]',
isFeeTokenInRoute: 'bool',
},
},
{ weth, pools, isFeeTokenInRoute: data.isFeeTokenInRoute },
);
return {
targetExchange: data.router,
payload,
networkFee: '0',
};
}
}
5 changes: 4 additions & 1 deletion src/dex/solidly/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
UniswapV2Data,
UniswapV2PoolOrderedParams,
DexParams as UniswapV2DexParams,
UniswapPool,
} from '../uniswap-v2/types';
import { UniswapV2Pair } from '../uniswap-v2/uniswap-v2';

Expand All @@ -18,7 +19,9 @@ export interface SolidlyPoolOrderedParams extends UniswapV2PoolOrderedParams {
stable: boolean;
}

export type SolidlyData = UniswapV2Data;
export type SolidlyData = UniswapV2Data & { isFeeTokenInRoute: boolean };

export type SolidlyPool = UniswapPool;

export interface DexParams extends Omit<UniswapV2DexParams, 'feeCode'> {
feeCode: number;
Expand Down
5 changes: 5 additions & 0 deletions tests/constants-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ export const Tokens: {
address: '0x5cc61a78f164885776aa610fb0fe1257df78e59b',
decimals: 18,
},
wBOMB: {
address: '0xc09a82ad5075b3067d80f54f05e1e22229699cc1',
decimals: 18,
},
TOR: {
address: '0x74e23df9110aa9ea0b6ff2faee01e740ca1c642e',
decimals: 18,
Expand Down Expand Up @@ -798,6 +802,7 @@ export const Holders: {
nETH: '0x16b658270ac50c0063940ed287c401b3df7ccf70',
WETH: '0x2400bb4d7221ba530daee061d5afe219e9223eae',
SPIRIT: '0x0d0707963952f2fba59dd06f2b425ace40b492fe',
wBOMB: '0x28aa4f9ffe21365473b64c161b566c3cdead0108',
TOR: '0x70de4b5ed310fd93da3c0bae824fb99cb4d44dd8',
},
[Network.BSC]: {
Expand Down

0 comments on commit a5c5bba

Please sign in to comment.