Skip to content

Commit

Permalink
fix contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
timongll committed Aug 31, 2023
1 parent e35e6f2 commit 924a6dd
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 90 deletions.
10 changes: 6 additions & 4 deletions shared/src/hooks/useERC20Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useWeb3Context } from "./web3Context";
import { IERC20__factory } from "../codegen/factories/IERC20__factory";
import { IERC20 } from "../codegen";
import { getERC20TokenAddress } from "../constants/constants";
import useWeb3Wallet from "./useWeb3Wallet";

export const getERC20Token = (
library: any,
Expand All @@ -21,15 +22,16 @@ export const getERC20Token = (
};

const useERC20Token = (token: ERC20Token) => {
const { active, chainId, library } = useWeb3React();
const { provider } = useWeb3Context();
const {chainId, provider } = useWeb3React();
const { active } = useWeb3Wallet();
const { provider: defaultProvider } = useWeb3Context();
const [contract, setContract] = useState<IERC20>();

useEffect(() => {
if (provider && chainId) {
setContract(getERC20Token(library || provider, token, chainId, active));
setContract(getERC20Token(provider || defaultProvider, token, chainId, active));
}
}, [chainId, provider, active, library, token]);
}, [chainId, provider, active, token, defaultProvider]);

return contract;
};
Expand Down
9 changes: 5 additions & 4 deletions shared/src/hooks/useFetchEarnVaultData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import { useEVMWeb3Context } from "./useEVMWeb3Context";
import { isVaultSupportedOnChain } from "../utils/vault";
import { RibbonEarnVault } from "../codegen";
import { calculatePricePerShare } from "../utils/math";
import useWeb3Wallet from "./useWeb3Wallet";

const useFetchEarnVaultData = (): V2VaultData => {
const {
chainId,
active: web3Active,
account: web3Account,
library,
provider,
} = useWeb3React();
const { active: web3Active } = useWeb3Wallet();
const account = impersonateAddress ? impersonateAddress : web3Account;
const { transactionsCounter } = usePendingTransactions();
const { getProviderForNetwork } = useEVMWeb3Context();
Expand Down Expand Up @@ -64,7 +65,7 @@ const useFetchEarnVaultData = (): V2VaultData => {
);

const contract = getVaultContract(
library || inferredProviderFromVault,
active ? provider : inferredProviderFromVault,
vault,
active
) as RibbonEarnVault;
Expand Down Expand Up @@ -244,7 +245,7 @@ const useFetchEarnVaultData = (): V2VaultData => {
if (!isProduction()) {
console.timeEnd("V2 Vault Data Fetch"); // eslint-disable-line
}
}, [account, chainId, library, web3Active, getProviderForNetwork]);
}, [getProviderForNetwork, web3Active, chainId, provider, account]);

useEffect(() => {
doMulticall();
Expand Down
12 changes: 7 additions & 5 deletions shared/src/hooks/useFetchLiquidityMiningData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import { getERC20Token } from "./useERC20Token";
import { getStakingReward } from "./useStakingReward";
import { usePendingTransactions } from "./pendingTransactionsContext";
import { getERC20TokenNameFromVault } from "../models/eth";
import useWeb3Wallet from "./useWeb3Wallet";

const useFetchLiquidityMiningData = (): LiquidityMiningPoolData => {
const { active, chainId, account: web3Account, library } = useWeb3React();
const { provider } = useWeb3Context();
const { chainId, account: web3Account, provider } = useWeb3React();
const { active } = useWeb3Wallet();
const { provider: defaultProvider } = useWeb3Context();
const account = impersonateAddress ? impersonateAddress : web3Account;
const { transactionsCounter } = usePendingTransactions();

Expand Down Expand Up @@ -52,15 +54,15 @@ const useFetchLiquidityMiningData = (): LiquidityMiningPoolData => {
const responses = await Promise.all(
StakingVaultList.map(async (vault) => {
const tokenContract = getERC20Token(
library || provider,
provider || defaultProvider,
getERC20TokenNameFromVault(vault, "v1"),
chainId,
active
);
if (!tokenContract) {
return { vault };
}
const contract = getStakingReward(library || provider, vault, active);
const contract = getStakingReward(provider || defaultProvider, vault, active);
if (!contract || !VaultLiquidityMiningMap.lm[vault]) {
return { vault };
}
Expand Down Expand Up @@ -161,7 +163,7 @@ const useFetchLiquidityMiningData = (): LiquidityMiningPoolData => {
if (!isProduction()) {
console.timeEnd("Liquidity Mining Pool Data Fetch"); // eslint-disable-line
}
}, [account, active, chainId, library, provider]);
}, [account, active, chainId, defaultProvider, provider]);

useEffect(() => {
doMulticall();
Expand Down
6 changes: 3 additions & 3 deletions shared/src/hooks/useFetchPoolData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { getLendContract } from "./getLendContract";
import { PoolAddressMap, PoolList } from "../constants/lendConstants";

export const useFetchPoolData = () => {
const { library } = useWeb3React();
const { provider } = useWeb3React();
const [data, setData] = useState<any>();
const [loading, setLoading] = useState<boolean>(true);
const doMulticall = useCallback(async () => {
const responses = await Promise.all(
PoolList.map(async (pool) => {
const contract = getLendContract(
library,
provider,
PoolAddressMap[pool].lend,
false
);
Expand Down Expand Up @@ -58,7 +58,7 @@ export const useFetchPoolData = () => {
);
setData(mapping);
setLoading(false);
}, [library]);
}, [provider]);

useEffect(() => {
doMulticall();
Expand Down
11 changes: 6 additions & 5 deletions shared/src/hooks/useFetchVaultData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import {
import { isProduction } from "../utils/env";
import { usePendingTransactions } from "./pendingTransactionsContext";
import { isVaultSupportedOnChain } from "../utils/vault";
import useWeb3Wallet from "./useWeb3Wallet";

const useFetchVaultData = (): VaultData => {
const {
chainId,
library,
active: web3Active,
provider,
account: web3Account,
} = useWeb3React();
const { provider } = useWeb3Context();
const { active: web3Active } = useWeb3Wallet();
const { provider: defaultProvider } = useWeb3Context();
const account = impersonateAddress ? impersonateAddress : web3Account;
const { transactionsCounter } = usePendingTransactions();

Expand All @@ -49,7 +50,7 @@ const useFetchVaultData = (): VaultData => {
const active = Boolean(
web3Active && isVaultSupportedOnChain(vault, chainId || 1)
);
const contract = getVault(library || provider, vault, active);
const contract = getVault(provider || defaultProvider, vault, active);

if (!contract) {
return { vault };
Expand Down Expand Up @@ -146,7 +147,7 @@ const useFetchVaultData = (): VaultData => {
if (!isProduction()) {
console.timeEnd("V1 Vault Data Fetch"); // eslint-disable-line
}
}, [account, web3Active, library, provider, chainId]);
}, [web3Active, chainId, provider, defaultProvider, account]);

useEffect(() => {
doMulticall();
Expand Down
9 changes: 5 additions & 4 deletions shared/src/hooks/useLendLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import { PoolAddressMap, PoolList } from "../constants/lendConstants";
export const useLendLink = () => {
const [hasLendPosition, setHasLendPosition] = useState(false);
const { account } = useWeb3Wallet();
const { active, library } = useWeb3React();
const { provider } = useWeb3Context();
const { provider } = useWeb3React();
const { active } = useWeb3Wallet();
const { provider: defaultProvider } = useWeb3Context();

useEffect(() => {
if (!account) {
setHasLendPosition(false);
}
PoolList.forEach((pool) => {
const poolContract = getLendContract(
library || provider,
provider || defaultProvider,
PoolAddressMap[pool].lend,
active
);
Expand All @@ -32,7 +33,7 @@ export const useLendLink = () => {
});
}
});
}, [account, active, library, provider]);
}, [account, active, defaultProvider, provider]);

return hasLendPosition ? URLS.lendApp : URLS.lend;
};
10 changes: 7 additions & 3 deletions shared/src/hooks/useLidoCurvePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useCallback, useEffect, useState } from "react";
import { CurveLidoPool, CurveLidoPool__factory } from "../codegen";
import { CurveLidoPoolAddress } from "../constants/constants";
import { amountAfterSlippage } from "../utils/math";
import useWeb3Wallet from "./useWeb3Wallet";
import { useWeb3Context } from "./web3Context";

export const getLidoCurvePool = (library: any) => {
if (library) {
Expand All @@ -15,13 +17,15 @@ export const getLidoCurvePool = (library: any) => {
};

const useLidoCurvePool = () => {
const { library, active } = useWeb3React();
const { provider } = useWeb3React();
const { active } = useWeb3Wallet();
const { provider: defaultProvider } = useWeb3Context();
const [lidoCurvePool, setLidoCurvePool] = useState<CurveLidoPool>();

useEffect(() => {
const pool = getLidoCurvePool(library);
const pool = getLidoCurvePool(provider || defaultProvider);
setLidoCurvePool(pool);
}, [active, library]);
}, [active, defaultProvider, provider]);

// Get the exchange rate for converting ETH -> stETH
// amountETH is eth in 18 decimals
Expand Down
10 changes: 6 additions & 4 deletions shared/src/hooks/useLiquidityGaugeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LiquidityGaugeController } from "../codegen/LiquidityGaugeController";
import { LiquidityGaugeControllerFactory } from "../codegen/LiquidityGaugeControllerFactory";
import { LiquidityGaugeControllerAddress } from "../constants/constants";
import { useWeb3Context } from "./web3Context";
import useWeb3Wallet from "./useWeb3Wallet";

export const getLiquidityGaugeController = (
library: any,
Expand All @@ -19,17 +20,18 @@ export const getLiquidityGaugeController = (
};

const useLiquidityGaugeController = () => {
const { active, library } = useWeb3React();
const { provider } = useWeb3Context();
const { provider } = useWeb3React();
const { active } = useWeb3Wallet();
const { provider: defaultProvider } = useWeb3Context();
const [contract, setContract] = useState<LiquidityGaugeController | null>(
null
);

useEffect(() => {
if (provider) {
setContract(getLiquidityGaugeController(library || provider, active));
setContract(getLiquidityGaugeController(provider || defaultProvider, active));
}
}, [provider, active, library]);
}, [provider, active, defaultProvider]);

return contract;
};
Expand Down
12 changes: 7 additions & 5 deletions shared/src/hooks/useLiquidityGaugeV5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { useEffect, useState } from "react";
import { LiquidityGaugeV5 } from "../codegen";
import { LiquidityGaugeV5Factory } from "../codegen/LiquidityGaugeV5Factory";
import { VaultLiquidityMiningMap, VaultOptions } from "../constants/constants";
import { useETHWeb3Context } from "./web3Context";
import { useETHWeb3Context, useWeb3Context } from "./web3Context";
import useWeb3Wallet from "./useWeb3Wallet";

export const getLiquidityGaugeV5 = (
library: any,
Expand All @@ -24,20 +25,21 @@ export const getLiquidityGaugeV5 = (
};

const useLiquidityGaugeV5 = (vaultOption: VaultOptions) => {
const { active, library } = useWeb3React();
const { provider } = useETHWeb3Context();
const { provider } = useWeb3React();
const { active } = useWeb3Wallet();
const { provider: defaultProvider } = useWeb3Context();
const [contract, setContract] = useState<LiquidityGaugeV5 | null>(null);

useEffect(() => {
if (provider) {
const vault = getLiquidityGaugeV5(
library || provider,
provider || defaultProvider,
vaultOption,
active
);
setContract(vault);
}
}, [provider, active, library, vaultOption]);
}, [provider, active, vaultOption, defaultProvider]);

return contract;
};
Expand Down
12 changes: 7 additions & 5 deletions shared/src/hooks/useLiquidityTokenMinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
isEthNetwork,
LiquidityTokenMinterAddress,
} from "../constants/constants";
import { useETHWeb3Context } from "./web3Context";
import { useETHWeb3Context, useWeb3Context } from "./web3Context";
import useWeb3Wallet from "./useWeb3Wallet";

export const getLiquidityTokenMinter = (
library: any,
Expand All @@ -22,18 +23,19 @@ export const getLiquidityTokenMinter = (
};

const useLiquidityTokenMinter = () => {
const { active, chainId, library } = useWeb3React();
const { provider } = useETHWeb3Context();
const { chainId, provider } = useWeb3React();
const { active } = useWeb3Wallet();
const { provider: defaultProvider } = useWeb3Context();
const [contract, setContract] = useState<LiquidityTokenMinter | null>(null);

useEffect(() => {
if (active && chainId && isEthNetwork(chainId)) {
setContract(getLiquidityTokenMinter(library, active));
setContract(getLiquidityTokenMinter(provider || defaultProvider, active));
return;
}

setContract(getLiquidityTokenMinter(provider, false));
}, [provider, chainId, active, library]);
}, [provider, chainId, active, defaultProvider]);

return contract;
};
Expand Down
34 changes: 19 additions & 15 deletions shared/src/hooks/usePausedPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEffect, useMemo, useState } from "react";
import { RibbonVaultPauser } from "../codegen";
import {
isBinanceVault,
isEarnVault,
isSolanaVault,
VaultAddressMap,
VaultOptions,
Expand Down Expand Up @@ -43,25 +44,28 @@ export const usePausedPosition = (
account &&
!isVIP() &&
!isBinanceVault(vaultOption) &&
!isSolanaVault(vaultOption)
!isSolanaVault(vaultOption) &&
!isEarnVault(vaultOption)
) {
pauseContract
.getPausePosition(vaultAddress, account)
.then(([pauseRound, pauseAmount]) => {
setPausedShares(pauseAmount);
setCanResume(pauseRound !== 0 && pauseRound < round); // edge case round returns 0
setCanPause(
isPracticallyZero(pauseAmount, decimals) &&
!isPracticallyZero(lockedBalanceInAsset, decimals)
);
if (pauseRound === round) {
v2Contract.pricePerShare().then((val) => {
setRoundPricePerShare(val);
});
} else {
v2Contract.roundPricePerShare(pauseRound).then((val) => {
setRoundPricePerShare(val);
});
if (pauseRound !== 0) {
setPausedShares(pauseAmount);
setCanResume(pauseRound !== 0 && pauseRound < round); // edge case round returns 0
setCanPause(
isPracticallyZero(pauseAmount, decimals) &&
!isPracticallyZero(lockedBalanceInAsset, decimals)
);
if (pauseRound === round) {
v2Contract.pricePerShare().then((val) => {
setRoundPricePerShare(val);
});
} else {
v2Contract.roundPricePerShare(pauseRound).then((val) => {
setRoundPricePerShare(val);
});
}
}
});
}
Expand Down
Loading

0 comments on commit 924a6dd

Please sign in to comment.