import { Injectable } from '@angular/core' ; import BigNumber from 'bignumber.js' ; import InstantTradeToken from '@features/instant-trade/models/instant-trade-token' ; import { MethodData } from '@shared/models/blockchain/method-data' ; import { AlgebraQuoterController } from '@features/instant-trade/services/instant-trade-service/providers/polygon/algebra-service/utils/quoter-controller/algebra-quoter-controller' ; import { algebraConstants , maxTransitTokens , quoterContract } from '@features/instant-trade/services/instant-trade-service/providers/polygon/algebra-service/algebra-constants' ; import { AlgebraInstantTrade , AlgebraRoute } from '@features/instant-trade/services/instant-trade-service/providers/polygon/algebra-service/models/algebra-instant-trade' ; import { CommonUniV3AlgebraService } from '@features/instant-trade/services/instant-trade-service/providers/common/uni-v3-algebra/common-service/common-uni-v3-algebra.service' ; import { Web3Pure } from '@core/services/blockchain/blockchain-adapters/common/web3-pure' ; import { INSTANT_TRADES_PROVIDERS } from '@shared/models/instant-trade/instant-trade-providers' ; import InsufficientLiquidityError from '@core/errors/models/instant-trade/insufficient-liquidity-error' ; @ Injectable ( { providedIn : 'root' } ) export class AlgebraService extends CommonUniV3AlgebraService { public readonly providerType = INSTANT_TRADES_PROVIDERS . ALGEBRA ; private readonly quoterController : AlgebraQuoterController ; constructor ( ) { super ( algebraConstants ) ; this . quoterController = new AlgebraQuoterController ( this . blockchainAdapter , quoterContract ) ; this . useTestingModeService . isTestingMode . subscribe ( isTestingMode => { if ( isTestingMode ) { this . quoterController . setTestingMode ( ) ; } } ) ; } public async calculateTrade ( fromToken , fromAmount , toToken ) { const { fromTokenWrapped , toTokenWrapped } = this . getWrappedTokens ( fromToken , toToken ) ; const fromAmountAbsolute = Web3Pure . toWei ( fromAmount , fromToken . decimals ) ; const route = await this . getRoute ( fromTokenWrapped , toTokenWrapped , fromAmountAbsolute ) ; return { blockchain : this . blockchain , from : { token : fromToken , amount : fromAmount } , to : { token : toToken , amount : Web3Pure . fromWei ( route . outputAbsoluteAmount , toToken . decimals ) } , path : route . path , route } ; } param fromToken param toToken param amountAbsolute private async getRoute ( fromToken , toToken , amountAbsolute ) { const routes = ( await this . quoterController . getAllRoutes ( fromToken , toToken , amountAbsolute , this . settings . disableMultihops ? 0 : maxTransitTokens ) ) . sort ( ( a , b ) => b . outputAbsoluteAmount . comparedTo ( a . outputAbsoluteAmount ) ) ; if ( routes . length === 0 ) { throw new InsufficientLiquidityError ( ) ; } return routes [ 0 ] ; } protected getSwapRouterExactInputMethodParams ( route , fromAmountAbsolute , toTokenAddress , walletAddress , deadline ) { const amountOutMin = this . getAmountOutMin ( route ) ; if ( route . path . length === 2 ) { return { methodName : 'exactInputSingle' , methodArguments : [ [ route . path [ 0 ] . address , toTokenAddress , walletAddress , deadline , fromAmountAbsolute , amountOutMin , 0 ] ] } ; } return { methodName : 'exactInput' , methodArguments : [ [ AlgebraQuoterController . getEncodedPath ( route . path ) , walletAddress , deadline , fromAmountAbsolute , amountOutMin ] ] } ; } }