Skip to content

Commit

Permalink
feat: add any pair capability to refinance strategy (#153)
Browse files Browse the repository at this point in the history
- Reintroduce the two swaps to refinance strategy
- Simplify spotPrice method on SwapManager and SwapProviders. Gets one
price only. Calculates a derived spot price when the quote token is a
token and not a currency symbol.
- Calculate the borrowAmount on the target protocol when a debtSwap is
required. Accounts for slippage and summer fees when determining how
much to borrow to repay flashloan
- Account for summer fees when getting quotes on swap steps
- Add getSummerFee method to SwapManager (currently resolves only to 20
basis pts - will update shortly)

TODO:
- Add config and implement getSummerFee appropriately.
  • Loading branch information
zerotucks authored Apr 5, 2024
1 parent 86703fe commit 2bbad1c
Show file tree
Hide file tree
Showing 42 changed files with 753 additions and 474 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ jobs:
ONE_INCH_API_KEY: ${{ secrets.ONE_INCH_API_KEY }}
ONE_INCH_API_VERSION: ${{ secrets.ONE_INCH_API_VERSION }}
ONE_INCH_API_URL: ${{ secrets.ONE_INCH_API_URL }}
ONE_INCH_API_SPOT_KEY: ${{ secrets.ONE_INCH_API_SPOT_KEY }}
ONE_INCH_API_SPOT_VERSION: ${{ secrets.ONE_INCH_API_SPOT_VERSION }}
ONE_INCH_API_SPOT_URL: ${{ secrets.ONE_INCH_API_SPOT_URL }}
ONE_INCH_ALLOWED_SWAP_PROTOCOLS: ${{ secrets.ONE_INCH_ALLOWED_SWAP_PROTOCOLS }}
ONE_INCH_SWAP_CHAIN_IDS: ${{ secrets.ONE_INCH_SWAP_CHAIN_IDS }}
MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }}
3 changes: 3 additions & 0 deletions .github/workflows/build-unit-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ jobs:
ONE_INCH_API_KEY: ${{ secrets.ONE_INCH_API_KEY }}
ONE_INCH_API_VERSION: ${{ secrets.ONE_INCH_API_VERSION }}
ONE_INCH_API_URL: ${{ secrets.ONE_INCH_API_URL }}
ONE_INCH_API_SPOT_KEY: ${{ secrets.ONE_INCH_API_SPOT_KEY }}
ONE_INCH_API_SPOT_VERSION: ${{ secrets.ONE_INCH_API_SPOT_VERSION }}
ONE_INCH_API_SPOT_URL: ${{ secrets.ONE_INCH_API_SPOT_URL }}
ONE_INCH_ALLOWED_SWAP_PROTOCOLS: ${{ secrets.ONE_INCH_ALLOWED_SWAP_PROTOCOLS }}
ONE_INCH_SWAP_CHAIN_IDS: ${{ secrets.ONE_INCH_SWAP_CHAIN_IDS }}
MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { ConfigItem } from '../types/ConfigItem'
import { ConfigKey } from '../types/ConfigKey'

const CONFIG_KEYS: ConfigKey[] = [
'ONE_INCH_API_KEY',
'ONE_INCH_API_VERSION',
'ONE_INCH_API_KEY',
'ONE_INCH_API_URL',
'ONE_INCH_API_SPOT_KEY',
'ONE_INCH_API_SPOT_URL',
'ONE_INCH_API_SPOT_VERSION',
'ONE_INCH_ALLOWED_SWAP_PROTOCOLS',
'ONE_INCH_SWAP_CHAIN_IDS',
]
Expand Down
89 changes: 0 additions & 89 deletions sdk/order-planner-service/tests/mocks/SwapManagerMock.ts
Original file line number Diff line number Diff line change
@@ -1,89 +0,0 @@
import { Address, ChainInfo, Percentage, Token, TokenAmount } from '@summerfi/sdk-common/common'
import { ISwapManager } from '@summerfi/swap-common/interfaces'
import { QuoteData, SwapData, SpotData } from '@summerfi/sdk-common/swap'

export class SwapManagerMock implements ISwapManager {
private _swapDataReturnValue: SwapData = {} as SwapData
private _quoteDataReturnValue: QuoteData = {} as QuoteData
private _spotDataReturnValue: SpotData = {} as SpotData

private _lastGetSwapDataExactInputParams:
| {
chainInfo: ChainInfo
fromAmount: TokenAmount
toToken: Token
recipient: Address
slippage: Percentage
}
| undefined

private _lastGetSwapQuoteExactInputParams:
| {
chainInfo: ChainInfo
fromAmount: TokenAmount
toToken: Token
}
| undefined

setSwapData(swapData: SwapData): void {
this._swapDataReturnValue = swapData
}

setQuoteData(quoteData: QuoteData): void {
this._quoteDataReturnValue = quoteData
}

async getSwapDataExactInput(params: {
chainInfo: ChainInfo
fromAmount: TokenAmount
toToken: Token
recipient: Address
slippage: Percentage
}): Promise<SwapData> {
this._lastGetSwapDataExactInputParams = params
return this._swapDataReturnValue
}

async getSwapQuoteExactInput(params: {
chainInfo: ChainInfo
fromAmount: TokenAmount
toToken: Token
}): Promise<QuoteData> {
this._lastGetSwapQuoteExactInputParams = params
return this._quoteDataReturnValue
}

async getSpotPrices(params: { chainInfo: ChainInfo; tokens: Token[] }): Promise<SpotData> {
return this._spotDataReturnValue
}

get swapDataReturnValue(): SwapData {
return this._swapDataReturnValue
}

get quoteDataReturnValue(): QuoteData {
return this._quoteDataReturnValue
}

get lastGetSwapDataExactInputParams():
| {
chainInfo: ChainInfo
fromAmount: TokenAmount
toToken: Token
recipient: Address
slippage: Percentage
}
| undefined {
return this._lastGetSwapDataExactInputParams
}

get lastGetSwapQuoteExactInputParams():
| {
chainInfo: ChainInfo
fromAmount: TokenAmount
toToken: Token
}
| undefined {
return this._lastGetSwapQuoteExactInputParams
}
}
12 changes: 0 additions & 12 deletions sdk/order-planner-service/tests/mocks/UserMock.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import { DeploymentIndex } from '@summerfi/deployment-utils'
import { ISwapManager } from '@summerfi/swap-common/interfaces'
import { Address, AddressValue, ChainFamilyMap, ChainInfo } from '@summerfi/sdk-common/common'
import { ProtocolName } from '@summerfi/sdk-common/protocols'
import { SwapManagerMock, UserMock } from '@summerfi/testing-utils'
import { IPositionsManager } from '@summerfi/sdk-common/orders'
import { SetupDeployments } from '../utils/SetupDeployments'
import { UserMock } from '../mocks/UserMock'
import { SwapManagerMock } from '../mocks/SwapManagerMock'
import { getRefinanceSimulation } from '../utils/RefinanceSimulation/RefinanceSimulation'
import { OrderPlannerService } from '../../src/implementation/OrderPlannerService'
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const SwapActionBuilder: ActionBuilder<steps.SwapStep> = async (params):
arguments: {
fromAmount: step.inputs.fromTokenAmount,
toMinimumAmount: step.inputs.toTokenAmount,
fee: step.inputs.fee,
fee: step.inputs.summerFee,
withData: swapData.calldata,
collectFeeInFromToken: true,
},
Expand Down
14 changes: 6 additions & 8 deletions sdk/protocol-plugins/tests/builders/SwapActionBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ describe('Swap Action Builder', () => {
routes: [],
fromTokenAmount: fromAmount,
toTokenAmount: toAmount,
prices: [
Price.createFrom({
value: '0',
quoteToken: fromAmount.token,
baseToken: toAmount.token,
}),
],
fee: fee,
spotPrice: Price.createFrom({
value: '0',
quoteToken: fromAmount.token,
baseToken: toAmount.token,
}),
summerFee: fee,
slippage,
},
outputs: {
Expand Down
73 changes: 0 additions & 73 deletions sdk/protocol-plugins/tests/tests.spec.ts

This file was deleted.

3 changes: 2 additions & 1 deletion sdk/sdk-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"devDependencies": {
"@summerfi/eslint-config": "workspace:*",
"@summerfi/jest-config": "workspace:*",
"@summerfi/typescript-config": "workspace:*"
"@summerfi/typescript-config": "workspace:*",
"@summerfi/testing-utils": "workspace:*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@ export class RefinanceSimulationManager extends IRPCClient {
params: IRefinanceParameters,
): Promise<ISimulation<SimulationType.Refinance>> {
const refinanceParameters: IRefinanceParameters = {
position: {
type: params.position.type,
positionId: params.position.positionId,
debtAmount: params.position.debtAmount,
collateralAmount: params.position.collateralAmount,
sourcePosition: {
type: params.sourcePosition.type,
positionId: params.sourcePosition.positionId,
debtAmount: params.sourcePosition.debtAmount,
collateralAmount: params.sourcePosition.collateralAmount,
pool: {
poolId: params.position.pool.poolId,
protocol: params.position.pool.protocol,
type: params.position.pool.type,
poolId: params.sourcePosition.pool.poolId,
protocol: params.sourcePosition.pool.protocol,
type: params.sourcePosition.pool.type,
},
},
targetPool: {
poolId: params.targetPool.poolId,
protocol: params.targetPool.protocol,
type: params.targetPool.type,
targetPosition: {
type: params.targetPosition.type,
positionId: params.targetPosition.positionId,
debtAmount: params.targetPosition.debtAmount,
collateralAmount: params.targetPosition.collateralAmount,
pool: {
poolId: params.targetPosition.pool.poolId,
protocol: params.targetPosition.pool.protocol,
type: params.targetPosition.pool.type,
},
},
slippage: params.slippage,
}
Expand Down
Loading

0 comments on commit 2bbad1c

Please sign in to comment.