-
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.
- Loading branch information
Showing
9 changed files
with
250 additions
and
2 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
38 changes: 38 additions & 0 deletions
38
...atures/on-chain/calculation-manager/providers/dexes/linea/vooi/constants/omni-pool-abi.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,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' | ||
} | ||
]; |
5 changes: 5 additions & 0 deletions
5
...ures/on-chain/calculation-manager/providers/dexes/linea/vooi/constants/pool-id-mapping.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,5 @@ | ||
export const vooiPoolIdMapping: Record<string, number> = { | ||
'0x4af15ec2a0bd43db75dd04e62faa3b8ef36b00d5': 2, // DAI | ||
'0xa219439258ca9da29e9cc4ce5596924745e12b93': 1, // USDT | ||
'0x176211869ca2b568f2a7d4ee941e073a821ee1ff': 0 // USDC | ||
}; |
21 changes: 21 additions & 0 deletions
21
src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/constants/vooi-abi.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,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' | ||
} | ||
]; |
7 changes: 7 additions & 0 deletions
7
...tures/on-chain/calculation-manager/providers/dexes/linea/vooi/models/vooi-trade-struct.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,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; | ||
} |
116 changes: 116 additions & 0 deletions
116
src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/vooi-provider.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,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); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/features/on-chain/calculation-manager/providers/dexes/linea/vooi/vooi-trade.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,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 | ||
); | ||
} | ||
} |