-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HorizonDEX for Linea blockchain (#424)
- Loading branch information
Showing
24 changed files
with
768 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
209 changes: 209 additions & 0 deletions
209
...on/uniswap-v3-abstract/utils/quoter-controller/horizondex-uniswap-v3-quoter-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import BigNumber from 'bignumber.js'; | ||
import { RubicSdkError } from 'src/common/errors'; | ||
import { Token } from 'src/common/tokens'; | ||
import { compareAddresses } from 'src/common/utils/blockchain'; | ||
import { Cache } from 'src/common/utils/decorators'; | ||
import { notNull } from 'src/common/utils/object'; | ||
import { EvmBlockchainName } from 'src/core/blockchain/models/blockchain-name'; | ||
import { MethodData } from 'src/core/blockchain/web3-public-service/web3-public/models/method-data'; | ||
import { Exact } from 'src/features/on-chain/calculation-manager/providers/common/on-chain-trade/evm-on-chain-trade/models/exact'; | ||
import { UniswapV3Route } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-abstract/models/uniswap-v3-route'; | ||
import { UniswapV3RouterConfiguration } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-abstract/models/uniswap-v3-router-configuration'; | ||
import { | ||
UNISWAP_V3_QUOTER_CONTRACT_ABI, | ||
UNISWAP_V3_QUOTER_CONTRACT_ADDRESS | ||
} from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-abstract/utils/quoter-controller/constants/quoter-contract-data'; | ||
import { LiquidityPool } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-abstract/utils/quoter-controller/models/liquidity-pool'; | ||
import { UniswapV3QuoterController } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-abstract/utils/quoter-controller/uniswap-v3-quoter-controller'; | ||
import { AbiItem } from 'web3-utils'; | ||
|
||
interface GetQuoterMethodsDataOptions { | ||
routesLiquidityPools: LiquidityPool[]; | ||
from: Token; | ||
to: Token; | ||
exact: Exact; | ||
weiAmount: string; | ||
maxTransitTokens: number; | ||
} | ||
export class HorizondexUniswapV3QuoterController extends UniswapV3QuoterController { | ||
constructor( | ||
blockchain: EvmBlockchainName, | ||
routerConfiguration: UniswapV3RouterConfiguration<string>, | ||
quoterContractAddress: string = UNISWAP_V3_QUOTER_CONTRACT_ADDRESS, | ||
quoterContractABI: AbiItem[] = UNISWAP_V3_QUOTER_CONTRACT_ABI | ||
) { | ||
super( | ||
blockchain, | ||
routerConfiguration, | ||
quoterContractAddress, | ||
quoterContractABI, | ||
[300, 500, 1000, 3000, 10000] | ||
); | ||
} | ||
|
||
/** | ||
* Returns swap method's name and arguments to pass it to Quoter contract. | ||
* @param poolsPath Pools, included in the route. | ||
* @param from From token. | ||
* @param to To token. | ||
* @param exact Is exact input or output trade. | ||
* @param weiAmount Amount of tokens to trade. | ||
*/ | ||
@Cache | ||
protected static getQuoterMethodData( | ||
poolsPath: LiquidityPool[], | ||
from: Token, | ||
to: Token, | ||
exact: Exact, | ||
weiAmount: string | ||
): { | ||
poolsPath: LiquidityPool[]; | ||
methodData: MethodData; | ||
} { | ||
if (poolsPath.length === 1 && poolsPath?.[0]) { | ||
const methodName = | ||
exact === 'input' ? 'quoteExactInputSingle' : 'quoteExactOutputSingle'; | ||
const sqrtPriceLimitX96 = 0; | ||
return { | ||
poolsPath, | ||
methodData: { | ||
methodName, | ||
methodArguments: [ | ||
from.address, | ||
to.address, | ||
weiAmount, | ||
poolsPath[0].fee, | ||
sqrtPriceLimitX96 | ||
] | ||
} | ||
}; | ||
} | ||
|
||
const methodName = exact === 'input' ? 'quoteExactInput' : 'quoteExactOutput'; | ||
const tokensPath = exact === 'input' ? poolsPath : poolsPath.reverse(); | ||
const initialTokenAddress = exact === 'input' ? from.address : to.address; | ||
return { | ||
poolsPath, | ||
methodData: { | ||
methodName, | ||
methodArguments: [ | ||
UniswapV3QuoterController.getEncodedPoolsPath(tokensPath, initialTokenAddress), | ||
weiAmount | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public async getAllRoutes( | ||
from: Token, | ||
to: Token, | ||
exact: Exact, | ||
weiAmount: string, | ||
routeMaxTransitTokens: number | ||
): Promise<UniswapV3Route[]> { | ||
const routesLiquidityPools = await this.getAllLiquidityPools(from, to); | ||
const options: Omit<GetQuoterMethodsDataOptions, 'maxTransitTokens'> = { | ||
routesLiquidityPools, | ||
from, | ||
to, | ||
exact, | ||
weiAmount | ||
}; | ||
const quoterMethodsData = [...Array(routeMaxTransitTokens + 1)] | ||
.map((_, maxTransitTokens) => | ||
this.getQuoterMethodsData( | ||
{ | ||
...options, | ||
maxTransitTokens | ||
}, | ||
[], | ||
from.address | ||
) | ||
) | ||
.flat(); | ||
|
||
const results = await this.web3Public.multicallContractMethods< | ||
string | { returnedAmount: string } | ||
>( | ||
this.quoterContractAddress, | ||
this.quoterContractABI, | ||
quoterMethodsData.map(quoterMethodData => { | ||
if (quoterMethodData.methodData.methodName.toLowerCase().includes('single')) { | ||
return { | ||
methodName: quoterMethodData.methodData.methodName, | ||
methodArguments: [quoterMethodData.methodData.methodArguments] | ||
}; | ||
} | ||
|
||
return quoterMethodData.methodData; | ||
}) | ||
); | ||
|
||
return results | ||
.map((result, index) => { | ||
const pool = quoterMethodsData?.[index]; | ||
if (!pool) { | ||
throw new RubicSdkError('Pool has to be defined'); | ||
} | ||
if (result.success) { | ||
return { | ||
outputAbsoluteAmount: new BigNumber( | ||
result?.output! instanceof Object | ||
? result?.output?.returnedAmount | ||
: result.output! | ||
), | ||
poolsPath: pool.poolsPath, | ||
initialTokenAddress: from.address | ||
}; | ||
} | ||
return null; | ||
}) | ||
.filter(notNull); | ||
} | ||
|
||
/** | ||
* Returns swap methods' names and arguments, built with passed pools' addresses, to use it in Quoter contract. | ||
*/ | ||
protected getQuoterMethodsData( | ||
options: GetQuoterMethodsDataOptions, | ||
path: LiquidityPool[], | ||
lastTokenAddress: string | ||
): { poolsPath: LiquidityPool[]; methodData: MethodData }[] { | ||
const { routesLiquidityPools, from, to, exact, weiAmount, maxTransitTokens } = options; | ||
|
||
if (path.length === maxTransitTokens) { | ||
const pools = routesLiquidityPools.filter(pool => | ||
pool.isPoolWithTokens(lastTokenAddress, to.address) | ||
); | ||
return pools.map(pool => | ||
HorizondexUniswapV3QuoterController.getQuoterMethodData( | ||
path.concat(pool), | ||
from, | ||
to, | ||
exact, | ||
weiAmount | ||
) | ||
); | ||
} | ||
|
||
return routesLiquidityPools | ||
.filter(pool => !path.includes(pool)) | ||
.map(pool => { | ||
const methodsData: { poolsPath: LiquidityPool[]; methodData: MethodData }[] = []; | ||
if (compareAddresses(pool.token0.address, lastTokenAddress)) { | ||
const extendedPath = path.concat(pool); | ||
methodsData.push( | ||
...this.getQuoterMethodsData(options, extendedPath, pool.token1.address) | ||
); | ||
} | ||
if (compareAddresses(pool.token1.address, lastTokenAddress)) { | ||
const extendedPath = path.concat(pool); | ||
methodsData.push( | ||
...this.getQuoterMethodsData(options, extendedPath, pool.token0.address) | ||
); | ||
} | ||
return methodsData; | ||
}) | ||
.flat(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...oviders/dexes/common/uniswap-v3-abstract/utils/quoter-controller/models/liquidity-pool.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...manager/providers/dexes/common/uniswap-v3-algebra-abstract/models/unwrapWethMethodName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type UnwrapWethMethodName = 'unwrapWETH9' | 'unwrapWNativeToken' | 'unwrapWeth'; |
Oops, something went wrong.