Skip to content

Commit

Permalink
fix: standardized import naming, corrected typos, and made various mi…
Browse files Browse the repository at this point in the history
…nor improvements
  • Loading branch information
Bob Lu committed Dec 2, 2023
1 parent 5182d4a commit cb162b3
Show file tree
Hide file tree
Showing 23 changed files with 211 additions and 195 deletions.
2 changes: 1 addition & 1 deletion packages/lending/src/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Lending AaveV3 SDK', function () {
Adapter.registerProtocol(LendingProtocol);
Adapter.registerSwaper(LendingSwaper);
const adapter = new Adapter(1, provider);
const protocolId = 'aavev3';
const protocolId = 'aave-v3';

let portfolio: Portfolio;

Expand Down
114 changes: 57 additions & 57 deletions packages/lending/src/adapter.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/lending/src/adapter.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Logic } from './protocol.type';
import { Portfolio } from './protocol.portfolio';
import * as api from '@protocolink/api';
import * as apisdk from '@protocolink/api';
import * as common from '@protocolink/common';

export type BaseFields = {
Expand All @@ -12,9 +12,9 @@ export type BaseFields = {
portfolio?: Portfolio;
afterPortfolio?: Portfolio;
};
estimateResult: api.RouterDataEstimateResult;
estimateResult: apisdk.RouterDataEstimateResult;
buildRouterTransactionRequest: (
args?: Omit<api.RouterData, 'chainId' | 'account' | 'logics'>,
args?: Omit<apisdk.RouterData, 'chainId' | 'account' | 'logics'>,
apiKey?: string
) => Promise<common.TransactionRequest>;
logics: Logic[];
Expand Down
9 changes: 5 additions & 4 deletions packages/lending/src/flashloaner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FlashLoanFields, FlashLoanLogic } from '@protocolink/api';
import * as api from '@protocolink/api';
import * as apisdk from '@protocolink/api';
import * as common from '@protocolink/common';
import * as logics from '@protocolink/logics';
import { providers } from 'ethers';
Expand Down Expand Up @@ -31,9 +30,11 @@ export abstract class FlashLoaner extends common.Web3Toolkit {
return !!this.tokenMap[token.address];
}

abstract quote(params: api.protocols.aavev3.FlashLoanParams): Promise<logics.aavev3.FlashLoanLogicQuotation>;
abstract quote(params: apisdk.protocols.aavev3.FlashLoanParams): Promise<logics.aavev3.FlashLoanLogicQuotation>;

abstract newFlashLoanLogicPair(loans: FlashLoanFields['loans']): [FlashLoanLogic, FlashLoanLogic];
abstract newFlashLoanLogicPair(
loans: apisdk.FlashLoanFields['loans']
): [apisdk.FlashLoanLogic, apisdk.FlashLoanLogic];
}

export interface FlashLoanerClass {
Expand Down
4 changes: 2 additions & 2 deletions packages/lending/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO: BOOOB
import * as api from '@protocolink/api';
import * as apisdk from '@protocolink/api';

api.init({ baseURL: 'https://api-beta.protocolink.com' });
apisdk.init({ baseURL: 'https://api-beta.protocolink.com' });

export * as adapter from './adapter';
export * as protocol from './protocol';
Expand Down
16 changes: 8 additions & 8 deletions packages/lending/src/protocols/aave-v2/lending-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import { PriceOracleInterface } from './contracts/PriceOracle';
import { Protocol } from 'src/protocol';
import { ProtocolDataProviderInterface } from './contracts/ProtocolDataProvider';
import { RAY_DECIMALS, SECONDS_PER_YEAR, calculateCompoundedRate, normalize } from '@aave/math-utils';
import * as apisdk from '@protocolink/api';
import * as common from '@protocolink/common';
import { isWrappedNativeToken, wrapToken } from 'src/helper';
import { protocols } from '@protocolink/api';

export class LendingProtocol extends Protocol {
static readonly markets = supportedChainIds.map((chainId) => ({
Expand Down Expand Up @@ -330,34 +330,34 @@ export class LendingProtocol extends Protocol {
}

async newSupplyLogic(params: SupplyParams) {
const supplyQuotation = await protocols.aavev2.getDepositQuotation(this.chainId, {
const supplyQuotation = await apisdk.protocols.aavev2.getDepositQuotation(this.chainId, {
input: params.input,
tokenOut: toAToken(this.chainId, params.input.token),
});
return protocols.aavev2.newDepositLogic({ ...supplyQuotation, balanceBps: common.BPS_BASE });
return apisdk.protocols.aavev2.newDepositLogic({ ...supplyQuotation, balanceBps: common.BPS_BASE });
}

async newWithdrawLogic(params: WithdrawParams) {
const withdrawQuotation = await protocols.aavev2.getWithdrawQuotation(this.chainId, {
const withdrawQuotation = await apisdk.protocols.aavev2.getWithdrawQuotation(this.chainId, {
input: {
token: toAToken(this.chainId, params.output.token),
amount: params.output.amount,
},
tokenOut: params.output.token,
});
return protocols.aavev2.newWithdrawLogic(withdrawQuotation);
return apisdk.protocols.aavev2.newWithdrawLogic(withdrawQuotation);
}

newBorrowLogic = protocols.aavev2.newBorrowLogic;
newBorrowLogic = apisdk.protocols.aavev2.newBorrowLogic;

async newRepayLogic(params: RepayParams) {
if (!params.borrower || !params.interestRateMode) throw new Error('missing requied params');
const repayQuotation = await protocols.aavev2.getRepayQuotation(this.chainId, {
const repayQuotation = await apisdk.protocols.aavev2.getRepayQuotation(this.chainId, {
tokenIn: params.input.token,
borrower: params.borrower,
interestRateMode: params.interestRateMode,
});
repayQuotation.input.amount = params.input.amount;
return protocols.aavev2.newRepayLogic(repayQuotation);
return apisdk.protocols.aavev2.newRepayLogic(repayQuotation);
}
}
16 changes: 8 additions & 8 deletions packages/lending/src/protocols/aave-v3/lending-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import { PoolDataProviderInterface } from './contracts/PoolDataProvider';
import { Portfolio } from 'src/protocol.portfolio';
import { Protocol } from 'src/protocol';
import { RAY_DECIMALS, SECONDS_PER_YEAR, calculateCompoundedRate, normalize } from '@aave/math-utils';
import * as apisdk from '@protocolink/api';
import * as common from '@protocolink/common';
import { isWrappedNativeToken, wrapToken } from 'src/helper';
import { protocols } from '@protocolink/api';

export class LendingProtocol extends Protocol {
static readonly markets = supportedChainIds.map((chainId) => ({
Expand Down Expand Up @@ -334,34 +334,34 @@ export class LendingProtocol extends Protocol {
}

async newSupplyLogic(params: SupplyParams) {
const supplyQuotation = await protocols.aavev3.getSupplyQuotation(this.chainId, {
const supplyQuotation = await apisdk.protocols.aavev3.getSupplyQuotation(this.chainId, {
input: params.input,
tokenOut: toAToken(this.chainId, params.input.token),
});
return protocols.aavev3.newSupplyLogic({ ...supplyQuotation, balanceBps: common.BPS_BASE });
return apisdk.protocols.aavev3.newSupplyLogic({ ...supplyQuotation, balanceBps: common.BPS_BASE });
}

async newWithdrawLogic(params: WithdrawParams) {
const withdrawQuotation = await protocols.aavev3.getWithdrawQuotation(this.chainId, {
const withdrawQuotation = await apisdk.protocols.aavev3.getWithdrawQuotation(this.chainId, {
input: {
token: toAToken(this.chainId, params.output.token),
amount: params.output.amount,
},
tokenOut: params.output.token,
});
return protocols.aavev3.newWithdrawLogic(withdrawQuotation);
return apisdk.protocols.aavev3.newWithdrawLogic(withdrawQuotation);
}

newBorrowLogic = protocols.aavev3.newBorrowLogic;
newBorrowLogic = apisdk.protocols.aavev3.newBorrowLogic;

async newRepayLogic(params: RepayParams) {
if (!params.borrower || !params.interestRateMode) throw new Error('missing requied params');
const repayQuotation = await protocols.aavev3.getRepayQuotation(this.chainId, {
const repayQuotation = await apisdk.protocols.aavev3.getRepayQuotation(this.chainId, {
tokenIn: params.input.token,
borrower: params.borrower,
interestRateMode: params.interestRateMode,
});
repayQuotation.input.amount = params.input.amount;
return protocols.aavev3.newRepayLogic(repayQuotation);
return apisdk.protocols.aavev3.newRepayLogic(repayQuotation);
}
}
23 changes: 13 additions & 10 deletions packages/lending/src/protocols/compound-v3/lending-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
} from './configs';
import { Portfolio } from 'src/protocol.portfolio';
import { Protocol } from 'src/protocol';
import * as apisdk from '@protocolink/api';
import { calcAPR } from './utils';
import * as common from '@protocolink/common';
import * as logics from '@protocolink/logics';
import { protocols } from '@protocolink/api';
import { unwrapToken, wrapToken } from 'src/helper';

export class LendingProtocol extends Protocol {
Expand Down Expand Up @@ -307,14 +307,14 @@ export class LendingProtocol extends Protocol {
const { cometAddress, baseToken } = getMarketBaseConfig(this.chainId, params.marketId);
const cToken = await this.getToken(cometAddress);
if (params.input.token.unwrapped.is(baseToken.unwrapped)) {
const supplyQuotation = await protocols.compoundv3.getSupplyBaseQuotation(this.chainId, {
const supplyQuotation = await apisdk.protocols.compoundv3.getSupplyBaseQuotation(this.chainId, {
input: params.input,
tokenOut: cToken,
marketId: params.marketId,
});
return protocols.compoundv3.newSupplyBaseLogic({ ...supplyQuotation, balanceBps: common.BPS_BASE });
return apisdk.protocols.compoundv3.newSupplyBaseLogic({ ...supplyQuotation, balanceBps: common.BPS_BASE });
} else {
return protocols.compoundv3.newSupplyCollateralLogic({
return apisdk.protocols.compoundv3.newSupplyCollateralLogic({
input: params.input,
marketId: params.marketId,
balanceBps: common.BPS_BASE,
Expand All @@ -329,34 +329,37 @@ export class LendingProtocol extends Protocol {
const realAmount = new common.TokenAmount(params.output.token, params.output.amount);
realAmount.subWei(2);
if (params.output.token.wrapped.is(baseToken)) {
const withdrawBaseQuotation = await protocols.compoundv3.getWithdrawBaseQuotation(this.chainId, {
const withdrawBaseQuotation = await apisdk.protocols.compoundv3.getWithdrawBaseQuotation(this.chainId, {
input: {
token: cToken,
amount: realAmount.amount,
},
tokenOut: params.output.token,
marketId: params.marketId,
});
const withdrawBaseLogic = await protocols.compoundv3.newWithdrawBaseLogic({
const withdrawBaseLogic = await apisdk.protocols.compoundv3.newWithdrawBaseLogic({
...withdrawBaseQuotation,
balanceBps: common.BPS_BASE,
});
withdrawBaseLogic.fields.output.amount = realAmount.subWei(1).amount;
return withdrawBaseLogic;
} else {
return protocols.compoundv3.newWithdrawCollateralLogic({ output: params.output, marketId: params.marketId });
return apisdk.protocols.compoundv3.newWithdrawCollateralLogic({
output: params.output,
marketId: params.marketId,
});
}
}

newBorrowLogic = protocols.compoundv3.newBorrowLogic;
newBorrowLogic = apisdk.protocols.compoundv3.newBorrowLogic;

async newRepayLogic(params: RepayParams): Promise<any> {
const repayQuotation = await protocols.compoundv3.getRepayQuotation(this.chainId, {
const repayQuotation = await apisdk.protocols.compoundv3.getRepayQuotation(this.chainId, {
tokenIn: params.input.token,
borrower: params.borrower,
marketId: params.marketId,
});
repayQuotation.input.amount = params.input.amount;
return protocols.compoundv3.newRepayLogic(repayQuotation);
return apisdk.protocols.compoundv3.newRepayLogic(repayQuotation);
}
}
13 changes: 6 additions & 7 deletions packages/lending/src/protocols/openocean-v2/lending-swaper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { NAME } from './configs';
import { Swaper } from 'src/swaper';
import * as api from '@protocolink/api';
import * as apisdk from '@protocolink/api';
import * as common from '@protocolink/common';
import * as logics from '@protocolink/logics';
import { quote } from '@protocolink/api';

export class LendingSwaper extends Swaper {
static readonly supportedChainIds = [1088];
Expand All @@ -19,17 +18,17 @@ export class LendingSwaper extends Swaper {

async tokens() {
if (!this._tokens) {
this._tokens = await api.protocols.openoceanv2.getSwapTokenTokenList(this.chainId);
this._tokens = await apisdk.protocols.openoceanv2.getSwapTokenTokenList(this.chainId);
}
return this._tokens!;
}

async quote(params: api.protocols.paraswapv5.SwapTokenParams): Promise<logics.openoceanv2.SwapTokenLogicFields> {
return await quote(this.chainId, logics.openoceanv2.SwapTokenLogic.rid, params);
async quote(params: apisdk.protocols.openoceanv2.SwapTokenParams): Promise<logics.openoceanv2.SwapTokenLogicFields> {
return await apisdk.quote(this.chainId, logics.openoceanv2.SwapTokenLogic.rid, params);
}

newSwapTokenLogic(fields: api.protocols.openoceanv2.SwapTokenFields): api.protocols.openoceanv2.SwapTokenLogic {
return api.protocols.openoceanv2.newSwapTokenLogic(fields);
newSwapTokenLogic(fields: apisdk.protocols.openoceanv2.SwapTokenFields): apisdk.protocols.openoceanv2.SwapTokenLogic {
return apisdk.protocols.openoceanv2.newSwapTokenLogic(fields);
}

isExactIn() {
Expand Down
13 changes: 6 additions & 7 deletions packages/lending/src/protocols/paraswap-v5/lending-swaper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { NAME } from './configs';
import { Swaper } from 'src/swaper';
import * as api from '@protocolink/api';
import * as apisdk from '@protocolink/api';
import * as common from '@protocolink/common';
import * as logics from '@protocolink/logics';
import { quote } from '@protocolink/api';

export class LendingSwaper extends Swaper {
static readonly supportedChainIds = [1, 10, 137, 42161, 43114];
Expand All @@ -19,17 +18,17 @@ export class LendingSwaper extends Swaper {

async tokens() {
if (!this._tokens) {
this._tokens = await api.protocols.paraswapv5.getSwapTokenTokenList(this.chainId);
this._tokens = await apisdk.protocols.paraswapv5.getSwapTokenTokenList(this.chainId);
}
return this._tokens!;
}

async quote(params: api.protocols.paraswapv5.SwapTokenParams): Promise<logics.paraswapv5.SwapTokenLogicFields> {
return await quote(this.chainId, logics.paraswapv5.SwapTokenLogic.rid, params);
async quote(params: apisdk.protocols.paraswapv5.SwapTokenParams): Promise<logics.paraswapv5.SwapTokenLogicFields> {
return await apisdk.quote(this.chainId, logics.paraswapv5.SwapTokenLogic.rid, params);
}

newSwapTokenLogic(fields: api.protocols.paraswapv5.SwapTokenFields): api.protocols.paraswapv5.SwapTokenLogic {
return api.protocols.paraswapv5.newSwapTokenLogic(fields);
newSwapTokenLogic(fields: apisdk.protocols.paraswapv5.SwapTokenFields): apisdk.protocols.paraswapv5.SwapTokenLogic {
return apisdk.protocols.paraswapv5.newSwapTokenLogic(fields);
}

isExactIn() {
Expand Down
16 changes: 8 additions & 8 deletions packages/lending/src/protocols/radiant-v2/lending-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import { PriceOracleInterface } from './contracts/PriceOracle';
import { Protocol } from 'src/protocol';
import { ProtocolDataProviderInterface } from './contracts/ProtocolDataProvider';
import { RAY_DECIMALS, SECONDS_PER_YEAR, calculateCompoundedRate, normalize } from '@aave/math-utils';
import * as apisdk from '@protocolink/api';
import * as common from '@protocolink/common';
import { isWrappedNativeToken, wrapToken } from 'src/helper';
import { protocols } from '@protocolink/api';
import { providers } from 'ethers';

export class LendingProtocol extends Protocol {
Expand Down Expand Up @@ -280,34 +280,34 @@ export class LendingProtocol extends Protocol {
}

async newSupplyLogic(params: SupplyParams) {
const supplyQuotation = await protocols.radiantv2.getDepositQuotation(this.chainId, {
const supplyQuotation = await apisdk.protocols.radiantv2.getDepositQuotation(this.chainId, {
input: params.input,
tokenOut: toRToken(this.chainId, params.input.token),
});
return protocols.radiantv2.newDepositLogic({ ...supplyQuotation, balanceBps: common.BPS_BASE });
return apisdk.protocols.radiantv2.newDepositLogic({ ...supplyQuotation, balanceBps: common.BPS_BASE });
}

async newWithdrawLogic(params: WithdrawParams) {
const withdrawQuotation = await protocols.radiantv2.getWithdrawQuotation(this.chainId, {
const withdrawQuotation = await apisdk.protocols.radiantv2.getWithdrawQuotation(this.chainId, {
input: {
token: toRToken(this.chainId, params.output.token),
amount: params.output.amount,
},
tokenOut: params.output.token,
});
return protocols.radiantv2.newWithdrawLogic(withdrawQuotation);
return apisdk.protocols.radiantv2.newWithdrawLogic(withdrawQuotation);
}

newBorrowLogic = protocols.radiantv2.newBorrowLogic;
newBorrowLogic = apisdk.protocols.radiantv2.newBorrowLogic;

async newRepayLogic(params: RepayParams) {
if (!params.borrower || !params.interestRateMode) throw new Error('missing requied params');
const repayQuotation = await protocols.radiantv2.getRepayQuotation(this.chainId, {
const repayQuotation = await apisdk.protocols.radiantv2.getRepayQuotation(this.chainId, {
tokenIn: params.input.token,
borrower: params.borrower,
interestRateMode: params.interestRateMode,
});
repayQuotation.input.amount = params.input.amount;
return protocols.radiantv2.newRepayLogic(repayQuotation);
return apisdk.protocols.radiantv2.newRepayLogic(repayQuotation);
}
}
10 changes: 5 additions & 5 deletions packages/lending/src/swaper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TokenToTokenParams } from './swaper.type';
import * as api from '@protocolink/api';
import { SwapTokenLogicParams } from './swaper.type';
import * as apisdk from '@protocolink/api';
import * as common from '@protocolink/common';
import * as logics from '@protocolink/logics';
import { providers } from 'ethers';
Expand All @@ -20,11 +20,11 @@ export abstract class Swaper extends common.Web3Toolkit {
return true;
}

abstract quote(params: TokenToTokenParams): Promise<logics.paraswapv5.SwapTokenLogicFields>;
abstract quote(params: SwapTokenLogicParams): Promise<logics.paraswapv5.SwapTokenLogicFields>;

abstract newSwapTokenLogic(fields: any): api.protocols.paraswapv5.SwapTokenLogic;
abstract newSwapTokenLogic(fields: any): apisdk.protocols.paraswapv5.SwapTokenLogic;

abstract isExactIn(logic: api.protocols.paraswapv5.SwapTokenLogic): boolean;
abstract isExactIn(logic: apisdk.protocols.paraswapv5.SwapTokenLogic): boolean;
}

export interface SwaperClass {
Expand Down
5 changes: 5 additions & 0 deletions packages/lending/src/swaper.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as apisdk from '@protocolink/api';
import * as common from '@protocolink/common';

export enum TradeType {
Expand All @@ -19,6 +20,10 @@ export interface TokenToTokenExactOutParams {

export type TokenToTokenParams = TokenToTokenExactInParams | TokenToTokenExactOutParams;

export type SwapTokenLogicParams =
| apisdk.protocols.openoceanv2.SwapTokenParams
| apisdk.protocols.paraswapv5.SwapTokenParams;

export function isTokenToTokenExactInParams(v: any): v is TokenToTokenExactInParams {
return !!v.input && !!v.tokenOut;
}
Expand Down
Loading

0 comments on commit cb162b3

Please sign in to comment.