Skip to content

Commit

Permalink
Close position UI, fix for NotEnoughAllowance (#57)
Browse files Browse the repository at this point in the history
* Adjust allowance to be at least $1 and 150%

* Wait a bit

* Fetch fresh debt when closing position to get accurate allowance

* No need to wait

* Bump timeouts

* Dont auto-close transaction errors

* Record test videos

* Lint harder
  • Loading branch information
noisekit authored Nov 8, 2024
1 parent 95e5973 commit 427848a
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 20 deletions.
1 change: 1 addition & 0 deletions liquidity/components/BorrowModal/BorrowModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export const BorrowModal: React.FC<{
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
throw Error('Borrow failed', { cause: error });
}
Expand Down
1 change: 1 addition & 0 deletions liquidity/components/ClaimModal/ClaimModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export const ClaimModal: React.FC<{
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
throw Error(isBorrow ? 'Borrow' : 'Claim' + ' failed', { cause: error });
}
Expand Down
3 changes: 3 additions & 0 deletions liquidity/components/DepositModal/DepositModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export const DepositModal: DepositModalProps = ({ onClose, isOpen, title, liquid
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
throw Error('Wrapping failed', { cause: error });
}
Expand Down Expand Up @@ -376,6 +377,7 @@ export const DepositModal: DepositModalProps = ({ onClose, isOpen, title, liquid
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
throw Error('Approve failed', { cause: error });
}
Expand Down Expand Up @@ -454,6 +456,7 @@ export const DepositModal: DepositModalProps = ({ onClose, isOpen, title, liquid
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
throw Error('Lock collateral failed', { cause: error });
}
Expand Down
2 changes: 2 additions & 0 deletions liquidity/components/RepayModal/RepayModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export const RepayModal: React.FC<{
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
throw Error('Approve failed', { cause: error });
}
Expand Down Expand Up @@ -262,6 +263,7 @@ export const RepayModal: React.FC<{
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
throw Error('Repay failed', { cause: error });
}
Expand Down
1 change: 1 addition & 0 deletions liquidity/components/UndelegateModal/UndelegateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export const UndelegateModal: UndelegateModalProps = ({ onClose, isOpen, liquidi
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
throw Error('Unlock collateral failed', { cause: error });
}
Expand Down
1 change: 1 addition & 0 deletions liquidity/components/WithdrawModal/WithdrawModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export function WithdrawModal({
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
throw Error('Withdraw failed', { cause: error });
}
Expand Down
8 changes: 5 additions & 3 deletions liquidity/cypress/cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ module.exports = defineConfig({
});
return config;
},
video: true,

retries: {
runMode: 1,
openMode: 0,
},
defaultCommandTimeout: 60_000,
execTimeout: 60_000,
taskTimeout: 60_000,

defaultCommandTimeout: 120_000,
execTimeout: 120_000,
taskTimeout: 120_000,
},
});
5 changes: 3 additions & 2 deletions liquidity/cypress/cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ beforeEach(() => {

cy.on('log:added', onLogAdded);

cy.intercept('https://analytics.synthetix.io/matomo.js', { statusCode: 204 }).as('matomo');
cy.intercept('https://analytics.synthetix.io/matomo.js', { log: false });
cy.intercept('https://analytics.synthetix.io/matomo.js', { statusCode: 204, log: false }).as(
'matomo'
);
cy.intercept('https://cloudflare-eth.com/**', { statusCode: 400, log: false }).as(
'cloudflare-eth'
);
Expand Down
1 change: 1 addition & 0 deletions liquidity/lib/useClaimAllRewards/useClaimAllRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export function useClaimAllRewards(
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
}
},
Expand Down
42 changes: 40 additions & 2 deletions liquidity/ui/src/components/ClosePosition/ClosePositionOneStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import debug from 'debug';
import { ethers } from 'ethers';
import React from 'react';
import { LiquidityPositionUpdated } from '../Manage/LiquidityPositionUpdated';
import { fetchPositionDebt } from './fetchPositionDebt';
import { fetchPositionDebtWithPriceUpdate } from './fetchPositionDebtWithPriceUpdate';
import { fetchPriceUpdateTxn } from './fetchPriceUpdateTxn';
import { useAccountCollateral } from './useAccountCollateral';
import { usePositionDebt } from './usePositionDebt';

Expand Down Expand Up @@ -123,11 +126,33 @@ export function ClosePositionOneStep({
signer
);

const adjustedAllowance = positionDebt.lt(0)
const freshPriceUpdateTxn = await fetchPriceUpdateTxn({ PythVerfier, pythFeeds });
log('freshPriceUpdateTxn: %O', freshPriceUpdateTxn);

const freshPositionDebt = freshPriceUpdateTxn.value
? await fetchPositionDebtWithPriceUpdate({
provider,
CoreProxy,
Multicall3,
accountId: params.accountId,
poolId: params.poolId,
collateralTypeTokenAddress: collateralType.tokenAddress,
priceUpdateTxn: freshPriceUpdateTxn,
})
: await fetchPositionDebt({
provider,
CoreProxy,
accountId: params.accountId,
poolId: params.poolId,
collateralTypeTokenAddress: collateralType.tokenAddress,
});
log('freshPositionDebt: %O', freshPositionDebt);

const adjustedAllowance = freshPositionDebt.lt(1)
? // For the case when debt fluctuates from negative/zero to slightly positive
ethers.utils.parseEther('1.00')
: // Add extra buffer for debt fluctuations
positionDebt.mul(110).div(100);
freshPositionDebt.mul(120).div(100);
log('adjustedAllowance: %O', adjustedAllowance);

// "function approve(address to, uint256 tokenId)",
Expand Down Expand Up @@ -198,6 +223,19 @@ export function ClosePositionOneStep({
queryKey: [`${network?.id}-${network?.preset}`, 'AccountCollateralUnlockDate'],
});

queryClient.invalidateQueries({
queryKey: [network?.id, network?.preset, 'PriceUpdateTxn'],
});
queryClient.invalidateQueries({
queryKey: [network?.id, network?.preset, 'PositionDebt'],
});
queryClient.invalidateQueries({
queryKey: [network?.id, network?.preset, 'AccountCollateral'],
});
queryClient.invalidateQueries({
queryKey: [network?.id, network?.preset, 'AccountAvailableCollateral'],
});

// After mutation withERC7412, we guaranteed to have updated all the prices, dont care about await
refetchPriceUpdateTx();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ export const ClosePositionTransactions: FC<{
'Please try again.'
),
status: 'error',
duration: 3_600_000,
});
throw Error('Transaction failed', { cause: error });
}
Expand Down
22 changes: 22 additions & 0 deletions liquidity/ui/src/components/ClosePosition/fetchPriceUpdateTxn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { EvmPriceServiceConnection } from '@pythnetwork/pyth-evm-js';
import { ethers } from 'ethers';
import { offchainMainnetEndpoint } from '@snx-v3/constants';

export async function fetchPriceUpdateTxn({
PythVerfier,
pythFeeds,
}: {
PythVerfier: { address: string; abi: string[] };
pythFeeds: string[];
}) {
const PythVerifierInterface = new ethers.utils.Interface(PythVerfier.abi);
const priceService = new EvmPriceServiceConnection(offchainMainnetEndpoint);
const signedOffchainData = await priceService.getPriceFeedsUpdateData(pythFeeds);
const priceUpdateTxn = {
target: PythVerfier.address,
callData: PythVerifierInterface.encodeFunctionData('updatePriceFeeds', [signedOffchainData]),
value: ethers.BigNumber.from(pythFeeds.length),
requireSuccess: false,
};
return priceUpdateTxn;
}
15 changes: 2 additions & 13 deletions liquidity/ui/src/components/ClosePosition/usePriceUpdateTxn.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { EvmPriceServiceConnection } from '@pythnetwork/pyth-evm-js';
import { offchainMainnetEndpoint } from '@snx-v3/constants';
import { useProvider } from '@snx-v3/useBlockchain';
import { usePythFeeds } from '@snx-v3/usePythFeeds';
import { usePythVerifier } from '@snx-v3/usePythVerifier';
import { useQuery } from '@tanstack/react-query';
import debug from 'debug';
import { ethers } from 'ethers';
import { fetchPriceUpdateTxn } from './fetchPriceUpdateTxn';
import { useErrorParser } from './useErrorParser';
import { useSynthetix } from './useSynthetix';

Expand All @@ -31,17 +30,7 @@ export function usePriceUpdateTxn() {
throw 'OMFG';
}
log({ chainId, preset, priceIds: pythFeeds, PythVerfier });
const PythVerifierInterface = new ethers.utils.Interface(PythVerfier.abi);
const priceService = new EvmPriceServiceConnection(offchainMainnetEndpoint);
const signedOffchainData = await priceService.getPriceFeedsUpdateData(pythFeeds);
const priceUpdateTxn = {
target: PythVerfier.address,
callData: PythVerifierInterface.encodeFunctionData('updatePriceFeeds', [
signedOffchainData,
]),
value: ethers.BigNumber.from(pythFeeds.length),
requireSuccess: false,
};
const priceUpdateTxn = await fetchPriceUpdateTxn({ pythFeeds, PythVerfier });
log('priceUpdateTxn: %O', priceUpdateTxn);
return priceUpdateTxn;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export function MigrateUSDTransaction({
),
status: 'error',
variant: 'left-accent',
duration: 3_600_000,
});
}
}, [
Expand Down

0 comments on commit 427848a

Please sign in to comment.