Skip to content

Commit

Permalink
Close Position and Debt repayment on Base (#68)
Browse files Browse the repository at this point in the history
* Run e2e tests in parallel

* Close Position and debt repayment on Base

* New block, longer timeouts, fix Deposit

* Use Amount with suffix

* Add clearDebt and use it before delegating collateral

* Split USDC_Unlock_negative_debt and USDC_Unlock_positive_debt, rename tests

* remove extra deposit to a position with debt.

* XL

* Unlock actually works well on this block

* Limit anvil memory

---------

Co-authored-by: Noisekit <[email protected]>
  • Loading branch information
peiman3 and noisekit authored Nov 28, 2024
1 parent deaac78 commit f7baf82
Show file tree
Hide file tree
Showing 44 changed files with 372 additions and 233 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ jobs:
docker:
- image: cimg/node:<< pipeline.parameters.node-version >>
resource_class: xlarge
parallelism: 3
steps:
- checkout
- install-foundry
Expand Down Expand Up @@ -171,7 +172,7 @@ jobs:
echo "0" > /tmp/cypress_exit_code
export NODE_ENV=test
export CYPRESS_INFURA_KEY=$INFURA_KEY
yarn cypress run --e2e || echo $? > /tmp/cypress_exit_code
circleci tests glob 'cypress/e2e/**/*.e2e.js' | circleci tests run --verbose --split-by=timings --command="xargs yarn cypress run --e2e --spec" || echo $? > /tmp/cypress_exit_code
- save_cache:
key: *anvil-fork-cache
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ and all transactions will be automatically signed, without any popups
```sh
# Mainnets
anvil --auto-impersonate --chain-id 1 --fork-url wss://mainnet.infura.io/ws/v3/$INFURA_KEY --no-rate-limit --steps-tracing --fork-block-number 21233424
anvil --auto-impersonate --chain-id 8453 --fork-url wss://base-mainnet.infura.io/ws/v3/$INFURA_KEY --no-rate-limit --steps-tracing --fork-block-number 22683522
anvil --auto-impersonate --chain-id 8453 --fork-url wss://base-mainnet.infura.io/ws/v3/$INFURA_KEY --no-rate-limit --steps-tracing --fork-block-number 22946353
anvil --auto-impersonate --chain-id 42161 --fork-url wss://arbitrum-mainnet.infura.io/ws/v3/$INFURA_KEY --no-rate-limit --steps-tracing --fork-block-number 271813668
# Testnets
Expand Down
21 changes: 20 additions & 1 deletion liquidity/components/UndelegateModal/UndelegateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { Button, Divider, Link, Text, useToast } from '@chakra-ui/react';
import { Amount } from '@snx-v3/Amount';
import { ZEROWEI } from '@snx-v3/constants';
import { ContractError } from '@snx-v3/ContractError';
import { currency } from '@snx-v3/format';
import { currency, parseUnits } from '@snx-v3/format';
import { isBaseAndromeda } from '@snx-v3/isBaseAndromeda';
import { ManagePositionContext } from '@snx-v3/ManagePositionContext';
import { Multistep } from '@snx-v3/Multistep';
import { useApprove } from '@snx-v3/useApprove';
import { useNetwork } from '@snx-v3/useBlockchain';
import { CollateralType, useCollateralType } from '@snx-v3/useCollateralTypes';
import { useContractErrorParser } from '@snx-v3/useContractErrorParser';
import { useDebtRepayer } from '@snx-v3/useDebtRepayer';
import { LiquidityPosition } from '@snx-v3/useLiquidityPosition';
import { useParams } from '@snx-v3/useParams';
import { useUndelegate } from '@snx-v3/useUndelegate';
import { useUndelegateBaseAndromeda } from '@snx-v3/useUndelegateBaseAndromeda';
import { useUSDC } from '@snx-v3/useUSDC';
import { Wei, wei } from '@synthetixio/wei';
import { useQueryClient } from '@tanstack/react-query';
import { useMachine } from '@xstate/react';
Expand Down Expand Up @@ -143,6 +146,19 @@ export const UndelegateModal: UndelegateModalProps = ({ onClose, isOpen, liquidi
collateralChange,
currentCollateral: currentCollateral,
});

const debtExists = liquidityPosition?.debt.gt(0);
const currentDebt = debtExists && liquidityPosition ? liquidityPosition.debt : wei(0);
const { data: USDC } = useUSDC();
const { data: DebtRepayer } = useDebtRepayer();

const { approve, requireApproval } = useApprove({
contractAddress: USDC?.address,
//slippage for approval
amount: debtExists ? parseUnits(currentDebt.toString(), 6).mul(120).div(100) : 0,
spender: DebtRepayer?.address,
});

const { exec: undelegateBaseAndromeda } = useUndelegateBaseAndromeda({
accountId: params.accountId,
poolId: params.poolId,
Expand All @@ -169,6 +185,9 @@ export const UndelegateModal: UndelegateModalProps = ({ onClose, isOpen, liquidi
});

if (isBase) {
if (requireApproval) {
await approve(false);
}
await undelegateBaseAndromeda();
} else {
await execUndelegate();
Expand Down
3 changes: 3 additions & 0 deletions liquidity/components/UndelegateModal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
"@snx-v3/constants": "workspace:*",
"@snx-v3/format": "workspace:*",
"@snx-v3/isBaseAndromeda": "workspace:*",
"@snx-v3/useApprove": "workspace:*",
"@snx-v3/useBlockchain": "workspace:*",
"@snx-v3/useCollateralTypes": "workspace:*",
"@snx-v3/useContractErrorParser": "workspace:*",
"@snx-v3/useDebtRepayer": "workspace:*",
"@snx-v3/useLiquidityPosition": "workspace:*",
"@snx-v3/useParams": "workspace:*",
"@snx-v3/useUSDC": "workspace:*",
"@snx-v3/useUndelegate": "workspace:*",
"@snx-v3/useUndelegateBaseAndromeda": "workspace:*",
"@synthetixio/wei": "^2.74.4",
Expand Down
7 changes: 1 addition & 6 deletions liquidity/components/WithdrawModal/WithdrawModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ export const WithdrawModalUi: FC<{
<Multistep
step={1}
title="Withdraw"
subtitle={
<Text as="div">
<Amount value={amount} />
&nbsp;{symbol} will be withdrawn
</Text>
}
subtitle={<Amount value={amount} suffix={` ${symbol} will be withdrawn`} />}
status={{
failed: state.step === 1 && state.status === 'error',
success: state.step > 1,
Expand Down
12 changes: 12 additions & 0 deletions liquidity/cypress/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ declare global {
poolId: number;
}) => Promise<void>;

clearDebt: ({
address,
accountId,
symbol,
poolId,
}: {
address?: string;
accountId?: string;
symbol: string;
poolId: number;
}) => Promise<void>;

delegateCollateral: ({
address,
accountId,
Expand Down
4 changes: 2 additions & 2 deletions liquidity/cypress/cypress/e2e/1-main/Create_Account.e2e.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Create Account', () => {
describe(__filename, () => {
Cypress.env('chainId', '1');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -21,7 +21,7 @@ describe('Create Account', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });

cy.visit('/#/dashboard');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Dashboard - Connected', () => {
describe(__filename, () => {
Cypress.env('chainId', '1');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -21,7 +21,7 @@ describe('Dashboard - Connected', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });

cy.visit('/#/dashboard');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Dashboard - Not Connected', () => {
describe(__filename, () => {
Cypress.env('chainId', '1');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -13,7 +13,7 @@ describe('Dashboard - Not Connected', () => {
});
});

it('works', () => {
it(__filename, () => {
cy.visit('/#/dashboard');

cy.contains('h2', 'Dashboard').should('exist');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Manage SNX Position - Borrow', () => {
describe(__filename, () => {
Cypress.env('chainId', '1');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -21,7 +21,7 @@ describe('Manage SNX Position - Borrow', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });
cy.approveCollateral({ symbol: 'SNX', spender: 'CoreProxy' });
cy.getSNX({ amount: 2000 });
Expand All @@ -30,7 +30,7 @@ describe('Manage SNX Position - Borrow', () => {

cy.visit(`/#/positions/SNX/1?manageAction=claim&accountId=${Cypress.env('accountId')}`);

cy.get('[data-cy="claim form"]').should('exist');
cy.get('[data-cy="claim form"]', { timeout: 180_000 }).should('exist');
cy.contains('[data-status="info"]', 'You can take an interest-free loan up to').should('exist');

cy.get('[data-cy="claim amount input"]').should('exist');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Manage SNX Position - Deposit', () => {
describe(__filename, () => {
Cypress.env('chainId', '1');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -21,13 +21,13 @@ describe('Manage SNX Position - Deposit', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });
cy.getSNX({ amount: 500 });

cy.visit(`/#/positions/SNX/1?manageAction=deposit&accountId=${Cypress.env('accountId')}`);

cy.get('[data-cy="deposit and lock collateral form"]').should('exist');
cy.get('[data-cy="deposit and lock collateral form"]', { timeout: 180_000 }).should('exist');
cy.get('[data-cy="balance amount"]').should('exist').and('include.text', 'Max');

cy.get('[data-cy="deposit amount input"]').type('101');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Manage SNX Position - Unlock', () => {
describe(__filename, () => {
Cypress.env('chainId', '1');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -21,7 +21,7 @@ describe('Manage SNX Position - Unlock', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });
cy.getSNX({ amount: 2000 });
cy.approveCollateral({ symbol: 'SNX', spender: 'CoreProxy' });
Expand All @@ -30,7 +30,7 @@ describe('Manage SNX Position - Unlock', () => {

cy.visit(`/#/positions/SNX/1?manageAction=undelegate&accountId=${Cypress.env('accountId')}`);

cy.get('[data-cy="unlock collateral form"]').should('exist');
cy.get('[data-cy="unlock collateral form"]', { timeout: 180_000 }).should('exist');
cy.get('[data-cy="locked amount"]').should('exist').and('include.text', 'Max');

cy.get('[data-cy="undelegate amount input"]').should('exist');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Create Account', () => {
describe(__filename, () => {
Cypress.env('chainId', '42161');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -21,7 +21,7 @@ describe('Create Account', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });

cy.visit('/#/dashboard');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Dashboard - Connected', () => {
describe(__filename, () => {
Cypress.env('chainId', '42161');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -21,7 +21,7 @@ describe('Dashboard - Connected', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });

cy.visit('/#/dashboard');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Dashboard - Not Connected', () => {
describe(__filename, () => {
Cypress.env('chainId', '42161');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -13,7 +13,7 @@ describe('Dashboard - Not Connected', () => {
});
});

it('works', () => {
it(__filename, () => {
cy.visit('/#/dashboard');

cy.contains('h2', 'Dashboard').should('exist');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Manage WETH Position - Borrow', () => {
describe(__filename, () => {
Cypress.env('chainId', '42161');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -23,7 +23,7 @@ describe('Manage WETH Position - Borrow', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });
cy.approveCollateral({ symbol: 'WETH', spender: 'CoreProxy' });
cy.wrapEth({ amount: 20 });
Expand All @@ -32,7 +32,7 @@ describe('Manage WETH Position - Borrow', () => {

cy.visit(`/#/positions/WETH/1?manageAction=claim&accountId=${Cypress.env('accountId')}`);

cy.get('[data-cy="claim form"]').should('exist');
cy.get('[data-cy="claim form"]', { timeout: 180_000 }).should('exist');
cy.contains('[data-status="info"]', 'You can take an interest-free loan up to').should('exist');

cy.get('[data-cy="claim amount input"]').type('100');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Manage WETH Position - Deposit', () => {
describe(__filename, () => {
Cypress.env('chainId', '42161');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -21,7 +21,7 @@ describe('Manage WETH Position - Deposit', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });

// Make initial delegation
Expand All @@ -32,7 +32,7 @@ describe('Manage WETH Position - Deposit', () => {

cy.visit(`/#/positions/WETH/1?manageAction=deposit&accountId=${Cypress.env('accountId')}`);

cy.get('[data-cy="deposit and lock collateral form"]').should('exist');
cy.get('[data-cy="deposit and lock collateral form"]', { timeout: 180_000 }).should('exist');
cy.get('[data-cy="balance amount"]').should('exist').and('include.text', 'Max');

cy.get('[data-cy="deposit amount input"]').type('1');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Manage WETH Position - Repay', () => {
describe(__filename, () => {
Cypress.env('chainId', '42161');
Cypress.env('preset', 'main');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -21,7 +21,7 @@ describe('Manage WETH Position - Repay', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });
cy.approveCollateral({ symbol: 'WETH', spender: 'CoreProxy' });
cy.wrapEth({ amount: 20 });
Expand All @@ -31,7 +31,7 @@ describe('Manage WETH Position - Repay', () => {

cy.visit(`/#/positions/WETH/1?manageAction=repay&accountId=${Cypress.env('accountId')}`);

cy.get('[data-cy="repay debt form"]').should('exist');
cy.get('[data-cy="repay debt form"]', { timeout: 180_000 }).should('exist');
cy.get('[data-cy="current debt amount"]').should('exist').and('include.text', 'Max');

cy.get('[data-cy="repay amount input"]').type('5');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('Create Account', () => {
describe(__filename, () => {
Cypress.env('chainId', '8453');
Cypress.env('preset', 'andromeda');
Cypress.env('walletAddress', '0xc3Cf311e04c1f8C74eCF6a795Ae760dc6312F345');
Expand All @@ -8,7 +8,7 @@ describe('Create Account', () => {
cy.task('startAnvil', {
chainId: Cypress.env('chainId'),
forkUrl: `wss://base-mainnet.infura.io/ws/v3/${Cypress.env('INFURA_KEY')}`,
block: '22683522',
block: '22946353',
}).then(() => cy.log('Anvil started'));

cy.on('window:before:load', (win) => {
Expand All @@ -21,7 +21,7 @@ describe('Create Account', () => {
});
afterEach(() => cy.task('stopAnvil').then(() => cy.log('Anvil stopped')));

it('works', () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });

cy.visit('/#/dashboard');
Expand Down
Loading

0 comments on commit f7baf82

Please sign in to comment.