Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Morphoblue flashloan #34

Merged
merged 5 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 6 additions & 8 deletions src/logics/morphoblue/logic.flash-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,18 @@ export class FlashLoanLogic extends core.Logic implements core.LogicTokenListInt
}

async getTokenList() {
hihiben marked this conversation as resolved.
Show resolved Hide resolved
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);

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

const tokenList: FlashLoanLogicTokenList = await service.getTokens([...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;
}
hihiben marked this conversation as resolved.
Show resolved Hide resolved
Loading