Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rubicon-rfq DEX #797

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
629 changes: 629 additions & 0 deletions src/abi/rubicon-rfq/rubicon-rfq.abi.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type BaseConfig = {
idleDaoAuthToken?: string;
swaapV2AuthToken?: string;
dexalotAuthToken?: string;
rubiconRfqAuthToken?: string;
forceRpcFallbackDexs: string[];
};

Expand Down Expand Up @@ -287,6 +288,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
privateHttpProvider: process.env.HTTP_PROVIDER_42161,
hashFlowAuthToken: process.env.API_KEY_HASHFLOW_AUTH_TOKEN || '',
swaapV2AuthToken: process.env.API_KEY_SWAAP_V2_AUTH_TOKEN || '',
rubiconRfqAuthToken: process.env.API_KEY_RUBICON_RFQ_AUTH_TOKEN || '',
hashFlowDisabledMMs:
process.env[`HASHFLOW_DISABLED_MMS_42161`]?.split(',') || [],
augustusV6Address: '0x6a000f20005980200259b80c5102003040001068',
Expand Down Expand Up @@ -331,6 +333,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
},
hashFlowAuthToken: process.env.API_KEY_HASHFLOW_AUTH_TOKEN || '',
swaapV2AuthToken: process.env.API_KEY_SWAAP_V2_AUTH_TOKEN || '',
rubiconRfqAuthToken: process.env.API_KEY_RUBICON_RFQ_AUTH_TOKEN || '',
hashFlowDisabledMMs:
process.env[`HASHFLOW_DISABLED_MMS_10`]?.split(',') || [],
adapterAddresses: {
Expand Down Expand Up @@ -396,6 +399,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
privateHttpProvider: process.env.HTTP_PROVIDER_8453,
dexalotAuthToken: process.env.API_KEY_DEXALOT_AUTH_TOKEN || '',
hashFlowAuthToken: process.env.API_KEY_HASHFLOW_AUTH_TOKEN || '',
rubiconRfqAuthToken: process.env.API_KEY_RUBICON_RFQ_AUTH_TOKEN || '',
swaapV2AuthToken: process.env.API_KEY_SWAAP_V2_AUTH_TOKEN || '',
hashFlowDisabledMMs: [],
augustusV6Address: '0x6a000f20005980200259b80c5102003040001068',
Expand Down Expand Up @@ -462,6 +466,7 @@ export function generateConfig(network: number): Config {
idleDaoAuthToken: baseConfig.idleDaoAuthToken,
swaapV2AuthToken: baseConfig.swaapV2AuthToken,
dexalotAuthToken: baseConfig.dexalotAuthToken,
rubiconRfqAuthToken: baseConfig.rubiconRfqAuthToken,
hashFlowDisabledMMs: baseConfig.hashFlowDisabledMMs,
forceRpcFallbackDexs: baseConfig.forceRpcFallbackDexs,
apiKeyTheGraph: process.env.API_KEY_THE_GRAPH || '',
Expand Down
3 changes: 3 additions & 0 deletions src/dex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import { LitePsm } from './lite-psm/lite-psm';
import { UsualBond } from './usual-bond/usual-bond';
import { StkGHO } from './stkgho/stkgho';
import { SkyConverter } from './sky-converter/sky-converter';
import { RubiconRfq } from './rubicon-rfq/rubicon-rfq';

const LegacyDexes = [
CurveV2,
Expand Down Expand Up @@ -176,7 +177,9 @@ const Dexes = [
LitePsm,
UsualBond,
StkGHO,
RubiconRfq,
SkyConverter,
RubiconRfq,
];

export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
Expand Down
17 changes: 17 additions & 0 deletions src/dex/rubicon-rfq/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DexConfigMap } from '../../types';
import { Network, SwapSide } from '../../constants';
import { DexParams } from './types';

export const RubiconRfqConfig: DexConfigMap<DexParams> = {
RubiconRfq: {
[Network.ARBITRUM]: {
rfqAddress: '0x7988F58d6708AD5FA7597e0d19Be59Ed75027555',
},
[Network.OPTIMISM]: {
rfqAddress: '0x0218D22B2f134C5b3000DBcB768f71693238c856',
},
[Network.BASE]: {
rfqAddress: '0x6B49A0bD2744ACbDB2a4A901A3D5655323BD567E',
},
},
};
42 changes: 42 additions & 0 deletions src/dex/rubicon-rfq/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import BigNumber from 'bignumber.js';

// TODO: update
export const RUBICON_RFQ_API_URL = 'https://rfq.rubicon.finance';
export const RUBICON_RFQ_CLIENT_TAG = 'paraswap';

export const RUBICON_RFQ_MARKETS_ENDPOINT = '/markets';
export const RUBICON_RFQ_LIQ_ENDPOINT = '/liquidity';
export const RUBICON_RFQ_MARKET_MATCH_ENDPOINT = '/market-match';
export const RUBICON_RFQ_MARKET_MATCH_TIMEOUT_MS = 3_228;

export const RUBICON_RFQ_PARTIAL_FILL = 'partial';
export const RUBICON_RFQ_FULL_FILL = 'full';

export const RUBICON_RFQ_MARKETS_CACHE_TTL_S = 3;
export const RUBICON_RFQ_MARKETS_POLL_INTERVAL_MS = 1000;

export const RUBICON_RFQ_LIQ_CACHE_TTL_S = 8;
export const RUBICON_RFQ_LIQ_POLL_INTERVAL_MS = 5 * 1000;

export const RUBICON_RFQ_GAS_COST = 550_000;
export const MARKET_SPLIT = '_';

export const RUBICON_RFQ_MIN_SLIPPAGE_FACTOR_THRESHOLD_FOR_RESTRICTION =
new BigNumber('0.001');

export const RESTRICT_UNKNOWN_TTL_MS = 60 * 60 * 1000; // 60 mins
export const RESTRICT_INTERNAL_SERVER_ERR = 60 * 20 * 1000; // 20 mins
export const RESTRICT_NO_SIGNATURE_TTL_MS = 60 * 10 * 1000; // 10 mins
export const RESTRICT_NO_MATCH_TTL_MS = 60 * 10 * 1000; // 10 mins
export const RESTRICT_SLIPPAGE_TTL_MS = 60 * 30 * 1000; // 10 mins
export const RESTRICT_PARTIAL_FLL_TTL_MS = 1 * 30 * 1000; // 1 min.

export const UNKNOWN_ERROR_CODE = 'UNKNOWN';
export const ERROR_CODE_TO_RESTRICT_TTL = {
[UNKNOWN_ERROR_CODE]: RESTRICT_UNKNOWN_TTL_MS,
ERR_NO_SIGNATURE: RESTRICT_NO_SIGNATURE_TTL_MS,
ERR_NO_MATCH: RESTRICT_NO_MATCH_TTL_MS,
ERR_PARTIAL_FILL: RESTRICT_PARTIAL_FLL_TTL_MS,
ERR_BAD_SERVER: RESTRICT_INTERNAL_SERVER_ERR,
SLIPPAGE: RESTRICT_SLIPPAGE_TTL_MS,
};
123 changes: 123 additions & 0 deletions src/dex/rubicon-rfq/rate-fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Network } from '../../constants';
import { IDexHelper } from '../../dex-helper';
import { Fetcher, SkippingRequest } from '../../lib/fetcher/fetcher';
import { validateAndCast } from '../../lib/validators';
import { Logger } from '../../types';
import {
RubiconRfqMarketsResponse,
RubiconRfqLiquidityResponse,
RubiconRfqRateFetcherConfig,
} from './types';
import {
marketsResponseValidator,
liquidityResponseValidator,
} from './validators';

export class RateFetcher {
private liquidityFetcher: Fetcher<RubiconRfqLiquidityResponse>;
private rateFetcher: Fetcher<RubiconRfqMarketsResponse>;

private marketsCacheKey: string;
private marketsCacheTTL: number;

private liquidityCacheKey: string;
private liquidityCacheTTL: number;

constructor(
private dexHelper: IDexHelper,
private dexKey: string,
private network: Network,
private logger: Logger,
config: RubiconRfqRateFetcherConfig,
) {
this.marketsCacheKey = config.rateConfig.marketsCacheKey;
this.marketsCacheTTL = config.rateConfig.marketsCacheTTLSecs;

this.liquidityCacheKey = config.rateConfig.liquidityCacheKey;
this.liquidityCacheTTL = config.rateConfig.liquidityCacheTTLSecs;

this.liquidityFetcher = new Fetcher<RubiconRfqLiquidityResponse>(
dexHelper.httpRequest,
{
info: {
requestOptions: config.rateConfig.marketsReqParams,
requestFunc: async options => {
const { liquidityReqParams } = config.rateConfig;

options.url = liquidityReqParams.url;
options.params = liquidityReqParams.params;

const liquidity = await dexHelper.httpRequest.request(options);

return liquidity;
},
caster: (data: unknown) => {
return validateAndCast<RubiconRfqLiquidityResponse>(
data,
liquidityResponseValidator,
);
},
},
handler: this.handleLiquidityResponse.bind(this),
},
config.rateConfig.marketsIntervalMs,
logger,
);

this.rateFetcher = new Fetcher<RubiconRfqMarketsResponse>(
dexHelper.httpRequest,
{
info: {
requestOptions: config.rateConfig.marketsReqParams,
requestFunc: async options => {
const { marketsReqParams } = config.rateConfig;

options.url = marketsReqParams.url;
options.params = marketsReqParams.params;

const markets = await dexHelper.httpRequest.request(options);

return markets;
},
caster: (data: unknown) => {
return validateAndCast<RubiconRfqMarketsResponse>(
data,
marketsResponseValidator,
);
},
},
handler: this.handleMarketsResponse.bind(this),
},
config.rateConfig.marketsIntervalMs,
logger,
);
}

start() {
this.liquidityFetcher.startPolling();
this.rateFetcher.startPolling();
}

stop() {
this.liquidityFetcher.stopPolling();
this.rateFetcher.stopPolling();
}

private handleMarketsResponse(resp: RubiconRfqMarketsResponse): void {
const { markets } = resp;
this.dexHelper.cache.rawset(
this.marketsCacheKey,
JSON.stringify(markets),
this.marketsCacheTTL,
);
}

private handleLiquidityResponse(resp: RubiconRfqLiquidityResponse): void {
const { liquidityUsd } = resp;
this.dexHelper.cache.rawset(
this.liquidityCacheKey,
JSON.stringify(liquidityUsd),
this.liquidityCacheTTL,
);
}
}
87 changes: 87 additions & 0 deletions src/dex/rubicon-rfq/rubicon-rfq-e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint-disable no-console */
import dotenv from 'dotenv';
dotenv.config();

import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { Tokens, Holders } from '../../../tests/constants-e2e';
import { testE2E } from '../../../tests/utils-e2e';
import { generateConfig } from '../../config';
import { Network, ContractMethod, SwapSide } from '../../constants';

const sleepMs = 3000;

function testForNetwork(
network: Network,
dexKey: string,
tokenASymbol: string,
tokenBSymbol: string,
tokenAAmount: string,
tokenBAmount: string,
nativeTokenAmount: string,
) {
const provider = new StaticJsonRpcProvider(
generateConfig(network).privateHttpProvider,
network,
);
const tokens = Tokens[network];
const holders = Holders[network];

const sideToContractMethods = new Map([
[SwapSide.SELL, [ContractMethod.swapExactAmountIn]],
[SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
]);

describe(`${network}`, () => {
sideToContractMethods.forEach((contractMethods, side) =>
describe(`${side}`, () => {
contractMethods.forEach((contractMethod: ContractMethod) => {
describe(`${contractMethod}`, () => {
it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
await testE2E(
tokens[tokenASymbol],
tokens[tokenBSymbol],
holders[tokenASymbol],
side === SwapSide.SELL ? tokenAAmount : tokenBAmount,
side,
dexKey,
contractMethod,
network,
provider,
undefined,
undefined,
undefined,
undefined,
sleepMs,
);
});
});
});
}),
);
});
}

describe('RubiconRfq E2E', () => {
const dexKey = 'RubiconRfq';

describe('Arbitrum', () => {
const network = Network.ARBITRUM;

const tokenASymbol: string = 'WETH';
const tokenBSymbol: string = 'USDC';

const tokenAAmount: string = '700000000000000';
const tokenBAmount: string = '2000000';
const nativeTokenAmount = '';

testForNetwork(
network,
dexKey,
tokenASymbol,
tokenBSymbol,
tokenAAmount,
tokenBAmount,
nativeTokenAmount,
);
});
});
Loading