Skip to content

Commit

Permalink
Merge pull request #1612 from 0xoscario/algebra
Browse files Browse the repository at this point in the history
feat: add algebra position
  • Loading branch information
sameepsi authored Jan 7, 2025
2 parents 691ef43 + 1759aa9 commit df51918
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 27 deletions.
9 changes: 8 additions & 1 deletion src/components/v3/IncreaseLiquidityV3/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ApprovalState, useApproveCallback } from 'hooks/useV3ApproveCallback';
import {
NONFUNGIBLE_POSITION_MANAGER_ADDRESSES,
UNI_NFT_POSITION_MANAGER_ADDRESS,
ALGEBRA_INTEGRAL_NONFUNGIBLE_POSITION_MANAGER_ADDRESSES,
} from 'constants/v3/addresses';
import { useUSDCValue } from 'hooks/v3/useUSDCPrice';
import CurrencyInputPanel from 'components/v3/CurrencyInputPanel';
Expand All @@ -50,6 +51,7 @@ import { unwrappedToken } from 'utils/unwrappedToken';
import { useWeb3Modal } from '@web3modal/ethers5/react';
import { TransactionType } from 'models/enums';
import { wrappedCurrency } from 'utils/wrappedCurrency';
import { GlobalConst } from 'constants/index';

interface IncreaseLiquidityV3Props {
positionDetails: PositionPool;
Expand Down Expand Up @@ -110,6 +112,7 @@ export default function IncreaseLiquidityV3({
depositBDisabled,
ticksAtLimit,
feeTier,
liquidityRangeType,
} = useV3DerivedMintInfo(
baseCurrency ?? undefined,
quoteCurrency ?? undefined,
Expand All @@ -120,9 +123,13 @@ export default function IncreaseLiquidityV3({
const positionManagerAddress = useMemo(() => {
if (positionDetails.isUni) {
return UNI_NFT_POSITION_MANAGER_ADDRESS[chainId];
} else if (
liquidityRangeType === GlobalConst.v3LiquidityRangeType.ALGEBRA_INTEGRAL
) {
return ALGEBRA_INTEGRAL_NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[chainId];
}
return NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[chainId];
}, [chainId, positionDetails]);
}, [chainId, positionDetails, liquidityRangeType]);

const { onFieldAInput, onFieldBInput } = useV3MintActionHandlers(noLiquidity);

Expand Down
6 changes: 5 additions & 1 deletion src/config/layerx.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@
"ichi": {
"available": false
},
"algebraIntegral": {
"available": true
},
"lpLock": {
"available": false
},
"maxChunks": 20,
"blocksPerFetch": 25,
"poolInitCodeHash": "0xbce37a54eab2fcd71913a0d40723e04238970e7fc1159bfd58ad5b79531697e7"
"poolInitCodeHash": "0xbce37a54eab2fcd71913a0d40723e04238970e7fc1159bfd58ad5b79531697e7",
"algebraIntegralPoolInitCodeHash": "0x4b9e4a8044ce5695e06fce9421a63b6f5c3db8a561eebb30ea4c775469e36eaf"
}
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export const GlobalConst = {
UNIPILOT_RANGE: '2',
DEFIEDGE_RANGE: '3',
STEER_RANGE: '4',
ALGEBRA_INTEGRAL: '5',
},
walletName: {
METAMASK: 'Metamask',
Expand Down
7 changes: 7 additions & 0 deletions src/constants/v3/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ export const POOL_DEPLOYER_ADDRESS: AddressMap = {
[ChainId.ZKEVM]: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
[ChainId.LAYERX]: '0x56c2162254b0E4417288786eE402c2B41d4e181e',
};
export const ALGEBRA_INTEGRAL_POOL_DEPLOYER_ADDRESS: AddressMap = {
[ChainId.LAYERX]: '0x9815e9311a13E5b0DC93E6255b8B45Cd8b6c9773',
};

export const QUOTER_ADDRESSES: AddressMap = {
[ChainId.MATIC]: '0xa15F0D7377B2A0C0c10db057f641beD21028FC89',
Expand Down Expand Up @@ -182,6 +185,10 @@ export const SWAP_ROUTER_ADDRESS: AddressMap = {
[ChainId.MATIC]: '0xfaa746afc5ff7d5ef0aa469bb26ddd6cd8f13911',
};

export const ALGEBRA_INTEGRAL_NONFUNGIBLE_POSITION_MANAGER_ADDRESSES: AddressMap = {
[ChainId.LAYERX]: '0x60cdF877e536F6384D8D5aA20c266A8Ad5AE0a4c',
};

export const NONFUNGIBLE_POSITION_MANAGER_ADDRESSES: AddressMap = {
[ChainId.MATIC]: '0x8eF88E4c7CfbbaC1C163f7eddd4B578792201de6',
[ChainId.DOGECHAIN]: '0x0b012055F770AE7BB7a8303968A7Fb6088A2296e',
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/v3/usePools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
POOL_DEPLOYER_ADDRESS,
UNI_V3_FACTORY_ADDRESS,
ALGEBRA_INTEGRAL_POOL_DEPLOYER_ADDRESS,
} from 'constants/v3/addresses';
import { Currency, Token } from '@uniswap/sdk-core';
import { useMemo } from 'react';
Expand Down Expand Up @@ -32,6 +33,7 @@ export function usePools(
FeeAmount | undefined,
][],
isUni?: boolean,
isAlgebraIntegral?: boolean,
): [PoolState, Pool | null][] {
const { chainId } = useActiveWeb3React();

Expand All @@ -57,6 +59,8 @@ export function usePools(
const poolDeployerAddress =
chainId && value && value[2]
? UNI_V3_FACTORY_ADDRESS[chainId]
: isAlgebraIntegral
? ALGEBRA_INTEGRAL_POOL_DEPLOYER_ADDRESS[chainId]
: POOL_DEPLOYER_ADDRESS[chainId];
if (!poolDeployerAddress || !value) return undefined;

Expand All @@ -67,7 +71,7 @@ export function usePools(
fee: value[2],
});
});
}, [chainId, transformed]);
}, [chainId, transformed, isAlgebraIntegral]);

const globalState0s = useMultipleContractSingleData(
poolAddresses,
Expand Down Expand Up @@ -173,6 +177,7 @@ export function usePool(
currencyB: Currency | undefined,
feeAmount?: FeeAmount,
isUni?: boolean,
isAlgebraIntegral?: boolean,
): [PoolState, Pool | null] {
const poolKeys: [
Currency | undefined,
Expand All @@ -184,7 +189,7 @@ export function usePool(
feeAmount,
]);

return usePools(poolKeys, isUni)[0];
return usePools(poolKeys, isUni, isAlgebraIntegral)[0];
}

export function useTokensSymbols(token0: string, token1: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface IPresetRanges {
isDefiedge?: boolean;
isSteer?: boolean;
steerPairs?: SteerVault[];
isAlgebraIntegral?: boolean;
}

enum PresetProfits {
Expand All @@ -86,6 +87,7 @@ export function PresetRanges({
isDefiedge = false,
defiedgeStrategies,
isSteer = false,
isAlgebraIntegral = false,
steerPairs,
}: IPresetRanges) {
const { chainId } = useActiveWeb3React();
Expand Down Expand Up @@ -317,6 +319,10 @@ export function PresetRanges({
: [];
}

// if (isAlgebraIntegral) {
// return [];
// }

if (isStablecoinPair)
return [
{
Expand Down Expand Up @@ -377,6 +383,7 @@ export function PresetRanges({
isDefiedge,
isSteer,
isStablecoinPair,
isAlgebraIntegral,
t,
gammaPair,
gammaValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { CurrencyAmount } from '@uniswap/sdk-core';
import {
NONFUNGIBLE_POSITION_MANAGER_ADDRESSES,
UNI_NFT_POSITION_MANAGER_ADDRESS,
ALGEBRA_INTEGRAL_NONFUNGIBLE_POSITION_MANAGER_ADDRESSES,
} from 'constants/v3/addresses';
import {
calculateGasMargin,
Expand Down Expand Up @@ -106,9 +107,14 @@ export function AddLiquidityButton({
const positionManagerAddress = useMemo(() => {
if (mintInfo.feeTier && mintInfo.feeTier.id.includes('uni')) {
return UNI_NFT_POSITION_MANAGER_ADDRESS[chainId];
} else if (
mintInfo.liquidityRangeType ===
GlobalConst.v3LiquidityRangeType.ALGEBRA_INTEGRAL
) {
return ALGEBRA_INTEGRAL_NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[chainId];
}
return NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[chainId];
}, [chainId, mintInfo.feeTier]);
}, [chainId, mintInfo.feeTier, mintInfo.liquidityRangeType]);
const wethContract = useWETHContract();

const deadline = useTransactionDeadline();
Expand Down Expand Up @@ -721,6 +727,138 @@ export function AddLiquidityButton({
setTxPending(false);
setAddLiquidityErrorMessage(t('errorInTx'));
}
} else if (
mintInfo.liquidityRangeType ===
GlobalConst.v3LiquidityRangeType.ALGEBRA_INTEGRAL
) {
if (mintInfo.position && account && deadline) {
if (!positionManager) return;
const useNative = baseCurrency.isNative
? baseCurrency
: quoteCurrency.isNative
? quoteCurrency
: undefined;

let callParams;
if (mintInfo.feeTier && mintInfo.feeTier.id.includes('uni')) {
callParams = UniV3NonFunPosMan.addCallParameters(mintInfo.position, {
slippageTolerance: allowedSlippagePercent,
recipient: account,
deadline: deadline.toString(),
useNative,
createPool: mintInfo.noLiquidity,
});
} else if (
mintInfo.liquidityRangeType ===
GlobalConst.v3LiquidityRangeType.ALGEBRA_INTEGRAL
) {
callParams = NonFunPosMan.addCallParameters(mintInfo.position, {
slippageTolerance: allowedSlippagePercent,
recipient: account,
deadline: deadline.toString(),
useNative,
createPool: mintInfo.noLiquidity,
});
} else {
callParams = NonFunPosMan.addCallParameters(mintInfo.position, {
slippageTolerance: allowedSlippagePercent,
recipient: account,
deadline: deadline.toString(),
useNative,
createPool: mintInfo.noLiquidity,
});
}
const { calldata, value } = callParams;

const txn: { to: string; data: string; value: string } = {
to: positionManagerAddress,
data: calldata,
value,
};

setRejected && setRejected(false);

setAttemptingTxn(true);

library
.getSigner()
.estimateGas(txn)
.then((estimate) => {
const newTxn = {
...txn,
gasLimit: calculateGasMarginV3(chainId, estimate),
};

return library
.getSigner()
.sendTransaction(newTxn)
.then(async (response: TransactionResponse) => {
setAttemptingTxn(false);
setTxPending(true);
const summary = mintInfo.noLiquidity
? t('createPoolandaddLiquidity', {
symbolA: baseCurrency?.symbol,
symbolB: quoteCurrency?.symbol,
})
: t('addLiquidityWithTokens', {
symbolA: baseCurrency?.symbol,
symbolB: quoteCurrency?.symbol,
});
addTransaction(response, {
summary,
type: TransactionType.ADDED_LIQUIDITY,
tokens: [
((baseCurrency as unknown) as any)?.address ??
wrappedCurrency(Token.ETHER[chainId], chainId),
((quoteCurrency as unknown) as any)?.address ??
wrappedCurrency(Token.ETHER[chainId], chainId),
],
});

dispatch(setAddLiquidityTxHash({ txHash: response.hash }));

try {
const receipt = await response.wait();
finalizedTransaction(receipt, {
summary,
});
setTxPending(false);
handleAddLiquidity();
} catch (error) {
console.error('Failed to send transaction', error);
setTxPending(false);
setAddLiquidityErrorMessage(
error?.code === 'ACTION_REJECTED'
? t('txRejected')
: t('errorInTx'),
);
}
})
.catch((err) => {
console.error('Failed to send transaction', err);
setAttemptingTxn(false);
setAddLiquidityErrorMessage(
err?.code === 'ACTION_REJECTED'
? t('txRejected')
: t('errorInTx'),
);
});
})
.catch((error) => {
console.error('Failed to send transaction', error);
// we only care if the error is something _other_ than the user rejected the tx
setRejected && setRejected(true);
setAttemptingTxn(false);
setAddLiquidityErrorMessage(
error?.code === 'ACTION_REJECTED'
? t('txRejected')
: t('errorInTx'),
);
if (error?.code !== 'ACTION_REJECTED') {
console.error(error);
}
});
}
} else {
if (mintInfo.position && account && deadline) {
if (!positionManager) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useUSDCValue } from 'hooks/v3/useUSDCPrice';
import {
NONFUNGIBLE_POSITION_MANAGER_ADDRESSES,
UNI_NFT_POSITION_MANAGER_ADDRESS,
ALGEBRA_INTEGRAL_NONFUNGIBLE_POSITION_MANAGER_ADDRESSES,
} from 'constants/v3/addresses';
import { halfAmountSpend, maxAmountSpend } from 'utils/v3/maxAmountSpend';
import { tryParseAmount } from 'state/swap/v3/hooks';
Expand Down Expand Up @@ -128,13 +129,17 @@ export function EnterAmounts({
isWithNative && currencyB && currencyB.isNative
? currencyB.wrapped
: currencyB;

const positionManagerAddress = useMemo(() => {
if (mintInfo.feeTier && mintInfo.feeTier.id.includes('uni')) {
return UNI_NFT_POSITION_MANAGER_ADDRESS[chainId];
} else if (
mintInfo.liquidityRangeType ===
GlobalConst.v3LiquidityRangeType.ALGEBRA_INTEGRAL
) {
return ALGEBRA_INTEGRAL_NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[chainId];
}
return NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[chainId];
}, [chainId, mintInfo.feeTier]);
}, [chainId, mintInfo.feeTier, mintInfo.liquidityRangeType]);

const steerPeripheryContract = useSteerPeripheryContract();
const [approvalA, approveACallback] = useApproveCallback(
Expand All @@ -153,6 +158,9 @@ export function EnterAmounts({
: mintInfo.liquidityRangeType ===
GlobalConst.v3LiquidityRangeType.STEER_RANGE
? steerPeripheryContract?.address
: mintInfo.liquidityRangeType ===
GlobalConst.v3LiquidityRangeType.ALGEBRA_INTEGRAL
? ALGEBRA_INTEGRAL_NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[chainId]
: positionManagerAddress
: undefined,
);
Expand All @@ -172,6 +180,9 @@ export function EnterAmounts({
: mintInfo.liquidityRangeType ===
GlobalConst.v3LiquidityRangeType.STEER_RANGE
? steerPeripheryContract?.address
: mintInfo.liquidityRangeType ===
GlobalConst.v3LiquidityRangeType.ALGEBRA_INTEGRAL
? ALGEBRA_INTEGRAL_NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[chainId]
: positionManagerAddress
: undefined,
);
Expand Down
Loading

0 comments on commit df51918

Please sign in to comment.