Skip to content

Commit

Permalink
Add vooi pool (v4.20.0) (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
axtezy authored Aug 30, 2023
2 parents bfe0374 + cf4fdbb commit bcbeae7
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 2 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": "4.19.0",
"version": "4.20.0",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { pancakeRouterProviders } from 'src/features/on-chain/calculation-manage
import { UniswapV2TradeProviders } from 'src/features/on-chain/calculation-manager/constants/trade-providers/uniswap-v2-trade-providers';
import { UniswapV3TradeProviders } from 'src/features/on-chain/calculation-manager/constants/trade-providers/uniswap-v3-trade-providers';
import { OnChainTypedTradeProviders } from 'src/features/on-chain/calculation-manager/models/on-chain-typed-trade-provider';
import { VooiProvider } from 'src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/vooi-provider';
import { SyncSwapProvider } from 'src/features/on-chain/calculation-manager/providers/dexes/zksync/sync-swap/sync-swap-provider';

export const typedTradeProviders: OnChainTypedTradeProviders = [
Expand All @@ -17,7 +18,8 @@ export const typedTradeProviders: OnChainTypedTradeProviders = [
...BridgersTradeProviders,
// ...CurveTradeProviders, Removed because hack
...pancakeRouterProviders,
SyncSwapProvider
SyncSwapProvider,
VooiProvider
].reduce(
(acc, ProviderClass) => {
const provider = new ProviderClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const ON_CHAIN_TRADE_TYPE = {
VERSE: 'VERSE',
VIPER_SWAP: 'VIPER_SWAP',
VOLTAGE_SWAP: 'VOLTAGE_SWAP',
VOOI: 'VOOI',
VVS_FINANCE: 'VVS_FINANCE',

WAGYU_SWAP: 'WAGYU_SWAP',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { AbiItem } from 'web3-utils';

export const omniPoolAbi: AbiItem[] = [
{
inputs: [
{
internalType: 'uint256',
name: '_fromAsset',
type: 'uint256'
},
{
internalType: 'uint256',
name: '_toAsset',
type: 'uint256'
},
{
internalType: 'int256',
name: '_fromAmount',
type: 'int256'
}
],
name: 'quoteFrom',
outputs: [
{
internalType: 'uint256',
name: 'actualToAmount',
type: 'uint256'
},
{
internalType: 'uint256',
name: 'lpFeeAmount',
type: 'uint256'
}
],
stateMutability: 'view',
type: 'function'
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const vooiPoolIdMapping: Record<string, number> = {
'0x4af15ec2a0bd43db75dd04e62faa3b8ef36b00d5': 2, // DAI
'0xa219439258ca9da29e9cc4ce5596924745e12b93': 1, // USDT
'0x176211869ca2b568f2a7d4ee941e073a821ee1ff': 0 // USDC
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AbiItem } from 'web3-utils';

export const vooiAbi: AbiItem[] = [
{
inputs: [
{ internalType: 'uint256', name: '_fromID', type: 'uint256' },
{ internalType: 'uint256', name: '_toID', type: 'uint256' },
{ internalType: 'uint256', name: '_fromAmount', type: 'uint256' },
{ internalType: 'uint256', name: '_minToAmount', type: 'uint256' },
{ internalType: 'address', name: '_to', type: 'address' },
{ internalType: 'uint256', name: '_deadline', type: 'uint256' }
],
name: 'swap',
outputs: [
{ internalType: 'uint256', name: 'actualToAmount', type: 'uint256' },
{ internalType: 'uint256', name: 'lpFeeAmount', type: 'uint256' }
],
stateMutability: 'nonpayable',
type: 'function'
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { EvmOnChainTradeStruct } from 'src/features/on-chain/calculation-manager/providers/common/on-chain-trade/evm-on-chain-trade/models/evm-on-chain-trade-struct';

export interface VooiTradeStruct extends EvmOnChainTradeStruct {
fromPoolId: number;
toPoolId: number;
deadlineMinutes: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import BigNumber from 'bignumber.js';
import { RubicSdkError } from 'src/common/errors';
import { PriceToken, PriceTokenAmount } from 'src/common/tokens';
import { combineOptions, deadlineMinutesTimestamp } from 'src/common/utils/options';
import { BLOCKCHAIN_NAME, EvmBlockchainName } from 'src/core/blockchain/models/blockchain-name';
import { Injector } from 'src/core/injector/injector';
import { OnChainCalculationOptions } from 'src/features/on-chain/calculation-manager/providers/common/models/on-chain-calculation-options';
import { OnChainProxyFeeInfo } from 'src/features/on-chain/calculation-manager/providers/common/models/on-chain-proxy-fee-info';
import { ON_CHAIN_TRADE_TYPE } from 'src/features/on-chain/calculation-manager/providers/common/models/on-chain-trade-type';
import { EvmOnChainTrade } from 'src/features/on-chain/calculation-manager/providers/common/on-chain-trade/evm-on-chain-trade/evm-on-chain-trade';
import { evmProviderDefaultOptions } from 'src/features/on-chain/calculation-manager/providers/dexes/common/on-chain-provider/evm-on-chain-provider/constants/evm-provider-default-options';
import { EvmOnChainProvider } from 'src/features/on-chain/calculation-manager/providers/dexes/common/on-chain-provider/evm-on-chain-provider/evm-on-chain-provider';
import { getFromToTokensAmountsByExact } from 'src/features/on-chain/calculation-manager/providers/dexes/common/utils/get-from-to-tokens-amounts-by-exact';
import { omniPoolAbi } from 'src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/constants/omni-pool-abi';
import { vooiPoolIdMapping } from 'src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/constants/pool-id-mapping';
import { VooiTradeStruct } from 'src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/models/vooi-trade-struct';
import { VooiTrade } from 'src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/vooi-trade';

export class VooiProvider extends EvmOnChainProvider {
public readonly blockchain = BLOCKCHAIN_NAME.LINEA;

public readonly type = ON_CHAIN_TRADE_TYPE.VOOI;

private readonly defaultOptions = {
...evmProviderDefaultOptions,
deadlineMinutes: deadlineMinutesTimestamp(10)
};

private readonly contractAddress = '0xBc7f67fA9C72f9fcCf917cBCEe2a50dEb031462A';

private readonly omniPoolAddress = '0x87E4c4517B28844f575Cfbbc4CABBD867863EA6E';

public async calculate(
fromToken: PriceTokenAmount<EvmBlockchainName>,
toToken: PriceToken<EvmBlockchainName>,
options?: OnChainCalculationOptions
): Promise<EvmOnChainTrade> {
const fromPoolId = vooiPoolIdMapping[fromToken.address.toLowerCase()];
const toPoolId = vooiPoolIdMapping[toToken.address.toLowerCase()];

if (fromPoolId === undefined || toPoolId === undefined) {
throw new RubicSdkError('Vooi DEX supports only USDC.e, USDT, DAI token');
}

const fullOptions = combineOptions(options, this.defaultOptions);

let weiAmountWithoutFee = fromToken.weiAmount;
let proxyFeeInfo: OnChainProxyFeeInfo | undefined;
if (fullOptions.useProxy) {
const proxyContractInfo = await this.handleProxyContract(
new PriceTokenAmount({
...fromToken.asStruct,
weiAmount: fromToken.weiAmount
}),
fullOptions
);
weiAmountWithoutFee = proxyContractInfo.fromWithoutFee.weiAmount;
proxyFeeInfo = proxyContractInfo.proxyFeeInfo;
}

// let gasPriceInfo: GasPriceInfo | undefined;
// if (fullOptions.gasCalculation !== 'disabled') {
// try {
// gasPriceInfo = await this.getGasPriceInfo();
// } catch {}
// }

const output = await this.getRoute(fromPoolId, toPoolId, weiAmountWithoutFee.toFixed());
if (!output) {
throw new RubicSdkError('Can not estimate the route');
}

const { from, to, fromWithoutFee } = getFromToTokensAmountsByExact(
fromToken,
toToken,
'input',
fromToken.weiAmount,
weiAmountWithoutFee,
output
);

const tradeStruct: VooiTradeStruct = {
from,
to,
fromPoolId,
toPoolId,
gasFeeInfo: null,
slippageTolerance: fullOptions.slippageTolerance,
deadlineMinutes: fullOptions.deadlineMinutes,
useProxy: fullOptions.useProxy,
proxyFeeInfo,
fromWithoutFee,
withDeflation: fullOptions.withDeflation,
usedForCrossChain: fullOptions.usedForCrossChain,
path: [from, to]
};

return new VooiTrade(tradeStruct, fullOptions.providerAddress);

// if (fullOptions.gasCalculation === 'disabled') {
// return new VooiTrade(tradeStruct, fullOptions.providerAddress);
// }

// const gasFeeInfo = getGasFeeInfo(estimatedGas, gasPriceInfo!);
// return new VooiTrade({ ...tradeStruct, gasFeeInfo }, fullOptions.providerAddress);
}

private async getRoute(fromId: number, toId: number, fromAmount: string): Promise<BigNumber> {
const web3 = Injector.web3PublicService.getWeb3Public(this.blockchain);
const result = await web3.callContractMethod<{
actualToAmount: string;
lpFeeAmount: string;
}>(this.omniPoolAddress, omniPoolAbi, 'quoteFrom', [fromId, toId, fromAmount]);
return new BigNumber(result.actualToAmount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { deadlineMinutesTimestamp } from 'src/common/utils/options';
import { EvmWeb3Pure } from 'src/core/blockchain/web3-pure/typed-web3-pure/evm-web3-pure/evm-web3-pure';
import { EvmEncodeConfig } from 'src/core/blockchain/web3-pure/typed-web3-pure/evm-web3-pure/models/evm-encode-config';
import { EncodeTransactionOptions } from 'src/features/common/models/encode-transaction-options';
import {
ON_CHAIN_TRADE_TYPE,
OnChainTradeType
} from 'src/features/on-chain/calculation-manager/providers/common/models/on-chain-trade-type';
import { EvmOnChainTrade } from 'src/features/on-chain/calculation-manager/providers/common/on-chain-trade/evm-on-chain-trade/evm-on-chain-trade';
import { vooiAbi } from 'src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/constants/vooi-abi';
import { VooiTradeStruct } from 'src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/models/vooi-trade-struct';

export class VooiTrade extends EvmOnChainTrade {
private fromPoolId: number;

private toPoolId: number;

private deadlineInMinutes: number;

public static get type(): OnChainTradeType {
return ON_CHAIN_TRADE_TYPE.VOOI;
}

public readonly type = ON_CHAIN_TRADE_TYPE.VOOI;

public readonly dexContractAddress = '0xBc7f67fA9C72f9fcCf917cBCEe2a50dEb031462A';

constructor(tradeStruct: VooiTradeStruct, providerAddress: string) {
super(tradeStruct, providerAddress);
this.fromPoolId = tradeStruct.fromPoolId;
this.toPoolId = tradeStruct.toPoolId;
this.deadlineInMinutes = tradeStruct.deadlineMinutes;
}

public async encodeDirect(options: EncodeTransactionOptions): Promise<EvmEncodeConfig> {
await this.checkFromAddress(options.fromAddress, true);
await this.checkReceiverAddress(options.receiverAddress);
const receiver = options?.receiverAddress || this.walletAddress;

const gasParams = this.getGasParams(options);

return EvmWeb3Pure.encodeMethodCall(
this.dexContractAddress,
vooiAbi,
'swap',
[
this.fromPoolId,
this.toPoolId,
this.from.stringWeiAmount,
this.toTokenAmountMin.stringWeiAmount,
receiver,
deadlineMinutesTimestamp(this.deadlineInMinutes)
],
this.fromWithoutFee.isNative ? this.fromWithoutFee.stringWeiAmount : '0',
gasParams
);
}
}

0 comments on commit bcbeae7

Please sign in to comment.