Skip to content

Commit

Permalink
feat: refactor refinance data types (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
robercano authored May 15, 2024
1 parent 8f97893 commit ab5f383
Show file tree
Hide file tree
Showing 41 changed files with 358 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Order Planner Service', () => {

const refinanceSimulation: ISimulation<SimulationType.Refinance> = getRefinanceSimulation({
sourcePosition,
targetPosition,
targetPool: targetPosition,
})

const order = await orderPlannerService.buildOrder({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { Position } from '@summerfi/sdk-common/common'

export function getRefinanceSimulation(params: {
sourcePosition: Position
targetPosition: Position
targetPool: Position
}): ISimulation<SimulationType.Refinance> {
const { sourcePosition, targetPosition } = params
const { sourcePosition, targetPool: targetPosition } = params

return {
simulationType: SimulationType.Refinance,
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -17,15 +17,15 @@ export interface IProtocolManager {
* @param poolId The pool ID
* @returns The lending pool for the specific protocol
*/
getLendingPool(poolId: unknown): Promise<ILendingPool>
getLendingPool(poolId: ILendingPoolId): Promise<ILendingPool>

/**
* @name getLendingPoolInfo
* @description Gets the extended lending pool information for the given pool ID
* @param poolId The pool ID
* @returns The extended lending pool information for the specific protocol
*/
getLendingPoolInfo(poolId: unknown): Promise<ILendingPoolInfo>
getLendingPoolInfo(poolId: ILendingPoolId): Promise<ILendingPoolInfo>

/** POSITIONS */

Expand All @@ -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<IPosition>
getPosition(positionId: IPositionId): Promise<IPosition>

/** IMPORT POSITION */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ProtocolManager implements IProtocolManager {
}

/** @see IProtocolManager.getLendingPool */
async getLendingPool(poolId: unknown): Promise<ILendingPool> {
async getLendingPool(poolId: ILendingPoolId): Promise<ILendingPool> {
this._validateLendingPoolId(poolId)

const plugin = this._pluginsRegistry.getPlugin({ protocolName: poolId.protocol.name })
Expand All @@ -49,7 +49,7 @@ export class ProtocolManager implements IProtocolManager {
}

/** @see IProtocolManager.getLendingPoolInfo */
async getLendingPoolInfo(poolId: unknown): Promise<ILendingPoolInfo> {
async getLendingPoolInfo(poolId: ILendingPoolId): Promise<ILendingPoolInfo> {
this._validateLendingPoolId(poolId)

const plugin = this._pluginsRegistry.getPlugin({ protocolName: poolId.protocol.name })
Expand All @@ -60,7 +60,7 @@ export class ProtocolManager implements IProtocolManager {
}

/** @see IProtocolManager.getPosition */
async getPosition(positionId: unknown): Promise<IPosition> {
async getPosition(positionId: IPositionId): Promise<IPosition> {
this._validatePositionId(positionId)

throw new Error('Not implemented')
Expand Down
8 changes: 2 additions & 6 deletions sdk/sdk-client/src/implementation/ProtocolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ export class ProtocolClient extends Protocol implements IProtocolClient {
}

getLendingPool(params: { poolId: ILendingPoolIdData }): Promise<Maybe<ILendingPool>> {
return this._rpcClient.protocols.getLendingPool.query({
poolId: params.poolId,
})
return this._rpcClient.protocols.getLendingPool.query(params.poolId)
}

getLendingPoolInfo(params: { poolId: ILendingPoolIdData }): Promise<Maybe<ILendingPoolInfo>> {
return this._rpcClient.protocols.getLendingPoolInfo.query({
poolId: params.poolId,
})
return this._rpcClient.protocols.getLendingPoolInfo.query(params.poolId)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class RefinanceSimulationManager extends IRPCClient {
}

public async simulateRefinancePosition(
params: IRefinanceParameters,
refinanceParameters: IRefinanceParameters,
): Promise<ISimulation<RefinanceSimulationTypes>> {
return this.rpcClient.simulation.refinance.query(params)
return this.rpcClient.simulation.refinance.query(refinanceParameters)
}
}
15 changes: 7 additions & 8 deletions sdk/sdk-client/tests/queries/getLendingPool.subtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down
15 changes: 7 additions & 8 deletions sdk/sdk-client/tests/queries/getLendingPoolInfo.subtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down
91 changes: 38 additions & 53 deletions sdk/sdk-client/tests/queries/simulateRefinance.subtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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: [],
Expand Down Expand Up @@ -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)
Expand All @@ -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()
}
2 changes: 1 addition & 1 deletion sdk/sdk-common/src/common/implementation/ChainInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChainId } from '../aliases/ChainId'

/**
* @name ChainInfo
* @see IChainInfoData
* @see IChainInfo
*/
export class ChainInfo implements IChainInfo {
readonly chainId: ChainId
Expand Down
33 changes: 33 additions & 0 deletions sdk/sdk-common/src/orders/implementation/RefinanceParameters.ts
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions sdk/sdk-common/src/orders/implementation/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './PositionsManager'
export * from './RefinanceParameters'
Loading

0 comments on commit ab5f383

Please sign in to comment.