Skip to content

Commit

Permalink
delegateCollateralAndromeda task
Browse files Browse the repository at this point in the history
  • Loading branch information
noisekit committed Dec 22, 2024
1 parent cfc738b commit 525eaa2
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 10 deletions.
14 changes: 14 additions & 0 deletions liquidity/cypress/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ declare global {
poolId: number;
}) => Promise<void>;

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

depositCollateral: ({
address,
accountId,
Expand Down
14 changes: 4 additions & 10 deletions liquidity/cypress/cypress/e2e/8453-andromeda/USDC_Withdraw.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ describe(__filename, () => {
it(__filename, () => {
cy.setEthBalance({ balance: 100 });
cy.getUSDC({ amount: 1000 });
cy.clearDebt({ symbol: 'USDC', poolId: 1 });
// Reduce delegation to 150
cy.delegateCollateral({ symbol: 'USDC', amount: 150, poolId: 1 });
cy.delegateCollateralAndromeda({ symbol: 'sUSDC', amount: 150, poolId: 1 });
// Remove withdrawals delay
cy.setWithdrawTimeout({ timeout: '0' });

cy.visit(
`?${makeSearch({
Expand All @@ -46,6 +47,7 @@ describe(__filename, () => {

cy.get('[data-cy="withdraw amount input"]').should('exist');
cy.get('[data-cy="withdraw amount input"]').type('1');

cy.get('[data-cy="withdraw submit"]').should('be.enabled');
cy.get('[data-cy="withdraw submit"]').click();

Expand All @@ -56,14 +58,6 @@ describe(__filename, () => {
cy.get('[data-cy="withdraw confirm button"]').should('include.text', 'Execute Transaction');
cy.get('[data-cy="withdraw confirm button"]').click();

cy.contains('[data-status="error"]', 'AccountActivityTimeoutPending').should('exist');
cy.get('[data-status="error"] [aria-label="Close"]').click();

cy.setWithdrawTimeout({ timeout: '0' });

cy.get('[data-cy="withdraw confirm button"]').should('include.text', 'Retry');
cy.get('[data-cy="withdraw confirm button"]').click();

cy.contains('[data-status="success"]', 'Collateral successfully Withdrawn', {
timeout: 120_000,
}).should('exist');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import {
importAccountProxy,
importAllErrors,
importCoreProxy,
importDebtRepayer,
importMulticall3,
importSpotMarketProxy,
} from '@snx-v3/contracts';
import { parseContractError } from '@snx-v3/parseContractError';
import { ethers } from 'ethers';
import { getCollateralConfig } from './getCollateralConfig';

export async function delegateCollateralAndromeda({
address = Cypress.env('walletAddress'),
accountId = Cypress.env('accountId'),
symbol,
amount,
poolId,
}) {
console.log('delegateCollateral', { address, accountId, symbol, amount, poolId });

const config = await getCollateralConfig({ symbol });
const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545');
const signer = provider.getSigner(address);

const AllErrors = await importAllErrors(Cypress.env('chainId'), Cypress.env('preset'));

const Multicall3 = await importMulticall3(Cypress.env('chainId'), Cypress.env('preset'));
const Multicall3Contract = new ethers.Contract(Multicall3.address, Multicall3.abi, signer);

const CoreProxy = await importCoreProxy(Cypress.env('chainId'), Cypress.env('preset'));
const CoreProxyInterface = new ethers.utils.Interface(CoreProxy.abi);

const DebtRepayer = await importDebtRepayer(Cypress.env('chainId'), Cypress.env('preset'));
const DebtRepayerInterface = new ethers.utils.Interface(DebtRepayer.abi);

const AccountProxy = await importAccountProxy(Cypress.env('chainId'), Cypress.env('preset'));
const AccountProxyContract = new ethers.Contract(AccountProxy.address, AccountProxy.abi, signer);

const SpotMarketProxy = await importSpotMarketProxy(
Cypress.env('chainId'),
Cypress.env('preset')
);

const USDC = await getCollateralConfig({ symbol: 'USDC' });
const TokenContract = new ethers.Contract(
USDC.tokenAddress,
['function approve(address spender, uint256 amount) returns (bool)'],
signer
);
await (await TokenContract.approve(DebtRepayer.address, ethers.constants.MaxUint256)).wait();
await (await AccountProxyContract.approve(DebtRepayer.address, accountId)).wait();

console.log(`DebtRepayer.address`, DebtRepayer.address);

const depositDebtToRepayTx = {
target: DebtRepayer.address,
callData: DebtRepayerInterface.encodeFunctionData('depositDebtToRepay', [
//
CoreProxy.address,
SpotMarketProxy.address,
AccountProxy.address,
ethers.BigNumber.from(accountId),
ethers.BigNumber.from(poolId),
config.tokenAddress,
'1', // USDC_BASE_MARKET
]),
requireSuccess: true,
};
console.log(`depositDebtToRepayTx`, depositDebtToRepayTx);
const delegateCollateralTx = {
target: CoreProxy.address,
callData: CoreProxyInterface.encodeFunctionData('delegateCollateral', [
//
ethers.BigNumber.from(accountId),
ethers.BigNumber.from(poolId),
config.tokenAddress,
ethers.utils.parseEther(`${amount}`),
ethers.utils.parseEther(`1`),
]),
requireSuccess: true,
};
console.log(`delegateCollateralTx`, delegateCollateralTx);
const args = [
//
[
depositDebtToRepayTx.target,
depositDebtToRepayTx.requireSuccess,
depositDebtToRepayTx.callData,
],
[
delegateCollateralTx.target,
delegateCollateralTx.requireSuccess,
delegateCollateralTx.callData,
],
];

try {
await Multicall3Contract.estimateGas.aggregate3(args);
} catch (error) {
console.log(
'delegateCollateral estimateGas ERROR',
parseContractError({
error,
AllErrors,
})
);
throw error;
}

try {
const txn = await Multicall3Contract.aggregate3(args);
console.log('delegateCollateral txn', txn);
const receipt = await txn.wait();
console.log('delegateCollateral receipt', txn);
return receipt;
} catch (error) {
console.log(
'delegateCollateral sendTransaction ERROR',
parseContractError({
error,
AllErrors,
})
);
throw error;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { importCoreProxy } from '@snx-v3/contracts';
import { ethers } from 'ethers';
import { setEthBalance } from './setEthBalance';

export async function setWithdrawTimeout(timeout) {
console.log('WithdrawTimeout', { timeout });
Expand All @@ -12,6 +13,8 @@ export async function setWithdrawTimeout(timeout) {
const owner = await CoreProxyContract.owner();
const signer = provider.getSigner(owner);

await setEthBalance({ address: owner, balance: 100 });

const txn = await CoreProxyContract.connect(signer).setConfig(
ethers.utils.formatBytes32String('accountTimeoutWithdraw'),
ethers.utils.formatBytes32String(timeout)
Expand Down
2 changes: 2 additions & 0 deletions liquidity/cypress/cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { approveCollateral } from './commands/approveCollateral';
import { borrowUsd } from './commands/borrowUsd';
import { clearDebt } from './commands/clearDebt';
import { delegateCollateral } from './commands/delegateCollateral';
import { delegateCollateralAndromeda } from './commands/delegateCollateralAndromeda';
import { depositCollateral } from './commands/depositCollateral';
import { getSNX } from './commands/getSNX';
import { getUSDC } from './commands/getUSDC';
Expand Down Expand Up @@ -36,6 +37,7 @@ addTxnCommand('approveCollateral', approveCollateral, { timeout: 30_000 });
addTxnCommand('borrowUsd', borrowUsd, { timeout: 180_000 });
addTxnCommand('clearDebt', clearDebt, { timeout: 180_000 });
addTxnCommand('delegateCollateral', delegateCollateral, { timeout: 180_000 });
addTxnCommand('delegateCollateralAndromeda', delegateCollateralAndromeda, { timeout: 180_000 });
addTxnCommand('depositCollateral', depositCollateral, { timeout: 60_000 });
addTxnCommand('getSNX', getSNX, { timeout: 60_000 });
addTxnCommand('getSUSD', getSUSD, { timeout: 60_000 });
Expand Down

0 comments on commit 525eaa2

Please sign in to comment.