From 27f01d24b284e45af08c7a9488be8437fea4f12b Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Fri, 22 Mar 2024 17:59:32 +0100 Subject: [PATCH 01/26] Added PositionUtils class --- pnpm-lock.yaml | 40 +++++- sdk/sdk-client/package.json | 5 +- sdk/sdk-client/src/index.ts | 1 + sdk/sdk-client/src/utils/PositionUtils.ts | 53 ++++++++ sdk/sdk-client/tests/PositonUtils.spec.ts | 127 ++++++++++++++++++ sdk/sdk-client/tests/TestUtils.ts | 20 +++ .../src/common/implementation/Percentage.ts | 4 + 7 files changed, 247 insertions(+), 3 deletions(-) create mode 100644 sdk/sdk-client/src/utils/PositionUtils.ts create mode 100644 sdk/sdk-client/tests/PositonUtils.spec.ts create mode 100644 sdk/sdk-client/tests/TestUtils.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index aedd5303ed..52102f512e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -86,7 +86,7 @@ importers: version: 2.39.7 ts-jest: specifier: ^29.1.1 - version: 29.1.1(@babel/core@7.24.0)(esbuild@0.19.11)(jest@29.7.0)(typescript@5.3.3) + version: 29.1.1(@babel/core@7.23.7)(esbuild@0.19.11)(jest@29.7.0)(typescript@5.3.3) tsc-alias: specifier: ^1.8.8 version: 1.8.8 @@ -828,6 +828,9 @@ importers: '@trpc/client': specifier: 11.0.0-next-beta.264 version: 11.0.0-next-beta.264(@trpc/server@11.0.0-next-beta.264) + bignumber.js: + specifier: ^9.1.2 + version: 9.1.2 superjson: specifier: ^1.13.3 version: 1.13.3 @@ -16246,6 +16249,41 @@ packages: engines: {node: '>=14.16'} dev: false + /ts-jest@29.1.1(@babel/core@7.23.7)(esbuild@0.19.11)(jest@29.7.0)(typescript@5.3.3): + resolution: {integrity: sha512-D6xjnnbP17cC85nliwGiL+tpoKN0StpgE0TeOjXQTU6MVCfsB4v7aW05CgQ/1OywGb0x/oy9hHFnN+sczTiRaA==} + engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} + hasBin: true + peerDependencies: + '@babel/core': '>=7.0.0-beta.0 <8' + '@jest/types': ^29.0.0 + babel-jest: ^29.0.0 + esbuild: '*' + jest: ^29.0.0 + typescript: '>=4.3 <6' + peerDependenciesMeta: + '@babel/core': + optional: true + '@jest/types': + optional: true + babel-jest: + optional: true + esbuild: + optional: true + dependencies: + '@babel/core': 7.23.7 + bs-logger: 0.2.6 + esbuild: 0.19.11 + fast-json-stable-stringify: 2.1.0 + jest: 29.7.0(@types/node@20.11.5) + jest-util: 29.7.0 + json5: 2.2.3 + lodash.memoize: 4.1.2 + make-error: 1.3.6 + semver: 7.5.4 + typescript: 5.3.3 + yargs-parser: 21.1.1 + dev: true + /ts-jest@29.1.1(@babel/core@7.24.0)(esbuild@0.19.11)(jest@29.7.0)(typescript@5.3.3): resolution: {integrity: sha512-D6xjnnbP17cC85nliwGiL+tpoKN0StpgE0TeOjXQTU6MVCfsB4v7aW05CgQ/1OywGb0x/oy9hHFnN+sczTiRaA==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} diff --git a/sdk/sdk-client/package.json b/sdk/sdk-client/package.json index a4a7df28ce..c26bc08643 100644 --- a/sdk/sdk-client/package.json +++ b/sdk/sdk-client/package.json @@ -19,11 +19,12 @@ "lint:fix": "eslint . --fix" }, "dependencies": { - "@summerfi/sdk-common": "workspace:*", - "@summerfi/sdk-server": "workspace:*", "@summerfi/protocol-manager": "workspace:*", "@summerfi/protocol-plugins": "workspace:*", + "@summerfi/sdk-common": "workspace:*", + "@summerfi/sdk-server": "workspace:*", "@trpc/client": "11.0.0-next-beta.264", + "bignumber.js": "^9.1.2", "superjson": "^1.13.3", "viem": "^2.7.9" }, diff --git a/sdk/sdk-client/src/index.ts b/sdk/sdk-client/src/index.ts index d073296743..aa365d258e 100644 --- a/sdk/sdk-client/src/index.ts +++ b/sdk/sdk-client/src/index.ts @@ -14,3 +14,4 @@ export * from './interfaces/IPortfolioManager' export * from './interfaces/IProtocolsManager' export * from './interfaces/ITokensManager' export * from './interfaces/IUsersManager' +export * from './utils/PositionUtils' diff --git a/sdk/sdk-client/src/utils/PositionUtils.ts b/sdk/sdk-client/src/utils/PositionUtils.ts new file mode 100644 index 0000000000..9554231bf6 --- /dev/null +++ b/sdk/sdk-client/src/utils/PositionUtils.ts @@ -0,0 +1,53 @@ +import { Percentage, type Position } from '@summerfi/sdk-common/common' +import BigNumber from 'bignumber.js' + +export class PositionUtils { + static getLTV({ + position, + collateralPrice, + debtPrice, + }: { + position: Position + collateralPrice: string + debtPrice: string + }): Percentage { + // Determine the Collateral Value: + const collValue = new BigNumber(position.collateralAmount.amount).times(collateralPrice) + // Determine the Borrowed Value: + const debtValue = new BigNumber(position.debtAmount.amount).times(debtPrice) + // If the collateral value is 0, return 0. + if (collValue.isZero()) { + return Percentage.createFrom({ percentage: 0 }) + } + // Divide the borrowed value by the collateral value and multiply by 100 to get the percentage LTV. + const ltvRatio = debtValue.div(collValue).times(100).toNumber() + return Percentage.createFrom({ percentage: ltvRatio }) + } + + static getLiquidationPrice({ + position, + liquidationThreshold, + debtPrice, + }: { + position: Position + // TODO: passed as param because it is not defined in Position + liquidationThreshold: Percentage + collateralPrice: string + debtPrice: string + }): string { + // Determine the Collateral Value: + const collateralAmount = new BigNumber(position.collateralAmount.amount) + // Determine the Borrowed Value: + const debtAmount = new BigNumber(position.debtAmount.amount) + // If the debt value is 0, return 0. + if (debtAmount.isZero()) { + return '0' + } + const debtValue = debtAmount.times(debtPrice) + // (loanAmount * loanPrice) / (liquidationThreshold * collateralAmount) + const liquidationPrice = debtValue.div( + collateralAmount.times(liquidationThreshold.toProportion()), + ) + return liquidationPrice.toString() + } +} diff --git a/sdk/sdk-client/tests/PositonUtils.spec.ts b/sdk/sdk-client/tests/PositonUtils.spec.ts new file mode 100644 index 0000000000..41fb2fa3b3 --- /dev/null +++ b/sdk/sdk-client/tests/PositonUtils.spec.ts @@ -0,0 +1,127 @@ +import { Percentage, TokenAmount, type Position } from '@summerfi/sdk-common/common' + +import { PositionUtils } from '../src/utils/PositionUtils' +import { WETH, DAI } from './TestUtils' + +describe.only('PositionUtils', () => { + describe('getLTV', () => { + it('should correctly calculate LTV', () => { + const collateralAmount = TokenAmount.createFrom({ amount: '1', token: WETH }) + const collateralPrice = '1000' + const debtAmount = TokenAmount.createFrom({ amount: '250', token: DAI }) + const debtPrice = '1' + + const result = PositionUtils.getLTV({ + position: { collateralAmount, debtAmount } as Position, + collateralPrice, + debtPrice, + }) + + expect(result).toEqual(Percentage.createFrom({ percentage: 25 })) + }) + + it('should return 0 when debt is equal to 0', () => { + const collateralAmount = TokenAmount.createFrom({ amount: '1', token: WETH }) + const collateralPrice = '1000' + const debtAmount = TokenAmount.createFrom({ amount: '0', token: DAI }) + const debtPrice = '1' + + const result = PositionUtils.getLTV({ + position: { collateralAmount, debtAmount } as Position, + collateralPrice, + debtPrice, + }) + + expect(result).toEqual(Percentage.createFrom({ percentage: 0 })) + }) + + it('should return 0 when collateral amount is 0', () => { + const collateralAmount = TokenAmount.createFrom({ amount: '0', token: WETH }) + const collateralPrice = '1000' + const debtAmount = TokenAmount.createFrom({ amount: '1000', token: DAI }) + const debtPrice = '1' + + const result = PositionUtils.getLTV({ + position: { collateralAmount, debtAmount } as Position, + collateralPrice, + debtPrice, + }) + + expect(result).toEqual(Percentage.createFrom({ percentage: 0 })) + }) + + it('should return 100 when debt is equal to collateral', () => { + const collateralAmount = TokenAmount.createFrom({ amount: '1', token: WETH }) + const collateralPrice = '1000' + const debtAmount = TokenAmount.createFrom({ amount: '1000', token: DAI }) + const debtPrice = '1' + + const result = PositionUtils.getLTV({ + position: { collateralAmount, debtAmount } as Position, + collateralPrice, + debtPrice, + }) + + expect(result).toEqual(Percentage.createFrom({ percentage: 100 })) + }) + }) + + describe('getLiquidationPrice', () => { + it('should correctly calculate liquidation price when debt amount is not 0', () => { + const liquidationThreshold = Percentage.createFrom({ percentage: 80 }) + const collateralPrice = '3000' + const debtPrice = '1' + + const liquidationPrice = PositionUtils.getLiquidationPrice({ + position: { + collateralAmount: TokenAmount.createFrom({ amount: '2', token: WETH }), + debtAmount: TokenAmount.createFrom({ amount: '5000', token: DAI }), + } as Position, + liquidationThreshold, + collateralPrice, + debtPrice, + }) + expect(liquidationPrice).toEqual('3125') + + const liquidationPrice2 = PositionUtils.getLiquidationPrice({ + position: { + collateralAmount: TokenAmount.createFrom({ amount: '2', token: WETH }), + debtAmount: TokenAmount.createFrom({ amount: '6000', token: DAI }), + } as Position, + liquidationThreshold, + collateralPrice, + debtPrice, + }) + expect(liquidationPrice2).toEqual('3750') + + const liquidationPrice3 = PositionUtils.getLiquidationPrice({ + position: { + collateralAmount: TokenAmount.createFrom({ amount: '2', token: WETH }), + debtAmount: TokenAmount.createFrom({ amount: '1', token: DAI }), + } as Position, + liquidationThreshold, + collateralPrice, + debtPrice, + }) + expect(liquidationPrice3).toEqual('0.625') + }) + + it('should return 0 when debt amount is 0', () => { + const liquidationThreshold = Percentage.createFrom({ percentage: 80 }) + const collateralPrice = '1000' + const debtPrice = '1' + + const liquidationPrice = PositionUtils.getLiquidationPrice({ + position: { + collateralAmount: TokenAmount.createFrom({ amount: '100', token: WETH }), + debtAmount: TokenAmount.createFrom({ amount: '0', token: DAI }), + } as Position, + liquidationThreshold, + collateralPrice, + debtPrice, + }) + + expect(liquidationPrice).toEqual('0') + }) + }) +}) diff --git a/sdk/sdk-client/tests/TestUtils.ts b/sdk/sdk-client/tests/TestUtils.ts new file mode 100644 index 0000000000..53b23bae93 --- /dev/null +++ b/sdk/sdk-client/tests/TestUtils.ts @@ -0,0 +1,20 @@ +import { Address, ChainFamilyMap, Token } from '@summerfi/sdk-common/common' + +export const chainInfo = ChainFamilyMap.Ethereum.Mainnet + +// Tokens +export const WETH = Token.createFrom({ + chainInfo, + address: Address.createFrom({ value: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' }), + symbol: 'WETH', + name: 'Wrapped Ether', + decimals: 18, +}) + +export const DAI = Token.createFrom({ + chainInfo, + address: Address.createFrom({ value: '0x6B175474E89094C44Da98b954EedeAC495271d0F' }), + symbol: 'DAI', + name: 'Dai Stablecoin', + decimals: 18, +}) diff --git a/sdk/sdk-common/src/common/implementation/Percentage.ts b/sdk/sdk-common/src/common/implementation/Percentage.ts index 33eeebadb7..655322bae7 100644 --- a/sdk/sdk-common/src/common/implementation/Percentage.ts +++ b/sdk/sdk-common/src/common/implementation/Percentage.ts @@ -28,6 +28,10 @@ export class Percentage implements IPercentageSerialized { return Percentage.createFrom({ percentage: this.value + percentage.value }) } + toProportion(): number { + return this.value / 100 + } + toBaseUnit(params: { decimals: number }): string { const factor = new BigNumber(10).pow(params.decimals) return new BigNumber(this.value).multipliedBy(factor).toFixed(0).toString() From 569da63b5852d09d5ea8d56e35444fdc7d300ce2 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Fri, 22 Mar 2024 20:30:50 +0100 Subject: [PATCH 02/26] Refactor --- sdk/sdk-client/src/utils/PositionUtils.ts | 12 +++++++----- sdk/sdk-client/tests/PositonUtils.spec.ts | 12 ++++++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/sdk/sdk-client/src/utils/PositionUtils.ts b/sdk/sdk-client/src/utils/PositionUtils.ts index 9554231bf6..30f2932bcf 100644 --- a/sdk/sdk-client/src/utils/PositionUtils.ts +++ b/sdk/sdk-client/src/utils/PositionUtils.ts @@ -1,20 +1,22 @@ -import { Percentage, type Position } from '@summerfi/sdk-common/common' +import { Percentage, type Position, type TokenAmount } from '@summerfi/sdk-common/common' import BigNumber from 'bignumber.js' export class PositionUtils { static getLTV({ - position, + collateralAmount, + debtAmount, collateralPrice, debtPrice, }: { - position: Position + collateralAmount: TokenAmount + debtAmount: TokenAmount collateralPrice: string debtPrice: string }): Percentage { // Determine the Collateral Value: - const collValue = new BigNumber(position.collateralAmount.amount).times(collateralPrice) + const collValue = new BigNumber(collateralAmount.amount).times(collateralPrice) // Determine the Borrowed Value: - const debtValue = new BigNumber(position.debtAmount.amount).times(debtPrice) + const debtValue = new BigNumber(debtAmount.amount).times(debtPrice) // If the collateral value is 0, return 0. if (collValue.isZero()) { return Percentage.createFrom({ percentage: 0 }) diff --git a/sdk/sdk-client/tests/PositonUtils.spec.ts b/sdk/sdk-client/tests/PositonUtils.spec.ts index 41fb2fa3b3..167ad7b19a 100644 --- a/sdk/sdk-client/tests/PositonUtils.spec.ts +++ b/sdk/sdk-client/tests/PositonUtils.spec.ts @@ -12,7 +12,8 @@ describe.only('PositionUtils', () => { const debtPrice = '1' const result = PositionUtils.getLTV({ - position: { collateralAmount, debtAmount } as Position, + collateralAmount, + debtAmount, collateralPrice, debtPrice, }) @@ -27,7 +28,8 @@ describe.only('PositionUtils', () => { const debtPrice = '1' const result = PositionUtils.getLTV({ - position: { collateralAmount, debtAmount } as Position, + collateralAmount, + debtAmount, collateralPrice, debtPrice, }) @@ -42,7 +44,8 @@ describe.only('PositionUtils', () => { const debtPrice = '1' const result = PositionUtils.getLTV({ - position: { collateralAmount, debtAmount } as Position, + collateralAmount, + debtAmount, collateralPrice, debtPrice, }) @@ -57,7 +60,8 @@ describe.only('PositionUtils', () => { const debtPrice = '1' const result = PositionUtils.getLTV({ - position: { collateralAmount, debtAmount } as Position, + collateralAmount, + debtAmount, collateralPrice, debtPrice, }) From 530c7d0760933f1d9a8adce06b564e40f6501ca6 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Fri, 22 Mar 2024 20:33:18 +0100 Subject: [PATCH 03/26] Refactor --- sdk/sdk-client/src/utils/PositionUtils.ts | 12 ++++++------ sdk/sdk-client/tests/PositonUtils.spec.ts | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/sdk/sdk-client/src/utils/PositionUtils.ts b/sdk/sdk-client/src/utils/PositionUtils.ts index 30f2932bcf..c34cb876a2 100644 --- a/sdk/sdk-client/src/utils/PositionUtils.ts +++ b/sdk/sdk-client/src/utils/PositionUtils.ts @@ -3,20 +3,20 @@ import BigNumber from 'bignumber.js' export class PositionUtils { static getLTV({ - collateralAmount, - debtAmount, + collateralTokenAmount, + debtTokenAmount, collateralPrice, debtPrice, }: { - collateralAmount: TokenAmount - debtAmount: TokenAmount + collateralTokenAmount: TokenAmount + debtTokenAmount: TokenAmount collateralPrice: string debtPrice: string }): Percentage { // Determine the Collateral Value: - const collValue = new BigNumber(collateralAmount.amount).times(collateralPrice) + const collValue = new BigNumber(collateralTokenAmount.amount).times(collateralPrice) // Determine the Borrowed Value: - const debtValue = new BigNumber(debtAmount.amount).times(debtPrice) + const debtValue = new BigNumber(debtTokenAmount.amount).times(debtPrice) // If the collateral value is 0, return 0. if (collValue.isZero()) { return Percentage.createFrom({ percentage: 0 }) diff --git a/sdk/sdk-client/tests/PositonUtils.spec.ts b/sdk/sdk-client/tests/PositonUtils.spec.ts index 167ad7b19a..35dea6453a 100644 --- a/sdk/sdk-client/tests/PositonUtils.spec.ts +++ b/sdk/sdk-client/tests/PositonUtils.spec.ts @@ -12,8 +12,8 @@ describe.only('PositionUtils', () => { const debtPrice = '1' const result = PositionUtils.getLTV({ - collateralAmount, - debtAmount, + collateralTokenAmount: collateralAmount, + debtTokenAmount: debtAmount, collateralPrice, debtPrice, }) @@ -28,8 +28,8 @@ describe.only('PositionUtils', () => { const debtPrice = '1' const result = PositionUtils.getLTV({ - collateralAmount, - debtAmount, + collateralTokenAmount: collateralAmount, + debtTokenAmount: debtAmount, collateralPrice, debtPrice, }) @@ -44,8 +44,8 @@ describe.only('PositionUtils', () => { const debtPrice = '1' const result = PositionUtils.getLTV({ - collateralAmount, - debtAmount, + collateralTokenAmount: collateralAmount, + debtTokenAmount: debtAmount, collateralPrice, debtPrice, }) @@ -60,8 +60,8 @@ describe.only('PositionUtils', () => { const debtPrice = '1' const result = PositionUtils.getLTV({ - collateralAmount, - debtAmount, + collateralTokenAmount: collateralAmount, + debtTokenAmount: debtAmount, collateralPrice, debtPrice, }) From a5f91195e99335a1032f866ff11849fba0160061 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Tue, 26 Mar 2024 14:34:18 +0100 Subject: [PATCH 04/26] Fixed review comments --- sdk/sdk-client/src/utils/PositionUtils.ts | 30 ++++++----- sdk/sdk-client/tests/PositonUtils.spec.ts | 61 ++++++++++------------- 2 files changed, 44 insertions(+), 47 deletions(-) diff --git a/sdk/sdk-client/src/utils/PositionUtils.ts b/sdk/sdk-client/src/utils/PositionUtils.ts index c34cb876a2..035abb23c0 100644 --- a/sdk/sdk-client/src/utils/PositionUtils.ts +++ b/sdk/sdk-client/src/utils/PositionUtils.ts @@ -1,22 +1,27 @@ -import { Percentage, type Position, type TokenAmount } from '@summerfi/sdk-common/common' +import { + Percentage, + type Position, + type Price, + type TokenAmount, +} from '@summerfi/sdk-common/common' import BigNumber from 'bignumber.js' export class PositionUtils { static getLTV({ collateralTokenAmount, debtTokenAmount, - collateralPrice, - debtPrice, + collateralPriceInUsd, + debtPriceInUsd, }: { collateralTokenAmount: TokenAmount debtTokenAmount: TokenAmount - collateralPrice: string - debtPrice: string + collateralPriceInUsd: Price + debtPriceInUsd: Price }): Percentage { // Determine the Collateral Value: - const collValue = new BigNumber(collateralTokenAmount.amount).times(collateralPrice) + const collValue = new BigNumber(collateralTokenAmount.amount).times(collateralPriceInUsd.value) // Determine the Borrowed Value: - const debtValue = new BigNumber(debtTokenAmount.amount).times(debtPrice) + const debtValue = new BigNumber(debtTokenAmount.amount).times(debtPriceInUsd.value) // If the collateral value is 0, return 0. if (collValue.isZero()) { return Percentage.createFrom({ percentage: 0 }) @@ -26,16 +31,15 @@ export class PositionUtils { return Percentage.createFrom({ percentage: ltvRatio }) } - static getLiquidationPrice({ + static getLiquidationPriceInUsd({ position, liquidationThreshold, - debtPrice, + debtPriceInUsd, }: { position: Position - // TODO: passed as param because it is not defined in Position + // TODO: it is not defined in Position yet, we should use Pool in the future liquidationThreshold: Percentage - collateralPrice: string - debtPrice: string + debtPriceInUsd: Price }): string { // Determine the Collateral Value: const collateralAmount = new BigNumber(position.collateralAmount.amount) @@ -45,7 +49,7 @@ export class PositionUtils { if (debtAmount.isZero()) { return '0' } - const debtValue = debtAmount.times(debtPrice) + const debtValue = debtAmount.times(debtPriceInUsd.value) // (loanAmount * loanPrice) / (liquidationThreshold * collateralAmount) const liquidationPrice = debtValue.div( collateralAmount.times(liquidationThreshold.toProportion()), diff --git a/sdk/sdk-client/tests/PositonUtils.spec.ts b/sdk/sdk-client/tests/PositonUtils.spec.ts index 35dea6453a..2fd1577d2d 100644 --- a/sdk/sdk-client/tests/PositonUtils.spec.ts +++ b/sdk/sdk-client/tests/PositonUtils.spec.ts @@ -1,21 +1,28 @@ -import { Percentage, TokenAmount, type Position } from '@summerfi/sdk-common/common' +import { Percentage, TokenAmount, type Position, Price } from '@summerfi/sdk-common/common' import { PositionUtils } from '../src/utils/PositionUtils' import { WETH, DAI } from './TestUtils' -describe.only('PositionUtils', () => { +describe('PositionUtils', () => { + const ethPriceInUsd = Price.createFrom({ + value: '1000', + baseToken: WETH, + }) + const daiPriceInUsd = Price.createFrom({ + value: '1', + baseToken: DAI, + }) + describe('getLTV', () => { it('should correctly calculate LTV', () => { const collateralAmount = TokenAmount.createFrom({ amount: '1', token: WETH }) - const collateralPrice = '1000' const debtAmount = TokenAmount.createFrom({ amount: '250', token: DAI }) - const debtPrice = '1' const result = PositionUtils.getLTV({ collateralTokenAmount: collateralAmount, debtTokenAmount: debtAmount, - collateralPrice, - debtPrice, + collateralPriceInUsd: ethPriceInUsd, + debtPriceInUsd: daiPriceInUsd, }) expect(result).toEqual(Percentage.createFrom({ percentage: 25 })) @@ -23,15 +30,13 @@ describe.only('PositionUtils', () => { it('should return 0 when debt is equal to 0', () => { const collateralAmount = TokenAmount.createFrom({ amount: '1', token: WETH }) - const collateralPrice = '1000' const debtAmount = TokenAmount.createFrom({ amount: '0', token: DAI }) - const debtPrice = '1' const result = PositionUtils.getLTV({ collateralTokenAmount: collateralAmount, debtTokenAmount: debtAmount, - collateralPrice, - debtPrice, + collateralPriceInUsd: ethPriceInUsd, + debtPriceInUsd: daiPriceInUsd, }) expect(result).toEqual(Percentage.createFrom({ percentage: 0 })) @@ -39,15 +44,13 @@ describe.only('PositionUtils', () => { it('should return 0 when collateral amount is 0', () => { const collateralAmount = TokenAmount.createFrom({ amount: '0', token: WETH }) - const collateralPrice = '1000' const debtAmount = TokenAmount.createFrom({ amount: '1000', token: DAI }) - const debtPrice = '1' const result = PositionUtils.getLTV({ collateralTokenAmount: collateralAmount, debtTokenAmount: debtAmount, - collateralPrice, - debtPrice, + collateralPriceInUsd: ethPriceInUsd, + debtPriceInUsd: daiPriceInUsd, }) expect(result).toEqual(Percentage.createFrom({ percentage: 0 })) @@ -55,15 +58,13 @@ describe.only('PositionUtils', () => { it('should return 100 when debt is equal to collateral', () => { const collateralAmount = TokenAmount.createFrom({ amount: '1', token: WETH }) - const collateralPrice = '1000' const debtAmount = TokenAmount.createFrom({ amount: '1000', token: DAI }) - const debtPrice = '1' const result = PositionUtils.getLTV({ collateralTokenAmount: collateralAmount, debtTokenAmount: debtAmount, - collateralPrice, - debtPrice, + collateralPriceInUsd: ethPriceInUsd, + debtPriceInUsd: daiPriceInUsd, }) expect(result).toEqual(Percentage.createFrom({ percentage: 100 })) @@ -73,56 +74,48 @@ describe.only('PositionUtils', () => { describe('getLiquidationPrice', () => { it('should correctly calculate liquidation price when debt amount is not 0', () => { const liquidationThreshold = Percentage.createFrom({ percentage: 80 }) - const collateralPrice = '3000' - const debtPrice = '1' - const liquidationPrice = PositionUtils.getLiquidationPrice({ + const liquidationPrice = PositionUtils.getLiquidationPriceInUsd({ position: { collateralAmount: TokenAmount.createFrom({ amount: '2', token: WETH }), debtAmount: TokenAmount.createFrom({ amount: '5000', token: DAI }), } as Position, liquidationThreshold, - collateralPrice, - debtPrice, + debtPriceInUsd: daiPriceInUsd, }) expect(liquidationPrice).toEqual('3125') - const liquidationPrice2 = PositionUtils.getLiquidationPrice({ + const liquidationPrice2 = PositionUtils.getLiquidationPriceInUsd({ position: { collateralAmount: TokenAmount.createFrom({ amount: '2', token: WETH }), debtAmount: TokenAmount.createFrom({ amount: '6000', token: DAI }), } as Position, liquidationThreshold, - collateralPrice, - debtPrice, + debtPriceInUsd: daiPriceInUsd, }) expect(liquidationPrice2).toEqual('3750') - const liquidationPrice3 = PositionUtils.getLiquidationPrice({ + const liquidationPrice3 = PositionUtils.getLiquidationPriceInUsd({ position: { collateralAmount: TokenAmount.createFrom({ amount: '2', token: WETH }), debtAmount: TokenAmount.createFrom({ amount: '1', token: DAI }), } as Position, liquidationThreshold, - collateralPrice, - debtPrice, + debtPriceInUsd: daiPriceInUsd, }) expect(liquidationPrice3).toEqual('0.625') }) it('should return 0 when debt amount is 0', () => { const liquidationThreshold = Percentage.createFrom({ percentage: 80 }) - const collateralPrice = '1000' - const debtPrice = '1' - const liquidationPrice = PositionUtils.getLiquidationPrice({ + const liquidationPrice = PositionUtils.getLiquidationPriceInUsd({ position: { collateralAmount: TokenAmount.createFrom({ amount: '100', token: WETH }), debtAmount: TokenAmount.createFrom({ amount: '0', token: DAI }), } as Position, liquidationThreshold, - collateralPrice, - debtPrice, + debtPriceInUsd: daiPriceInUsd, }) expect(liquidationPrice).toEqual('0') From 59596f713849226d671a8e3dc886fb42e7754ed1 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Tue, 26 Mar 2024 18:55:32 +0100 Subject: [PATCH 05/26] Fixed merge issue --- sdk/sdk-client/package.json | 1 - sdk/sdk-client/src/utils/PositionUtils.ts | 4 ++-- sdk/sdk-client/tests/PositonUtils.spec.ts | 22 +++++++++++++++------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/sdk/sdk-client/package.json b/sdk/sdk-client/package.json index 57abf41f3d..a5ef9df848 100644 --- a/sdk/sdk-client/package.json +++ b/sdk/sdk-client/package.json @@ -19,7 +19,6 @@ "lint:fix": "eslint . --fix" }, "dependencies": { - "@summerfi/protocol-manager": "workspace:*", "@summerfi/protocol-manager-service": "workspace:*", "@summerfi/protocol-plugins": "workspace:*", "@summerfi/sdk-common": "workspace:*", diff --git a/sdk/sdk-client/src/utils/PositionUtils.ts b/sdk/sdk-client/src/utils/PositionUtils.ts index 035abb23c0..909cbf926e 100644 --- a/sdk/sdk-client/src/utils/PositionUtils.ts +++ b/sdk/sdk-client/src/utils/PositionUtils.ts @@ -24,11 +24,11 @@ export class PositionUtils { const debtValue = new BigNumber(debtTokenAmount.amount).times(debtPriceInUsd.value) // If the collateral value is 0, return 0. if (collValue.isZero()) { - return Percentage.createFrom({ percentage: 0 }) + return Percentage.createFrom({ value: 0 }) } // Divide the borrowed value by the collateral value and multiply by 100 to get the percentage LTV. const ltvRatio = debtValue.div(collValue).times(100).toNumber() - return Percentage.createFrom({ percentage: ltvRatio }) + return Percentage.createFrom({ value: ltvRatio }) } static getLiquidationPriceInUsd({ diff --git a/sdk/sdk-client/tests/PositonUtils.spec.ts b/sdk/sdk-client/tests/PositonUtils.spec.ts index 2fd1577d2d..5e116b44e2 100644 --- a/sdk/sdk-client/tests/PositonUtils.spec.ts +++ b/sdk/sdk-client/tests/PositonUtils.spec.ts @@ -1,4 +1,10 @@ -import { Percentage, TokenAmount, type Position, Price } from '@summerfi/sdk-common/common' +import { + Percentage, + TokenAmount, + type Position, + Price, + CurrencySymbol, +} from '@summerfi/sdk-common/common' import { PositionUtils } from '../src/utils/PositionUtils' import { WETH, DAI } from './TestUtils' @@ -7,10 +13,12 @@ describe('PositionUtils', () => { const ethPriceInUsd = Price.createFrom({ value: '1000', baseToken: WETH, + quoteToken: CurrencySymbol.USD, }) const daiPriceInUsd = Price.createFrom({ value: '1', baseToken: DAI, + quoteToken: CurrencySymbol.USD, }) describe('getLTV', () => { @@ -25,7 +33,7 @@ describe('PositionUtils', () => { debtPriceInUsd: daiPriceInUsd, }) - expect(result).toEqual(Percentage.createFrom({ percentage: 25 })) + expect(result).toEqual(Percentage.createFrom({ value: 25 })) }) it('should return 0 when debt is equal to 0', () => { @@ -39,7 +47,7 @@ describe('PositionUtils', () => { debtPriceInUsd: daiPriceInUsd, }) - expect(result).toEqual(Percentage.createFrom({ percentage: 0 })) + expect(result).toEqual(Percentage.createFrom({ value: 0 })) }) it('should return 0 when collateral amount is 0', () => { @@ -53,7 +61,7 @@ describe('PositionUtils', () => { debtPriceInUsd: daiPriceInUsd, }) - expect(result).toEqual(Percentage.createFrom({ percentage: 0 })) + expect(result).toEqual(Percentage.createFrom({ value: 0 })) }) it('should return 100 when debt is equal to collateral', () => { @@ -67,13 +75,13 @@ describe('PositionUtils', () => { debtPriceInUsd: daiPriceInUsd, }) - expect(result).toEqual(Percentage.createFrom({ percentage: 100 })) + expect(result).toEqual(Percentage.createFrom({ value: 100 })) }) }) describe('getLiquidationPrice', () => { it('should correctly calculate liquidation price when debt amount is not 0', () => { - const liquidationThreshold = Percentage.createFrom({ percentage: 80 }) + const liquidationThreshold = Percentage.createFrom({ value: 80 }) const liquidationPrice = PositionUtils.getLiquidationPriceInUsd({ position: { @@ -107,7 +115,7 @@ describe('PositionUtils', () => { }) it('should return 0 when debt amount is 0', () => { - const liquidationThreshold = Percentage.createFrom({ percentage: 80 }) + const liquidationThreshold = Percentage.createFrom({ value: 80 }) const liquidationPrice = PositionUtils.getLiquidationPriceInUsd({ position: { From 4c343149a5e892b8e5885a51d0ca9a0081d6f47f Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Tue, 26 Mar 2024 19:18:13 +0100 Subject: [PATCH 06/26] Fixed type in test --- sdk/sdk-client/tests/TestUtils.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sdk/sdk-client/tests/TestUtils.ts b/sdk/sdk-client/tests/TestUtils.ts index 53b23bae93..fe6a1359b8 100644 --- a/sdk/sdk-client/tests/TestUtils.ts +++ b/sdk/sdk-client/tests/TestUtils.ts @@ -1,11 +1,14 @@ -import { Address, ChainFamilyMap, Token } from '@summerfi/sdk-common/common' +import { Address, ChainFamilyMap, Token, AddressType } from '@summerfi/sdk-common/common' export const chainInfo = ChainFamilyMap.Ethereum.Mainnet // Tokens export const WETH = Token.createFrom({ chainInfo, - address: Address.createFrom({ value: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' }), + address: Address.createFrom({ + value: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', + type: AddressType.Ethereum, + }), symbol: 'WETH', name: 'Wrapped Ether', decimals: 18, @@ -13,7 +16,10 @@ export const WETH = Token.createFrom({ export const DAI = Token.createFrom({ chainInfo, - address: Address.createFrom({ value: '0x6B175474E89094C44Da98b954EedeAC495271d0F' }), + address: Address.createFrom({ + value: '0x6B175474E89094C44Da98b954EedeAC495271d0F', + type: AddressType.Ethereum, + }), symbol: 'DAI', name: 'Dai Stablecoin', decimals: 18, From 6abbad823ae2b0c9b07e1b5f465ba61d2958922c Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Wed, 27 Mar 2024 10:53:00 +0100 Subject: [PATCH 07/26] Updated exports from common --- sdk/sdk-common/package.json | 6 ++++++ sdk/sdk-common/src/index.ts | 7 +++++++ 2 files changed, 13 insertions(+) create mode 100644 sdk/sdk-common/src/index.ts diff --git a/sdk/sdk-common/package.json b/sdk/sdk-common/package.json index fef5a4cf61..ab6a58ee13 100644 --- a/sdk/sdk-common/package.json +++ b/sdk/sdk-common/package.json @@ -1,7 +1,13 @@ { "name": "@summerfi/sdk-common", "version": "0.0.1", + "main": "dist/index.js", + "types": "dist/index.d.ts", "exports": { + ".": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + }, "./*": { "import": "./src/*/index.ts", "types": "./src/*/index.d.ts" diff --git a/sdk/sdk-common/src/index.ts b/sdk/sdk-common/src/index.ts new file mode 100644 index 0000000000..2a7f2afc40 --- /dev/null +++ b/sdk/sdk-common/src/index.ts @@ -0,0 +1,7 @@ +export * from './common' +export * from './exchange' +export * from './orders' +export * from './protocols' +export * from './simulation' +export * from './user' +export * from './utils' From 55dde9274c1f23f1f05f59c3202e1948fc348134 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:52:38 +0100 Subject: [PATCH 08/26] Updated bundle scripts --- .gitignore | 2 +- sdk/sdk-client/bundle/package.json | 14 ++++++++++++++ sdk/sdk-client/package.json | 3 ++- sdk/sdk-common/bundle/package.json | 14 ++++++++++++++ sdk/sdk-common/package.json | 7 ++++--- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 sdk/sdk-client/bundle/package.json create mode 100644 sdk/sdk-common/bundle/package.json diff --git a/.gitignore b/.gitignore index c30bed1fd5..7de4a0df05 100644 --- a/.gitignore +++ b/.gitignore @@ -66,4 +66,4 @@ artifacts .vscode # private http envs -*.private.env.json \ No newline at end of file +*.private.env.json diff --git a/sdk/sdk-client/bundle/package.json b/sdk/sdk-client/bundle/package.json new file mode 100644 index 0000000000..1e25a0516b --- /dev/null +++ b/sdk/sdk-client/bundle/package.json @@ -0,0 +1,14 @@ +{ + "name": "summerfi-sdk-client", + "version": "0.0.1", + "main": "dist/index.js", + "types": "dist/src/index.d.ts", + "packageManager": "yarn@1.22.21", + "scripts": {}, + "dependencies": { + "@trpc/client": "11.0.0-next-beta.264", + "bignumber.js": "^9.1.2", + "superjson": "^1.13.3", + "viem": "^2.7.9" + } +} diff --git a/sdk/sdk-client/package.json b/sdk/sdk-client/package.json index a5ef9df848..57e0267ccc 100644 --- a/sdk/sdk-client/package.json +++ b/sdk/sdk-client/package.json @@ -16,7 +16,8 @@ "testw": "jest --watch", "build": "tsc -b -v tsconfig.build.json", "lint": "eslint .", - "lint:fix": "eslint . --fix" + "lint:fix": "eslint . --fix", + "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist" }, "dependencies": { "@summerfi/protocol-manager-service": "workspace:*", diff --git a/sdk/sdk-common/bundle/package.json b/sdk/sdk-common/bundle/package.json new file mode 100644 index 0000000000..d839792a99 --- /dev/null +++ b/sdk/sdk-common/bundle/package.json @@ -0,0 +1,14 @@ +{ + "name": "summerfi-sdk-common", + "version": "0.0.5", + "main": "dist/index.js", + "types": "dist/src/index.d.ts", + "packageManager": "yarn@1.22.21", + "scripts": {}, + "dependencies": { + "bignumber.js": "9.0.1", + "superjson": "^1.13.3", + "viem": "^2.2.0" + }, + "devDependencies": {} +} diff --git a/sdk/sdk-common/package.json b/sdk/sdk-common/package.json index ab6a58ee13..7f68c1a163 100644 --- a/sdk/sdk-common/package.json +++ b/sdk/sdk-common/package.json @@ -2,11 +2,11 @@ "name": "@summerfi/sdk-common", "version": "0.0.1", "main": "dist/index.js", - "types": "dist/index.d.ts", + "types": "dist/src/index.d.ts", "exports": { ".": { "import": "./dist/index.js", - "types": "./dist/index.d.ts" + "types": "./dist/src/index.d.ts" }, "./*": { "import": "./src/*/index.ts", @@ -21,7 +21,8 @@ "build": "tsc -b -v tsconfig.build.json", "lint": "eslint .", "lint:fix": "eslint . --fix", - "check-circular": "madge --circular --extensions ts ./src" + "check-circular": "madge --circular --extensions ts ./src", + "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist" }, "dependencies": { "bignumber.js": "9.0.1", From e2470a431c95a613e338b79951c980d8068b976d Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:53:47 +0100 Subject: [PATCH 09/26] Deployed to npm --- sdk/sdk-client/bundle/package.json | 2 +- sdk/sdk-common/bundle/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/sdk-client/bundle/package.json b/sdk/sdk-client/bundle/package.json index 1e25a0516b..db7cdd9ed9 100644 --- a/sdk/sdk-client/bundle/package.json +++ b/sdk/sdk-client/bundle/package.json @@ -1,6 +1,6 @@ { "name": "summerfi-sdk-client", - "version": "0.0.1", + "version": "0.0.2", "main": "dist/index.js", "types": "dist/src/index.d.ts", "packageManager": "yarn@1.22.21", diff --git a/sdk/sdk-common/bundle/package.json b/sdk/sdk-common/bundle/package.json index d839792a99..f00ccf0e54 100644 --- a/sdk/sdk-common/bundle/package.json +++ b/sdk/sdk-common/bundle/package.json @@ -1,6 +1,6 @@ { "name": "summerfi-sdk-common", - "version": "0.0.5", + "version": "0.0.6", "main": "dist/index.js", "types": "dist/src/index.d.ts", "packageManager": "yarn@1.22.21", From e16c2c6ffe49f94d633df0591e9c78cb45b35caf Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Thu, 28 Mar 2024 11:03:58 +0100 Subject: [PATCH 10/26] bump versions --- sdk/sdk-client/bundle/package.json | 2 +- sdk/sdk-common/bundle/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/sdk-client/bundle/package.json b/sdk/sdk-client/bundle/package.json index db7cdd9ed9..357b75fde9 100644 --- a/sdk/sdk-client/bundle/package.json +++ b/sdk/sdk-client/bundle/package.json @@ -1,6 +1,6 @@ { "name": "summerfi-sdk-client", - "version": "0.0.2", + "version": "0.0.7", "main": "dist/index.js", "types": "dist/src/index.d.ts", "packageManager": "yarn@1.22.21", diff --git a/sdk/sdk-common/bundle/package.json b/sdk/sdk-common/bundle/package.json index f00ccf0e54..ac51e34328 100644 --- a/sdk/sdk-common/bundle/package.json +++ b/sdk/sdk-common/bundle/package.json @@ -1,6 +1,6 @@ { "name": "summerfi-sdk-common", - "version": "0.0.6", + "version": "0.0.7", "main": "dist/index.js", "types": "dist/src/index.d.ts", "packageManager": "yarn@1.22.21", From 0daee79636661a6f8669873840705eb8023d82c5 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Thu, 28 Mar 2024 16:11:10 +0100 Subject: [PATCH 11/26] release --- sdk/sdk-client/bundle/package.json | 4 ++-- sdk/sdk-common/bundle/package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/sdk-client/bundle/package.json b/sdk/sdk-client/bundle/package.json index 357b75fde9..e3fc866c85 100644 --- a/sdk/sdk-client/bundle/package.json +++ b/sdk/sdk-client/bundle/package.json @@ -1,7 +1,7 @@ { "name": "summerfi-sdk-client", - "version": "0.0.7", - "main": "dist/index.js", + "version": "0.0.8", + "module": "dist/index.js", "types": "dist/src/index.d.ts", "packageManager": "yarn@1.22.21", "scripts": {}, diff --git a/sdk/sdk-common/bundle/package.json b/sdk/sdk-common/bundle/package.json index ac51e34328..89ddbf5243 100644 --- a/sdk/sdk-common/bundle/package.json +++ b/sdk/sdk-common/bundle/package.json @@ -1,7 +1,7 @@ { "name": "summerfi-sdk-common", - "version": "0.0.7", - "main": "dist/index.js", + "version": "0.0.8", + "module": "dist/index.js", "types": "dist/src/index.d.ts", "packageManager": "yarn@1.22.21", "scripts": {}, From 1e54541950c1624565c2a2e8b6e40dd1fcee0f79 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Wed, 3 Apr 2024 10:17:03 +0200 Subject: [PATCH 12/26] merge artifacts --- pnpm-lock.yaml | 64 ++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4aad095882..0645b01ef5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -89,7 +89,7 @@ importers: version: 2.39.7 ts-jest: specifier: ^29.1.1 - version: 29.1.1(@babel/core@7.23.7)(esbuild@0.19.11)(jest@29.7.0)(typescript@5.3.3) + version: 29.1.1(@babel/core@7.24.0)(esbuild@0.19.11)(jest@29.7.0)(typescript@5.3.3) tsc-alias: specifier: ^1.8.8 version: 1.8.8 @@ -885,6 +885,21 @@ importers: specifier: workspace:* version: link:../../packages/typescript-config + sdk/sdk-client/bundle: + dependencies: + '@trpc/client': + specifier: 11.0.0-next-beta.264 + version: 11.0.0-next-beta.264(@trpc/server@11.0.0-next-beta.264) + bignumber.js: + specifier: ^9.1.2 + version: 9.1.2 + superjson: + specifier: ^1.13.3 + version: 1.13.3 + viem: + specifier: ^2.7.9 + version: 2.9.3(typescript@5.3.3) + sdk/sdk-common: dependencies: '@summerfi/common': @@ -907,6 +922,18 @@ importers: specifier: workspace:* version: link:../../packages/typescript-config + sdk/sdk-common/bundle: + dependencies: + bignumber.js: + specifier: 9.0.1 + version: 9.0.1 + superjson: + specifier: ^1.13.3 + version: 1.13.3 + viem: + specifier: ^2.2.0 + version: 2.9.3(typescript@5.3.3) + sdk/sdk-e2e: devDependencies: '@summerfi/core-contracts': @@ -16106,41 +16133,6 @@ packages: engines: {node: '>=14.16'} dev: false - /ts-jest@29.1.1(@babel/core@7.23.7)(esbuild@0.19.11)(jest@29.7.0)(typescript@5.3.3): - resolution: {integrity: sha512-D6xjnnbP17cC85nliwGiL+tpoKN0StpgE0TeOjXQTU6MVCfsB4v7aW05CgQ/1OywGb0x/oy9hHFnN+sczTiRaA==} - engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} - hasBin: true - peerDependencies: - '@babel/core': '>=7.0.0-beta.0 <8' - '@jest/types': ^29.0.0 - babel-jest: ^29.0.0 - esbuild: '*' - jest: ^29.0.0 - typescript: '>=4.3 <6' - peerDependenciesMeta: - '@babel/core': - optional: true - '@jest/types': - optional: true - babel-jest: - optional: true - esbuild: - optional: true - dependencies: - '@babel/core': 7.23.7 - bs-logger: 0.2.6 - esbuild: 0.19.11 - fast-json-stable-stringify: 2.1.0 - jest: 29.7.0(@types/node@20.11.5) - jest-util: 29.7.0 - json5: 2.2.3 - lodash.memoize: 4.1.2 - make-error: 1.3.6 - semver: 7.5.4 - typescript: 5.3.3 - yargs-parser: 21.1.1 - dev: true - /ts-jest@29.1.1(@babel/core@7.24.0)(esbuild@0.19.11)(jest@29.7.0)(typescript@5.3.3): resolution: {integrity: sha512-D6xjnnbP17cC85nliwGiL+tpoKN0StpgE0TeOjXQTU6MVCfsB4v7aW05CgQ/1OywGb0x/oy9hHFnN+sczTiRaA==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} From d334be0401544513740895668d981d299162fd51 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Wed, 3 Apr 2024 13:27:06 +0200 Subject: [PATCH 13/26] published 0.0.9 --- sdk/sdk-client/bundle/package.json | 2 +- sdk/sdk-client/package.json | 3 ++- sdk/sdk-common/bundle/package.json | 2 +- sdk/sdk-common/package.json | 3 ++- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/sdk/sdk-client/bundle/package.json b/sdk/sdk-client/bundle/package.json index e3fc866c85..3a67146c75 100644 --- a/sdk/sdk-client/bundle/package.json +++ b/sdk/sdk-client/bundle/package.json @@ -1,6 +1,6 @@ { "name": "summerfi-sdk-client", - "version": "0.0.8", + "version": "0.0.9", "module": "dist/index.js", "types": "dist/src/index.d.ts", "packageManager": "yarn@1.22.21", diff --git a/sdk/sdk-client/package.json b/sdk/sdk-client/package.json index f84089a0dc..2e997c17d6 100644 --- a/sdk/sdk-client/package.json +++ b/sdk/sdk-client/package.json @@ -17,7 +17,8 @@ "build": "tsc -b -v tsconfig.build.json", "lint": "eslint .", "lint:fix": "eslint . --fix", - "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist" + "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist", + "publish:npm": "pnpm bundle:npm && cd bundle && npm publish && cd .." }, "dependencies": { "@summerfi/sdk-common": "workspace:*", diff --git a/sdk/sdk-common/bundle/package.json b/sdk/sdk-common/bundle/package.json index 89ddbf5243..29836569cf 100644 --- a/sdk/sdk-common/bundle/package.json +++ b/sdk/sdk-common/bundle/package.json @@ -1,6 +1,6 @@ { "name": "summerfi-sdk-common", - "version": "0.0.8", + "version": "0.0.9", "module": "dist/index.js", "types": "dist/src/index.d.ts", "packageManager": "yarn@1.22.21", diff --git a/sdk/sdk-common/package.json b/sdk/sdk-common/package.json index 6a22f5438a..02191a254a 100644 --- a/sdk/sdk-common/package.json +++ b/sdk/sdk-common/package.json @@ -22,7 +22,8 @@ "lint": "eslint .", "lint:fix": "eslint . --fix", "check-circular": "madge --circular --extensions ts ./src", - "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist" + "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist", + "publish:npm": "pnpm bundle:npm && cd bundle && npm publish && cd .." }, "dependencies": { "bignumber.js": "9.0.1", From 250d5b2e3f2c70b09cf703a4337df8044e967b70 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Wed, 3 Apr 2024 13:51:00 +0200 Subject: [PATCH 14/26] updated bundle scripts to use tsconfig for build --- sdk/sdk-client/package.json | 2 +- sdk/sdk-common/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/sdk-client/package.json b/sdk/sdk-client/package.json index 2e997c17d6..99e05e23ce 100644 --- a/sdk/sdk-client/package.json +++ b/sdk/sdk-client/package.json @@ -17,7 +17,7 @@ "build": "tsc -b -v tsconfig.build.json", "lint": "eslint .", "lint:fix": "eslint . --fix", - "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist", + "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist -p tsconfig.build.json", "publish:npm": "pnpm bundle:npm && cd bundle && npm publish && cd .." }, "dependencies": { diff --git a/sdk/sdk-common/package.json b/sdk/sdk-common/package.json index 02191a254a..b96797334a 100644 --- a/sdk/sdk-common/package.json +++ b/sdk/sdk-common/package.json @@ -22,7 +22,7 @@ "lint": "eslint .", "lint:fix": "eslint . --fix", "check-circular": "madge --circular --extensions ts ./src", - "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist", + "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist -p tsconfig.build.json", "publish:npm": "pnpm bundle:npm && cd bundle && npm publish && cd .." }, "dependencies": { From 397098b39c3590c000f90b88d3e69afc91f33de0 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:01:38 +0200 Subject: [PATCH 15/26] Published a new package protocols-plugins --- sdk/protocol-plugins/bundle/package.json | 18 ++++++++++++++++++ sdk/protocol-plugins/package.json | 8 +++++++- sdk/protocol-plugins/src/index.ts | 5 +++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 sdk/protocol-plugins/bundle/package.json create mode 100644 sdk/protocol-plugins/src/index.ts diff --git a/sdk/protocol-plugins/bundle/package.json b/sdk/protocol-plugins/bundle/package.json new file mode 100644 index 0000000000..264be41217 --- /dev/null +++ b/sdk/protocol-plugins/bundle/package.json @@ -0,0 +1,18 @@ +{ + "name": "summerfi-protocol-plugins", + "version": "0.0.9", + "module": "dist/index.js", + "types": "dist/index.d.ts", + "packageManager": "yarn@1.22.21", + "scripts": {}, + "dependencies": { + "@summerfi/sdk-common": "workspace:*", + "@summerfi/deployment-utils": "workspace:*", + "@summerfi/deployment-types": "workspace:*", + "@summerfi/protocol-plugins-common": "workspace:*", + "@summerfi/testing-utils": "workspace:*", + "@summerfi/swap-common": "workspace:*", + "bignumber.js": "9.0.1", + "zod": "^3.22.4" + } +} diff --git a/sdk/protocol-plugins/package.json b/sdk/protocol-plugins/package.json index 204e9e5c45..d73d67ca93 100644 --- a/sdk/protocol-plugins/package.json +++ b/sdk/protocol-plugins/package.json @@ -2,6 +2,10 @@ "name": "@summerfi/protocol-plugins", "version": "0.0.1", "exports": { + ".": { + "import": "./src/index.ts", + "types": "./src/index.d.ts" + }, "./*": { "import": "./src/*/index.ts", "types": "./src/*/index.d.ts" @@ -16,7 +20,9 @@ "testw:integration": "jest tests/integration --watch", "lint": "eslint .", "lint:fix": "eslint . --fix", - "build": "tsc -b -v tsconfig.build.json" + "build": "tsc -b -v tsconfig.build.json", + "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js --external:crypto --external:tls --external:https --external:net --external:http --external:stream --external:zlib --external:assert --keep-names && tsc --emitDeclarationOnly --outDir bundle/dist -p tsconfig.build.json", + "publish:npm": "pnpm bundle:npm && cd bundle && npm publish && cd .." }, "dependencies": { "@summerfi/sdk-common": "workspace:*", diff --git a/sdk/protocol-plugins/src/index.ts b/sdk/protocol-plugins/src/index.ts new file mode 100644 index 0000000000..52572630aa --- /dev/null +++ b/sdk/protocol-plugins/src/index.ts @@ -0,0 +1,5 @@ +export * from './plugins/aave-v3' +export * from './plugins/spark' +export * from './plugins/maker' +export * from './plugins/common' +export * from './implementation' From f3dcd2b52a3937570a57d88f99a336f79c8109a7 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:04:58 +0200 Subject: [PATCH 16/26] Added error handling to the sdk client Added missing serialization registrations --- sdk/sdk-client/src/rpc/SDKClient.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/sdk/sdk-client/src/rpc/SDKClient.ts b/sdk/sdk-client/src/rpc/SDKClient.ts index ef822b0cc2..937e088a10 100644 --- a/sdk/sdk-client/src/rpc/SDKClient.ts +++ b/sdk/sdk-client/src/rpc/SDKClient.ts @@ -11,6 +11,7 @@ export * from '@summerfi/sdk-common/user' export * from '@summerfi/sdk-common/services' export * from '@summerfi/sdk-common/utils' export * from '@summerfi/sdk-common/protocols' +export * from '@summerfi/protocol-plugins' const EnableDeserialize = false @@ -21,9 +22,9 @@ export function createRPCClient(apiURL: string): RPCClientType { links: [ loggerLink({ enabled: (opts) => opts.direction === 'down' && opts.result instanceof Error, - logger: (data) => { - console.log(JSON.stringify(data, null, 2)) - }, + // logger: (data) => { + // console.log(JSON.stringify(data, null, 2)) + // }, }), httpBatchLink({ url: apiURL, @@ -32,9 +33,17 @@ export function createRPCClient(apiURL: string): RPCClientType { output: { serialize: SerializationService.getTransformer().serialize, deserialize: (object) => { - return EnableDeserialize - ? SerializationService.getTransformer().deserialize(object) - : SerializationService.getTransformer().parse(JSON.stringify(object, null, 2)) + try { + const res = SerializationService.getTransformer().parse( + JSON.stringify(object, null, 2), + ) + // console.log('deserialized', res) + return EnableDeserialize + ? SerializationService.getTransformer().deserialize(object) + : res + } catch (error) { + console.log(error) + } }, }, }, From df4e23d37b3764f50f828e13bc059e8a89e80a2c Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:06:15 +0200 Subject: [PATCH 17/26] Updated npm packages bundling scripts and type definitions --- sdk/sdk-client/bundle/package.json | 2 +- sdk/sdk-client/package.json | 3 ++- sdk/sdk-common/bundle/package.json | 2 +- sdk/sdk-common/package.json | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/sdk/sdk-client/bundle/package.json b/sdk/sdk-client/bundle/package.json index 3a67146c75..2f8ed3baae 100644 --- a/sdk/sdk-client/bundle/package.json +++ b/sdk/sdk-client/bundle/package.json @@ -2,7 +2,7 @@ "name": "summerfi-sdk-client", "version": "0.0.9", "module": "dist/index.js", - "types": "dist/src/index.d.ts", + "types": "dist/index.d.ts", "packageManager": "yarn@1.22.21", "scripts": {}, "dependencies": { diff --git a/sdk/sdk-client/package.json b/sdk/sdk-client/package.json index 99e05e23ce..4441708c0e 100644 --- a/sdk/sdk-client/package.json +++ b/sdk/sdk-client/package.json @@ -17,12 +17,13 @@ "build": "tsc -b -v tsconfig.build.json", "lint": "eslint .", "lint:fix": "eslint . --fix", - "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist -p tsconfig.build.json", + "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js --external:crypto --external:tls --external:https --external:net --external:http --external:stream --external:zlib --external:assert --keep-names && tsc --emitDeclarationOnly --outDir bundle/dist -p tsconfig.build.json", "publish:npm": "pnpm bundle:npm && cd bundle && npm publish && cd .." }, "dependencies": { "@summerfi/sdk-common": "workspace:*", "@summerfi/sdk-server": "workspace:*", + "@summerfi/protocol-plugins": "workspace:*", "@trpc/client": "11.0.0-next-beta.264", "superjson": "^1.13.3" }, diff --git a/sdk/sdk-common/bundle/package.json b/sdk/sdk-common/bundle/package.json index 29836569cf..cf487b1872 100644 --- a/sdk/sdk-common/bundle/package.json +++ b/sdk/sdk-common/bundle/package.json @@ -2,7 +2,7 @@ "name": "summerfi-sdk-common", "version": "0.0.9", "module": "dist/index.js", - "types": "dist/src/index.d.ts", + "types": "dist/index.d.ts", "packageManager": "yarn@1.22.21", "scripts": {}, "dependencies": { diff --git a/sdk/sdk-common/package.json b/sdk/sdk-common/package.json index b96797334a..8cb14d6b65 100644 --- a/sdk/sdk-common/package.json +++ b/sdk/sdk-common/package.json @@ -22,7 +22,7 @@ "lint": "eslint .", "lint:fix": "eslint . --fix", "check-circular": "madge --circular --extensions ts ./src", - "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js && tsc --emitDeclarationOnly --outDir bundle/dist -p tsconfig.build.json", + "bundle:npm": "esbuild src/index.ts --platform=neutral --target=es2020 --conditions= --main-fields=main --bundle --sourcemap --outfile=bundle/dist/index.js --keep-names && tsc --emitDeclarationOnly --outDir bundle/dist -p tsconfig.build.json", "publish:npm": "pnpm bundle:npm && cd bundle && npm publish && cd .." }, "dependencies": { From 3ae18b6cd89c2c339dd2c90c120cb7d4f632b7b7 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:06:42 +0200 Subject: [PATCH 18/26] Added services to npm package --- sdk/sdk-common/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/sdk-common/src/index.ts b/sdk/sdk-common/src/index.ts index 2a7f2afc40..05d583ab2e 100644 --- a/sdk/sdk-common/src/index.ts +++ b/sdk/sdk-common/src/index.ts @@ -5,3 +5,4 @@ export * from './protocols' export * from './simulation' export * from './user' export * from './utils' +export * from './services' From 9b757bfccc1b3efec76a86dd001291fa268df4ef Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:12:19 +0200 Subject: [PATCH 19/26] fixed tests config --- sdk/sdk-client/tsconfig.test.json | 1 + sdk/sdk-e2e/tsconfig.test.json | 1 + 2 files changed, 2 insertions(+) diff --git a/sdk/sdk-client/tsconfig.test.json b/sdk/sdk-client/tsconfig.test.json index 740ffdc0d0..7ff6ef2cf9 100644 --- a/sdk/sdk-client/tsconfig.test.json +++ b/sdk/sdk-client/tsconfig.test.json @@ -7,6 +7,7 @@ "@summerfi/sdk-common/*": ["node_modules/@summerfi/sdk-common/src/*"], "@summerfi/sdk-server/*": ["node_modules/@summerfi/sdk-server/src/*"], "@summerfi/protocol-plugins/*": ["../protocol-plugins/src/*"], + "@summerfi/protocol-plugins": ["../protocol-plugins/src/index.ts"], "@summerfi/protocol-plugins-common": ["../protocol-plugins-common/src/index.ts"] } }, diff --git a/sdk/sdk-e2e/tsconfig.test.json b/sdk/sdk-e2e/tsconfig.test.json index 28abf8bca3..3666f79777 100644 --- a/sdk/sdk-e2e/tsconfig.test.json +++ b/sdk/sdk-e2e/tsconfig.test.json @@ -10,6 +10,7 @@ "node_modules/@summerfi/protocol-manager-service/src/index.ts" ], "@summerfi/protocol-plugins/*": ["node_modules/@summerfi/protocol-plugins/src/*"], + "@summerfi/protocol-plugins": ["node_modules/@summerfi/protocol-plugins/src/index.ts"], "@summerfi/protocol-plugins-common": [ "node_modules/@summerfi/protocol-plugins-common/src/index.ts" ], From f24a0a9a8b5cab11cbc5c48b9c1432fc1088cdf1 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:31:44 +0200 Subject: [PATCH 20/26] lock file --- pnpm-lock.yaml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0645b01ef5..bf801f0ef5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -860,8 +860,38 @@ importers: specifier: workspace:* version: link:../../packages/typescript-config + sdk/protocol-plugins/bundle: + dependencies: + '@summerfi/deployment-types': + specifier: workspace:* + version: link:../../../packages/deployment-types + '@summerfi/deployment-utils': + specifier: workspace:* + version: link:../../../packages/deployment-utils + '@summerfi/protocol-plugins-common': + specifier: workspace:* + version: link:../../protocol-plugins-common + '@summerfi/sdk-common': + specifier: workspace:* + version: link:../../sdk-common + '@summerfi/swap-common': + specifier: workspace:* + version: link:../../swap-common + '@summerfi/testing-utils': + specifier: workspace:* + version: link:../../testing-utils + bignumber.js: + specifier: 9.0.1 + version: 9.0.1 + zod: + specifier: ^3.22.4 + version: 3.22.4 + sdk/sdk-client: dependencies: + '@summerfi/protocol-plugins': + specifier: workspace:* + version: link:../protocol-plugins '@summerfi/sdk-common': specifier: workspace:* version: link:../sdk-common From d5f55dcb5aa31dce7336308fcf592c150809b2cd Mon Sep 17 00:00:00 2001 From: Cristian Dascalu Date: Fri, 5 Apr 2024 14:17:49 +0200 Subject: [PATCH 21/26] Publish packages to npmjs (#155) gh actions to publish sdk-client and sdk-common to npmjs --- .github/workflows/publish-client-package.yaml | 38 ++++++++++ .github/workflows/publish-common-package.yaml | 38 ++++++++++ .../workflows/publish-packages-manually.yaml | 72 +++++++++++++++++++ .github/workflows/publish-packages.yaml | 56 +++++++++++++++ 4 files changed, 204 insertions(+) create mode 100644 .github/workflows/publish-client-package.yaml create mode 100644 .github/workflows/publish-common-package.yaml create mode 100644 .github/workflows/publish-packages-manually.yaml create mode 100644 .github/workflows/publish-packages.yaml diff --git a/.github/workflows/publish-client-package.yaml b/.github/workflows/publish-client-package.yaml new file mode 100644 index 0000000000..b59df3458f --- /dev/null +++ b/.github/workflows/publish-client-package.yaml @@ -0,0 +1,38 @@ +name: Publish Client Package + +on: + workflow_call: + inputs: + npm_access_token: + required: true + type: secret + version_changed: + required: true + type: boolean + +jobs: + publish-client-package: + if: ${{ inputs.version_changed }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '20' + registry-url: 'https://registry.npmjs.org' + corepack: true + + - name: Install pnpm + run: corepack enable && corepack prepare pnpm@latest --activate + + - name: Install dependencies with pnpm + run: pnpm i + + - name: Build with pnpm + run: pnpm build + + - name: Publish package with pnpm + run: pnpm publish:npm + working-directory: sdk/sdk-client + env: + NPM_ACCESS_TOKEN: ${{ inputs.npm_access_token }} diff --git a/.github/workflows/publish-common-package.yaml b/.github/workflows/publish-common-package.yaml new file mode 100644 index 0000000000..3d546e84ef --- /dev/null +++ b/.github/workflows/publish-common-package.yaml @@ -0,0 +1,38 @@ +name: Publish Common Package + +on: + workflow_call: + inputs: + npm_access_token: + required: true + type: secret + version_changed: + required: true + type: boolean + +jobs: + publish-common-package: + if: ${{ inputs.version_changed }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '20' + registry-url: 'https://registry.npmjs.org' + corepack: true + + - name: Install pnpm + run: corepack enable && corepack prepare pnpm@latest --activate + + - name: Install dependencies with pnpm + run: pnpm i + + - name: Build with pnpm + run: pnpm build + + - name: Publish package with pnpm + run: pnpm publish:npm + working-directory: sdk/sdk-common + env: + NPM_ACCESS_TOKEN: ${{ inputs.npm_access_token }} diff --git a/.github/workflows/publish-packages-manually.yaml b/.github/workflows/publish-packages-manually.yaml new file mode 100644 index 0000000000..4741c59835 --- /dev/null +++ b/.github/workflows/publish-packages-manually.yaml @@ -0,0 +1,72 @@ +name: Publish Packages + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} + cancel-in-progress: false + +on: + workflow_dispatch: + inputs: + branch: + description: 'Branch name' + required: true + type: choice + options: + - 'main' + - 'dev' + publish_option: + description: 'Select package(s) to publish' + required: true + type: choice + options: + - 'common' + - 'client' + - 'all' + +jobs: + prepare: + name: Prepare + runs-on: ubuntu-latest + outputs: + common_changed: ${{ steps.common-version-check.outputs.changed }} + client_changed: ${{ steps.client-version-check.outputs.changed }} + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 2 + ref: ${{ github.event.inputs.branch || github.ref_name }} + - id: common-version-check + run: | + if git diff HEAD^ HEAD -- sdk/sdk-common/bundle/package.json | grep '"version":'; then + echo "::set-output name=changed::true" + else + echo "::set-output name=changed::false" + fi + - id: client-version-check + run: | + if git diff HEAD^ HEAD -- sdk/sdk-client/bundle/package.json | grep '"version":'; then + echo "::set-output name=changed::true" + else + echo "::set-output name=changed::false" + fi + + publish-common: + needs: prepare + if: >- + (needs.prepare.outputs.common_changed == 'true') && + ((github.event.inputs.publish_option == 'common') || (github.event.inputs.publish_option == 'all')) + uses: ./.github/workflows/publish-common-package.yaml + with: + npm_access_token: ${{ secrets.NPM_ACCESS_TOKEN }} + version_changed: ${{ needs.prepare.outputs.common_changed }} + + publish-client: + needs: prepare + if: >- + (needs.prepare.outputs.client_changed == 'true') && + ((github.event.inputs.publish_option == 'client') || (github.event.inputs.publish_option == 'all')) + uses: ./.github/workflows/publish-client-package.yaml + with: + npm_access_token: ${{ secrets.NPM_ACCESS_TOKEN }} + version_changed: ${{ needs.prepare.outputs.client_changed }} diff --git a/.github/workflows/publish-packages.yaml b/.github/workflows/publish-packages.yaml new file mode 100644 index 0000000000..12b33c92f9 --- /dev/null +++ b/.github/workflows/publish-packages.yaml @@ -0,0 +1,56 @@ +name: Publish Packages + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} + cancel-in-progress: false + +on: + push: + branches: + - main + paths: + - 'sdk/sdk-common/bundle/package.json' + - 'sdk/sdk-client/bundle/package.json' + +jobs: + prepare: + name: Prepare + runs-on: ubuntu-latest + outputs: + common_changed: ${{ steps.common-version-check.outputs.changed }} + client_changed: ${{ steps.client-version-check.outputs.changed }} + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 2 + - id: common-version-check + run: | + if git diff HEAD^ HEAD -- sdk/sdk-common/bundle/package.json | grep '"version":'; then + echo "::set-output name=changed::true" + else + echo "::set-output name=changed::false" + fi + - id: client-version-check + run: | + if git diff HEAD^ HEAD -- sdk/sdk-client/bundle/package.json | grep '"version":'; then + echo "::set-output name=changed::true" + else + echo "::set-output name=changed::false" + fi + + publish-common: + needs: prepare + if: ${{ needs.prepare.outputs.common_changed == 'true' }} + uses: ./.github/workflows/publish-common-package.yaml + with: + npm_access_token: ${{ secrets.NPM_ACCESS_TOKEN }} + version_changed: ${{ needs.prepare.outputs.common_changed }} + + publish-client: + needs: prepare + if: ${{ needs.prepare.outputs.client_changed == 'true' }} + uses: ./.github/workflows/publish-client-package.yaml + with: + npm_access_token: ${{ secrets.NPM_ACCESS_TOKEN }} + version_changed: ${{ needs.prepare.outputs.client_changed }} From 66d8be230b2da6359f6dbe744579281e4ed8decf Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Mon, 15 Apr 2024 16:11:26 +0200 Subject: [PATCH 22/26] Fixed envs --- stacks/sdk.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/stacks/sdk.ts b/stacks/sdk.ts index 124e2ccbdc..dbe1100702 100644 --- a/stacks/sdk.ts +++ b/stacks/sdk.ts @@ -8,6 +8,9 @@ const { ONE_INCH_API_URL, ONE_INCH_ALLOWED_SWAP_PROTOCOLS, ONE_INCH_SWAP_CHAIN_IDS, + ONE_INCH_API_SPOT_URL, + ONE_INCH_API_SPOT_VERSION, + ONE_INCH_API_SPOT_KEY, } = process.env export function addSdkConfig({ stack, api }: StackContext & { api: Api }) { @@ -16,7 +19,10 @@ export function addSdkConfig({ stack, api }: StackContext & { api: Api }) { !ONE_INCH_API_VERSION || !ONE_INCH_API_URL || !ONE_INCH_ALLOWED_SWAP_PROTOCOLS || - !ONE_INCH_SWAP_CHAIN_IDS + !ONE_INCH_SWAP_CHAIN_IDS || + !ONE_INCH_API_SPOT_URL || + !ONE_INCH_API_SPOT_VERSION || + !ONE_INCH_API_SPOT_KEY ) { throw new Error( 'OneInch configuration env variables are missing: ' + @@ -27,7 +33,12 @@ export function addSdkConfig({ stack, api }: StackContext & { api: Api }) { ONE_INCH_API_URL, ONE_INCH_ALLOWED_SWAP_PROTOCOLS, ONE_INCH_SWAP_CHAIN_IDS, + ONE_INCH_API_SPOT_URL, + ONE_INCH_API_SPOT_VERSION, + ONE_INCH_API_SPOT_KEY, }), + null, + 2, ), ) } @@ -42,6 +53,9 @@ export function addSdkConfig({ stack, api }: StackContext & { api: Api }) { ONE_INCH_API_URL, ONE_INCH_ALLOWED_SWAP_PROTOCOLS, ONE_INCH_SWAP_CHAIN_IDS, + ONE_INCH_API_SPOT_URL, + ONE_INCH_API_SPOT_VERSION, + ONE_INCH_API_SPOT_KEY, }, }) From 74301bc2c893b9c9c45cf7c702139f7cc7bd68e9 Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Mon, 15 Apr 2024 18:14:43 +0200 Subject: [PATCH 23/26] Added logging --- sdk/sdk-server/src/TRPC.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sdk/sdk-server/src/TRPC.ts b/sdk/sdk-server/src/TRPC.ts index 7cde017529..d7341be5e0 100644 --- a/sdk/sdk-server/src/TRPC.ts +++ b/sdk/sdk-server/src/TRPC.ts @@ -10,4 +10,12 @@ export const router = t.router export const createCallerFactory = t.createCallerFactory -export const publicProcedure = t.procedure +export const publicProcedure = t.procedure.use(async (opts) => { + const { getRawInput, path, type } = opts + console.log('- path => ', path) + console.log('- type => ', type) + console.log('- rawInput => ', JSON.stringify(await getRawInput(), null, 2)) + + const result = await opts.next() + return result +}) From 6fa74ad653e759ce7b321818ba1d741d8778353f Mon Sep 17 00:00:00 2001 From: Roberto Cano <3525807+robercano@users.noreply.github.com> Date: Tue, 16 Apr 2024 10:49:27 +0200 Subject: [PATCH 24/26] fix: same token comparison --- sdk/sdk-common/src/common/utils/TokenUtils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/sdk-common/src/common/utils/TokenUtils.ts b/sdk/sdk-common/src/common/utils/TokenUtils.ts index bafd4d8e20..fa1c12c699 100644 --- a/sdk/sdk-common/src/common/utils/TokenUtils.ts +++ b/sdk/sdk-common/src/common/utils/TokenUtils.ts @@ -1,5 +1,8 @@ import { IToken } from '../interfaces/IToken' export function isSameTokens(a: IToken, b: IToken): boolean { - return a.address === b.address && a.chainInfo.chainId === b.chainInfo.chainId + return ( + a.address.value.toLowerCase() === b.address.value.toLowerCase() && + a.chainInfo.chainId === b.chainInfo.chainId + ) } From 28838a931c11694f1fbc90eb0586245f4a9ee12b Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:07:42 +0200 Subject: [PATCH 25/26] Added logging env variable --- sdk/.env.template | 4 +++- sdk/sdk-server/src/TRPC.ts | 8 +++++--- stacks/sdk.ts | 2 ++ turbo.json | 1 + 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/sdk/.env.template b/sdk/.env.template index 85fd8cf22c..e2bea3f9b6 100644 --- a/sdk/.env.template +++ b/sdk/.env.template @@ -11,4 +11,6 @@ ONE_INCH_API_URL='https://api-oasis.1inch.io' ONE_INCH_ALLOWED_SWAP_PROTOCOLS='UNISWAP_V3,PMM1,PMM2,PMM3,PMM4,UNISWAP_V2,SUSHI,CURVE,CURVE_V2,PSM,WSTETH,BALANCER,BALANCER_V2,BALANCER_V2_WRAPPER,ST_ETH,WETH,ROCKET_POOL' # ChainIDs where 1Inch swaps are allowed (comma separated) -ONE_INCH_SWAP_CHAIN_IDS='1' \ No newline at end of file +ONE_INCH_SWAP_CHAIN_IDS='1' + +SDK_LOGGING_ENABLED= diff --git a/sdk/sdk-server/src/TRPC.ts b/sdk/sdk-server/src/TRPC.ts index d7341be5e0..dc5ef7711f 100644 --- a/sdk/sdk-server/src/TRPC.ts +++ b/sdk/sdk-server/src/TRPC.ts @@ -12,9 +12,11 @@ export const createCallerFactory = t.createCallerFactory export const publicProcedure = t.procedure.use(async (opts) => { const { getRawInput, path, type } = opts - console.log('- path => ', path) - console.log('- type => ', type) - console.log('- rawInput => ', JSON.stringify(await getRawInput(), null, 2)) + if (process.env.SDK_LOGGING_ENABLED) { + console.log('- path => ', path) + console.log('- type => ', type) + console.log('- rawInput => ', JSON.stringify(await getRawInput(), null, 2)) + } const result = await opts.next() return result diff --git a/stacks/sdk.ts b/stacks/sdk.ts index dbe1100702..c5c9a12c88 100644 --- a/stacks/sdk.ts +++ b/stacks/sdk.ts @@ -11,6 +11,7 @@ const { ONE_INCH_API_SPOT_URL, ONE_INCH_API_SPOT_VERSION, ONE_INCH_API_SPOT_KEY, + SDK_LOGGING_ENABLED, } = process.env export function addSdkConfig({ stack, api }: StackContext & { api: Api }) { @@ -56,6 +57,7 @@ export function addSdkConfig({ stack, api }: StackContext & { api: Api }) { ONE_INCH_API_SPOT_URL, ONE_INCH_API_SPOT_VERSION, ONE_INCH_API_SPOT_KEY, + SDK_LOGGING_ENABLED: SDK_LOGGING_ENABLED!, }, }) diff --git a/turbo.json b/turbo.json index eaff06d6a7..c836c08a69 100644 --- a/turbo.json +++ b/turbo.json @@ -30,6 +30,7 @@ "ONE_INCH_ALLOWED_SWAP_PROTOCOLS", "ONE_INCH_SWAP_CHAIN_IDS", "SDK_API_URL", + "SDK_LOGGING_ENABLED", "MAINNET_RPC_URL" ], "pipeline": { From 6c2558ad38d383683a1c538f9e460ee2653f999c Mon Sep 17 00:00:00 2001 From: Piotr Witek <739075+piotrwitek@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:13:55 +0200 Subject: [PATCH 26/26] updated lock --- pnpm-lock.yaml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c2fcfe235a..23da170bb7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -931,7 +931,7 @@ importers: version: 1.13.3 viem: specifier: ^2.7.9 - version: 2.9.3(typescript@5.3.3) + version: 2.9.19(typescript@5.4.5)(zod@3.22.4) sdk/sdk-common: dependencies: @@ -965,7 +965,7 @@ importers: version: 1.13.3 viem: specifier: ^2.2.0 - version: 2.9.3(typescript@5.3.3) + version: 2.9.19(typescript@5.4.5)(zod@3.22.4) sdk/sdk-e2e: devDependencies: @@ -6155,7 +6155,6 @@ packages: /@noble/hashes@1.3.3: resolution: {integrity: sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==} engines: {node: '>= 16'} - dev: true /@noble/hashes@1.4.0: resolution: {integrity: sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==} @@ -6599,7 +6598,7 @@ packages: resolution: {integrity: sha512-N1ZhksgwD3OBlwTv3R6KFEcPojl/W4ElJOeCZdi+vuI5QmTFwLq3OFf2zd2ROpKvxFdgZ6hUpb0dx9bVNEwYCA==} dependencies: '@noble/curves': 1.2.0 - '@noble/hashes': 1.3.2 + '@noble/hashes': 1.3.3 '@scure/base': 1.1.6 /@scure/bip32@1.3.3: @@ -6620,7 +6619,7 @@ packages: /@scure/bip39@1.2.1: resolution: {integrity: sha512-Z3/Fsz1yr904dduJD0NpiyRHhRYHdcnyh73FZWiV+/qhWi83wNJ3NWolYqCEN+ZWsUz2TWwajJggcRE9r1zUYg==} dependencies: - '@noble/hashes': 1.3.2 + '@noble/hashes': 1.3.3 '@scure/base': 1.1.6 /@scure/bip39@1.2.2: