-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: utility add flash loan aggregator logic
- Loading branch information
Showing
12 changed files
with
250 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@protocolink/logics': patch | ||
--- | ||
|
||
feat: utility add flash loan aggregator logic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './logic.custom-data'; | ||
export * from './logic.flash-loan-aggregator'; | ||
export * from './logic.multi-send'; | ||
export * from './logic.send-token'; | ||
export * from './logic.wrapped-native-token'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { FlashLoanAggregatorLogic } from './logic.flash-loan-aggregator'; | ||
import * as common from '@protocolink/common'; | ||
import { expect } from 'chai'; | ||
|
||
describe('Utility FlashLoanAggregatorLogic', function () { | ||
context('Test getTokenList', async function () { | ||
FlashLoanAggregatorLogic.supportedChainIds.forEach((chainId) => { | ||
it(`network: ${common.toNetworkId(chainId)}`, async function () { | ||
const logic = new FlashLoanAggregatorLogic(chainId); | ||
const tokenList = await logic.getTokenList(); | ||
expect(tokenList).to.have.lengthOf.above(0); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import * as aavev2 from '../aave-v2'; | ||
import * as aavev3 from '../aave-v3'; | ||
import * as balancerv2 from '../balancer-v2'; | ||
import * as common from '@protocolink/common'; | ||
import * as core from '@protocolink/core'; | ||
|
||
export const supportedFlashLoanLogics = [aavev2.FlashLoanLogic, aavev3.FlashLoanLogic, balancerv2.FlashLoanLogic]; | ||
|
||
export type FlashLoanAggregatorLogicTokenList = common.Token[]; | ||
|
||
export type FlashLoanAggregatorLogicParams = core.TokensOutFields; | ||
|
||
export type FlashLoanAggregatorLogicQuotation = { | ||
protocolId: string; | ||
loans: common.TokenAmounts; | ||
repays: common.TokenAmounts; | ||
fees: common.TokenAmounts; | ||
feeBps: number; | ||
callback: string; | ||
}; | ||
|
||
export type FlashLoanAggregatorLogicFields = core.FlashLoanFields<{ protocolId: string; referralCode?: number }>; | ||
|
||
@core.LogicDefinitionDecorator() | ||
export class FlashLoanAggregatorLogic | ||
extends core.Logic | ||
implements core.LogicTokenListInterface, core.LogicBuilderInterface | ||
{ | ||
static readonly supportedChainIds = Array.from( | ||
supportedFlashLoanLogics.reduce((accumulator, FlashLoanLogic) => { | ||
for (const chainId of FlashLoanLogic.supportedChainIds) { | ||
accumulator.add(chainId); | ||
} | ||
return accumulator; | ||
}, new Set<number>()) | ||
); | ||
|
||
async getTokenList() { | ||
const flashLoanLogics = supportedFlashLoanLogics.filter((FlashLoanLogic) => | ||
FlashLoanLogic.supportedChainIds.includes(this.chainId) | ||
); | ||
const allTokens = await Promise.all( | ||
flashLoanLogics.map((FlashLoanLogic) => { | ||
const flashLoanLogic = new FlashLoanLogic(this.chainId, this.provider); | ||
return flashLoanLogic.getTokenList(); | ||
}) | ||
); | ||
const tmp: Record<string, boolean> = {}; | ||
const tokenList: FlashLoanAggregatorLogicTokenList = []; | ||
for (const tokens of allTokens) { | ||
for (const token of tokens) { | ||
if (tmp[token.address]) continue; | ||
tokenList.push(token); | ||
tmp[token.address] = true; | ||
} | ||
} | ||
|
||
return tokenList; | ||
} | ||
|
||
async quote(params: FlashLoanAggregatorLogicParams) { | ||
const flashLoanLogics = supportedFlashLoanLogics.filter((FlashLoanLogic) => | ||
FlashLoanLogic.supportedChainIds.includes(this.chainId) | ||
); | ||
|
||
const quotations: FlashLoanAggregatorLogicQuotation[] = []; | ||
await Promise.all( | ||
flashLoanLogics.map(async (FlashLoanLogic) => { | ||
const flashLoanLogic = new FlashLoanLogic(this.chainId, this.provider); | ||
try { | ||
const quotation = await flashLoanLogic.quote(params); | ||
quotations.push({ | ||
protocolId: FlashLoanLogic.protocolId, | ||
callback: flashLoanLogic.callbackAddress, | ||
...quotation, | ||
}); | ||
} catch {} | ||
}) | ||
); | ||
|
||
let quotation = quotations[0]; | ||
for (let i = 1; i < quotations.length; i++) { | ||
if (quotations[i].feeBps < quotation.feeBps) { | ||
quotation = quotations[i]; | ||
} | ||
} | ||
|
||
return quotation; | ||
} | ||
|
||
async build(fields: FlashLoanAggregatorLogicFields) { | ||
const { protocolId, ...others } = fields; | ||
const FlashLoanLogic = supportedFlashLoanLogics.find((Logic) => Logic.protocolId === protocolId)!; | ||
const routerLogic = await new FlashLoanLogic(this.chainId, this.provider).build(others); | ||
|
||
return routerLogic; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.