Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add handler for any pair #163

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
22 changes: 21 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,27 @@ 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 +34,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)
})
Loading