Skip to content

Commit

Permalink
fix: flashloan tokenlist and get market tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-CCH committed Jan 5, 2024
1 parent 94f17c3 commit 343afdb
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 79 deletions.
10 changes: 4 additions & 6 deletions src/logics/morphoblue/logic.borrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ export class BorrowLogic extends core.Logic implements core.LogicTokenListInterf
const markets = getMarkets(this.chainId);

for (const market of markets) {
const loanTokens = await service.getLoanTokens(market.id);
for (const loanToken of loanTokens!) {
tokenList[market.id] = [];
if (loanToken.isWrapped) tokenList[market.id].push(loanToken.unwrapped);
tokenList[market.id].push(loanToken);
}
const loanToken = await service.getLoanToken(market.id);
tokenList[market.id] = [];
if (loanToken.isWrapped) tokenList[market.id].push(loanToken.unwrapped);
tokenList[market.id].push(loanToken);
}

return tokenList;
Expand Down
14 changes: 9 additions & 5 deletions src/logics/morphoblue/logic.flash-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@ export class FlashLoanLogic extends core.Logic implements core.LogicTokenListInt

async getTokenList() {
const tokenList: FlashLoanLogicTokenList = [];
const tokenSet = new Set<string>();
const service = new Service(this.chainId, this.provider);

const markets = getMarkets(this.chainId);

for (const market of markets) {
const tokens = await service.getMarketTokens(market.id);
tokenSet.add(tokens.loanToken);
tokenSet.add(tokens.collateralToken);
}

for (const token of tokens) {
tokenList.push(token.loanToken, token.collateralToken);
}
const tokenAddressList = [...tokenSet];
for (const tokenAddress of tokenAddressList) {
tokenList.push(await service.getToken(tokenAddress));
}
const tokenSet = new Set(tokenList.map((token) => token));
return [...tokenSet];

return tokenList;
}

async quote(params: FlashLoanLogicParams) {
Expand Down
10 changes: 4 additions & 6 deletions src/logics/morphoblue/logic.repay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ export class RepayLogic extends core.Logic implements core.LogicTokenListInterfa
const markets = getMarkets(this.chainId);

for (const market of markets) {
const loanTokens = await service.getLoanTokens(market.id);
for (const loanToken of loanTokens!) {
tokenList[market.id] = [];
if (loanToken.isWrapped) tokenList[market.id].push(loanToken.unwrapped);
tokenList[market.id].push(loanToken);
}
const loanToken = await service.getLoanToken(market.id);
tokenList[market.id] = [];
if (loanToken.isWrapped) tokenList[market.id].push(loanToken.unwrapped);
tokenList[market.id].push(loanToken);
}

return tokenList;
Expand Down
10 changes: 4 additions & 6 deletions src/logics/morphoblue/logic.supply-collateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ export class SupplyCollateralLogic
const markets = getMarkets(this.chainId);

for (const market of markets) {
const collateralTokens = await service.getCollateralTokens(market.id);
for (const collateralToken of collateralTokens!) {
tokenList[market.id] = [];
if (collateralToken.isWrapped) tokenList[market.id].push(collateralToken.unwrapped);
tokenList[market.id].push(collateralToken);
}
const collateralToken = await service.getCollateralToken(market.id);
tokenList[market.id] = [];
if (collateralToken.isWrapped) tokenList[market.id].push(collateralToken.unwrapped);
tokenList[market.id].push(collateralToken);
}

return tokenList;
Expand Down
10 changes: 4 additions & 6 deletions src/logics/morphoblue/logic.supply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ export class SupplyLogic extends core.Logic implements core.LogicTokenListInterf
const markets = getMarkets(this.chainId);

for (const market of markets) {
const loanTokens = await service.getLoanTokens(market.id);
for (const loanToken of loanTokens!) {
tokenList[market.id] = [];
if (loanToken.isWrapped) tokenList[market.id].push(loanToken.unwrapped);
tokenList[market.id].push(loanToken);
}
const loanToken = await service.getLoanToken(market.id);
tokenList[market.id] = [];
if (loanToken.isWrapped) tokenList[market.id].push(loanToken.unwrapped);
tokenList[market.id].push(loanToken);
}

return tokenList;
Expand Down
10 changes: 4 additions & 6 deletions src/logics/morphoblue/logic.withdraw-collateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ export class WithdrawCollateralLogic
const markets = getMarkets(this.chainId);

for (const market of markets) {
const collateralTokens = await service.getCollateralTokens(market.id);
for (const collateralToken of collateralTokens!) {
tokenList[market.id] = [];
if (collateralToken.isWrapped) tokenList[market.id].push(collateralToken.unwrapped);
tokenList[market.id].push(collateralToken);
}
const collateralToken = await service.getCollateralToken(market.id);
tokenList[market.id] = [];
if (collateralToken.isWrapped) tokenList[market.id].push(collateralToken.unwrapped);
tokenList[market.id].push(collateralToken);
}

return tokenList;
Expand Down
10 changes: 4 additions & 6 deletions src/logics/morphoblue/logic.withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ export class WithdrawLogic extends core.Logic implements core.LogicTokenListInte
const markets = getMarkets(this.chainId);

for (const market of markets) {
const loanTokens = await service.getLoanTokens(market.id);
for (const loanToken of loanTokens!) {
tokenList[market.id] = [];
if (loanToken.isWrapped) tokenList[market.id].push(loanToken.unwrapped);
tokenList[market.id].push(loanToken);
}
const loanToken = await service.getLoanToken(market.id);
tokenList[market.id] = [];
if (loanToken.isWrapped) tokenList[market.id].push(loanToken.unwrapped);
tokenList[market.id].push(loanToken);
}

return tokenList;
Expand Down
52 changes: 18 additions & 34 deletions src/logics/morphoblue/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,34 @@ export class Service extends common.Web3Toolkit {
return this._morphoIface;
}

private loanTokens?: common.Token[];
async getLoanTokens(marketId: string) {
if (!this.loanTokens) {
await this.getMarketTokens(marketId);
}
return this.loanTokens;
async getLoanToken(marketId: string) {
const tokens = await this.getMarketTokens(marketId);
const loanToken = await this.getToken(tokens.loanToken);
return loanToken;
}

private collateralTokens?: common.Token[];
async getCollateralTokens(marketId: string) {
if (!this.collateralTokens) {
await this.getMarketTokens(marketId);
}
return this.collateralTokens;
async getCollateralToken(marketId: string) {
const tokens = await this.getMarketTokens(marketId);
const collateralToken = await this.getToken(tokens.collateralToken);
return collateralToken;
}

async getMarket(marketId: string) {
return getMarket(this.chainId, marketId);
}

private tokens?: Tokens[];
async getMarketTokens(marketId: string) {
if (!this.tokens) {
const calls: common.Multicall3.CallStruct[] = [];
calls.push({
target: this.morpho.address,
callData: this.morphoIface.encodeFunctionData('idToMarketParams', [marketId]),
});
const { returnData } = await this.multicall3.callStatic.aggregate(calls);

this.tokens = [];
this.loanTokens = [];
this.collateralTokens = [];
let { loanToken, collateralToken } = this.morphoIface.decodeFunctionResult('idToMarketParams', returnData[0]);

loanToken = await this.getToken(loanToken);
collateralToken = await this.getToken(collateralToken);

this.tokens.push({ loanToken, collateralToken });
this.loanTokens.push(loanToken);
this.collateralTokens.push(collateralToken);
}
const calls: common.Multicall3.CallStruct[] = [];
calls.push({
target: this.morpho.address,
callData: this.morphoIface.encodeFunctionData('idToMarketParams', [marketId]),
});
const { returnData } = await this.multicall3.callStatic.aggregate(calls);

const { loanToken, collateralToken } = this.morphoIface.decodeFunctionResult('idToMarketParams', returnData[0]);

return this.tokens;
const tokens: Tokens = { loanToken, collateralToken };
return tokens;
}

async getSupplyBalance(marketId: string, account: string) {
Expand Down
6 changes: 2 additions & 4 deletions src/logics/morphoblue/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as common from '@protocolink/common';

export interface Tokens {
loanToken: common.Token;
collateralToken: common.Token;
loanToken: string;
collateralToken: string;
}

0 comments on commit 343afdb

Please sign in to comment.