From 12827727f625f82e23c156e24e2ef69fa8e4a4b1 Mon Sep 17 00:00:00 2001 From: Bob Lu Date: Sun, 10 Dec 2023 22:24:55 +0800 Subject: [PATCH] fix: rename swaper to swapper, add base token to portfolio, fix typo --- packages/lending/src/adapter.test.ts | 4 +- packages/lending/src/adapter.ts | 154 +++++++++--------- packages/lending/src/protocol.portfolio.ts | 11 +- .../protocols/compound-v3/lending-protocol.ts | 2 +- .../src/protocols/openocean-v2/configs.ts | 4 + .../src/protocols/openocean-v2/index.ts | 3 +- .../{lending-swaper.ts => lending-swapper.ts} | 12 +- .../src/protocols/paraswap-v5/configs.ts | 4 + .../src/protocols/paraswap-v5/index.ts | 2 +- .../{lending-swaper.ts => lending-swapper.ts} | 12 +- .../lending/src/{swaper.ts => swapper.ts} | 10 +- .../src/{swaper.type.ts => swapper.type.ts} | 0 packages/lending/test/hooks.ts | 4 +- .../test/transactions/collateral-swap.ts | 2 +- 14 files changed, 115 insertions(+), 109 deletions(-) rename packages/lending/src/protocols/openocean-v2/{lending-swaper.ts => lending-swapper.ts} (82%) rename packages/lending/src/protocols/paraswap-v5/{lending-swaper.ts => lending-swapper.ts} (80%) rename packages/lending/src/{swaper.ts => swapper.ts} (73%) rename packages/lending/src/{swaper.type.ts => swapper.type.ts} (100%) diff --git a/packages/lending/src/adapter.test.ts b/packages/lending/src/adapter.test.ts index 510b6a78..2fb83465 100644 --- a/packages/lending/src/adapter.test.ts +++ b/packages/lending/src/adapter.test.ts @@ -3,7 +3,7 @@ import { Adapter } from './adapter'; import { BaseParams } from './adapter.type'; import { LendingProtocol } from './protocols/aave-v3/lending-protocol'; -import { LendingSwaper } from './protocols/paraswap-v5/lending-swaper'; +import { LendingSwapper } from './protocols/paraswap-v5/lending-swapper'; import { Portfolio } from './protocol.portfolio'; import { expect } from 'chai'; import { mainnetTokens } from '@protocolink/test-helpers'; @@ -87,7 +87,7 @@ const zapRepayTestCases: BaseParams[] = [ describe('Lending AaveV3 SDK', function () { Adapter.registerProtocol(LendingProtocol); - Adapter.registerSwaper(LendingSwaper); + Adapter.registerSwapper(LendingSwapper); const adapter = new Adapter(1, provider); const protocolId = 'aave-v3'; diff --git a/packages/lending/src/adapter.ts b/packages/lending/src/adapter.ts index 7c17a692..d83ca8b3 100644 --- a/packages/lending/src/adapter.ts +++ b/packages/lending/src/adapter.ts @@ -1,13 +1,12 @@ import { BaseFields, BaseParams } from './adapter.type'; import { Portfolio } from './protocol.portfolio'; import { Protocol, ProtocolClass } from './protocol'; -import { Swaper, SwaperClass } from './swaper'; +import { Swapper, SwapperClass } from './swapper'; import * as apisdk from '@protocolink/api'; import * as common from '@protocolink/common'; import { defaultInterestRateMode, defaultSlippage } from './protocol.type'; import flatten from 'lodash/flatten'; import { providers } from 'ethers'; -import { wrapToken } from './helper'; type Options = { permitType: apisdk.Permit2Type | undefined; @@ -20,16 +19,16 @@ export class Adapter extends common.Web3Toolkit { this.Protocols.push(protocol); } - static Swapers: SwaperClass[] = []; + static Swappers: SwapperClass[] = []; - static registerSwaper(swaper: SwaperClass) { - this.Swapers.push(swaper); + static registerSwapper(swapper: SwapperClass) { + this.Swappers.push(swapper); } permitType: apisdk.Permit2Type | undefined = 'permit'; apiKey: string | undefined; protocolMap: Record = {}; - swapers: Swaper[] = []; + swappers: Swapper[] = []; constructor( chainId: number, @@ -46,9 +45,9 @@ export class Adapter extends common.Web3Toolkit { this.protocolMap[protocol.id] = protocol; } } - for (const Swaper of Adapter.Swapers) { - if (Swaper.isSupported(this.chainId)) { - this.swapers.push(new Swaper(chainId, provider)); + for (const Swapper of Adapter.Swappers) { + if (Swapper.isSupported(this.chainId)) { + this.swappers.push(new Swapper(chainId, provider)); } } } @@ -57,34 +56,34 @@ export class Adapter extends common.Web3Toolkit { return Object.keys(this.protocolMap); } - findSwaper(tokenOrTokens: common.Token | common.Token[]) { - const canCustomTokenSwappers: Swaper[] = []; - const tokensSupportedSwappers: Swaper[] = []; - let bestSwaper: Swaper | undefined; - for (const swaper of this.swapers) { - if (swaper.canCustomToken) { - canCustomTokenSwappers.push(swaper); + findSwapper(tokenOrTokens: common.Token | common.Token[]) { + const canCustomTokenSwappers: Swapper[] = []; + const tokensSupportedSwappers: Swapper[] = []; + let bestSwapper: Swapper | undefined; + for (const swapper of this.swappers) { + if (swapper.canCustomToken) { + canCustomTokenSwappers.push(swapper); } const isNotSupported = Array.isArray(tokenOrTokens) - ? tokenOrTokens.some((token) => !swaper.isSupportedToken(token)) - : !swaper.isSupportedToken(tokenOrTokens); + ? tokenOrTokens.some((token) => !swapper.isSupportedToken(token)) + : !swapper.isSupportedToken(tokenOrTokens); if (isNotSupported) { continue; } - tokensSupportedSwappers.push(swaper); + tokensSupportedSwappers.push(swapper); - bestSwaper = swaper; + bestSwapper = swapper; break; } - if (!bestSwaper) { - bestSwaper = tokensSupportedSwappers[0] ?? canCustomTokenSwappers[0]; + if (!bestSwapper) { + bestSwapper = tokensSupportedSwappers[0] ?? canCustomTokenSwappers[0]; } - if (!bestSwaper) { - bestSwaper = this.swapers[0]; + if (!bestSwapper) { + bestSwapper = this.swappers[0]; } - return bestSwaper; + return bestSwapper; } async getPortfolios(account: string): Promise { @@ -120,14 +119,19 @@ export class Adapter extends common.Web3Toolkit { const { srcAmount } = params; const srcToken = common.classifying(params.srcToken); const destToken = common.classifying(params.destToken); - const wrappedSrcToken = wrapToken(this.chainId, srcToken); - const wrappedDestToken = wrapToken(this.chainId, destToken); + const wrappedSrcToken = srcToken.wrapped; + const wrappedDestToken = destToken.wrapped; + const collateralSwapLogics: apisdk.Logic[] = []; const protocol = this.getProtocol(protocolId); portfolio = portfolio || (await protocol.getPortfolio(account, marketId)); const afterPortfolio = portfolio.clone(); + // compound v3 base token supplied not support collateral swap + if (protocolId === 'compound-v3' && portfolio?.baseToken?.is(srcToken.unwrapped)) + throw new Error('Compound V3 does not support the base token for performing collateral swaps'); + // ---------- flashloan ---------- const flashLoanAggregatorQuotation = await apisdk.protocols.utility.getFlashLoanAggregatorQuotation(this.chainId, { repays: [{ token: wrappedSrcToken, amount: srcAmount }], @@ -143,14 +147,14 @@ export class Adapter extends common.Web3Toolkit { collateralSwapLogics.push(flashLoanLoanLogic); // ---------- swap ---------- - const swaper = this.findSwaper([wrappedSrcToken, wrappedDestToken]); - const swapQuotation = await swaper.quote({ + const swapper = this.findSwapper([wrappedSrcToken, wrappedDestToken]); + const swapQuotation = await swapper.quote({ input: flashLoanTokenAmount, tokenOut: wrappedDestToken, slippage: defaultSlippage, }); - const swapTokenLogic = swaper.newSwapTokenLogic(swapQuotation); + const swapTokenLogic = swapper.newSwapTokenLogic(swapQuotation); collateralSwapLogics.push(swapTokenLogic); // ---------- supply ---------- @@ -160,12 +164,8 @@ export class Adapter extends common.Web3Toolkit { afterPortfolio.supply(swapQuotation.output.token, swapQuotation.output.amount); - // compound v3 base token supplied not support collateral swap - if (protocolId === 'compoundv3' && supplyLogic.fields.output) - throw new Error('Compound V3 does not support the base token for performing collateral swaps'); - // if not compound v3, compound v3 does not need return and add funds in collateral swap flow - if (protocolId !== 'compoundv3') { + if (protocolId !== 'compound-v3') { if (!supplyLogic.fields.output) throw new Error('incorrect supply result'); // ---------- return funds ---------- const returnLogic = apisdk.protocols.utility.newSendTokenLogic({ @@ -243,8 +243,8 @@ export class Adapter extends common.Web3Toolkit { const { srcAmount } = params; const srcToken = common.classifying(params.srcToken); const destToken = common.classifying(params.destToken); - const wrappedSrcToken = wrapToken(this.chainId, srcToken); - const wrappedDestToken = wrapToken(this.chainId, destToken); + const wrappedSrcToken = srcToken.wrapped; + const wrappedDestToken = destToken.wrapped; const debtSwapLogics: apisdk.Logic[] = []; const protocol = this.getProtocol(protocolId); @@ -253,15 +253,15 @@ export class Adapter extends common.Web3Toolkit { const afterPortfolio = portfolio.clone(); // ---------- Pre-calc quotation ---------- - const swaper = this.findSwaper([wrappedSrcToken, wrappedDestToken]); + const swapper = this.findSwapper([wrappedSrcToken, wrappedDestToken]); // get the quotation for how much dest token is needed to exchange for the src amount - let swapQuotation = await swaper.quote({ + let swapQuotation = await swapper.quote({ tokenIn: wrappedDestToken, output: { token: wrappedSrcToken, amount: srcAmount }, slippage: defaultSlippage, }); // convert swap type to exact in - swapQuotation = await swaper.quote({ input: swapQuotation.input, tokenOut: srcToken, slippage: defaultSlippage }); + swapQuotation = await swapper.quote({ input: swapQuotation.input, tokenOut: srcToken, slippage: defaultSlippage }); // ---------- flashloan ---------- const flashLoanAggregatorQuotation = await apisdk.protocols.utility.getFlashLoanAggregatorQuotation(this.chainId, { @@ -274,7 +274,7 @@ export class Adapter extends common.Web3Toolkit { debtSwapLogics.push(flashLoanLoanLogic); // ---------- swap ---------- - const swapTokenLogic = swaper.newSwapTokenLogic(swapQuotation); + const swapTokenLogic = swapper.newSwapTokenLogic(swapQuotation); debtSwapLogics.push(swapTokenLogic); // ---------- repay ---------- @@ -358,8 +358,8 @@ export class Adapter extends common.Web3Toolkit { const { srcAmount } = params; const srcToken = common.classifying(params.srcToken); const destToken = common.classifying(params.destToken); - const wrappedSrcToken = wrapToken(this.chainId, srcToken); - const wrappedDestToken = wrapToken(this.chainId, destToken); + const wrappedSrcToken = srcToken.wrapped; + const wrappedDestToken = destToken.wrapped; const leverageLonglogics: apisdk.Logic[] = []; const protocol = this.getProtocol(protocolId); @@ -368,15 +368,15 @@ export class Adapter extends common.Web3Toolkit { const afterPortfolio = portfolio.clone(); // ---------- Pre-calc quotation ---------- - const swaper = this.findSwaper([wrappedDestToken, wrappedSrcToken]); + const swapper = this.findSwapper([wrappedDestToken, wrappedSrcToken]); // retrieve the amount needed to borrow based on the collateral token and amount - let swapQuotation = await swaper.quote({ + let swapQuotation = await swapper.quote({ tokenIn: wrappedDestToken, output: { token: wrappedSrcToken, amount: srcAmount }, slippage: defaultSlippage, }); // convert swap type to exact in - swapQuotation = await swaper.quote({ + swapQuotation = await swapper.quote({ input: swapQuotation.input, tokenOut: wrappedSrcToken, slippage: defaultSlippage, @@ -395,7 +395,7 @@ export class Adapter extends common.Web3Toolkit { leverageLonglogics.push(flashLoanLoanLogic); // ---------- swap ---------- - const swapTokenLogic = swaper.newSwapTokenLogic(swapQuotation); + const swapTokenLogic = swapper.newSwapTokenLogic(swapQuotation); leverageLonglogics.push(swapTokenLogic); // ---------- supply ---------- @@ -473,8 +473,9 @@ export class Adapter extends common.Web3Toolkit { const { srcAmount } = params; const srcToken = common.classifying(params.srcToken); const destToken = common.classifying(params.destToken); - const wrappedSrcToken = wrapToken(this.chainId, srcToken); - const wrappedDestToken = wrapToken(this.chainId, destToken); + const wrappedSrcToken = srcToken.wrapped; + const wrappedDestToken = destToken.wrapped; + const leverageShortlogics: apisdk.Logic[] = []; const protocol = this.getProtocol(protocolId); @@ -495,13 +496,13 @@ export class Adapter extends common.Web3Toolkit { leverageShortlogics.push(flashLoanLoanLogic); // ---------- swap ---------- - const swaper = this.findSwaper([wrappedSrcToken, wrappedDestToken]); - const swapQuotation = await swaper.quote({ + const swapper = this.findSwapper([wrappedSrcToken, wrappedDestToken]); + const swapQuotation = await swapper.quote({ input: flashLoanTokenAmount, tokenOut: wrappedDestToken, slippage: defaultSlippage, }); - const swapTokenLogic = swaper.newSwapTokenLogic(swapQuotation); + const swapTokenLogic = swapper.newSwapTokenLogic(swapQuotation); leverageShortlogics.push(swapTokenLogic); // ---------- supply ---------- @@ -580,8 +581,9 @@ export class Adapter extends common.Web3Toolkit { const { srcAmount } = params; const srcToken = common.classifying(params.srcToken); const destToken = common.classifying(params.destToken); - const wrappedSrcToken = wrapToken(this.chainId, srcToken); - const wrappedDestToken = wrapToken(this.chainId, destToken); + const wrappedSrcToken = srcToken.wrapped; + const wrappedDestToken = destToken.wrapped; + const deleveragelogics: apisdk.Logic[] = []; const protocol = this.getProtocol(protocolId); @@ -589,15 +591,15 @@ export class Adapter extends common.Web3Toolkit { const afterPortfolio = portfolio.clone(); // ---------- Pre-calc quotation ---------- - const swaper = this.findSwaper([wrappedDestToken, wrappedSrcToken]); + const swapper = this.findSwapper([wrappedDestToken, wrappedSrcToken]); // get the quotation for how much collateral token is needed to exchange for the repay amount - let swapQuotation = await swaper.quote({ + let swapQuotation = await swapper.quote({ tokenIn: wrappedDestToken, output: { token: wrappedSrcToken, amount: srcAmount }, slippage: defaultSlippage, }); // convert swap type to exact in - swapQuotation = await swaper.quote({ + swapQuotation = await swapper.quote({ input: swapQuotation.input, tokenOut: wrappedDestToken, slippage: defaultSlippage, @@ -615,7 +617,7 @@ export class Adapter extends common.Web3Toolkit { deleveragelogics.push(flashLoanLoanLogic); // ---------- swap ---------- - const swapTokenLogic = swaper.newSwapTokenLogic(swapQuotation); + const swapTokenLogic = swapper.newSwapTokenLogic(swapQuotation); deleveragelogics.push(swapTokenLogic); // ---------- repay ---------- @@ -629,7 +631,7 @@ export class Adapter extends common.Web3Toolkit { afterPortfolio.repay(swapQuotation.output.token, swapQuotation.output.amount); // ---------- add funds ---------- - if (protocolId !== 'compoundv3') { + if (protocolId !== 'compound-v3') { const addLogic = apisdk.protocols.permit2.newPullTokenLogic({ input: swapQuotation.input, }); @@ -687,6 +689,7 @@ export class Adapter extends common.Web3Toolkit { const { srcAmount } = params; const srcToken = common.classifying(params.srcToken); const destToken = common.classifying(params.destToken); + const zapSupplylogics: apisdk.Logic[] = []; const protocol = this.getProtocol(protocolId); @@ -698,13 +701,13 @@ export class Adapter extends common.Web3Toolkit { // ---------- swap ---------- if (!srcToken.wrapped.is(destToken.wrapped)) { - const swaper = this.findSwaper([srcToken, destToken]); - const swapQuotation = await swaper.quote({ + const swapper = this.findSwapper([srcToken, destToken]); + const swapQuotation = await swapper.quote({ input: { token: srcToken, amount: srcAmount }, tokenOut: destToken, slippage: defaultSlippage, }); - const swapTokenLogic = swaper.newSwapTokenLogic(swapQuotation); + const swapTokenLogic = swapper.newSwapTokenLogic(swapQuotation); supplyTokenAmount = swapQuotation.output; zapSupplylogics.push(swapTokenLogic); } @@ -758,6 +761,7 @@ export class Adapter extends common.Web3Toolkit { const { srcAmount } = params; const srcToken = common.classifying(params.srcToken); const destToken = common.classifying(params.destToken); + const zapWithdrawlogics: apisdk.Logic[] = []; const protocol = this.getProtocol(protocolId); @@ -777,15 +781,15 @@ export class Adapter extends common.Web3Toolkit { afterPortfolio.withdraw(srcToken, withdrawLogic.fields.output.amount); // ---------- swap ---------- - if (!srcToken.is(destToken)) { - const swaper = this.findSwaper([srcToken, destToken]); - const swapQuotation = await swaper.quote({ + if (!srcToken.unwrapped.is(destToken.unwrapped)) { + const swapper = this.findSwapper([srcToken, destToken]); + const swapQuotation = await swapper.quote({ input: withdrawLogic.fields.output, tokenOut: destToken, slippage: defaultSlippage, }); outputTokenAmount = swapQuotation.output; - const swapTokenLogic = swaper.newSwapTokenLogic(swapQuotation); + const swapTokenLogic = swapper.newSwapTokenLogic(swapQuotation); zapWithdrawlogics.push(swapTokenLogic); } @@ -829,6 +833,7 @@ export class Adapter extends common.Web3Toolkit { const { srcAmount } = params; const srcToken = common.classifying(params.srcToken); const destToken = common.classifying(params.destToken); + const zapBorrowlogics: apisdk.Logic[] = []; const protocol = this.getProtocol(protocolId); @@ -849,15 +854,15 @@ export class Adapter extends common.Web3Toolkit { afterPortfolio.borrow(srcToken, srcAmount); // ---------- swap ---------- - if (!srcToken.is(destToken)) { - const swaper = this.findSwaper([srcToken, destToken]); - const swapQuotation = await swaper.quote({ + if (!srcToken.unwrapped.is(destToken.unwrapped)) { + const swapper = this.findSwapper([srcToken, destToken]); + const swapQuotation = await swapper.quote({ input: { token: srcToken, amount: srcAmount }, tokenOut: destToken, slippage: defaultSlippage, }); outputTokenAmount = swapQuotation.output; - const swapTokenLogic = swaper.newSwapTokenLogic(swapQuotation); + const swapTokenLogic = swapper.newSwapTokenLogic(swapQuotation); zapBorrowlogics.push(swapTokenLogic); } // ---------- tx related ---------- @@ -901,6 +906,7 @@ export class Adapter extends common.Web3Toolkit { const { srcAmount } = params; const srcToken = common.classifying(params.srcToken); const destToken = common.classifying(params.destToken); + const zapRepaylogics: apisdk.Logic[] = []; const protocol = this.getProtocol(protocolId); @@ -911,15 +917,15 @@ export class Adapter extends common.Web3Toolkit { let repayTokenAmount = new common.TokenAmount(srcToken, srcAmount); // ---------- swap ---------- - if (!srcToken.wrapped.is(destToken.wrapped)) { - const swaper = this.findSwaper([srcToken, destToken]); - const swapQuotation = await swaper.quote({ + if (!srcToken.unwrapped.is(destToken.unwrapped)) { + const swapper = this.findSwapper([srcToken, destToken]); + const swapQuotation = await swapper.quote({ input: { token: srcToken, amount: srcAmount }, tokenOut: destToken, slippage: defaultSlippage, }); repayTokenAmount = swapQuotation.output; - const swapTokenLogic = swaper.newSwapTokenLogic(swapQuotation); + const swapTokenLogic = swapper.newSwapTokenLogic(swapQuotation); zapRepaylogics.push(swapTokenLogic); } // ---------- repay ---------- diff --git a/packages/lending/src/protocol.portfolio.ts b/packages/lending/src/protocol.portfolio.ts index 0f106aa9..83569e26 100644 --- a/packages/lending/src/protocol.portfolio.ts +++ b/packages/lending/src/protocol.portfolio.ts @@ -136,6 +136,7 @@ export class Portfolio { chainId: number; protocolId: string; marketId: string; + baseToken?: common.Token; supplies: Supply[] = []; supplyMap: { [key in string]?: Supply } = {}; borrows: Borrow[] = []; @@ -154,11 +155,13 @@ export class Portfolio { protocolId: string, marketId: string, supplies: SupplyObject[], - borrows: BorrowObject[] + borrows: BorrowObject[], + baseToken?: common.Token ) { this.chainId = chainId; this.protocolId = protocolId; this.marketId = marketId; + if (baseToken) this.baseToken = baseToken; for (let i = 0; i < supplies.length; i++) { const supply = new Supply(supplies[i]); @@ -271,13 +274,11 @@ export class Portfolio { } findSupply(token: common.Token) { - return this.supplyMap[token.address] ?? this.supplyMap[token.address]; - // return this.supplyMap[unwrapToken(this.chainId, token).address] ?? this.supplyMap[token.address]; + return this.supplyMap[token.unwrapped.address] ?? this.supplyMap[token.address]; } findBorrow(token: common.Token) { - return this.borrowMap[token.address] ?? this.borrowMap[token.address]; - // return this.borrowMap[unwrapToken(this.chainId, token).address] ?? this.borrowMap[token.address]; + return this.borrowMap[token.unwrapped.address] ?? this.borrowMap[token.address]; } calcLeverageTimes(leverageToken: common.Token, afterPortfolio: Portfolio) { diff --git a/packages/lending/src/protocols/compound-v3/lending-protocol.ts b/packages/lending/src/protocols/compound-v3/lending-protocol.ts index 53667d9b..2d992199 100644 --- a/packages/lending/src/protocols/compound-v3/lending-protocol.ts +++ b/packages/lending/src/protocols/compound-v3/lending-protocol.ts @@ -292,7 +292,7 @@ export class LendingProtocol extends Protocol { }, ]; - const portfolio = new Portfolio(this.chainId, this.id, marketId, supplies, borrows); + const portfolio = new Portfolio(this.chainId, this.id, marketId, supplies, borrows, baseToken); return portfolio; } diff --git a/packages/lending/src/protocols/openocean-v2/configs.ts b/packages/lending/src/protocols/openocean-v2/configs.ts index ac6f473a..54a68eef 100644 --- a/packages/lending/src/protocols/openocean-v2/configs.ts +++ b/packages/lending/src/protocols/openocean-v2/configs.ts @@ -1 +1,5 @@ +import * as logics from '@protocolink/logics'; + export const NAME = 'openocean-v2'; + +export const supportedChainIds = logics.openoceanv2.SwapTokenLogic.supportedChainIds; diff --git a/packages/lending/src/protocols/openocean-v2/index.ts b/packages/lending/src/protocols/openocean-v2/index.ts index 65e59cce..dfd1ad0c 100644 --- a/packages/lending/src/protocols/openocean-v2/index.ts +++ b/packages/lending/src/protocols/openocean-v2/index.ts @@ -1 +1,2 @@ -export * from './lending-swaper'; +export * from './configs'; +export * from './lending-swapper'; diff --git a/packages/lending/src/protocols/openocean-v2/lending-swaper.ts b/packages/lending/src/protocols/openocean-v2/lending-swapper.ts similarity index 82% rename from packages/lending/src/protocols/openocean-v2/lending-swaper.ts rename to packages/lending/src/protocols/openocean-v2/lending-swapper.ts index bdab8444..a2b0d9f1 100644 --- a/packages/lending/src/protocols/openocean-v2/lending-swaper.ts +++ b/packages/lending/src/protocols/openocean-v2/lending-swapper.ts @@ -1,11 +1,11 @@ -import { NAME } from './configs'; -import { Swaper } from 'src/swaper'; +import { NAME, supportedChainIds } from './configs'; +import { Swapper } from 'src/swapper'; import * as apisdk from '@protocolink/api'; import * as common from '@protocolink/common'; import * as logics from '@protocolink/logics'; -export class LendingSwaper extends Swaper { - static readonly supportedChainIds = [1088]; +export class LendingSwapper extends Swapper { + static readonly supportedChainIds = supportedChainIds; readonly id = NAME; readonly canCustomToken = false; @@ -30,8 +30,4 @@ export class LendingSwaper extends Swaper { newSwapTokenLogic(fields: apisdk.protocols.openoceanv2.SwapTokenFields): apisdk.protocols.openoceanv2.SwapTokenLogic { return apisdk.protocols.openoceanv2.newSwapTokenLogic(fields); } - - isExactIn() { - return true; - } } diff --git a/packages/lending/src/protocols/paraswap-v5/configs.ts b/packages/lending/src/protocols/paraswap-v5/configs.ts index 7dde4a28..9064ed87 100644 --- a/packages/lending/src/protocols/paraswap-v5/configs.ts +++ b/packages/lending/src/protocols/paraswap-v5/configs.ts @@ -1 +1,5 @@ +import * as logics from '@protocolink/logics'; + export const NAME = 'paraswap-v5'; + +export const supportedChainIds = logics.paraswapv5.SwapTokenLogic.supportedChainIds; diff --git a/packages/lending/src/protocols/paraswap-v5/index.ts b/packages/lending/src/protocols/paraswap-v5/index.ts index 3cd67d0a..dfd1ad0c 100644 --- a/packages/lending/src/protocols/paraswap-v5/index.ts +++ b/packages/lending/src/protocols/paraswap-v5/index.ts @@ -1,2 +1,2 @@ export * from './configs'; -export * from './lending-swaper'; +export * from './lending-swapper'; diff --git a/packages/lending/src/protocols/paraswap-v5/lending-swaper.ts b/packages/lending/src/protocols/paraswap-v5/lending-swapper.ts similarity index 80% rename from packages/lending/src/protocols/paraswap-v5/lending-swaper.ts rename to packages/lending/src/protocols/paraswap-v5/lending-swapper.ts index 2f34e910..ed111adb 100644 --- a/packages/lending/src/protocols/paraswap-v5/lending-swaper.ts +++ b/packages/lending/src/protocols/paraswap-v5/lending-swapper.ts @@ -1,11 +1,11 @@ -import { NAME } from './configs'; -import { Swaper } from 'src/swaper'; +import { NAME, supportedChainIds } from './configs'; +import { Swapper } from 'src/swapper'; import * as apisdk from '@protocolink/api'; import * as common from '@protocolink/common'; import * as logics from '@protocolink/logics'; -export class LendingSwaper extends Swaper { - static readonly supportedChainIds = [1, 10, 137, 42161, 43114]; +export class LendingSwapper extends Swapper { + static readonly supportedChainIds = supportedChainIds; readonly id = NAME; readonly canCustomToken = false; @@ -30,8 +30,4 @@ export class LendingSwaper extends Swaper { newSwapTokenLogic(fields: apisdk.protocols.paraswapv5.SwapTokenFields): apisdk.protocols.paraswapv5.SwapTokenLogic { return apisdk.protocols.paraswapv5.newSwapTokenLogic(fields); } - - isExactIn() { - return true; - } } diff --git a/packages/lending/src/swaper.ts b/packages/lending/src/swapper.ts similarity index 73% rename from packages/lending/src/swaper.ts rename to packages/lending/src/swapper.ts index dd0ca1ea..4e1dea38 100644 --- a/packages/lending/src/swaper.ts +++ b/packages/lending/src/swapper.ts @@ -1,10 +1,10 @@ -import { SwapTokenLogicParams } from './swaper.type'; +import { SwapTokenLogicParams } from './swapper.type'; import * as apisdk from '@protocolink/api'; import * as common from '@protocolink/common'; import * as logics from '@protocolink/logics'; import { providers } from 'ethers'; -export abstract class Swaper extends common.Web3Toolkit { +export abstract class Swapper extends common.Web3Toolkit { static readonly supportedChainIds: number[]; static isSupported(chainId: number) { @@ -23,12 +23,10 @@ export abstract class Swaper extends common.Web3Toolkit { abstract quote(params: SwapTokenLogicParams): Promise; abstract newSwapTokenLogic(fields: any): apisdk.protocols.paraswapv5.SwapTokenLogic; - - abstract isExactIn(logic: apisdk.protocols.paraswapv5.SwapTokenLogic): boolean; } -export interface SwaperClass { - new (chainId: number, library: providers.Provider): Swaper; +export interface SwapperClass { + new (chainId: number, library: providers.Provider): Swapper; readonly supportedChainIds: number[]; isSupported: (chainId: number) => boolean; } diff --git a/packages/lending/src/swaper.type.ts b/packages/lending/src/swapper.type.ts similarity index 100% rename from packages/lending/src/swaper.type.ts rename to packages/lending/src/swapper.type.ts diff --git a/packages/lending/test/hooks.ts b/packages/lending/test/hooks.ts index 6cecdc19..0ed43570 100644 --- a/packages/lending/test/hooks.ts +++ b/packages/lending/test/hooks.ts @@ -2,7 +2,7 @@ import { LendingProtocol as AaveV2Lending } from 'src/protocols/aave-v2/lending- import { LendingProtocol as AaveV3Lending } from 'src/protocols/aave-v3/lending-protocol'; import { Adapter } from 'src/adapter'; import { LendingProtocol as CompoundV3Lending } from 'src/protocols/compound-v3/lending-protocol'; -import { LendingSwaper } from 'src/protocols/paraswap-v5/lending-swaper'; +import { LendingSwapper } from 'src/protocols/paraswap-v5/lending-swapper'; import { LendingProtocol as RadiantV2Lending } from 'src/protocols/radiant-v2/lending-protocol'; export async function setup() { @@ -10,5 +10,5 @@ export async function setup() { Adapter.registerProtocol(AaveV3Lending); Adapter.registerProtocol(CompoundV3Lending); Adapter.registerProtocol(RadiantV2Lending); - Adapter.registerSwaper(LendingSwaper); + Adapter.registerSwapper(LendingSwapper); } diff --git a/packages/lending/test/transactions/collateral-swap.ts b/packages/lending/test/transactions/collateral-swap.ts index 78be27f9..dee6caf4 100644 --- a/packages/lending/test/transactions/collateral-swap.ts +++ b/packages/lending/test/transactions/collateral-swap.ts @@ -42,7 +42,7 @@ describe('Transaction: Collateral swap', function () { }, }, { - skip: true, + skip: false, testingAccount: '0x53fb0162bC8d5EEc2fB1532923C4f8997BAce111', protocolId: 'compound-v3', marketId: 'USDC',