Skip to content

Commit

Permalink
feat: add position created to refinance simulator (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
robercano authored Apr 4, 2024
1 parent bb19df1 commit 86703fe
Show file tree
Hide file tree
Showing 46 changed files with 488 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "turbo run test --cache-dir=.turbo",
"test:integration": "turbo run test:integration --cache-dir=.turbo",
"check-circular": "turbo run check-circular --cache-dir=.turbo",
"cicheck": "turbo run cicheck --cache-dir=.turbo && pnpm format",
"cicheck": "turbo run cicheck --cache-dir=.turbo",
"sst:dev": "sst dev",
"sst:build": "sst build",
"sst:deploy:staging": "sst deploy --stage staging",
Expand Down
2 changes: 2 additions & 0 deletions sdk/order-planner-service/src/config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DepositBorrowActionBuilder,
FlashloanActionBuilder,
PaybackWithdrawActionBuilder,
PositionCreatedActionBuilder,
PullTokenActionBuilder,
RepayFlashloanActionBuilder,
ReturnFundsActionBuilder,
Expand All @@ -18,4 +19,5 @@ export const ActionBuildersConfig: ActionBuildersMap = {
[SimulationSteps.DepositBorrow]: DepositBorrowActionBuilder,
[SimulationSteps.PaybackWithdraw]: PaybackWithdrawActionBuilder,
[SimulationSteps.ReturnFunds]: ReturnFundsActionBuilder,
[SimulationSteps.NewPositionEvent]: PositionCreatedActionBuilder,
}
4 changes: 3 additions & 1 deletion sdk/order-planner-service/tests/utils/MakerSourcePosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@summerfi/sdk-common/common'
import { PoolType, ProtocolName } from '@summerfi/sdk-common/protocols'
import { ILKType, MakerPoolId } from '@summerfi/protocol-plugins/plugins/maker'
import { PositionType } from '@summerfi/sdk-common/common'

export function getMakerPosition(): Position {
const chainInfo: ChainInfo = ChainFamilyMap.Ethereum.Mainnet
Expand Down Expand Up @@ -56,7 +57,8 @@ export function getMakerPosition(): Position {
poolId,
}

const position = new Position({
const position = Position.createFrom({
type: PositionType.Multiply,
positionId: PositionId.createFrom({ id: 'makerPosition' }),
debtAmount,
collateralAmount,
Expand Down
4 changes: 3 additions & 1 deletion sdk/order-planner-service/tests/utils/SparkTargetPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ChainInfo,
Position,
PositionId,
PositionType,
Token,
TokenAmount,
} from '@summerfi/sdk-common/common'
Expand Down Expand Up @@ -56,7 +57,8 @@ export function getSparkPosition(): Position {
poolId,
}

const position = new Position({
const position = Position.createFrom({
type: PositionType.Multiply,
positionId: PositionId.createFrom({ id: 'sparkPosition' }),
debtAmount,
collateralAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ChainInfo,
Position,
PositionId,
PositionType,
Token,
TokenAmount,
} from '@summerfi/sdk-common/common'
Expand Down Expand Up @@ -78,7 +79,8 @@ describe('Execution Storage Mapper', () => {
poolId,
}

const position = new Position({
const position = Position.createFrom({
type: PositionType.Multiply,
positionId: PositionId.createFrom({ id: 'someposition' }),
debtAmount: borrowAmount,
collateralAmount: depositAmount,
Expand Down
4 changes: 3 additions & 1 deletion sdk/protocol-plugins-common/tests/StepBuilderContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ChainInfo,
Position,
PositionId,
PositionType,
Token,
TokenAmount,
} from '@summerfi/sdk-common/common'
Expand Down Expand Up @@ -87,7 +88,8 @@ describe('Step Builder Context', () => {
poolId,
}

const position = new Position({
const position = Position.createFrom({
type: PositionType.Multiply,
positionId: PositionId.createFrom({ id: 'someposition' }),
debtAmount: borrowAmount,
collateralAmount: depositAmount,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ActionCall, BaseAction, InputSlotsMapping } from '@summerfi/protocol-plugins-common'
import { Position } from '@summerfi/sdk-common/common'

export class PositionCreatedAction extends BaseAction {
public readonly config = {
name: 'PositionCreated',
version: 0,
parametersAbi:
'(string protocol, string positionType, address collateralToken, address debtToken)',
storageInputs: [],
storageOutputs: [],
} as const

public encodeCall(params: { position: Position }, paramsMapping?: InputSlotsMapping): ActionCall {
return this._encodeCall({
arguments: [
{
protocol: params.position.pool.protocol.name,
positionType: params.position.type,
collateralToken: params.position.collateralAmount.token.address.value,
debtToken: params.position.debtAmount.token.address.value,
},
],
mapping: paramsMapping,
})
}
}
1 change: 1 addition & 0 deletions sdk/protocol-plugins/src/plugins/common/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { ReturnFundsAction } from './ReturnFundsAction'
export { SendTokenAction } from './SendTokenAction'
export { SetApprovalAction } from './SetApprovalAction'
export { SwapAction } from './SwapAction'
export { PositionCreatedAction } from './PositionCreatedAction'
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { steps } from '@summerfi/sdk-common/simulation'
import { ActionBuilder } from '@summerfi/protocol-plugins-common'
import { PositionCreatedAction } from '../actions/PositionCreatedAction'

export const PositionCreatedActionBuilder: ActionBuilder<steps.NewPositionEvent> = async (
params,
): Promise<void> => {
const { context, step } = params

context.addActionCall({
step: step,
action: new PositionCreatedAction(),
arguments: {
position: step.inputs.position,
},
connectedInputs: {},
connectedOutputs: {},
})
}
1 change: 1 addition & 0 deletions sdk/protocol-plugins/src/plugins/common/builders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export {
} from './RepayFlashloanActionBuilder'
export { ReturnFundsActionBuilder } from './ReturnFundsActionBuilder'
export { SwapActionBuilder } from './SwapActionBuilder'
export { PositionCreatedActionBuilder } from './PositionCreatedActionBuilder'
8 changes: 2 additions & 6 deletions sdk/protocol-plugins/src/plugins/maker/types/MakerPoolId.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { ChainInfo } from '@summerfi/sdk-common/common'
import { IPoolId, ProtocolName, isPoolId } from '@summerfi/sdk-common/protocols'
import { IPoolId, IProtocol, ProtocolName, isPoolId } from '@summerfi/sdk-common/protocols'
import { ILKType } from '../enums/ILKType'

export interface MakerPoolId extends IPoolId {
protocol: {
name: ProtocolName.Maker
chainInfo: ChainInfo
}
protocol: IProtocol
ilkType: ILKType
// TODO: vaultId does not belong in the poolId
vaultId: string
Expand Down
96 changes: 96 additions & 0 deletions sdk/protocol-plugins/tests/actions/PositionCreatedAction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Address, Position, Token, TokenAmount, PositionType } from '@summerfi/sdk-common/common'
import { decodeActionCalldata, getTargetHash } from '@summerfi/testing-utils'
import { PositionCreatedAction } from '../../src/plugins/common/actions/PositionCreatedAction'
import { IProtocol, PoolType, ProtocolName } from '@summerfi/sdk-common/protocols'
import { MakerPoolId } from '../../src/plugins/maker/types/MakerPoolId'
import { ILKType } from '../../src/plugins/maker'

describe('PositionCreated Action', () => {
const action = new PositionCreatedAction()
const contractNameWithVersion = `${action.config.name}`

const DAI = Token.createFrom({
chainInfo: {
name: 'Mainnet',
chainId: 1,
},
address: Address.createFromEthereum({ value: '0x6B175474E89094C44Da98b954EedeAC495271d0F' }),
decimals: 18,
name: 'Dai Stablecoin',
symbol: 'DAI',
})

const WETH = Token.createFrom({
chainInfo: {
name: 'Mainnet',
chainId: 1,
},
address: Address.createFromEthereum({ value: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' }),
decimals: 18,
name: 'Wrapped Ether',
symbol: 'WETH',
})

const protocol: IProtocol = {
name: ProtocolName.Spark,
chainInfo: {
name: 'Mainnet',
chainId: 1,
},
}

const position = Position.createFrom({
type: PositionType.Multiply,
positionId: {
id: '0x123',
},
pool: {
type: PoolType.Lending,
protocol: protocol,
poolId: {
protocol: protocol,
vaultId: '0x123',
ilkType: ILKType.ETH_A,
} as MakerPoolId,
},
debtAmount: TokenAmount.createFrom({
token: DAI,
amount: '100',
}),
collateralAmount: TokenAmount.createFrom({
token: WETH,
amount: '100',
}),
})

it('should return the versioned name', () => {
expect(action.getVersionedName()).toBe(contractNameWithVersion)
})

it('should encode calls', async () => {
const call = action.encodeCall(
{
position: position,
},
[8, 9, 1, 3],
)

expect(call.targetHash).toBe(getTargetHash(action))

const actionDecodedArgs = decodeActionCalldata({
action,
calldata: call.callData,
})

expect(actionDecodedArgs).toBeDefined()
expect(actionDecodedArgs?.args).toEqual([
{
protocol: protocol.name,
positionType: position.type,
collateralToken: WETH.address.value,
debtToken: DAI.address.value,
},
])
expect(actionDecodedArgs?.mapping).toEqual([8, 9, 1, 3])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ChainInfo,
Position,
PositionId,
PositionType,
Token,
TokenAmount,
} from '@summerfi/sdk-common/common'
Expand Down Expand Up @@ -65,7 +66,8 @@ describe('Deposit Borrow Action Builder', () => {
poolId,
}

const position = new Position({
const position = Position.createFrom({
type: PositionType.Multiply,
positionId: PositionId.createFrom({ id: 'someposition' }),
debtAmount: borrowAmount,
collateralAmount: depositAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ChainInfo,
Position,
PositionId,
PositionType,
Token,
TokenAmount,
} from '@summerfi/sdk-common/common'
Expand Down Expand Up @@ -65,7 +66,8 @@ describe('Payback Withdraw Action Builder', () => {
poolId,
}

const position = new Position({
const position = Position.createFrom({
type: PositionType.Multiply,
positionId: PositionId.createFrom({ id: 'someposition' }),
debtAmount: withdrawAmount,
collateralAmount: paybackAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class RefinanceSimulationManager extends IRPCClient {
): Promise<ISimulation<SimulationType.Refinance>> {
const refinanceParameters: IRefinanceParameters = {
position: {
type: params.position.type,
positionId: params.position.positionId,
debtAmount: params.position.debtAmount,
collateralAmount: params.position.collateralAmount,
Expand Down
3 changes: 3 additions & 0 deletions sdk/sdk-client/tests/queries/newOrder.subtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Percentage,
Position,
PositionId,
PositionType,
RiskRatio,
Token,
TokenAmount,
Expand Down Expand Up @@ -56,6 +57,7 @@ export default async function simulateNewOrder() {
} as MakerLendingPool

const prevPosition: Position = {
type: PositionType.Multiply,
pool: pool,
debtAmount: TokenAmount.createFrom({ token: DAI, amount: '56.78' }),
collateralAmount: TokenAmount.createFrom({ token: WETH, amount: '105.98' }),
Expand All @@ -82,6 +84,7 @@ export default async function simulateNewOrder() {
sourcePosition: prevPosition,
swaps: [],
targetPosition: {
type: PositionType.Multiply,
positionId: PositionId.createFrom({ id: '1234567890' }),
debtAmount: TokenAmount.createFrom({ token: DAI, amount: '56.78' }),
collateralAmount: TokenAmount.createFrom({ token: WETH, amount: '105.98' }),
Expand Down
3 changes: 3 additions & 0 deletions sdk/sdk-client/tests/queries/simulateRefinance.subtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Percentage,
Position,
PositionId,
PositionType,
RiskRatio,
Token,
TokenAmount,
Expand All @@ -25,6 +26,7 @@ export default async function simulateRefinanceTest() {
sourcePosition: params.position,
swaps: [],
targetPosition: {
type: params.position.type,
positionId: PositionId.createFrom({ id: '0987654321' }),
debtAmount: params.position.debtAmount,
collateralAmount: params.position.collateralAmount,
Expand Down Expand Up @@ -82,6 +84,7 @@ export default async function simulateRefinanceTest() {
} as MakerLendingPool

const prevPosition: Position = {
type: PositionType.Multiply,
pool: pool,
debtAmount: TokenAmount.createFrom({ token: DAI, amount: '56.78' }),
collateralAmount: TokenAmount.createFrom({ token: WETH, amount: '105.98' }),
Expand Down
4 changes: 4 additions & 0 deletions sdk/sdk-common/src/common/enums/PositionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum PositionType {
Borrow = 'Borrow',
Multiply = 'Multiply',
}
1 change: 1 addition & 0 deletions sdk/sdk-common/src/common/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export {
EthereumChainNames,
OptimismChainNames,
} from './ChainNames'
export { PositionType } from './PositionType'
5 changes: 4 additions & 1 deletion sdk/sdk-common/src/common/implementation/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import { SerializationService } from '../../services/SerializationService'
import { PositionId } from './PositionId'
import { RiskRatio } from './RiskRatio'
import { TokenAmount } from './TokenAmount'
import { PositionType } from '../enums/PositionType'

// we should add assests prop instead of the amounts
export class Position implements IPosition {
readonly type: PositionType
readonly positionId: PositionId
readonly debtAmount: TokenAmount
readonly collateralAmount: TokenAmount
readonly pool: IPool

constructor(params: IPosition) {
private constructor(params: IPosition) {
this.type = params.type
this.positionId = params.positionId
this.debtAmount = TokenAmount.createFrom(params.debtAmount)
this.collateralAmount = TokenAmount.createFrom(params.collateralAmount)
Expand Down
2 changes: 2 additions & 0 deletions sdk/sdk-common/src/common/interfaces/IPosition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { IPool } from '../../protocols/interfaces/IPool'
import { PositionType } from '../enums/PositionType'
import { PositionId } from '../implementation/PositionId'
import { ITokenAmount } from './ITokenAmount'

export interface IPosition {
readonly type: PositionType
readonly positionId: PositionId
readonly debtAmount: ITokenAmount
readonly collateralAmount: ITokenAmount
Expand Down
Loading

0 comments on commit 86703fe

Please sign in to comment.