Skip to content
Open
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
12 changes: 7 additions & 5 deletions src/hooks/actionHooks/useAddCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useAddCollateral = () => {
const { wrapAsset } = useWrapUnwrapAsset();
const { addEth } = useAddRemoveEth();

const {isActionAllowed} = useAllowAction();
const { isActionAllowed } = useAllowAction();

const { refetch: refetchBaseBal } = useBalance({
address: account,
Expand All @@ -48,7 +48,8 @@ export const useAddCollateral = () => {

const addCollateral = async (vault: IVault | undefined, input: string) => {
if (!contracts) return;
if (!isActionAllowed(ActionCodes.ADD_COLLATERAL)) return; // return if action is not allowed
if (!selectedSeries) return console.error('No series selected');
if (!isActionAllowed(ActionCodes.ADD_COLLATERAL, selectedSeries)) return; // return if action is not allowed

/* use the vault id provided OR 0 if new/ not provided */
const vaultId = vault?.id || BLANK_VAULT;
Expand Down Expand Up @@ -143,9 +144,10 @@ export const useAddCollateral = () => {
];

/* TRANSACT */
// await transact(calls, txCode);
toast.warn('Transactions via the UI have been paused due to a reported issue. All funds are safe. Please follow our Twitter account for more information.')

// await transact(calls, txCode);
toast.warn(
'Transactions via the UI have been paused due to a reported issue. All funds are safe. Please follow our Twitter account for more information.'
);

/* then update UI */
refetchBaseBal();
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/actionHooks/useAddLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const useAddLiquidity = () => {
historyActions: { updateStrategyHistory },
} = useContext(HistoryContext);

const {isActionAllowed} = useAllowAction();
const { isActionAllowed } = useAllowAction();

const { addEth } = useAddRemoveEth();
const { getTimeTillMaturity } = useTimeTillMaturity();
Expand All @@ -71,7 +71,8 @@ export const useAddLiquidity = () => {
matchingVault: IVault | undefined = undefined
) => {
if (!contracts) return;
if (!isActionAllowed(ActionCodes.ADD_LIQUIDITY)) return; // return if action is not allowed
if (!strategy.currentSeries) return console.error('No current series for strategy', strategy.id);
if (!isActionAllowed(ActionCodes.ADD_LIQUIDITY, strategy.currentSeries)) return; // return if action is not allowed

const txCode = getTxCode(ActionCodes.ADD_LIQUIDITY, strategy.id);

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/actionHooks/useBorrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export const useBorrow = () => {
const { sign, transact } = useChain();
const { getTimeTillMaturity } = useTimeTillMaturity();

const {isActionAllowed} = useAllowAction();
const { isActionAllowed } = useAllowAction();

const borrow = async (vault: IVault | undefined, input: string | undefined, collInput: string | undefined) => {
if (!contracts) return;
if (!isActionAllowed(ActionCodes.BORROW)) return; // return if action is not allowed

if (!selectedSeries) return console.error('No series selected');
if (!isActionAllowed(ActionCodes.BORROW, selectedSeries)) return; // return if action is not allowed

/* generate the reproducible txCode for tx tracking and tracing */
const txCode = getTxCode(ActionCodes.BORROW, selectedSeries?.id!);
Expand Down
13 changes: 5 additions & 8 deletions src/hooks/actionHooks/useClaimRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import { formatUnits } from 'ethers/lib/utils';
import { useCallback, useContext, useEffect, useState } from 'react';
import { TxContext } from '../../contexts/TxContext';
import { UserContext } from '../../contexts/UserContext';
import { ActionCodes, IAsset, IStrategy} from '../../types';
import { ActionCodes, IAsset, IStrategy } from '../../types';

import { useSigner, useAccount } from 'wagmi';
import useAccountPlus from '../useAccountPlus';
import useAllowAction from '../useAllowAction';

const useClaimRewards = (strategy: IStrategy | undefined) => {

const { data: signer, isError, isLoading } = useSigner();
const { address:account } = useAccountPlus();
const {isActionAllowed} = useAllowAction();
const { address: account } = useAccountPlus();
const { isActionAllowed } = useAllowAction();

const { userState, userActions } = useContext(UserContext);
const { assetMap } = userState;
Expand All @@ -27,13 +26,11 @@ const useClaimRewards = (strategy: IStrategy | undefined) => {
const [rewardsToken, setRewardsToken] = useState<IAsset>();

const claimRewards = async () => {

if (!account) throw new Error('no account detected when claiming rewards');
if (!signer) throw new Error('no signer detected when claiming rewards');
if (!strategy) throw new Error('no strategy detected when claiming rewards');

if (!isActionAllowed(ActionCodes.CLAIM_REWARDS)) return; // return if action is not allowed

if (!strategy.currentSeries) return console.error('No series selected');
if (!isActionAllowed(ActionCodes.CLAIM_REWARDS, strategy.currentSeries)) return; // return if action is not allowed

const claim = async () => await strategy.strategyContract.connect(signer).claim(account);

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/actionHooks/useClosePosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const useClosePosition = () => {
getValuesFromNetwork: boolean = true // get market values by network call or offline calc (default: NETWORK)
) => {
if (!contracts) return;
if (!isActionAllowed(ActionCodes.CLOSE_POSITION)) return; // return if action is not allowed
if (!isActionAllowed(ActionCodes.CLOSE_POSITION, series)) return; // return if action is not allowed

const txCode = getTxCode(ActionCodes.CLOSE_POSITION, series.id);
const base = assetMap?.get(series.baseId)!;
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/actionHooks/useLend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export const useLend = () => {

const lend = async (input: string | undefined, series: ISeries) => {
if (!contracts) return;
if (!isActionAllowed(ActionCodes.LEND)) return; // return if action is not allowed

if (!isActionAllowed(ActionCodes.LEND, series)) return; // return if action is not allowed

/* generate the reproducible txCode for tx tracking and tracing */
const txCode = getTxCode(ActionCodes.LEND, series.id);
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/actionHooks/useRemoveCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import useAllowAction from '../useAllowAction';

export const useRemoveCollateral = () => {
const { userState, userActions } = useContext(UserContext);
const { selectedIlk, assetMap } = userState;
const { selectedIlk, assetMap, selectedSeries } = userState;
const { address: account } = useAccountPlus();
const { chain } = useNetwork();
const provider = useProvider();
Expand All @@ -40,8 +40,8 @@ export const useRemoveCollateral = () => {

const removeCollateral = async (vault: IVault, input: string, unwrapOnRemove: boolean = true) => {
if (!contracts) return;
if (!isActionAllowed(ActionCodes.REMOVE_COLLATERAL)) return; // return if action is not allowed

if (!selectedSeries) return console.error('No selected series');
if (!isActionAllowed(ActionCodes.REMOVE_COLLATERAL, selectedSeries)) return; // return if action is not allowed

/* generate the txCode for tx tracking and tracing */
const txCode = getTxCode(ActionCodes.REMOVE_COLLATERAL, vault.id);
Expand Down
38 changes: 19 additions & 19 deletions src/hooks/actionHooks/useRepayDebt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const useRepayDebt = () => {
const { getTimeTillMaturity, isMature } = useTimeTillMaturity();
const chainId = useChainId();

const {isActionAllowed} = useAllowAction();
const { isActionAllowed } = useAllowAction();

/**
* REPAY FN
Expand All @@ -56,17 +56,17 @@ export const useRepayDebt = () => {
* @param reclaimCollateral
*/
const repay = async (vault: IVault, input: string | undefined, reclaimCollateral: boolean) => {

if (!contracts) return;

const txCode = getTxCode(ActionCodes.REPAY, vault.id);

const ladleAddress = contracts.get(ContractNames.LADLE)?.address;
const series: ISeries = seriesMap?.get(vault.seriesId)!;
const series = seriesMap?.get(vault.seriesId);
if (!series) return console.error('Series not found');
const base: IAsset = assetMap?.get(vault.baseId)!;
const ilk: IAsset = assetMap?.get(vault.ilkId)!;

if (!isActionAllowed(ActionCodes.REPAY, series )) return; // return if action is not allowed
if (!isActionAllowed(ActionCodes.REPAY, series)) return; // return if action is not allowed

const isEthCollateral = ETH_BASED_ASSETS.includes(vault.ilkId);
const isEthBase = ETH_BASED_ASSETS.includes(series.baseId);
Expand All @@ -79,22 +79,23 @@ export const useRepayDebt = () => {
const cleanInput = cleanValue(input, base.decimals);
const _input = input ? ethers.utils.parseUnits(cleanInput, base.decimals) : ethers.constants.Zero;

const _maxSharesIn = series.sharesReserves.eq(ZERO_BN) ? ZERO_BN
: maxBaseIn(
series.sharesReserves,
series.fyTokenReserves,
getTimeTillMaturity(series.maturity),
series.ts,
series.g1,
series.decimals,
series.c,
series.mu
);
const _maxSharesIn = series.sharesReserves.eq(ZERO_BN)
? ZERO_BN
: maxBaseIn(
series.sharesReserves,
series.fyTokenReserves,
getTimeTillMaturity(series.maturity),
series.ts,
series.g1,
series.decimals,
series.c,
series.mu
);

/* Check if the trade of that size is possible */
const tradeIsNotPossible = series.getShares(_input).gt(_maxSharesIn);
const tradeIsNotPossible = series.getShares(_input).gt(_maxSharesIn);

diagnostics && tradeIsNotPossible ? console.log('Trade is not possible:'): console.log('Trade is possible:')
diagnostics && tradeIsNotPossible ? console.log('Trade is not possible:') : console.log('Trade is possible:');
diagnostics && tradeIsNotPossible && console.log('Trade input', _input.toString());
diagnostics && tradeIsNotPossible && console.log('TradeMax base in:', _maxSharesIn.toString());

Expand Down Expand Up @@ -221,7 +222,6 @@ export const useRepayDebt = () => {

...removeEthCallData,
...unwrapAssetCallData,

];
await transact(calls, txCode);

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/actionHooks/useRollDebt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export const useRollDebt = () => {
const { isActionAllowed } = useAllowAction();

const rollDebt = async (vault: IVault, toSeries: ISeries) => {

if (!isActionAllowed(ActionCodes.ROLL_DEBT)) return; // return if action is not allowed
const fromSeries = seriesMap?.get(vault.seriesId);
if (!fromSeries) return console.error('No series selected');
if (!isActionAllowed(ActionCodes.ROLL_DEBT, fromSeries)) return; // return if action is not allowed

const txCode = getTxCode(ActionCodes.ROLL_DEBT, vault.id);
const base = assetMap?.get(vault.baseId);
const hasDebt = vault.accruedArt.gt(ZERO_BN);
const fromSeries = seriesMap?.get(vault.seriesId);

const calls: ICallData[] = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/actionHooks/useRollPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const useRollPosition = () => {
*/
const rollPosition = async (input: string | undefined, fromSeries: ISeries, toSeries: ISeries) => {
if (!contracts) return;
if (!isActionAllowed(ActionCodes.ROLL_POSITION)) return; // return if action is not allowed
if (!isActionAllowed(ActionCodes.ROLL_POSITION, fromSeries)) return; // return if action is not allowed

/* generate the reproducible txCode for tx tracking and tracing */
const txCode = getTxCode(ActionCodes.ROLL_POSITION, fromSeries.id);
Expand Down
16 changes: 4 additions & 12 deletions src/hooks/useAllowAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@ import { ActionCodes, ISeries } from '../types';
* @returns boolean
*/
const useAllowAction = () => {
const { userState } = useContext(UserContext);
const isActionAllowed = (action: ActionCodes, series: ISeries) => {
if (series.allowActions.includes('allow_all') || series.allowActions.includes(action)) return true;

const isActionAllowed = (action: ActionCodes, series?: ISeries): boolean => {
const seriesToUse = series || userState.selectedSeries;

if (seriesToUse) {
if (seriesToUse.allowActions.includes('allow_all') || seriesToUse.allowActions.includes(action)) {return true}
else {
toast.warn(`Action temporarily not allowed on this series.`);
return false;
}
}
return false; // deny action by default if conditions are not met
toast.warn(`Action temporarily not allowed on this series.`);
return false;
};

return { isActionAllowed };
Expand Down