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

Get uniproxy address from hypervisor. #878

Merged
merged 8 commits into from
Jul 7, 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
577 changes: 577 additions & 0 deletions src/constants/abis/gamma-clearing.json

Large diffs are not rendered by default.

376 changes: 311 additions & 65 deletions src/constants/abis/gamma-uniproxy.json

Large diffs are not rendered by default.

138 changes: 138 additions & 0 deletions src/constants/abis/gamma-uniproxy1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
[
{
"inputs": [
{
"internalType": "address",
"name": "_clearance",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "clearance",
"outputs": [
{
"internalType": "contract IClearing",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "deposit0",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "deposit1",
"type": "uint256"
},
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "address",
"name": "pos",
"type": "address"
},
{
"internalType": "uint256[4]",
"name": "minIn",
"type": "uint256[4]"
}
],
"name": "deposit",
"outputs": [
{
"internalType": "uint256",
"name": "shares",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "pos",
"type": "address"
},
{
"internalType": "address",
"name": "token",
"type": "address"
},
{
"internalType": "uint256",
"name": "_deposit",
"type": "uint256"
}
],
"name": "getDepositAmount",
"outputs": [
{
"internalType": "uint256",
"name": "amountStart",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amountEnd",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "newClearance",
"type": "address"
}
],
"name": "transferClearance",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
5 changes: 0 additions & 5 deletions src/constants/v3/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ export const NONFUNGIBLE_POSITION_MANAGER_ADDRESSES: AddressMap = {
[ChainId.ZKEVM]: '0xd8E1E7009802c914b0d39B31Fc1759A865b727B1',
};

export const GAMMA_UNIPROXY_ADDRESSES: AddressMap = {
[ChainId.MATIC]: '0xe0A61107E250f8B5B24bf272baBFCf638569830C',
[ChainId.ZKEVM]: '0x66CD859053c458688044d816117D5Bdf42A56813',
};

export const GAMMA_MASTERCHEF_ADDRESSES: AddressMap[] = [
{
[ChainId.MATIC]: '0x20ec0d06f447d550fc6edee42121bc8c1817b97d',
Expand Down
29 changes: 24 additions & 5 deletions src/hooks/useContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { abi as IUniswapV2Router02ABI } from '@uniswap/v2-periphery/build/IUnisw
import QUICKConversionABI from 'constants/abis/quick-conversion.json';
import {
GAMMA_MASTERCHEF_ADDRESSES,
GAMMA_UNIPROXY_ADDRESSES,
MULTICALL_ADDRESS,
NONFUNGIBLE_POSITION_MANAGER_ADDRESSES,
QUOTER_ADDRESSES,
Expand All @@ -45,8 +44,10 @@ import NewQuoterABI from 'constants/abis/v3/quoter.json';
import MULTICALL2_ABI from 'constants/abis/v3/multicall.json';
import NFTPosMan from 'constants/abis/v3/nft-pos-man.json';
import GammaUniProxy from 'constants/abis/gamma-uniproxy.json';
import GammaUniProxy1 from 'constants/abis/gamma-uniproxy1.json';
import GammaMasterChef from 'constants/abis/gamma-masterchef.json';
import GammaPairABI from 'constants/abis/gamma-hypervisor.json';
import { useSingleCallResult } from 'state/multicall/v3/hooks';

export function useContract<T extends Contract = Contract>(
addressOrAddressMap: string | { [chainId: number]: string } | undefined,
Expand Down Expand Up @@ -295,10 +296,28 @@ export function useV3NFTPositionManagerContract(
);
}

export function useGammaUNIProxyContract(withSignerIfPossible?: boolean) {
export function useGammaUNIProxyContract(
pairAddress?: string,
withSignerIfPossible?: boolean,
) {
const hypervisorContract = useGammaHypervisorContract(pairAddress);
const uniProxyResult = useSingleCallResult(
hypervisorContract,
'whitelistedAddress',
);
const uniProxyAddress =
!uniProxyResult.loading &&
uniProxyResult.result &&
uniProxyResult.result.length > 0
? uniProxyResult.result[0]
: undefined;
return useContract(
GAMMA_UNIPROXY_ADDRESSES,
GammaUniProxy,
uniProxyAddress,
uniProxyAddress &&
uniProxyAddress.toLowerCase() ===
'0xa42d55074869491d60ac05490376b74cf19b00e6'
? GammaUniProxy1
: GammaUniProxy,
withSignerIfPossible,
);
}
Expand All @@ -324,7 +343,7 @@ export function useMasterChefContracts(withSignerIfPossible?: boolean) {
}

export function useGammaHypervisorContract(
address: string,
address?: string,
withSignerIfPossible?: boolean,
) {
return useContract(address, GammaPairABI, withSignerIfPossible);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const AddGammaLiquidity: React.FC<{
return approvalB !== ApprovalState.APPROVED;
}, [approvalB]);

const gammaUNIPROXYContract = useGammaUNIProxyContract();
const gammaUNIPROXYContract = useGammaUNIProxyContract(gammaPairAddress);
const gammaTokens = token0 && token1 ? [token0, token1] : [];
const depositAmountsData = useSingleContractMultipleData(
gammaPairAddress && gammaTokens.length > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ETHER, JSBI, WETH } from '@uniswap/sdk';
import { formatUnits, parseUnits } from 'ethers/lib/utils';
import { useGammaUNIProxyContract, useWETHContract } from 'hooks/useContract';
import { TransactionResponse } from '@ethersproject/abstract-provider';
import { useSingleContractMultipleData } from 'state/multicall/v3/hooks';
import { useSingleCallResult } from 'state/multicall/v3/hooks';
import { useCurrencyBalance } from 'state/wallet/hooks';

interface IncreaseGammaLiquidityModalProps {
Expand All @@ -39,75 +39,46 @@ export default function IncreaseGammaLiquidityModal({
const { t } = useTranslation();
const { chainId, account } = useActiveWeb3React();
const [isBaseInput, setIsBaseInput] = useState(true);
const gammaUNIPROXYContract = useGammaUNIProxyContract();

const tokens = [position.token0, position.token1];
const gammaUNIPROXYContract = useGammaUNIProxyContract(position.pairAddress);
const [deposit0, setDeposit0] = useState('');
const [deposit1, setDeposit1] = useState('');

const depositAmountsData = useSingleContractMultipleData(
const independentToken = isBaseInput ? position.token0 : position.token1;
const dependentToken = isBaseInput ? position.token1 : position.token0;
const independentDeposit = isBaseInput
? Number(deposit0).toFixed(position.token0.decimals)
: Number(deposit1).toFixed(position.token1.decimals);
const depositAmountsData = useSingleCallResult(
gammaUNIPROXYContract,
'getDepositAmount',
tokens.map((token) => [
[
position.pairAddress,
token.address,
parseUnits('1', token.decimals),
]),
independentToken.address,
parseUnits(independentDeposit, independentToken.decimals),
],
);

const depositRange = useMemo(() => {
if (depositAmountsData.length < 1) return;
const baseDepositMin =
!depositAmountsData[1].loading &&
depositAmountsData[1].result &&
depositAmountsData[1].result.length > 1
? Number(
formatUnits(
depositAmountsData[1].result[0],
position.token0.decimals,
),
)
: 0;
const baseDepositMax =
!depositAmountsData[1].loading &&
depositAmountsData[1].result &&
depositAmountsData[1].result.length > 1
const depositMin =
!depositAmountsData.loading &&
depositAmountsData.result &&
depositAmountsData.result.length > 1
? Number(
formatUnits(
depositAmountsData[1].result[1],
position.token0.decimals,
),
formatUnits(depositAmountsData.result[0], dependentToken.decimals),
)
: 0;
const quoteDepositMin =
!depositAmountsData[0].loading &&
depositAmountsData[0].result &&
depositAmountsData[0].result.length > 1
const depositMax =
!depositAmountsData.loading &&
depositAmountsData.result &&
depositAmountsData.result.length > 1
? Number(
formatUnits(
depositAmountsData[0].result[0],
position.token1.decimals,
),
)
: 0;
const quoteDepositMax =
!depositAmountsData[0].loading &&
depositAmountsData[0].result &&
depositAmountsData[0].result.length > 1
? Number(
formatUnits(
depositAmountsData[0].result[1],
position.token1.decimals,
),
formatUnits(depositAmountsData.result[1], dependentToken.decimals),
)
: 0;

return {
base: { min: baseDepositMin, max: baseDepositMax },
quote: { min: quoteDepositMin, max: quoteDepositMax },
};
}, [depositAmountsData, position.token0.decimals, position.token1.decimals]);
return { min: depositMin, max: depositMax };
}, [depositAmountsData, dependentToken.decimals]);

const [deposit0, setDeposit0] = useState('');
const [deposit1, setDeposit1] = useState('');
const ethBalance = useCurrencyBalance(
account ?? undefined,
chainId ? ETHER[chainId] : undefined,
Expand Down Expand Up @@ -321,18 +292,14 @@ export default function IncreaseGammaLiquidityModal({
if (depositRange) {
if (isBaseInput) {
if (Number(deposit0) > 0) {
const quoteDeposit =
((depositRange.quote.min + depositRange.quote.max) / 2) *
Number(deposit0);
const quoteDeposit = (depositRange.min + depositRange.max) / 2;
setDeposit1(quoteDeposit.toFixed(position.token1.decimals));
} else {
setDeposit1('');
}
} else {
if (Number(deposit1) > 0) {
const baseDeposit =
((depositRange.base.min + depositRange.base.max) / 2) *
Number(deposit1);
const baseDeposit = (depositRange.min + depositRange.max) / 2;
setDeposit0(baseDeposit.toFixed(position.token0.decimals));
} else {
setDeposit0('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ export function PresetRanges({

if (isStablecoinPair)
return [
{
type: Presets.FULL,
title: t('fullRange'),
min: 0,
max: Infinity,
risk: PresetProfits.VERY_LOW,
profit: PresetProfits.VERY_LOW,
},
{
type: Presets.STABLE,
title: t('stablecoins'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useActiveWeb3React } from 'hooks';
import { useIsExpertMode, useUserSlippageTolerance } from 'state/user/hooks';
import { NonfungiblePositionManager as NonFunPosMan } from 'v3lib/nonfungiblePositionManager';
import { Percent, Currency } from '@uniswap/sdk-core';
import { useAppDispatch, useAppSelector } from 'state/hooks';
import { useAppDispatch } from 'state/hooks';
import {
useTransactionAdder,
useTransactionFinalizer,
Expand Down Expand Up @@ -74,7 +74,7 @@ export function AddLiquidityButton({
const preset = useActivePreset();

const positionManager = useV3NFTPositionManagerContract();
const gammaUNIPROXYContract = useGammaUNIProxyContract();

const wethContract = useWETHContract();

const deadline = useTransactionDeadline();
Expand Down Expand Up @@ -111,6 +111,7 @@ export function AddLiquidityButton({
gammaPair && gammaPair.length > 0
? gammaPair.find((pair) => pair.type === preset)?.address
: undefined;
const gammaUNIPROXYContract = useGammaUNIProxyContract(gammaPairAddress);
const amountA = mintInfo.parsedAmounts[Field.CURRENCY_A];
const amountB = mintInfo.parsedAmounts[Field.CURRENCY_B];
const wmaticBalance = useCurrencyBalance(
Expand Down Expand Up @@ -291,6 +292,7 @@ export function AddLiquidityButton({
setRejected && setRejected(false);

setAttemptingTxn(true);

try {
const estimatedGas = await gammaUNIPROXYContract.estimateGas.deposit(
(GammaPairs[chainId][baseCurrencyAddress + '-' + quoteCurrencyAddress]
Expand Down
Loading