From 03854ed963776713f2ff7120d9aa7e1b2593a602 Mon Sep 17 00:00:00 2001 From: greedythib Date: Fri, 29 Sep 2023 16:28:09 +0200 Subject: [PATCH] feat: add wl --- src/types/merkl.ts | 3 ++ src/types/utils.ts | 52 +++++++++++++++++++++++++-- src/utils/merkl.ts | 90 ++++++++++++++++++++++------------------------ 3 files changed, 95 insertions(+), 50 deletions(-) diff --git a/src/types/merkl.ts b/src/types/merkl.ts index 28ed894..8423bc1 100644 --- a/src/types/merkl.ts +++ b/src/types/merkl.ts @@ -83,6 +83,9 @@ export type WrapperType = WrapperTypeMapping[T]; export enum BlacklistWrapper { Blacklist = 3, } +export enum WhitelistWrapper { + Whitelist = 0, +} export enum AMMAlgorithmType { UniswapV3 = 0, diff --git a/src/types/utils.ts b/src/types/utils.ts index 4a2c4f5..30a58d9 100644 --- a/src/types/utils.ts +++ b/src/types/utils.ts @@ -1,10 +1,19 @@ import { BigNumber, utils } from 'ethers'; +import { Interface } from 'ethers/lib/utils'; import request, { gql } from 'graphql-request'; import invariant from 'tiny-invariant'; -import { calculatorUsedWrappersList, merklSubgraphAMMEndpoints } from '../constants'; +import { + AlgebraV19NonFungibleManager__factory, + AlgebraV19Pool__factory, + BaseXNonFungiblePositionManager__factory, + calculatorUsedWrappersList, + merklSubgraphAMMEndpoints, + UniswapV3NFTManager__factory, + UniswapV3Pool__factory, +} from '../constants'; import { SOLIDITY_TYPE_MAXIMA, SolidityType } from './constants'; -import { AMMType, MerklSupportedChainIdsType } from './merkl'; +import { AMMAlgorithmType, AMMType, MerklSupportedChainIdsType } from './merkl'; export function validateSolidityTypeInstance(value: BigNumber, solidityType: SolidityType): void { // invariant(value.gte(0), `${value} is not a ${solidityType}.`); @@ -107,3 +116,42 @@ export enum BorrowActionType { getDebtIn, permit, } + +/** + * NonFungiblePositionManager + */ +export const NonFungiblePositionManagerInterface = (ammType: AMMAlgorithmType): Interface => { + if (ammType === AMMAlgorithmType.AlgebraV1_9) { + return AlgebraV19NonFungibleManager__factory.createInterface(); + } else if (ammType === AMMAlgorithmType.UniswapV3) { + return UniswapV3NFTManager__factory.createInterface(); + } else if (ammType === AMMAlgorithmType.BaseX) { + return BaseXNonFungiblePositionManager__factory.createInterface(); + } else { + throw new Error('Invalid AMM type'); + } +}; + +/** + * Pools + */ +export const PoolInterface = (ammType: AMMAlgorithmType): Interface => { + if (ammType === AMMAlgorithmType.AlgebraV1_9) { + return AlgebraV19Pool__factory.createInterface(); + } else if (ammType === AMMAlgorithmType.UniswapV3 || ammType === AMMAlgorithmType.BaseX) { + return UniswapV3Pool__factory.createInterface(); + } else { + throw new Error('Invalid AMM type'); + } +}; + +export const SqrtPrice = { + [AMMAlgorithmType.AlgebraV1_9]: 'price', + [AMMAlgorithmType.UniswapV3]: 'sqrtPriceX96', + [AMMAlgorithmType.BaseX]: 'sqrtPriceX96', +}; +export const PoolState = { + [AMMAlgorithmType.AlgebraV1_9]: 'globalState', + [AMMAlgorithmType.UniswapV3]: 'slot0', + [AMMAlgorithmType.BaseX]: 'slot0', +}; diff --git a/src/utils/merkl.ts b/src/utils/merkl.ts index 97a95d3..3a7ddc7 100644 --- a/src/utils/merkl.ts +++ b/src/utils/merkl.ts @@ -1,58 +1,18 @@ import { BigNumber, ethers, utils } from 'ethers'; -import { Interface } from 'ethers/lib/utils'; import keccak256 from 'keccak256'; import MerkleTree from 'merkletreejs'; -import { - AlgebraV19NonFungibleManager__factory, - AlgebraV19Pool__factory, - BaseXNonFungiblePositionManager__factory, - UniswapV3NFTManager__factory, - UniswapV3Pool__factory, -} from '../constants/types'; import { ExtensiveDistributionParametersStructOutput } from '../constants/types/DistributionCreator'; -import { AggregatedRewardsType, AMMAlgorithmType, AMMType, MerklSupportedChainIdsType, UnderlyingTreeType } from '../types'; +import { + AggregatedRewardsType, + AMMType, + BlacklistWrapper, + MerklSupportedChainIdsType, + UnderlyingTreeType, + WhitelistWrapper, +} from '../types'; import { fetchMerklAMMType } from '../types/utils'; -/** - * NonFungiblePositionManager - */ -export const NonFungiblePositionManagerInterface = (ammType: AMMAlgorithmType): Interface => { - if (ammType === AMMAlgorithmType.AlgebraV1_9) { - return AlgebraV19NonFungibleManager__factory.createInterface(); - } else if (ammType === AMMAlgorithmType.UniswapV3) { - return UniswapV3NFTManager__factory.createInterface(); - } else if (ammType === AMMAlgorithmType.BaseX) { - return BaseXNonFungiblePositionManager__factory.createInterface(); - } else { - throw new Error('Invalid AMM type'); - } -}; - -/** - * Pools - */ -export const PoolInterface = (ammType: AMMAlgorithmType): Interface => { - if (ammType === AMMAlgorithmType.AlgebraV1_9) { - return AlgebraV19Pool__factory.createInterface(); - } else if (ammType === AMMAlgorithmType.UniswapV3 || ammType === AMMAlgorithmType.BaseX) { - return UniswapV3Pool__factory.createInterface(); - } else { - throw new Error('Invalid AMM type'); - } -}; - -export const SwapPriceField = { - [AMMAlgorithmType.AlgebraV1_9]: 'price', - [AMMAlgorithmType.UniswapV3]: 'sqrtPriceX96', - [AMMAlgorithmType.BaseX]: 'sqrtPriceX96', -}; -export const PoolStateName = { - [AMMAlgorithmType.AlgebraV1_9]: 'globalState', - [AMMAlgorithmType.UniswapV3]: 'slot0', - [AMMAlgorithmType.BaseX]: 'slot0', -}; - /** * @param underylingTreeData * @@ -147,3 +107,37 @@ export const buildPoolList = async ( return pools; }; + +export const getBlacklist = (wrapperList: string[], wrapperType: number[]): string[] => { + const blacklist: string[] = []; + if (!wrapperList || wrapperList.length !== wrapperType.length) return blacklist; + for (let k = 0; k < wrapperType.length; k++) { + if (wrapperType[k] === BlacklistWrapper.Blacklist) { + blacklist.push(wrapperList[k]); + } + } + return blacklist; +}; + +export const isBlacklisted = (user: string, wrapperList: string[], wrapperType: number[]): boolean => { + const pos = wrapperList.indexOf(utils.getAddress(user)); + if (pos === -1) return false; + return wrapperType[pos] === BlacklistWrapper.Blacklist; +}; + +export const getWhitelist = (wrapperList: string[], wrapperType: number[]): string[] => { + const whitelist: string[] = []; + if (!wrapperList || wrapperList.length !== wrapperType.length) return whitelist; + for (let k = 0; k < wrapperType.length; k++) { + if (wrapperType[k] === WhitelistWrapper.Whitelist) { + whitelist.push(wrapperList[k]); + } + } + return whitelist; +}; + +export const isWhitelisted = (user: string, whitelist: string[]): boolean => { + const pos = whitelist.indexOf(utils.getAddress(user)); + if (pos === -1) return false; + return true; +};