Skip to content

Commit

Permalink
feat: add handler for any pair
Browse files Browse the repository at this point in the history
  • Loading branch information
paszkowskiDamian committed Apr 11, 2024
1 parent ccd5120 commit 2bfa302
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions sdk/sdk-common/src/common/utils/TokenUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IToken } from "../interfaces/IToken";

export function isSameTokens(a: IToken, b: IToken): boolean {
return a.address === b.address && a.chainInfo.chainId === b.chainInfo.chainId
}
1 change: 1 addition & 0 deletions sdk/sdk-common/src/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './PositionUtils'
export * from './PercentageUtils'
export * from './TokenUtils'
14 changes: 13 additions & 1 deletion sdk/sdk-server/src/handlers/getRefinanceSimulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import type { ISimulation, SimulationType } from '@summerfi/sdk-common/simulatio
import {
refinanceLendingToLendingSamePair,
type IRefinanceDependencies,
refinanceLendingToLendingAnyPair,
} from '@summerfi/simulator-service/strategies'
import type { IRefinanceParameters } from '@summerfi/sdk-common/orders'
import { publicProcedure } from '../TRPC'
import { isSameTokens } from '@summerfi/sdk-common/common'

const inputSchema = z.custom<IRefinanceParameters>((parameters) => parameters !== undefined)

function isToSamePair(parameters: IRefinanceParameters): boolean {
return isSameTokens(parameters.sourcePosition.debtAmount.token, parameters.targetPosition.debtAmount.token) &&
isSameTokens(parameters.sourcePosition.collateralAmount.token, parameters.targetPosition.collateralAmount.token)
}

export const getRefinanceSimulation = publicProcedure
.input(inputSchema)
.query(async (opts): Promise<ISimulation<SimulationType.Refinance>> => {
Expand All @@ -19,5 +26,10 @@ export const getRefinanceSimulation = publicProcedure
protocolManager: opts.ctx.protocolManager,
}

return await refinanceLendingToLendingSamePair(args, dependencies)
// TODO: in the end we should use just any pair
if (isToSamePair(opts.input)) {
return refinanceLendingToLendingSamePair(args, dependencies)
}

return refinanceLendingToLendingAnyPair(args, dependencies)
})

0 comments on commit 2bfa302

Please sign in to comment.