From ab5f3839d3e1b705784c3e5d22501f604ac1e900 Mon Sep 17 00:00:00 2001 From: Roberto Cano <3525807+robercano@users.noreply.github.com> Date: Wed, 15 May 2024 20:56:54 +0200 Subject: [PATCH] feat: refactor refinance data types (#250) --- .../tests/service/OrderPlannerService.spec.ts | 2 +- .../RefinanceSimulation.ts | 4 +- .../src/interfaces/IProtocolManager.ts | 8 +- .../src/implementation/ProtocolManager.ts | 6 +- .../src/implementation/ProtocolClient.ts | 8 +- .../simulations/RefinanceSimulationManager.ts | 4 +- .../tests/queries/getLendingPool.subtest.ts | 15 ++- .../queries/getLendingPoolInfo.subtest.ts | 15 ++- .../queries/simulateRefinance.subtest.ts | 91 ++++++++----------- .../src/common/implementation/ChainInfo.ts | 2 +- .../implementation/RefinanceParameters.ts | 33 +++++++ .../src/orders/implementation/index.ts | 1 + .../refinance/IRefinanceParameters.ts | 42 +++++++-- .../src/orders/interfaces/refinance/index.ts | 2 +- .../src/protocols/interfaces/ILendingPool.ts | 2 +- .../protocols/interfaces/ILendingPoolId.ts | 2 +- .../protocols/interfaces/ILendingPoolInfo.ts | 2 +- .../src/protocols/interfaces/IPool.ts | 2 +- .../src/protocols/interfaces/IPoolId.ts | 2 +- .../src/protocols/interfaces/IPoolInfo.ts | 2 +- .../src/protocols/interfaces/IProtocol.ts | 2 +- sdk/sdk-common/src/user/IUser.ts | 37 +++++++- ...refinanceMakerSparkAlreadyImported.test.ts | 16 ++-- .../tests/refinanceMakerSparkAnyPair.test.ts | 16 ++-- .../tests/refinanceMorphoSparkAnyPair.test.ts | 16 ++-- sdk/sdk-server/src/TRPC.ts | 2 +- sdk/sdk-server/src/Types.ts | 0 sdk/sdk-server/src/handlers/buildOrder.ts | 20 ++-- sdk/sdk-server/src/handlers/getLendingPool.ts | 18 ++-- .../src/handlers/getLendingPoolInfo.ts | 14 +-- sdk/sdk-server/src/handlers/getPosition.ts | 16 ++-- .../src/handlers/getRefinanceSimulation.ts | 53 +++-------- .../src/handlers/getTokenByAddress.ts | 26 +++--- sdk/sdk-server/src/handlers/getTokenByName.ts | 10 +- .../src/handlers/getTokenBySymbol.ts | 12 ++- .../common/RefinanceStrategyRouter.ts | 45 +++++++++ .../src/strategies/common/index.ts | 1 + .../RefinanceLendingToLendingAnyPair.ts | 19 ++-- .../RefinanceLendingToLendingNoDebt.ts | 15 +-- .../RefinanceLendingToLendingSamePair.ts | 12 ++- sdk/simulator-service/tests/simulator.test.ts | 6 +- 41 files changed, 358 insertions(+), 243 deletions(-) create mode 100644 sdk/sdk-common/src/orders/implementation/RefinanceParameters.ts delete mode 100644 sdk/sdk-server/src/Types.ts create mode 100644 sdk/simulator-service/src/strategies/common/RefinanceStrategyRouter.ts diff --git a/sdk/order-planner-service/tests/service/OrderPlannerService.spec.ts b/sdk/order-planner-service/tests/service/OrderPlannerService.spec.ts index cf1fdc7807..be9a7079fc 100644 --- a/sdk/order-planner-service/tests/service/OrderPlannerService.spec.ts +++ b/sdk/order-planner-service/tests/service/OrderPlannerService.spec.ts @@ -92,7 +92,7 @@ describe('Order Planner Service', () => { const refinanceSimulation: ISimulation = getRefinanceSimulation({ sourcePosition, - targetPosition, + targetPool: targetPosition, }) const order = await orderPlannerService.buildOrder({ diff --git a/sdk/order-planner-service/tests/utils/RefinanceSimulation/RefinanceSimulation.ts b/sdk/order-planner-service/tests/utils/RefinanceSimulation/RefinanceSimulation.ts index 39ed8cfd09..8d6881a9b9 100644 --- a/sdk/order-planner-service/tests/utils/RefinanceSimulation/RefinanceSimulation.ts +++ b/sdk/order-planner-service/tests/utils/RefinanceSimulation/RefinanceSimulation.ts @@ -10,9 +10,9 @@ import { Position } from '@summerfi/sdk-common/common' export function getRefinanceSimulation(params: { sourcePosition: Position - targetPosition: Position + targetPool: Position }): ISimulation { - const { sourcePosition, targetPosition } = params + const { sourcePosition, targetPool: targetPosition } = params return { simulationType: SimulationType.Refinance, diff --git a/sdk/protocol-manager-common/src/interfaces/IProtocolManager.ts b/sdk/protocol-manager-common/src/interfaces/IProtocolManager.ts index 407cce6438..9863575032 100644 --- a/sdk/protocol-manager-common/src/interfaces/IProtocolManager.ts +++ b/sdk/protocol-manager-common/src/interfaces/IProtocolManager.ts @@ -1,4 +1,4 @@ -import { Maybe } from '@summerfi/sdk-common' +import { ILendingPoolId, IPositionId, Maybe } from '@summerfi/sdk-common' import { IPosition } from '@summerfi/sdk-common/common' import { IExternalPosition, IPositionsManager, TransactionInfo } from '@summerfi/sdk-common/orders' import { ILendingPool, ILendingPoolInfo } from '@summerfi/sdk-common/protocols' @@ -17,7 +17,7 @@ export interface IProtocolManager { * @param poolId The pool ID * @returns The lending pool for the specific protocol */ - getLendingPool(poolId: unknown): Promise + getLendingPool(poolId: ILendingPoolId): Promise /** * @name getLendingPoolInfo @@ -25,7 +25,7 @@ export interface IProtocolManager { * @param poolId The pool ID * @returns The extended lending pool information for the specific protocol */ - getLendingPoolInfo(poolId: unknown): Promise + getLendingPoolInfo(poolId: ILendingPoolId): Promise /** POSITIONS */ @@ -35,7 +35,7 @@ export interface IProtocolManager { * @param positionId The position ID for the specific protocol * @returns The position for the specific protocol */ - getPosition(positionId: unknown): Promise + getPosition(positionId: IPositionId): Promise /** IMPORT POSITION */ diff --git a/sdk/protocol-manager-service/src/implementation/ProtocolManager.ts b/sdk/protocol-manager-service/src/implementation/ProtocolManager.ts index 6668759d2d..0418faff1a 100644 --- a/sdk/protocol-manager-service/src/implementation/ProtocolManager.ts +++ b/sdk/protocol-manager-service/src/implementation/ProtocolManager.ts @@ -38,7 +38,7 @@ export class ProtocolManager implements IProtocolManager { } /** @see IProtocolManager.getLendingPool */ - async getLendingPool(poolId: unknown): Promise { + async getLendingPool(poolId: ILendingPoolId): Promise { this._validateLendingPoolId(poolId) const plugin = this._pluginsRegistry.getPlugin({ protocolName: poolId.protocol.name }) @@ -49,7 +49,7 @@ export class ProtocolManager implements IProtocolManager { } /** @see IProtocolManager.getLendingPoolInfo */ - async getLendingPoolInfo(poolId: unknown): Promise { + async getLendingPoolInfo(poolId: ILendingPoolId): Promise { this._validateLendingPoolId(poolId) const plugin = this._pluginsRegistry.getPlugin({ protocolName: poolId.protocol.name }) @@ -60,7 +60,7 @@ export class ProtocolManager implements IProtocolManager { } /** @see IProtocolManager.getPosition */ - async getPosition(positionId: unknown): Promise { + async getPosition(positionId: IPositionId): Promise { this._validatePositionId(positionId) throw new Error('Not implemented') diff --git a/sdk/sdk-client/src/implementation/ProtocolClient.ts b/sdk/sdk-client/src/implementation/ProtocolClient.ts index 423350f9b5..4f850e2fd9 100644 --- a/sdk/sdk-client/src/implementation/ProtocolClient.ts +++ b/sdk/sdk-client/src/implementation/ProtocolClient.ts @@ -19,15 +19,11 @@ export class ProtocolClient extends Protocol implements IProtocolClient { } getLendingPool(params: { poolId: ILendingPoolIdData }): Promise> { - return this._rpcClient.protocols.getLendingPool.query({ - poolId: params.poolId, - }) + return this._rpcClient.protocols.getLendingPool.query(params.poolId) } getLendingPoolInfo(params: { poolId: ILendingPoolIdData }): Promise> { - return this._rpcClient.protocols.getLendingPoolInfo.query({ - poolId: params.poolId, - }) + return this._rpcClient.protocols.getLendingPoolInfo.query(params.poolId) } /** diff --git a/sdk/sdk-client/src/implementation/simulations/RefinanceSimulationManager.ts b/sdk/sdk-client/src/implementation/simulations/RefinanceSimulationManager.ts index cdfee96cb0..a595fcd787 100644 --- a/sdk/sdk-client/src/implementation/simulations/RefinanceSimulationManager.ts +++ b/sdk/sdk-client/src/implementation/simulations/RefinanceSimulationManager.ts @@ -9,8 +9,8 @@ export class RefinanceSimulationManager extends IRPCClient { } public async simulateRefinancePosition( - params: IRefinanceParameters, + refinanceParameters: IRefinanceParameters, ): Promise> { - return this.rpcClient.simulation.refinance.query(params) + return this.rpcClient.simulation.refinance.query(refinanceParameters) } } diff --git a/sdk/sdk-client/tests/queries/getLendingPool.subtest.ts b/sdk/sdk-client/tests/queries/getLendingPool.subtest.ts index 03ac278ff9..cf741dc9f0 100644 --- a/sdk/sdk-client/tests/queries/getLendingPool.subtest.ts +++ b/sdk/sdk-client/tests/queries/getLendingPool.subtest.ts @@ -11,21 +11,20 @@ import { AddressType } from '@summerfi/sdk-common' export default async function getLendingPoolTest() { type GetLendingPoolType = RPCClientType['protocols']['getLendingPool']['query'] - const getLendingPoolQuery: GetLendingPoolType = jest.fn(async (params) => { - expect(params).toBeDefined() - expect(params.poolId).toBeDefined() + const getLendingPoolQuery: GetLendingPoolType = jest.fn(async (poolId) => { + expect(poolId).toBeDefined() - if (!isMakerLendingPoolId(params.poolId)) { + if (!isMakerLendingPoolId(poolId)) { fail('PoolId is not MakerPoolId') } - expect(params.poolId.protocol).toBeDefined() - expect(params.poolId.protocol.name).toBe(ProtocolName.Maker) - expect(params.poolId.ilkType).toBe(ILKType.ETH_A) + expect(poolId.protocol).toBeDefined() + expect(poolId.protocol.name).toBe(ProtocolName.Maker) + expect(poolId.ilkType).toBe(ILKType.ETH_A) return { type: PoolType.Lending, - id: params.poolId, + id: poolId, } as unknown as MakerLendingPool }) diff --git a/sdk/sdk-client/tests/queries/getLendingPoolInfo.subtest.ts b/sdk/sdk-client/tests/queries/getLendingPoolInfo.subtest.ts index 0f391e9be2..2b6df408c2 100644 --- a/sdk/sdk-client/tests/queries/getLendingPoolInfo.subtest.ts +++ b/sdk/sdk-client/tests/queries/getLendingPoolInfo.subtest.ts @@ -11,21 +11,20 @@ import { AddressType } from '@summerfi/sdk-common' export default async function getLendingPoolInfoTest() { type GetLendingPoolInfoType = RPCClientType['protocols']['getLendingPoolInfo']['query'] - const getLendingPoolInfoQuery: GetLendingPoolInfoType = jest.fn(async (params) => { - expect(params).toBeDefined() - expect(params.poolId).toBeDefined() + const getLendingPoolInfoQuery: GetLendingPoolInfoType = jest.fn(async (poolId) => { + expect(poolId).toBeDefined() - if (!isMakerLendingPoolId(params.poolId)) { + if (!isMakerLendingPoolId(poolId)) { fail('PoolId is not MakerPoolId') } - expect(params.poolId.protocol).toBeDefined() - expect(params.poolId.protocol.name).toBe(ProtocolName.Maker) - expect(params.poolId.ilkType).toBe(ILKType.ETH_A) + expect(poolId.protocol).toBeDefined() + expect(poolId.protocol.name).toBe(ProtocolName.Maker) + expect(poolId.ilkType).toBe(ILKType.ETH_A) return { type: PoolType.Lending, - id: params.poolId, + id: poolId, } as unknown as MakerLendingPoolInfo }) diff --git a/sdk/sdk-client/tests/queries/simulateRefinance.subtest.ts b/sdk/sdk-client/tests/queries/simulateRefinance.subtest.ts index 18b2491248..d89ab25e5a 100644 --- a/sdk/sdk-client/tests/queries/simulateRefinance.subtest.ts +++ b/sdk/sdk-client/tests/queries/simulateRefinance.subtest.ts @@ -3,25 +3,23 @@ import { SDKManager } from '../../src/implementation/SDKManager' import { RPCClientType, SparkPositionId } from '../../src/rpc/SDKClient' import { ILKType, - IMakerLendingPoolData, + MakerLendingPool, + MakerLendingPoolId, + MakerPosition, MakerPositionId, + MakerProtocol, } from '@summerfi/protocol-plugins/plugins/maker' import { ISimulation, SimulationType } from '@summerfi/sdk-common/simulation' import { Address, - AddressType, ChainFamilyMap, ChainInfo, Percentage, - Position, PositionType, Token, TokenAmount, } from '@summerfi/sdk-common/common' -import { IRefinanceParameters } from '@summerfi/sdk-common/orders' -import { IMakerProtocolData } from '@summerfi/protocol-plugins/plugins/maker/interfaces/IMakerProtocol' -import { IMakerLendingPoolIdData } from '@summerfi/protocol-plugins/plugins/maker/interfaces/IMakerLendingPoolId' -import { IPositionData } from '@summerfi/sdk-common' +import { RefinanceParameters } from '@summerfi/sdk-common' export default async function simulateRefinanceTest() { type SimulateRefinanceType = RPCClientType['simulation']['refinance']['query'] @@ -32,9 +30,15 @@ export default async function simulateRefinanceTest() { targetPosition: { type: params.sourcePosition.type, id: SparkPositionId.createFrom({ id: '0987654321' }), - debtAmount: params.targetPosition.debtAmount, - collateralAmount: params.targetPosition.collateralAmount, - pool: params.targetPosition.pool, + debtAmount: TokenAmount.createFrom({ + amount: params.sourcePosition.debtAmount.amount, + token: params.targetPool.debtToken, + }), + collateralAmount: TokenAmount.createFrom({ + amount: params.sourcePosition.collateralAmount.amount, + token: params.targetPool.collateralToken, + }), + pool: params.targetPool, }, swaps: [], steps: [], @@ -72,72 +76,53 @@ export default async function simulateRefinanceTest() { decimals: 18, }) - const protocol: IMakerProtocolData = { + const USDC = Token.createFrom({ + chainInfo, + address: Address.createFromEthereum({ value: '0x6b175474e89094c44da98b954eedeac495271d0f' }), + symbol: 'USDC', + name: 'USD Coin', + decimals: 6, + }) + + const protocol = MakerProtocol.createFrom({ name: ProtocolName.Maker, chainInfo: chainInfo, - } + }) - const poolId: IMakerLendingPoolIdData = { + const poolId = MakerLendingPoolId.createFrom({ protocol: protocol, ilkType: ILKType.ETH_A, - collateralToken: { - address: { - type: AddressType.Ethereum, - value: '0x6b175474e89094c44da98b954eedeac495271d0f', - }, - chainInfo: { chainId: 1, name: 'Ethereum' }, - name: 'USD Coin', - symbol: 'USDC', - decimals: 6, - }, - debtToken: { - address: { - type: AddressType.Ethereum, - value: '0x6b175474e89094c44da98b954eedeac495271d0f', - }, - chainInfo: { chainId: 1, name: 'Ethereum' }, - name: 'USD Coin', - symbol: 'USDC', - decimals: 6, - }, - } + collateralToken: USDC, + debtToken: USDC, + }) - const pool: IMakerLendingPoolData = { + const pool = MakerLendingPool.createFrom({ type: PoolType.Lending, id: poolId, collateralToken: poolId.collateralToken, debtToken: poolId.debtToken, - } + }) - const prevPosition: IPositionData = { + const prevPosition = MakerPosition.createFrom({ type: PositionType.Multiply, pool: pool, debtAmount: TokenAmount.createFrom({ token: DAI, amount: '56.78' }), collateralAmount: TokenAmount.createFrom({ token: WETH, amount: '105.98' }), id: MakerPositionId.createFrom({ id: '1234567890', vaultId: '34' }), - } + }) - const targetPool: IMakerLendingPoolData = { + const targetPool = MakerLendingPool.createFrom({ type: PoolType.Lending as const, id: poolId, collateralToken: poolId.collateralToken, debtToken: poolId.debtToken, - } + }) - const targetPosition = { - type: PositionType.Multiply, - id: { - id: 'newEmptyPositionFromPool', - }, - debtAmount: prevPosition.debtAmount, - collateralAmount: prevPosition.collateralAmount, - pool: targetPool, - } as unknown as Position - const refinanceParameters: IRefinanceParameters = { + const refinanceParameters = RefinanceParameters.createFrom({ sourcePosition: prevPosition, - targetPosition: targetPosition, + targetPool: targetPool, slippage: Percentage.createFrom({ value: 0.5 }), - } + }) const simulation = await sdkManager.simulator.refinance.simulateRefinancePosition(refinanceParameters) @@ -148,6 +133,6 @@ export default async function simulateRefinanceTest() { expect(simulation.sourcePosition?.id).toBe(prevPosition.id) expect(simulation.targetPosition).toBeDefined() expect(simulation.targetPosition.id).toBeDefined() - expect(simulation.targetPosition.pool.id).toBe(targetPool.id) + expect(simulation.targetPosition.pool.id).toEqual(targetPool.id) expect(simulation.steps).toBeDefined() } diff --git a/sdk/sdk-common/src/common/implementation/ChainInfo.ts b/sdk/sdk-common/src/common/implementation/ChainInfo.ts index 658edcdad5..4d1b55efc3 100644 --- a/sdk/sdk-common/src/common/implementation/ChainInfo.ts +++ b/sdk/sdk-common/src/common/implementation/ChainInfo.ts @@ -4,7 +4,7 @@ import { ChainId } from '../aliases/ChainId' /** * @name ChainInfo - * @see IChainInfoData + * @see IChainInfo */ export class ChainInfo implements IChainInfo { readonly chainId: ChainId diff --git a/sdk/sdk-common/src/orders/implementation/RefinanceParameters.ts b/sdk/sdk-common/src/orders/implementation/RefinanceParameters.ts new file mode 100644 index 0000000000..ade38ab320 --- /dev/null +++ b/sdk/sdk-common/src/orders/implementation/RefinanceParameters.ts @@ -0,0 +1,33 @@ +import { SerializationService } from '../../services/SerializationService' +import { IRefinanceParameters } from '../interfaces' +import { IPosition } from '../../common/interfaces/IPosition' +import { IPercentage } from '../../common/interfaces/IPercentage' +import { ILendingPool } from '../../protocols' + +/** + * @name RefinanceParameters + * @see IRefinanceParameters + */ +export class RefinanceParameters implements IRefinanceParameters { + readonly sourcePosition: IPosition + readonly targetPool: ILendingPool + readonly slippage: IPercentage + + /** Factory method */ + static createFrom(params: IRefinanceParameters): RefinanceParameters { + return new RefinanceParameters(params) + } + + /** Sealed constructor */ + private constructor(params: IRefinanceParameters) { + this.sourcePosition = params.sourcePosition + this.targetPool = params.targetPool + this.slippage = params.slippage + } + + toString(): string { + return `Refinance Parameters (source: ${this.sourcePosition}, target: ${this.targetPool}, slippage: ${this.slippage})` + } +} + +SerializationService.registerClass(RefinanceParameters) diff --git a/sdk/sdk-common/src/orders/implementation/index.ts b/sdk/sdk-common/src/orders/implementation/index.ts index ce0b197a98..a933500a2e 100644 --- a/sdk/sdk-common/src/orders/implementation/index.ts +++ b/sdk/sdk-common/src/orders/implementation/index.ts @@ -1 +1,2 @@ export * from './PositionsManager' +export * from './RefinanceParameters' diff --git a/sdk/sdk-common/src/orders/interfaces/refinance/IRefinanceParameters.ts b/sdk/sdk-common/src/orders/interfaces/refinance/IRefinanceParameters.ts index 7590e9444f..c14e55025c 100644 --- a/sdk/sdk-common/src/orders/interfaces/refinance/IRefinanceParameters.ts +++ b/sdk/sdk-common/src/orders/interfaces/refinance/IRefinanceParameters.ts @@ -1,12 +1,38 @@ -import { IPercentageData } from '../../../common/interfaces/IPercentage' -import { IPositionData } from '../../../common/interfaces/IPosition' +import { z } from 'zod' +import { IPercentage, PercentageDataSchema } from '../../../common/interfaces/IPercentage' +import { IPosition, PositionDataSchema } from '../../../common/interfaces/IPosition' +import { ILendingPool, LendingPoolDataSchema } from '../../../protocols' -export interface IRefinanceParameters { - sourcePosition: IPositionData - targetPosition: IPositionData - slippage: IPercentageData +/** + * Parameters for a refinance simulation + */ +export interface IRefinanceParameters extends IRefinanceParametersData { + /** Existing position to be refinanced */ + readonly sourcePosition: IPosition + /** Target pool where the source position will be moved */ + readonly targetPool: ILendingPool + /** Maximum slippage allowed for the simulation */ + readonly slippage: IPercentage } -export function isRefinanceParameters(parameters: unknown): parameters is IRefinanceParameters { - return typeof parameters === 'object' && parameters !== null && 'slippage' in parameters +/** + * Zod schema for the refinance parameters + */ +export const RefinanceParametersDataSchema = z.object({ + sourcePosition: PositionDataSchema, + targetPool: LendingPoolDataSchema, + slippage: PercentageDataSchema, +}) + +export type IRefinanceParametersData = Readonly> + +/** + * Type guard for the refinance parameters + * @param maybeRefinanceParameters Parameters to check + * @returns True if the parameters are valid + */ +export function isRefinanceParameters( + maybeRefinanceParameters: unknown, +): maybeRefinanceParameters is IRefinanceParameters { + return RefinanceParametersDataSchema.safeParse(maybeRefinanceParameters).success } diff --git a/sdk/sdk-common/src/orders/interfaces/refinance/index.ts b/sdk/sdk-common/src/orders/interfaces/refinance/index.ts index 0da3b3a7aa..d3abb6bea2 100644 --- a/sdk/sdk-common/src/orders/interfaces/refinance/index.ts +++ b/sdk/sdk-common/src/orders/interfaces/refinance/index.ts @@ -1 +1 @@ -export type { IRefinanceParameters } from './IRefinanceParameters' +export * from './IRefinanceParameters' diff --git a/sdk/sdk-common/src/protocols/interfaces/ILendingPool.ts b/sdk/sdk-common/src/protocols/interfaces/ILendingPool.ts index 0639a3da1f..0e45232f41 100644 --- a/sdk/sdk-common/src/protocols/interfaces/ILendingPool.ts +++ b/sdk/sdk-common/src/protocols/interfaces/ILendingPool.ts @@ -47,6 +47,6 @@ export type ILendingPoolData = Readonly> * * It also asserts the type so that TypeScript knows that the object is an ILendingPool */ -export function isLendingPool(maybePool: unknown): maybePool is ILendingPoolData { +export function isLendingPool(maybePool: unknown): maybePool is ILendingPool { return LendingPoolDataSchema.safeParse(maybePool).success } diff --git a/sdk/sdk-common/src/protocols/interfaces/ILendingPoolId.ts b/sdk/sdk-common/src/protocols/interfaces/ILendingPoolId.ts index d037ee6635..fa9535237e 100644 --- a/sdk/sdk-common/src/protocols/interfaces/ILendingPoolId.ts +++ b/sdk/sdk-common/src/protocols/interfaces/ILendingPoolId.ts @@ -36,6 +36,6 @@ export type ILendingPoolIdData = Readonly> * @param maybePool * @returns true if the object is an IPool */ -export function isPool(maybePool: unknown): maybePool is IPoolData { +export function isPool(maybePool: unknown): maybePool is IPool { return PoolDataSchema.safeParse(maybePool).success } diff --git a/sdk/sdk-common/src/protocols/interfaces/IPoolId.ts b/sdk/sdk-common/src/protocols/interfaces/IPoolId.ts index 92f2a73e58..ca6aa0d404 100644 --- a/sdk/sdk-common/src/protocols/interfaces/IPoolId.ts +++ b/sdk/sdk-common/src/protocols/interfaces/IPoolId.ts @@ -30,6 +30,6 @@ export type IPoolIdData = Readonly> * @param maybePoolId * @returns true if the object is an IPoolId */ -export function isPoolId(maybePoolId: unknown): maybePoolId is IPoolIdData { +export function isPoolId(maybePoolId: unknown): maybePoolId is IPoolId { return PoolIdDataSchema.safeParse(maybePoolId).success } diff --git a/sdk/sdk-common/src/protocols/interfaces/IPoolInfo.ts b/sdk/sdk-common/src/protocols/interfaces/IPoolInfo.ts index 7509572d7e..8e167da207 100644 --- a/sdk/sdk-common/src/protocols/interfaces/IPoolInfo.ts +++ b/sdk/sdk-common/src/protocols/interfaces/IPoolInfo.ts @@ -33,6 +33,6 @@ export type IPoolInfoData = Readonly> * @param maybePoolInfo * @returns true if the object is an IPoolInfo */ -export function isPoolInfo(maybePoolInfo: unknown): maybePoolInfo is IPoolInfoData { +export function isPoolInfo(maybePoolInfo: unknown): maybePoolInfo is IPoolInfo { return PoolInfoDataSchema.safeParse(maybePoolInfo).success } diff --git a/sdk/sdk-common/src/protocols/interfaces/IProtocol.ts b/sdk/sdk-common/src/protocols/interfaces/IProtocol.ts index 907e89dfa5..8ad183a031 100644 --- a/sdk/sdk-common/src/protocols/interfaces/IProtocol.ts +++ b/sdk/sdk-common/src/protocols/interfaces/IProtocol.ts @@ -42,6 +42,6 @@ export type IProtocolData = Readonly> * @param maybeProtocol * @returns true if the object is an IProtocol */ -export function isProtocol(maybeProtocol: unknown): maybeProtocol is IProtocolData { +export function isProtocol(maybeProtocol: unknown): maybeProtocol is IProtocol { return ProtocolDataSchema.safeParse(maybeProtocol).success } diff --git a/sdk/sdk-common/src/user/IUser.ts b/sdk/sdk-common/src/user/IUser.ts index 66e8447ca2..b47512cb81 100644 --- a/sdk/sdk-common/src/user/IUser.ts +++ b/sdk/sdk-common/src/user/IUser.ts @@ -1,6 +1,35 @@ -import { ChainInfo, Wallet } from '@summerfi/sdk-common/common' +import { ChainInfoDataSchema, IChainInfo } from '../common/interfaces/IChainInfo' +import { IWallet, WalletDataSchema } from '../common/interfaces/IWallet' +import { z } from 'zod' -export interface IUser { - wallet: Wallet - chainInfo: ChainInfo +/** + * Represents a user of the system connected with a wallet on a particular chain + */ +export interface IUser extends IUserData { + /** The wallet of the user */ + readonly wallet: IWallet + /** The chain the user is connected to */ + readonly chainInfo: IChainInfo +} + +/** + * Zod schema for the data part of IUser + */ +export const UserDataSchema = z.object({ + wallet: WalletDataSchema, + chainInfo: ChainInfoDataSchema, +}) + +/** + * Type for the data part of the IUser interface + */ +export type IUserData = Readonly> + +/** + * Type guard for IUser + * @param maybeUser Object to be checked + * @returns true if the object is an IUser + */ +export function isUser(maybeUser: unknown): maybeUser is IUser { + return UserDataSchema.safeParse(maybeUser).success } diff --git a/sdk/sdk-e2e/tests/refinanceMakerSparkAlreadyImported.test.ts b/sdk/sdk-e2e/tests/refinanceMakerSparkAlreadyImported.test.ts index 61cd7594d3..d9d551f404 100644 --- a/sdk/sdk-e2e/tests/refinanceMakerSparkAlreadyImported.test.ts +++ b/sdk/sdk-e2e/tests/refinanceMakerSparkAlreadyImported.test.ts @@ -7,14 +7,13 @@ import { type Maybe, ChainFamilyMap, AddressValue, - newEmptyPositionFromPool, PositionType, } from '@summerfi/sdk-common/common' import { ProtocolName } from '@summerfi/sdk-common/protocols' import { makeSDK, type Chain, type User, ProtocolClient } from '@summerfi/sdk-client' import { CommonTokenSymbols } from '@summerfi/sdk-common/common/enums' -import { PositionsManager, IRefinanceParameters, Order } from '@summerfi/sdk-common/orders' +import { PositionsManager, Order, RefinanceParameters } from '@summerfi/sdk-common/orders' import { ISimulation, RefinanceSimulationTypes } from '@summerfi/sdk-common/simulation' import { TransactionUtils } from './utils/TransactionUtils' import { @@ -182,13 +181,14 @@ describe.skip('Refinance Maker Spark | SDK', () => { assert(false, 'Spark pool type is not lending') } - const emptyTargetPosition = newEmptyPositionFromPool(sparkPool) + const refinanceParameters = RefinanceParameters.createFrom({ + sourcePosition: makerPosition, + targetPool: sparkPool, + slippage: Percentage.createFrom({ value: 0.2 }), + }) + const refinanceSimulation: ISimulation = - await sdk.simulator.refinance.simulateRefinancePosition({ - sourcePosition: makerPosition, - targetPosition: emptyTargetPosition, - slippage: Percentage.createFrom({ value: 0.2 }), - } as IRefinanceParameters) + await sdk.simulator.refinance.simulateRefinancePosition(refinanceParameters) expect(refinanceSimulation).toBeDefined() diff --git a/sdk/sdk-e2e/tests/refinanceMakerSparkAnyPair.test.ts b/sdk/sdk-e2e/tests/refinanceMakerSparkAnyPair.test.ts index 217f05f44d..579a6d4478 100644 --- a/sdk/sdk-e2e/tests/refinanceMakerSparkAnyPair.test.ts +++ b/sdk/sdk-e2e/tests/refinanceMakerSparkAnyPair.test.ts @@ -5,13 +5,12 @@ import { Address, type Maybe, ChainFamilyMap, - newEmptyPositionFromPool, PositionType, } from '@summerfi/sdk-common/common' import { ProtocolName, isLendingPool } from '@summerfi/sdk-common/protocols' import { ProtocolClient, makeSDK, type Chain, type User } from '@summerfi/sdk-client' -import { PositionsManager, IRefinanceParameters, Order } from '@summerfi/sdk-common/orders' +import { PositionsManager, Order, RefinanceParameters } from '@summerfi/sdk-common/orders' import { ISimulation } from '@summerfi/sdk-common/simulation' import { TransactionUtils } from './utils/TransactionUtils' @@ -177,13 +176,14 @@ describe.skip('Refinance Maker Spark | SDK', () => { assert(false, 'Spark pool type is not lending') } - const emptyTargetPosition = newEmptyPositionFromPool(sparkPool) + const refinanceParameters = RefinanceParameters.createFrom({ + sourcePosition: makerPosition, + targetPool: sparkPool, + slippage: Percentage.createFrom({ value: 0.2 }), + }) + const refinanceSimulation: ISimulation = - await sdk.simulator.refinance.simulateRefinancePosition({ - sourcePosition: makerPosition, - targetPosition: emptyTargetPosition, - slippage: Percentage.createFrom({ value: 0.2 }), - } as IRefinanceParameters) + await sdk.simulator.refinance.simulateRefinancePosition(refinanceParameters) expect(refinanceSimulation).toBeDefined() diff --git a/sdk/sdk-e2e/tests/refinanceMorphoSparkAnyPair.test.ts b/sdk/sdk-e2e/tests/refinanceMorphoSparkAnyPair.test.ts index ecfdd2f78e..6c5042b384 100644 --- a/sdk/sdk-e2e/tests/refinanceMorphoSparkAnyPair.test.ts +++ b/sdk/sdk-e2e/tests/refinanceMorphoSparkAnyPair.test.ts @@ -11,11 +11,10 @@ import { Address, type Maybe, ChainFamilyMap, - newEmptyPositionFromPool, PositionType, IToken, } from '@summerfi/sdk-common' -import { PositionsManager, IRefinanceParameters, Order } from '@summerfi/sdk-common/orders' +import { PositionsManager, Order, RefinanceParameters } from '@summerfi/sdk-common/orders' import { SparkLendingPoolId, isSparkLendingPoolId, @@ -159,13 +158,14 @@ describe.skip('Refinance Morpho Spark | SDK', () => { assert(false, 'Spark pool type is not lending') } - const emptyTargetPosition = newEmptyPositionFromPool(sparkPool) + const refinanceParameters = RefinanceParameters.createFrom({ + sourcePosition: morphoPosition, + targetPool: sparkPool, + slippage: Percentage.createFrom({ value: 0.2 }), + }) + const refinanceSimulation: ISimulation = - await sdk.simulator.refinance.simulateRefinancePosition({ - sourcePosition: morphoPosition, - targetPosition: emptyTargetPosition, - slippage: Percentage.createFrom({ value: 0.2 }), - } as IRefinanceParameters) + await sdk.simulator.refinance.simulateRefinancePosition(refinanceParameters) expect(refinanceSimulation).toBeDefined() diff --git a/sdk/sdk-server/src/TRPC.ts b/sdk/sdk-server/src/TRPC.ts index dc5ef7711f..b8aa327d12 100644 --- a/sdk/sdk-server/src/TRPC.ts +++ b/sdk/sdk-server/src/TRPC.ts @@ -15,7 +15,7 @@ export const publicProcedure = t.procedure.use(async (opts) => { if (process.env.SDK_LOGGING_ENABLED) { console.log('- path => ', path) console.log('- type => ', type) - console.log('- rawInput => ', JSON.stringify(await getRawInput(), null, 2)) + console.log('- rawInput => ', await getRawInput()) } const result = await opts.next() diff --git a/sdk/sdk-server/src/Types.ts b/sdk/sdk-server/src/Types.ts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/sdk/sdk-server/src/handlers/buildOrder.ts b/sdk/sdk-server/src/handlers/buildOrder.ts index d7136eef50..d8002e9af6 100644 --- a/sdk/sdk-server/src/handlers/buildOrder.ts +++ b/sdk/sdk-server/src/handlers/buildOrder.ts @@ -1,21 +1,29 @@ import { z } from 'zod' -import type { IPositionsManager, Order } from '@summerfi/sdk-common/orders' +import type { Order } from '@summerfi/sdk-common/orders' import type { ISimulation, SimulationType } from '@summerfi/sdk-common/simulation' -import type { IUser } from '@summerfi/sdk-common/user' +import { isUser } from '@summerfi/sdk-common/user' import { Maybe } from '@summerfi/sdk-common/common' import { publicProcedure } from '../TRPC' +import { isPositionsManager } from '@summerfi/sdk-common' export const buildOrder = publicProcedure .input( z.object({ - user: z.custom((user) => user !== undefined), - positionsManager: z.custom( - (positionsManager) => positionsManager !== undefined, - ), + user: z.any(), + positionsManager: z.any(), simulation: z.custom>((simulation) => simulation !== undefined), }), ) .mutation(async (opts): Promise> => { + if (!isUser(opts.input.user)) { + throw new Error('Invalid user') + } + if (!isPositionsManager(opts.input.positionsManager)) { + throw new Error('Invalid positions manager') + } + + // TODO: validate Simulation with a type guard instead of a Zod schema + return opts.ctx.orderPlannerService.buildOrder({ user: opts.input.user, positionsManager: opts.input.positionsManager, diff --git a/sdk/sdk-server/src/handlers/getLendingPool.ts b/sdk/sdk-server/src/handlers/getLendingPool.ts index d51855dcca..6e937047a5 100644 --- a/sdk/sdk-server/src/handlers/getLendingPool.ts +++ b/sdk/sdk-server/src/handlers/getLendingPool.ts @@ -1,13 +1,11 @@ +import { isLendingPoolId } from '@summerfi/sdk-common' import { publicProcedure } from '../TRPC' -import { LendingPoolIdDataSchema } from '@summerfi/sdk-common/protocols' import { z } from 'zod' -export const getLendingPool = publicProcedure - .input( - z.object({ - poolId: LendingPoolIdDataSchema.catchall(z.any()), - }), - ) - .query(async (opts) => { - return await opts.ctx.protocolManager.getLendingPool(opts.input.poolId) - }) +export const getLendingPool = publicProcedure.input(z.any()).query(async (opts) => { + if (!isLendingPoolId(opts.input)) { + throw new Error('Invalid lending pool id') + } + + return opts.ctx.protocolManager.getLendingPool(opts.input) +}) diff --git a/sdk/sdk-server/src/handlers/getLendingPoolInfo.ts b/sdk/sdk-server/src/handlers/getLendingPoolInfo.ts index 6a7f37056c..ef22f22696 100644 --- a/sdk/sdk-server/src/handlers/getLendingPoolInfo.ts +++ b/sdk/sdk-server/src/handlers/getLendingPoolInfo.ts @@ -1,9 +1,11 @@ +import { isLendingPoolId } from '@summerfi/sdk-common' import { publicProcedure } from '../TRPC' -import { PoolIdDataSchema } from '@summerfi/sdk-common/protocols' import { z } from 'zod' -export const getLendingPoolInfo = publicProcedure - .input(z.object({ poolId: PoolIdDataSchema })) - .query(async (opts) => { - return await opts.ctx.protocolManager.getLendingPoolInfo(opts.input.poolId) - }) +export const getLendingPoolInfo = publicProcedure.input(z.any()).query(async (opts) => { + if (!isLendingPoolId(opts.input)) { + throw new Error('Invalid lending pool id') + } + + return opts.ctx.protocolManager.getLendingPoolInfo(opts.input) +}) diff --git a/sdk/sdk-server/src/handlers/getPosition.ts b/sdk/sdk-server/src/handlers/getPosition.ts index 7f5f4feace..a2c941c7d4 100644 --- a/sdk/sdk-server/src/handlers/getPosition.ts +++ b/sdk/sdk-server/src/handlers/getPosition.ts @@ -1,15 +1,13 @@ import { z } from 'zod' -import type { ChainInfo, Position, PositionId, Wallet } from '@summerfi/sdk-common/common' import { publicProcedure } from '../TRPC' +import { IPosition, Maybe, isPositionId } from '@summerfi/sdk-common' export const getPosition = publicProcedure - .input( - z.object({ - id: z.custom((id) => id !== undefined), - chain: z.custom((chainInfo) => chainInfo !== undefined), - wallet: z.custom((wallet) => wallet !== undefined), - }), - ) - .query(async (): Promise => { + .input(z.any()) + .query(async (opts): Promise> => { + if (!isPositionId(opts.input)) { + throw new Error('Invalid position id') + } + throw new Error('Not implemented') }) diff --git a/sdk/sdk-server/src/handlers/getRefinanceSimulation.ts b/sdk/sdk-server/src/handlers/getRefinanceSimulation.ts index 0be39e8e0f..fac12c1ab8 100644 --- a/sdk/sdk-server/src/handlers/getRefinanceSimulation.ts +++ b/sdk/sdk-server/src/handlers/getRefinanceSimulation.ts @@ -1,47 +1,22 @@ -import { z } from 'zod' import type { ISimulation, RefinanceSimulationTypes } from '@summerfi/sdk-common/simulation' -import { - refinanceLendingToLendingSamePair, - type IRefinanceDependencies, - refinanceLendingToLendingAnyPair, - refinanceLendingToLendingNoDebt, -} from '@summerfi/simulator-service/strategies' -import type { IRefinanceParameters } from '@summerfi/sdk-common/orders' +import { refinanceStrategyRouter } from '@summerfi/simulator-service/strategies' import { publicProcedure } from '../TRPC' -import { Token } from '@summerfi/sdk-common/common' - -const inputSchema = z.custom((parameters) => parameters !== undefined) - -function isToSamePair(parameters: IRefinanceParameters): boolean { - const sourceDebtToken = Token.createFrom(parameters.sourcePosition.debtAmount.token) - const targetDebtToken = Token.createFrom(parameters.targetPosition.debtAmount.token) - const sourceCollateralToken = Token.createFrom(parameters.sourcePosition.collateralAmount.token) - const targetCollateralToken = Token.createFrom(parameters.targetPosition.collateralAmount.token) - - return ( - sourceDebtToken.equals(targetDebtToken) && sourceCollateralToken.equals(targetCollateralToken) - ) -} +import { isRefinanceParameters } from '@summerfi/sdk-common/orders' +import { z } from 'zod' export const getRefinanceSimulation = publicProcedure - .input(inputSchema) + .input(z.any()) .query(async (opts): Promise> => { - const args: IRefinanceParameters = opts.input - - const dependencies: IRefinanceDependencies = { - swapManager: opts.ctx.swapManager, - oracleManager: opts.ctx.oracleManager, - protocolManager: opts.ctx.protocolManager, - } - - if (opts.input.sourcePosition.debtAmount.amount === '0') { - return refinanceLendingToLendingNoDebt(args, dependencies) - } - - // TODO: in the end we should use just any pair - if (isToSamePair(opts.input)) { - return refinanceLendingToLendingSamePair(args, dependencies) + if (!isRefinanceParameters(opts.input)) { + throw new Error('Invalid refinance parameters') } - return refinanceLendingToLendingAnyPair(args, dependencies) + return refinanceStrategyRouter({ + refinanceParameters: opts.input, + refinanceDependencies: { + swapManager: opts.ctx.swapManager, + oracleManager: opts.ctx.oracleManager, + protocolManager: opts.ctx.protocolManager, + }, + }) }) diff --git a/sdk/sdk-server/src/handlers/getTokenByAddress.ts b/sdk/sdk-server/src/handlers/getTokenByAddress.ts index 2c48f6c714..9de41e2abd 100644 --- a/sdk/sdk-server/src/handlers/getTokenByAddress.ts +++ b/sdk/sdk-server/src/handlers/getTokenByAddress.ts @@ -1,24 +1,24 @@ import { publicProcedure } from '../TRPC' -import { - Address, - AddressDataSchema, - ChainInfo, - ChainInfoDataSchema, - Maybe, -} from '@summerfi/sdk-common' -import { Token } from '@summerfi/sdk-common/common' +import { IToken, Maybe, isAddress, isChainInfo } from '@summerfi/sdk-common' import { z } from 'zod' export const getTokenByAddress = publicProcedure .input( z.object({ - chainInfo: ChainInfoDataSchema, - address: AddressDataSchema, + chainInfo: z.any(), + address: z.any(), }), ) - .query(async (opts): Promise> => { + .query(async (opts): Promise> => { + if (!isChainInfo(opts.input.chainInfo)) { + throw new Error('Invalid chain info') + } + if (!isAddress(opts.input.address)) { + throw new Error('Invalid address') + } + return opts.ctx.tokensManager.getTokenByAddress({ - chainInfo: ChainInfo.createFrom(opts.input.chainInfo), - address: Address.createFrom(opts.input.address), + chainInfo: opts.input.chainInfo, + address: opts.input.address, }) }) diff --git a/sdk/sdk-server/src/handlers/getTokenByName.ts b/sdk/sdk-server/src/handlers/getTokenByName.ts index a59bf444c2..02d702eb84 100644 --- a/sdk/sdk-server/src/handlers/getTokenByName.ts +++ b/sdk/sdk-server/src/handlers/getTokenByName.ts @@ -1,17 +1,21 @@ import { publicProcedure } from '../TRPC' -import { ChainInfo, ChainInfoDataSchema, Maybe, Token } from '@summerfi/sdk-common' +import { Maybe, Token, isChainInfo } from '@summerfi/sdk-common' import { z } from 'zod' export const getTokenByName = publicProcedure .input( z.object({ - chainInfo: ChainInfoDataSchema, + chainInfo: z.any(), name: z.string(), }), ) .query(async (opts): Promise> => { + if (!isChainInfo(opts.input.chainInfo)) { + throw new Error('Invalid chain info') + } + return opts.ctx.tokensManager.getTokenByName({ - chainInfo: ChainInfo.createFrom(opts.input.chainInfo), + chainInfo: opts.input.chainInfo, name: opts.input.name, }) }) diff --git a/sdk/sdk-server/src/handlers/getTokenBySymbol.ts b/sdk/sdk-server/src/handlers/getTokenBySymbol.ts index 7835b06e19..c7a7f02e69 100644 --- a/sdk/sdk-server/src/handlers/getTokenBySymbol.ts +++ b/sdk/sdk-server/src/handlers/getTokenBySymbol.ts @@ -1,17 +1,21 @@ import { publicProcedure } from '../TRPC' -import { ChainInfo, ChainInfoDataSchema, Maybe, Token } from '@summerfi/sdk-common' +import { IToken, Maybe, isChainInfo } from '@summerfi/sdk-common' import { z } from 'zod' export const getTokenBySymbol = publicProcedure .input( z.object({ - chainInfo: ChainInfoDataSchema, + chainInfo: z.any(), symbol: z.string(), }), ) - .query(async (opts): Promise> => { + .query(async (opts): Promise> => { + if (!isChainInfo(opts.input.chainInfo)) { + throw new Error('Invalid chain info') + } + return opts.ctx.tokensManager.getTokenBySymbol({ - chainInfo: ChainInfo.createFrom(opts.input.chainInfo), + chainInfo: opts.input.chainInfo, symbol: opts.input.symbol, }) }) diff --git a/sdk/simulator-service/src/strategies/common/RefinanceStrategyRouter.ts b/sdk/simulator-service/src/strategies/common/RefinanceStrategyRouter.ts new file mode 100644 index 0000000000..c13bcd9d20 --- /dev/null +++ b/sdk/simulator-service/src/strategies/common/RefinanceStrategyRouter.ts @@ -0,0 +1,45 @@ +import { IRefinanceParameters } from '@summerfi/sdk-common/orders' +import { refinanceLendingToLendingAnyPair } from '../refinanceAnyPair/RefinanceLendingToLendingAnyPair' +import { refinanceLendingToLendingNoDebt } from '../refinanceNoDebt/RefinanceLendingToLendingNoDebt' +import { refinanceLendingToLendingSamePair } from '../refinanceSamePair/RefinanceLendingToLendingSamePair' +import { ISwapManager } from '@summerfi/swap-common/interfaces' +import { IOracleManager } from '@summerfi/oracle-common' +import { IProtocolManager } from '@summerfi/protocol-manager-common' + +function isToSamePair(parameters: IRefinanceParameters): boolean { + const { sourcePosition, targetPool } = parameters + + return ( + sourcePosition.debtAmount.token.equals(targetPool.debtToken) && + sourcePosition.collateralAmount.token.equals(targetPool.collateralToken) + ) +} + +/** + * Choses the correct refinance strategy based on the parameters + * @param refinanceParameters Parameters for the refinance simulation + * @param refinanceDependencies Dependencies for the simulation + * @returns The simulation result + */ +export function refinanceStrategyRouter(params: { + refinanceParameters: IRefinanceParameters + refinanceDependencies: { + swapManager: ISwapManager + oracleManager: IOracleManager + protocolManager: IProtocolManager + } +}) { + if (params.refinanceParameters.sourcePosition.debtAmount.amount === '0') { + return refinanceLendingToLendingNoDebt(params.refinanceParameters, params.refinanceDependencies) + } + + // TODO: in the end we should use just any pair + if (isToSamePair(params.refinanceParameters)) { + return refinanceLendingToLendingSamePair( + params.refinanceParameters, + params.refinanceDependencies, + ) + } + + return refinanceLendingToLendingAnyPair(params.refinanceParameters, params.refinanceDependencies) +} diff --git a/sdk/simulator-service/src/strategies/common/index.ts b/sdk/simulator-service/src/strategies/common/index.ts index cf701220bd..f85330ef32 100644 --- a/sdk/simulator-service/src/strategies/common/index.ts +++ b/sdk/simulator-service/src/strategies/common/index.ts @@ -1 +1,2 @@ export * from './Types' +export * from './RefinanceStrategyRouter' diff --git a/sdk/simulator-service/src/strategies/refinanceAnyPair/RefinanceLendingToLendingAnyPair.ts b/sdk/simulator-service/src/strategies/refinanceAnyPair/RefinanceLendingToLendingAnyPair.ts index 7559579707..9065be3e1d 100644 --- a/sdk/simulator-service/src/strategies/refinanceAnyPair/RefinanceLendingToLendingAnyPair.ts +++ b/sdk/simulator-service/src/strategies/refinanceAnyPair/RefinanceLendingToLendingAnyPair.ts @@ -9,7 +9,6 @@ import { } from '@summerfi/sdk-common/simulation' import { Simulator } from '../../implementation/simulator-engine' import { - Position, TokenAmount, Percentage, IToken, @@ -31,13 +30,16 @@ export async function refinanceLendingToLendingAnyPair( dependencies: IRefinanceDependencies, ): Promise> { // args validation - if (!isLendingPool(args.targetPosition.pool)) { + if (!isLendingPool(args.sourcePosition.pool)) { + throw new Error('Source pool is not a lending pool') + } + if (!isLendingPool(args.targetPool)) { throw new Error('Target pool is not a lending pool') } - const position = args.sourcePosition as Position - const sourcePool = await dependencies.protocolManager.getLendingPool(args.sourcePosition.pool.id) - const targetPool = await dependencies.protocolManager.getLendingPool(args.targetPosition.pool.id) + const position = args.sourcePosition + const sourcePool = args.sourcePosition.pool + const targetPool = args.targetPool if (!isLendingPool(sourcePool)) { throw new Error('Source pool is not a lending pool') @@ -47,12 +49,15 @@ export async function refinanceLendingToLendingAnyPair( throw new Error('Target pool is not a lending pool') } + console.log(sourcePool) + console.log(targetPool) + const FLASHLOAN_MARGIN = 1.001 const flashloanAmount = position.debtAmount.multiply(FLASHLOAN_MARGIN) const simulator = Simulator.create(refinanceLendingToLendingAnyPairStrategy) - const isCollateralSwapSkipped = !targetPool.collateralToken.equals(sourcePool.collateralToken) - const isDebtSwapSkipped = !targetPool.debtToken.equals(sourcePool.debtToken) + const isCollateralSwapSkipped = targetPool.collateralToken.equals(sourcePool.collateralToken) + const isDebtSwapSkipped = targetPool.debtToken.equals(sourcePool.debtToken) const simulation = await simulator .next(async () => ({ diff --git a/sdk/simulator-service/src/strategies/refinanceNoDebt/RefinanceLendingToLendingNoDebt.ts b/sdk/simulator-service/src/strategies/refinanceNoDebt/RefinanceLendingToLendingNoDebt.ts index 82ec54395a..f36424978b 100644 --- a/sdk/simulator-service/src/strategies/refinanceNoDebt/RefinanceLendingToLendingNoDebt.ts +++ b/sdk/simulator-service/src/strategies/refinanceNoDebt/RefinanceLendingToLendingNoDebt.ts @@ -5,7 +5,7 @@ import { TokenTransferTargetType, } from '@summerfi/sdk-common/simulation' import { Simulator } from '../../implementation/simulator-engine' -import { Percentage, Position, TokenAmount } from '@summerfi/sdk-common/common' +import { Percentage, TokenAmount } from '@summerfi/sdk-common/common' import { newEmptyPositionFromPool } from '@summerfi/sdk-common/common/utils' import { IRefinanceParameters } from '@summerfi/sdk-common/orders' import { isLendingPool } from '@summerfi/sdk-common/protocols' @@ -20,13 +20,16 @@ export async function refinanceLendingToLendingNoDebt( ISimulation > { // args validation - if (!isLendingPool(args.targetPosition.pool)) { + if (!isLendingPool(args.sourcePosition.pool)) { + throw new Error('Source pool is not a lending pool') + } + if (!isLendingPool(args.targetPool)) { throw new Error('Target pool is not a lending pool') } - const position = args.sourcePosition as Position - const sourcePool = await dependencies.protocolManager.getLendingPool(args.sourcePosition.pool.id) - const targetPool = await dependencies.protocolManager.getLendingPool(args.targetPosition.pool.id) + const position = args.sourcePosition + const sourcePool = args.sourcePosition.pool + const targetPool = args.targetPool if (!isLendingPool(targetPool)) { throw new Error('Target pool is not a lending pool') @@ -38,7 +41,7 @@ export async function refinanceLendingToLendingNoDebt( amount: '0', }) - const isCollateralSwapSkipped = !targetPool.collateralToken.equals(sourcePool.collateralToken) + const isCollateralSwapSkipped = targetPool.collateralToken.equals(sourcePool.collateralToken) const simulation = await simulator .next(async () => ({ diff --git a/sdk/simulator-service/src/strategies/refinanceSamePair/RefinanceLendingToLendingSamePair.ts b/sdk/simulator-service/src/strategies/refinanceSamePair/RefinanceLendingToLendingSamePair.ts index 746668d97c..9d3fe46549 100644 --- a/sdk/simulator-service/src/strategies/refinanceSamePair/RefinanceLendingToLendingSamePair.ts +++ b/sdk/simulator-service/src/strategies/refinanceSamePair/RefinanceLendingToLendingSamePair.ts @@ -6,7 +6,7 @@ import { TokenTransferTargetType, } from '@summerfi/sdk-common/simulation' import { Simulator } from '../../implementation/simulator-engine' -import { Position, TokenAmount } from '@summerfi/sdk-common/common' +import { TokenAmount } from '@summerfi/sdk-common/common' import { newEmptyPositionFromPool } from '@summerfi/sdk-common/common/utils' import { IRefinanceParameters } from '@summerfi/sdk-common/orders' import { isLendingPool } from '@summerfi/sdk-common/protocols' @@ -15,15 +15,19 @@ import { type IRefinanceDependencies } from '../common/Types' export async function refinanceLendingToLendingSamePair( args: IRefinanceParameters, + /* eslint-disable-next-line @typescript-eslint/no-unused-vars */ dependencies: IRefinanceDependencies, ): Promise> { // args validation - if (!isLendingPool(args.targetPosition.pool)) { + if (!isLendingPool(args.sourcePosition.pool)) { + throw new Error('Source pool is not a lending pool') + } + if (!isLendingPool(args.targetPool)) { throw new Error('Target pool is not a lending pool') } - const position = args.sourcePosition as Position - const targetPool = await dependencies.protocolManager.getLendingPool(args.targetPosition.pool.id) + const position = args.sourcePosition + const targetPool = args.targetPool if (!isLendingPool(targetPool)) { throw new Error('Target pool is not a lending pool') diff --git a/sdk/simulator-service/tests/simulator.test.ts b/sdk/simulator-service/tests/simulator.test.ts index 571744a814..eb767a27bc 100644 --- a/sdk/simulator-service/tests/simulator.test.ts +++ b/sdk/simulator-service/tests/simulator.test.ts @@ -3,7 +3,7 @@ import { refinanceLendingToLendingAnyPair, refinanceLendingToLendingSamePair, } from '../src/strategies' -import { Percentage, newEmptyPositionFromPool } from '@summerfi/sdk-common/common' +import { Percentage } from '@summerfi/sdk-common/common' import { otherTestCollateral, otherTestDebt, @@ -22,7 +22,7 @@ describe('Refinance', () => { simulation = await refinanceLendingToLendingSamePair( { sourcePosition: testSourcePosition, - targetPosition: newEmptyPositionFromPool(testTargetLendingPool), + targetPool: testTargetLendingPool, slippage: Percentage.createFrom({ value: 1 }), }, mockRefinanceContext, @@ -76,7 +76,7 @@ describe('Refinance', () => { simulation = await refinanceLendingToLendingAnyPair( { sourcePosition: testSourcePosition, - targetPosition: newEmptyPositionFromPool(testTargetLendingPoolRequiredSwaps), + targetPool: testTargetLendingPoolRequiredSwaps, slippage: Percentage.createFrom({ value: 1 }), }, mockRefinanceContextRequiredSwaps,