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

feat: add wl #271

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/types/merkl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export type WrapperType<T extends AMMType> = WrapperTypeMapping[T];
export enum BlacklistWrapper {
Blacklist = 3,
}
export enum WhitelistWrapper {
Whitelist = 0,
}

export enum AMMAlgorithmType {
UniswapV3 = 0,
Expand Down
52 changes: 50 additions & 2 deletions src/types/utils.ts
Original file line number Diff line number Diff line change
@@ -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}.`);
Expand Down Expand Up @@ -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',
};
90 changes: 42 additions & 48 deletions src/utils/merkl.ts
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -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;
};