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

Improved PositionUtils #181

Merged
merged 4 commits into from
Apr 22, 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
2 changes: 1 addition & 1 deletion sdk/sdk-client/bundle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "summerfi-sdk-client",
"version": "0.1.0",
"version": "0.1.1",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"packageManager": "yarn@1.22.21",
Expand Down
19 changes: 7 additions & 12 deletions sdk/sdk-client/src/utils/PositionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Percentage,
type Position,
type Price,
type TokenAmount,
} from '@summerfi/sdk-common/common'
import { Percentage, type Position, type TokenAmount } from '@summerfi/sdk-common/common'
import { BigNumber } from 'bignumber.js'

export class PositionUtils {
Expand All @@ -15,13 +10,13 @@ export class PositionUtils {
}: {
collateralTokenAmount: TokenAmount
debtTokenAmount: TokenAmount
collateralPriceInUsd: Price
debtPriceInUsd: Price
collateralPriceInUsd: string
debtPriceInUsd: string
}): Percentage {
// Determine the Collateral Value:
const collValue = new BigNumber(collateralTokenAmount.amount).times(collateralPriceInUsd.value)
const collValue = new BigNumber(collateralTokenAmount.amount).times(collateralPriceInUsd)
// Determine the Borrowed Value:
const debtValue = new BigNumber(debtTokenAmount.amount).times(debtPriceInUsd.value)
const debtValue = new BigNumber(debtTokenAmount.amount).times(debtPriceInUsd)
// If the collateral value is 0, return 0.
if (collValue.isZero()) {
return Percentage.createFrom({ value: 0 })
Expand All @@ -39,7 +34,7 @@ export class PositionUtils {
position: Position
// TODO: it is not defined in Position yet, we should use Pool in the future
liquidationThreshold: Percentage
debtPriceInUsd: Price
debtPriceInUsd: string
}): string {
// Determine the Collateral Value:
const collateralAmount = new BigNumber(position.collateralAmount.amount)
Expand All @@ -49,7 +44,7 @@ export class PositionUtils {
if (debtAmount.isZero()) {
return '0'
}
const debtValue = debtAmount.times(debtPriceInUsd.value)
const debtValue = debtAmount.times(debtPriceInUsd)
// (loanAmount * loanPrice) / (liquidationThreshold * collateralAmount)
const liquidationPrice = debtValue.div(
collateralAmount.times(liquidationThreshold.toProportion()),
Expand Down
20 changes: 3 additions & 17 deletions sdk/sdk-client/tests/PositionUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
import {
Percentage,
TokenAmount,
type Position,
Price,
CurrencySymbol,
} from '@summerfi/sdk-common/common'
import { Percentage, TokenAmount, type Position } from '@summerfi/sdk-common/common'

import { PositionUtils } from '../src/utils/PositionUtils'
import { WETH, DAI } from './TestUtils'

describe('PositionUtils', () => {
const ethPriceInUsd = Price.createFrom({
value: '1000',
baseToken: WETH,
quoteToken: CurrencySymbol.USD,
})
const daiPriceInUsd = Price.createFrom({
value: '1',
baseToken: DAI,
quoteToken: CurrencySymbol.USD,
})
const ethPriceInUsd = '1000'
const daiPriceInUsd = '1'

describe('getLTV', () => {
it('should correctly calculate LTV', () => {
Expand Down
2 changes: 1 addition & 1 deletion sdk/sdk-common/bundle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "summerfi-sdk-common",
"version": "0.0.9",
"version": "0.1.0",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"packageManager": "yarn@1.22.21",
Expand Down
2 changes: 1 addition & 1 deletion sdk/sdk-common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@summerfi/sdk-common",
"version": "0.1.0",
"version": "0.0.1",
piotrwitek marked this conversation as resolved.
Show resolved Hide resolved
"main": "dist/index.js",
"types": "dist/src/index.d.ts",
"exports": {
Expand Down
Loading