Skip to content

Commit

Permalink
add blazeSwap/enosys/sparkDex dexes for flare (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
IDIDOS authored Nov 18, 2024
2 parents 6e1fbe5 + 8da0654 commit 4315fdb
Show file tree
Hide file tree
Showing 38 changed files with 2,205 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubic-sdk",
"version": "5.44.1",
"version": "5.45.0",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/common/tokens/constants/wrapped-addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const wrappedAddress: Partial<Record<EvmBlockchainName, string>> = {
[BLOCKCHAIN_NAME.CORE]: '0x191e94fa59739e188dce837f7f6978d84727ad01',
[BLOCKCHAIN_NAME.BAHAMUT]: '0x4084ab20f8ffca76c19aaf854fb5fe9de6217fbb',
[BLOCKCHAIN_NAME.GRAVITY]: '0xBB859E225ac8Fb6BE1C7e38D87b767e95Fef0EbD',
[BLOCKCHAIN_NAME.FLARE]: '0x1d80c49bbbcd1c0911346656b529df9e5c2f783d',
// Testnet
[BLOCKCHAIN_NAME.GOERLI]: '0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6',
[BLOCKCHAIN_NAME.SCROLL_SEPOLIA]: '0x5300000000000000000000000000000000000004',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ export const MULTICALL_ADDRESSES: Record<Web3PublicSupportedBlockchain, string>
[BLOCKCHAIN_NAME.CORE]: '0xcA11bde05977b3631167028862bE2a173976CA11',
[BLOCKCHAIN_NAME.BAHAMUT]: '0xa385B1436fD2A6a1c6865E22c522A1aA40CaDCC6',
[BLOCKCHAIN_NAME.BITLAYER]: '0x5B256fE9e993902eCe49D138a5b1162cBb529474',
[BLOCKCHAIN_NAME.GRAVITY]: '0xcA11bde05977b3631167028862bE2a173976CA11'
[BLOCKCHAIN_NAME.GRAVITY]: '0xcA11bde05977b3631167028862bE2a173976CA11',
[BLOCKCHAIN_NAME.FLARE]: '0xcA11bde05977b3631167028862bE2a173976CA11'
};
3 changes: 2 additions & 1 deletion src/features/common/constants/proxy-supported-blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export const proxySupportedBlockchains = [
BLOCKCHAIN_NAME.CORE,
BLOCKCHAIN_NAME.BAHAMUT,
BLOCKCHAIN_NAME.BITLAYER,
BLOCKCHAIN_NAME.GRAVITY
BLOCKCHAIN_NAME.GRAVITY,
BLOCKCHAIN_NAME.FLARE
// BLOCKCHAIN_NAME.OKE_X_CHAIN,
// BLOCKCHAIN_NAME.GNOSIS,
// BLOCKCHAIN_NAME.FUSE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const ALTERNATIVE_ROUTER1_NETWORKS = [
BLOCKCHAIN_NAME.SEI,
BLOCKCHAIN_NAME.CORE,
BLOCKCHAIN_NAME.BAHAMUT,
BLOCKCHAIN_NAME.BITLAYER
BLOCKCHAIN_NAME.BITLAYER,
BLOCKCHAIN_NAME.FLARE
] as const;

function isAlternativeRouter1Network(blockchain: BlockchainName): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class RetroBridgeEvmTrade extends EvmCrossChainTrade implements RetroBrid
};
} catch (err) {
console.error(err?.message);
throw new RubicSdkError(err?.message);
throw err;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class RouterCrossChainTrade extends EvmCrossChainTrade {
};
} catch (err) {
console.log(err?.message);
throw new RubicSdkError(err?.message);
throw err;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class StargateV2CrossChainTrade extends EvmCrossChainTrade {
};
} catch (err) {
console.log(err?.message);
throw new RubicSdkError(err?.message);
throw err;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ import { MuteSwapProvider } from 'src/features/on-chain/calculation-manager/prov

import { KujataBahamutProvider } from '../../providers/dexes/bahamut/kujata-bahamut/kujata-bahamut-provider';
import { SilkSwapBahamutProvider } from '../../providers/dexes/bahamut/silk-swap-bahamut/silk-swap-bahamut-provider';
import { BlazeSwapFlareProvider } from '../../providers/dexes/flare/blaze-swap-flare/blaze-swap-flare-provider';
import { EnosysFlareProvider } from '../../providers/dexes/flare/enosys-flare/enosys-v2-flare/enosys-flare-provider';
import { SparkDexFlareProvider } from '../../providers/dexes/flare/spark-dex-flare/spark-dex-v2-flare/spark-dex-flare-provider';
import { SushiSwapZetachainProvider } from '../../providers/dexes/zetachain/sushi-swap-zetachain/sushi-swap-zetachain-provider';

export const UniswapV2TradeProviders = [
Expand Down Expand Up @@ -162,5 +165,9 @@ export const UniswapV2TradeProviders = [
SilkSwapBahamutProvider,
KujataBahamutProvider,
// Bitlayer
MacaronProvider
MacaronProvider,
// Flare
BlazeSwapFlareProvider,
SparkDexFlareProvider,
EnosysFlareProvider
] as const;
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import { FusionXProvider } from 'src/features/on-chain/calculation-manager/provi
import { UniSwapV3PolygonProvider } from 'src/features/on-chain/calculation-manager/providers/dexes/polygon/uni-swap-v3-polygon/uni-swap-v3-polygon-provider';
import { UniSwapV3ScrollSepoliaProvider } from 'src/features/on-chain/calculation-manager/providers/dexes/scroll-sepolia/uni-swap-v3-scroll-sepolia/uni-swap-v3-scroll-sepolia-provider';

import { EnosysV3FlareProvider } from '../../providers/dexes/flare/enosys-flare/enosys-v3-flare/enosys-v3-flare-provider';
import { SparkDexV3FlareProvider } from '../../providers/dexes/flare/spark-dex-flare/spark-dex-v3-flare/spark-dex-v3-flare-provider';

export const UniswapV3TradeProviders = [
UniSwapV3EthereumProvider,
UniSwapV3PolygonProvider,
UniSwapV3ArbitrumProvider,
UniSwapV3EthereumPowProvider,
// HorizondexProvider, // disabled due to risk of hacking
FusionXProvider,
UniSwapV3ScrollSepoliaProvider
UniSwapV3ScrollSepoliaProvider,
SparkDexV3FlareProvider,
EnosysV3FlareProvider
] as const;
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const ON_CHAIN_TRADE_TYPE = {
BEAM_SWAP: 'BEAM_SWAP',
BI_SWAP: 'BI_SWAP',
BRIDGERS: 'BRIDGERS',
BLAZE_SWAP: 'BLAZE_SWAP',

CAMELOT: 'CAMELOT',
CHERRY_SWAP: 'CHERRY_SWAP',
Expand All @@ -48,6 +49,8 @@ export const ON_CHAIN_TRADE_TYPE = {
ECHO_DEX: 'ECHO_DEX',
EDDY_FINANCE: 'EDDY_FINANCE',
ELK: 'ELK',
ENOSYS: 'ENOSYS',
ENOSYS_V3: 'ENOSYS_V3',

FENIX_V2: 'FENIX_V2',
FENIX_V3: 'FENIX_V3',
Expand Down Expand Up @@ -135,6 +138,8 @@ export const ON_CHAIN_TRADE_TYPE = {
SYNC_SWAP: 'SYNC_SWAP',
SILK_SWAP: 'SILK_SWAP',
SQUIDROUTER: 'SQUIDROUTER',
SPARK_DEX: 'SPARK_DEX',
SPARK_DEX_V3: 'SPARK_DEX_V3',

SYMBIOSIS_SWAP: 'SYMBIOSIS_SWAP', // Fake to show swap via unknown symbiosis dex

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { Token } from 'src/common/tokens';
import { compareAddresses } from 'src/common/utils/blockchain';
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 { AbiItem } from 'web3-utils';

import { UniswapV3RouterConfiguration } from '../../models/uniswap-v3-router-configuration';
import { LiquidityPool } from './models/liquidity-pool';
import { UniswapV3QuoterController } from './uniswap-v3-quoter-controller';

interface GetQuoterMethodsDataOptions {
routesLiquidityPools: LiquidityPool[];
from: Token;
to: Token;
exact: Exact;
weiAmount: string;
maxTransitTokens: number;
}

interface QuoteExactInputSingleParams {
tokenIn: string;
tokenOut: string;
amountIn: string;
fee: number;
sqrtPriceLimitX96: number;
}

export class SparkDexV3QuoterController extends UniswapV3QuoterController {
constructor(
blockchain: EvmBlockchainName,
routerConfiguration: UniswapV3RouterConfiguration<string>,
contractAddress: string,
contractAbi: AbiItem[],
factoryAddress?: string
) {
super(blockchain, routerConfiguration, contractAddress, contractAbi, factoryAddress);
}

/**
* 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.
*/
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: [
{
tokenIn: from.address,
tokenOut: to.address,
amountIn: weiAmount,
fee: poolsPath[0].fee,
sqrtPriceLimitX96
}
] as QuoteExactInputSingleParams[]
}
};
}

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
]
}
};
}

/**
* 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 =>
SparkDexV3QuoterController.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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { UniSwapV3ScrollSepoliaTrade } from 'src/features/on-chain/calculation-m
import { AbiItem } from 'web3-utils';

import { AlgebraIntegralTrade } from '../../arthera-testnet/algebra-integral/algebra-integral-trade';
import { SparkDexV3FlareTrade } from '../../flare/spark-dex-flare/spark-dex-v3-flare/spark-dex-v3-flare-trade';
import { CamelotGravityTrade } from '../../gravity/camelot-gravity/camelot-gravity-trade';

export abstract class UniswapV3AlgebraAbstractProvider<
Expand All @@ -55,7 +56,8 @@ export abstract class UniswapV3AlgebraAbstractProvider<
| typeof BerachainTestnetAlgebraTrade
| typeof ModeAlgebraTrade
| typeof BlastFenixTrade
| typeof CamelotGravityTrade;
| typeof CamelotGravityTrade
| typeof SparkDexV3FlareTrade;

protected abstract readonly quoterController: UniswapV3AlgebraQuoterController;

Expand Down
Loading

0 comments on commit 4315fdb

Please sign in to comment.