Skip to content

Commit

Permalink
Errors (#156)
Browse files Browse the repository at this point in the history
* Better errors handling and deduping

* Update more usages

* Shortcut priceFeeds extraction from errorData
  • Loading branch information
noisekit authored Jan 20, 2025
1 parent 4d7b454 commit 083bc46
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 321 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ and all transactions will be automatically signed, without any popups
4. Open devtools and set `localStorage` values

```js
localStorage.DEBUG = 'snx:*';
localStorage.DEBUG = 'true';
localStorage.debug = 'snx:*';
localStorage.MAGIC_WALLET = '0xWalletAddress';
```

Expand All @@ -101,3 +102,39 @@ and all transactions will be automatically signed, without any popups
# check your balance
cast balance 0xWalletAddress -e
```
## Testing with an empty test wallet (Base Mainnet)
Run Anvil at the block when the latest PM was deployed
```sh
anvil --auto-impersonate --chain-id 8453 --fork-url $RPC_BASE_MAINNET --no-rate-limit --accounts 0 --fork-block-number 25229684 --memory-limit 6442450944
```
Enable debugging and set magic wallet to `0xaaaa6c341C4Df916d9f0583Ba9Ea953618e5f008`
```js
localStorage.DEBUG = 'true';
localStorage.debug = 'snx:*';
localStorage.MAGIC_WALLET = '0xaaaa6c341C4Df916d9f0583Ba9Ea953618e5f008';
```
Fund the account with `ETH`, `USDC` and `snxUSD`
```sh
export USDC=0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
export AaveUSDCPool=0x4e65fE4DbA92790696d040ac24Aa414708F5c0AB
export walletAddress=0xaaaa6c341C4Df916d9f0583Ba9Ea953618e5f008
cast rpc anvil_setCode 0x1234123412341234123412341234123412341234 $(cast from-utf8 FORK)
cast rpc anvil_setBalance $AaveUSDCPool $(cast to-unit 1ether)
cast rpc anvil_setBalance $walletAddress $(cast to-unit 1ether)
export transfer='function transfer(address to, uint256 value) returns (bool)'
export balanceOf='function balanceOf(address account) view returns (uint256)'
cast send --unlocked --from $AaveUSDCPool $USDC $transfer $walletAddress 1000000000
cast call $USDC $balanceOf $walletAddress
export snxUSD="0x09d51516F38980035153a554c26Df3C6f51a23C3"
export CoreProxy="0x32C222A9A159782aFD7529c87FA34b96CA72C696"
cast rpc anvil_setBalance $CoreProxy $(cast to-unit 1ether)
cast send --unlocked --from $CoreProxy $snxUSD $transfer $walletAddress 1000ether
```
14 changes: 0 additions & 14 deletions liquidity/cypress/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,6 @@ 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
13 changes: 9 additions & 4 deletions liquidity/cypress/cypress/support/commands/borrowUsd.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { importAllErrors, importCoreProxy } from '@snx-v3/contracts';
import { parseContractError } from '@snx-v3/parseContractError';
import { importCoreProxy } from '@snx-v3/contracts';
import { importAllContractErrors, parseContractError } from '@snx-v3/parseContractError';
import { ethers } from 'ethers';
import { getCollateralConfig } from './getCollateralConfig';

Expand Down Expand Up @@ -27,8 +27,13 @@ export async function borrowUsd({
ethers.utils.parseEther(`${amount}`),
];
const gasLimit = await CoreProxyContract.estimateGas.mintUsd(...args).catch(async (error) => {
const AllErrors = await importAllErrors(Cypress.env('chainId'), Cypress.env('preset'));
console.log('borrowUsd ERROR', parseContractError({ error, AllErrors }));
console.log(
'borrowUsd ERROR',
parseContractError({
error,
abi: await importAllContractErrors(Cypress.env('chainId'), Cypress.env('preset')),
})
);
return ethers.BigNumber.from(10_000_000);
});
const txn = await CoreProxyContract.mintUsd(...args, { gasLimit: gasLimit.mul(2) });
Expand Down
16 changes: 10 additions & 6 deletions liquidity/cypress/cypress/support/commands/clearDebt.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
importDebtRepayer,
importAccountProxy,
importCoreProxy,
importDebtRepayer,
importSpotMarketProxy,
importAccountProxy,
importAllErrors,
} from '@snx-v3/contracts';
import { parseContractError } from '@snx-v3/parseContractError';
import { importAllContractErrors, parseContractError } from '@snx-v3/parseContractError';
import { ethers } from 'ethers';
import { getCollateralConfig } from './getCollateralConfig';

Expand Down Expand Up @@ -57,8 +56,13 @@ export async function clearDebt({
const gasLimit = await DebtRepayerContract.estimateGas
.depositDebtToRepay(...args)
.catch(async (error) => {
const AllErrors = await importAllErrors(Cypress.env('chainId'), Cypress.env('preset'));
console.log('clearDebt ERROR', parseContractError({ error, AllErrors }));
console.log(
'clearDebt ERROR',
parseContractError({
error,
abi: await importAllContractErrors(Cypress.env('chainId'), Cypress.env('preset')),
})
);
return ethers.BigNumber.from(10_000_000);
});
const txn = await DebtRepayerContract.depositDebtToRepay(...args, { gasLimit: gasLimit.mul(2) });
Expand Down
13 changes: 9 additions & 4 deletions liquidity/cypress/cypress/support/commands/delegateCollateral.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { importCoreProxy, importAllErrors } from '@snx-v3/contracts';
import { parseContractError } from '@snx-v3/parseContractError';
import { importCoreProxy } from '@snx-v3/contracts';
import { importAllContractErrors, parseContractError } from '@snx-v3/parseContractError';
import { ethers } from 'ethers';
import { getCollateralConfig } from './getCollateralConfig';

Expand Down Expand Up @@ -31,8 +31,13 @@ export async function delegateCollateral({
const gasLimit = await CoreProxyContract.estimateGas
.delegateCollateral(...args)
.catch(async (error) => {
const AllErrors = await importAllErrors(Cypress.env('chainId'), Cypress.env('preset'));
console.log('delegateCollateral ERROR', parseContractError({ error, AllErrors }));
console.log(
'delegateCollateral ERROR',
parseContractError({
error,
abi: await importAllContractErrors(Cypress.env('chainId'), Cypress.env('preset')),
})
);
return ethers.BigNumber.from(10_000_000);
});
const txn = await CoreProxyContract.delegateCollateral(...args, { gasLimit: gasLimit.mul(2) });
Expand Down

This file was deleted.

13 changes: 9 additions & 4 deletions liquidity/cypress/cypress/support/commands/depositCollateral.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { importAllErrors, importCoreProxy } from '@snx-v3/contracts';
import { parseContractError } from '@snx-v3/parseContractError';
import { importCoreProxy } from '@snx-v3/contracts';
import { importAllContractErrors, parseContractError } from '@snx-v3/parseContractError';
import { ethers } from 'ethers';
import { getCollateralConfig } from './getCollateralConfig';

Expand All @@ -25,8 +25,13 @@ export async function depositCollateral({
ethers.utils.parseEther(`${amount}`),
];
const gasLimit = await CoreProxyContract.estimateGas.deposit(...args).catch(async (error) => {
const AllErrors = await importAllErrors(Cypress.env('chainId'), Cypress.env('preset'));
console.log('delegateCollateral ERROR', parseContractError({ error, AllErrors }));
console.log(
'depositCollateral ERROR',
parseContractError({
error,
abi: await importAllContractErrors(Cypress.env('chainId'), Cypress.env('preset')),
})
);
return ethers.BigNumber.from(10_000_000);
});
const txn = await CoreProxyContract.deposit(...args, { gasLimit: gasLimit.mul(2) });
Expand Down
13 changes: 9 additions & 4 deletions liquidity/cypress/cypress/support/commands/depositSystemToken.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { importAllErrors, importCoreProxy, importSystemToken } from '@snx-v3/contracts';
import { parseContractError } from '@snx-v3/parseContractError';
import { importCoreProxy, importSystemToken } from '@snx-v3/contracts';
import { importAllContractErrors, parseContractError } from '@snx-v3/parseContractError';
import { ethers } from 'ethers';

export async function depositSystemToken({
Expand All @@ -23,8 +23,13 @@ export async function depositSystemToken({
ethers.utils.parseEther(`${amount}`),
];
const gasLimit = await CoreProxyContract.estimateGas.deposit(...args).catch(async (error) => {
const AllErrors = await importAllErrors(Cypress.env('chainId'), Cypress.env('preset'));
console.log('depositSystemToken ERROR', parseContractError({ error, AllErrors }));
console.log(
'depositSystemToken ERROR',
parseContractError({
error,
abi: await importAllContractErrors(Cypress.env('chainId'), Cypress.env('preset')),
})
);
return ethers.BigNumber.from(10_000_000);
});
const txn = await CoreProxyContract.deposit(...args, { gasLimit: gasLimit.mul(2) });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {
importAllErrors,
importAccountProxy,
importPositionManagerAndromedaStataUSDC,
importPositionManagerAndromedaUSDC,
importAccountProxy,
} from '@snx-v3/contracts';
import { parseContractError } from '@snx-v3/parseContractError';
import { importAllContractErrors, parseContractError } from '@snx-v3/parseContractError';
import { ethers } from 'ethers';
import { getCollateralConfig } from './getCollateralConfig';

Expand Down Expand Up @@ -62,13 +61,11 @@ export async function pmDecreasePosition({
const gasLimit = await PositionManagerContract.estimateGas
.decreasePosition(...args)
.catch(async (error) => {
const AllErrors = await importAllErrors(Cypress.env('chainId'), Cypress.env('preset'));
console.log(
'decreasePosition ERROR',
parseContractError({
error,
AllErrors,
extraAbi: PositionManager.abi,
abi: await importAllContractErrors(Cypress.env('chainId'), Cypress.env('preset')),
})
);
return ethers.BigNumber.from(10_000_000);
Expand Down
7 changes: 2 additions & 5 deletions liquidity/cypress/cypress/support/commands/pmSetupPosition.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {
importAllErrors,
importPositionManagerAndromedaStataUSDC,
importPositionManagerAndromedaUSDC,
} from '@snx-v3/contracts';
import { parseContractError } from '@snx-v3/parseContractError';
import { importAllContractErrors, parseContractError } from '@snx-v3/parseContractError';
import { ethers } from 'ethers';
import { getCollateralConfig } from './getCollateralConfig';

Expand Down Expand Up @@ -46,13 +45,11 @@ export async function pmSetupPosition({ address = Cypress.env('walletAddress'),
const gasLimit = await PositionManagerContract.estimateGas
.setupPosition(...args)
.catch(async (error) => {
const AllErrors = await importAllErrors(Cypress.env('chainId'), Cypress.env('preset'));
console.log(
'setupPosition ERROR',
parseContractError({
error,
AllErrors,
extraAbi: PositionManager.abi,
abi: await importAllContractErrors(Cypress.env('chainId'), Cypress.env('preset')),
})
);
return ethers.BigNumber.from(10_000_000);
Expand Down
2 changes: 0 additions & 2 deletions liquidity/cypress/cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -39,7 +38,6 @@ 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
Loading

0 comments on commit 083bc46

Please sign in to comment.