-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: test aave markets * chore: changeset * chore: format --------- Co-authored-by: maxencerb <[email protected]>
- Loading branch information
Showing
7 changed files
with
175 additions
and
0 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 @@ | ||
--- | ||
"@mangrovedao/mgv": patch | ||
--- | ||
|
||
Added check for aave markets |
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,114 @@ | ||
import type { | ||
Address, | ||
Client, | ||
MulticallParameters, | ||
ReadContractParameters, | ||
} from 'viem' | ||
import { multicall, readContract } from 'viem/actions' | ||
import { | ||
type CheckAaveAssetParams, | ||
type aaveRouterCheckAssetABI, | ||
checkAaveAssetParams, | ||
} from '../../builder/kandel/aave.js' | ||
import type { BuiltArgs, MarketParams } from '../../index.js' | ||
import { getAction } from '../../utils/getAction.js' | ||
|
||
type ReadSingleParams = ReadContractParameters< | ||
typeof aaveRouterCheckAssetABI, | ||
'checkAsset' | ||
> | ||
|
||
export type CheckAaveAssetArgs = CheckAaveAssetParams & | ||
Omit<ReadSingleParams, BuiltArgs> | ||
|
||
export async function checkAaveAsset( | ||
client: Client, | ||
aaveRouter: Address, | ||
args: CheckAaveAssetArgs, | ||
): Promise<boolean> { | ||
return getAction( | ||
client, | ||
readContract, | ||
'readContract', | ||
)({ | ||
...(args as unknown as ReadSingleParams), | ||
address: aaveRouter, | ||
...checkAaveAssetParams(args), | ||
}) | ||
} | ||
|
||
export type CheckAaveAssetsArgs = { tokens: Address[] } & Omit< | ||
MulticallParameters, | ||
'contracts' | 'allowFailure' | ||
> | ||
|
||
export async function checkAaveAssets( | ||
client: Client, | ||
aaveRouter: Address, | ||
args: CheckAaveAssetsArgs, | ||
): Promise<boolean[]> { | ||
return getAction( | ||
client, | ||
multicall, | ||
'multicall', | ||
)({ | ||
...args, | ||
contracts: args.tokens.map((token) => ({ | ||
address: aaveRouter, | ||
...checkAaveAssetParams({ token }), | ||
})), | ||
allowFailure: false, | ||
}) | ||
} | ||
|
||
export type CheckAaveMarketArgs = { market: MarketParams } & Omit< | ||
MulticallParameters, | ||
'contracts' | 'allowFailure' | ||
> | ||
|
||
export async function checkAaveMarket( | ||
client: Client, | ||
aaveRouter: Address, | ||
args: CheckAaveMarketArgs, | ||
): Promise<boolean> { | ||
const tokens = [args.market.base.address, args.market.quote.address] | ||
const available = await checkAaveAssets(client, aaveRouter, { | ||
...args, | ||
tokens, | ||
}) | ||
return available.every((a) => a) | ||
} | ||
|
||
export type CheckAaveMarketsArgs = { markets: MarketParams[] } & Omit< | ||
MulticallParameters, | ||
'contracts' | 'allowFailure' | ||
> | ||
|
||
export async function checkAaveMarkets( | ||
client: Client, | ||
aaveRouter: Address, | ||
args: CheckAaveMarketsArgs, | ||
): Promise<MarketParams[]> { | ||
const tokens = [ | ||
...new Set( | ||
args.markets.flatMap((m) => [ | ||
m.base.address.toLowerCase() as Address, | ||
m.quote.address.toLowerCase() as Address, | ||
]), | ||
), | ||
] | ||
const available = await checkAaveAssets(client, aaveRouter, { | ||
...args, | ||
tokens, | ||
}) | ||
const tokensMap = tokens.reduce((acc, token, i) => { | ||
acc.set(token, available[i] || false) | ||
return acc | ||
}, new Map<Address, boolean>()) | ||
|
||
return args.markets.filter( | ||
(m) => | ||
tokensMap.get(m.base.address.toLowerCase() as Address) && | ||
tokensMap.get(m.quote.address.toLowerCase() as Address), | ||
) | ||
} |
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,24 @@ | ||
import { type Address, type ContractFunctionParameters, parseAbi } from 'viem' | ||
|
||
export type CheckAaveAssetParams = { | ||
token: Address | ||
} | ||
|
||
export const aaveRouterCheckAssetABI = parseAbi([ | ||
'function checkAsset(address token) public view returns (bool)', | ||
]) | ||
|
||
export function checkAaveAssetParams(params: CheckAaveAssetParams) { | ||
return { | ||
abi: aaveRouterCheckAssetABI, | ||
functionName: 'checkAsset', | ||
args: [params.token], | ||
} satisfies Omit< | ||
ContractFunctionParameters< | ||
typeof aaveRouterCheckAssetABI, | ||
'view', | ||
'checkAsset' | ||
>, | ||
'address' | ||
> | ||
} |
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