Skip to content

Commit

Permalink
fix(a3p): optimize error messages and reduce logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge-Lopes committed Oct 31, 2024
1 parent f95c3a6 commit 1dd386a
Showing 1 changed file with 24 additions and 75 deletions.
99 changes: 24 additions & 75 deletions a3p-integration/proposals/z:acceptance/vaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ test.serial('open new vault', async t => {

const istBalanceBefore = await getISTBalance(USER1ADDR);
const activeVaultsBefore = await agopsVaults(USER1ADDR);
t.log('uist balance before:', istBalanceBefore);
t.log('active vaults before:', activeVaultsBefore);

const mint = '5.0';
const collateral = '10.0';
await openVault(USER1ADDR, mint, collateral);

const istBalanceAfter = await getISTBalance(USER1ADDR);
const activeVaultsAfter = await agopsVaults(USER1ADDR);
t.log('uist balance after:', istBalanceAfter);
t.log('active vaults after:', activeVaultsAfter);

t.is(
istBalanceBefore + 5,
Expand All @@ -62,15 +58,11 @@ test.serial('remove collateral', async t => {
const { vaultID, collateral: collateralBefore } =
await getLastVaultFromAddress(USER1ADDR);

t.log('vault collateral before:', collateralBefore);

await adjustVault(USER1ADDR, vaultID, { wantCollateral: 1.0 });

const { collateral: collateralAfter } =
await getLastVaultFromAddress(USER1ADDR);

t.log('vault collateral after:', collateralAfter);

t.is(
collateralBefore,
collateralAfter + 1_000_000n,
Expand All @@ -81,12 +73,10 @@ test.serial('remove collateral', async t => {
test.serial('remove IST', async t => {
const { vaultID, debt: debtBefore } =
await getLastVaultFromAddress(USER1ADDR);
t.log('vault debt before:', debtBefore);

await adjustVault(USER1ADDR, vaultID, { wantMinted: 1.0 });

const { debt: debtAfter } = await getLastVaultFromAddress(USER1ADDR);
t.log('vault debt after:', debtAfter);

t.is(
debtAfter,
Expand All @@ -98,30 +88,26 @@ test.serial('remove IST', async t => {
test.serial('add collateral', async t => {
const { vaultID, collateral: collateralBefore } =
await getLastVaultFromAddress(USER1ADDR);
t.log('vault collateral before:', collateralBefore);

await adjustVault(USER1ADDR, vaultID, { giveCollateral: 1.0 });

const { collateral: collateralAfter } =
await getLastVaultFromAddress(USER1ADDR);
t.log('vault collateral after:', collateralAfter);

t.is(
collateralBefore,
collateralAfter - 1_000_000n,
collateralAfter,
collateralBefore + 1_000_000n,
'The vault Collateral should increase after adding some ATOM',
);
});

test.serial('add IST', async t => {
const { vaultID, debt: debtBefore } =
await getLastVaultFromAddress(USER1ADDR);
t.log('vault debt before:', debtBefore);

await adjustVault(USER1ADDR, vaultID, { giveMinted: 1.0 });

const { debt: debtAfter } = await getLastVaultFromAddress(USER1ADDR);
t.log('vault debt after:', debtAfter);

t.is(
debtAfter,
Expand All @@ -132,18 +118,12 @@ test.serial('add IST', async t => {

test.serial('close vault', async t => {
const { vaultID, collateral } = await getLastVaultFromAddress(USER1ADDR);
t.log('vault collateral:', collateral);

const atomBalanceBefore = await getBalances([USER1ADDR], ATOM_DENOM);
t.log('atom balance before', atomBalanceBefore);

await closeVault(USER1ADDR, vaultID, 6.035);

const atomBalanceAfter = await getBalances([USER1ADDR], ATOM_DENOM);
t.log('atom balance after', atomBalanceAfter);

const { state } = await getLastVaultFromAddress(USER1ADDR);
t.log('vault state:', state);

t.is(
atomBalanceAfter,
Expand All @@ -158,7 +138,6 @@ test.serial(
async t => {
await bankSend(GOV1ADDR, `200000000000000000${ATOM_DENOM}`);
const activeVaultsBefore = await agopsVaults(GOV1ADDR);
t.log('active vaults before:', activeVaultsBefore);

const minInitialDebt = await getMinInitialDebt();

Expand All @@ -168,14 +147,11 @@ test.serial(
await t.throwsAsync(
() => openVault(GOV1ADDR, mint.toString(), collateral.toString()),
{
message: new RegExp(
`Error: Vault creation requires a minInitialDebt of {"brand":"\\[Alleged: IST brand\\]","value":"\\[${minInitialDebt * 1_000_000n}n\\]"}`,
),
message: /Error: Vault creation requires a minInitialDebt/,
},
);

const activeVaultsAfter = await agopsVaults(GOV1ADDR);
t.log('active vaults after:', activeVaultsAfter);

t.is(
activeVaultsAfter.length,
Expand All @@ -187,26 +163,20 @@ test.serial(

test.serial('user cannot open a vault above debt limit', async t => {
const activeVaultsBefore = await agopsVaults(GOV1ADDR);
t.log('active vaults before:', activeVaultsBefore);

const { availableDebtForMint, debtLimit, totalDebt } =
await getAvailableDebtForMint(VAULT_MANAGER);
const { availableDebtForMint } = await getAvailableDebtForMint(VAULT_MANAGER);

const mint = availableDebtForMint + 5n;
const collateral = mint * 2n;

const { adjustedToMintAmount } = await calculateMintFee(mint, VAULT_MANAGER);
await t.throwsAsync(
() => openVault(GOV1ADDR, mint.toString(), collateral.toString()),
{
message: new RegExp(
`Minting {"brand":"\\[Alleged: IST brand\\]","value":"\\[${adjustedToMintAmount.value}n\\]"} past {"brand":"\\[Alleged: IST brand\\]","value":"\\[${totalDebt}n\\]"} would hit total debt limit {"brand":"\\[Alleged: IST brand\\]","value":"\\[${debtLimit}n\\]"}`,
),
message: /Error: Minting.*would hit total debt limit/,
},
);

const activeVaultsAfter = await agopsVaults(GOV1ADDR);
t.log('active vaults after:', activeVaultsAfter);

t.is(
activeVaultsAfter.length,
Expand All @@ -218,8 +188,6 @@ test.serial('user cannot open a vault above debt limit', async t => {
test.serial('user can open a vault under debt limit', async t => {
const istBalanceBefore = await getISTBalance(GOV1ADDR);
const activeVaultsBefore = await agopsVaults(GOV1ADDR);
t.log('uist balance before:', istBalanceBefore);
t.log('active vaults before:', activeVaultsBefore);

const { availableDebtForMint } = await getAvailableDebtForMint(VAULT_MANAGER);

Expand All @@ -230,8 +198,6 @@ test.serial('user can open a vault under debt limit', async t => {

const istBalanceAfter = await getISTBalance(GOV1ADDR);
const activeVaultsAfter = await agopsVaults(GOV1ADDR);
t.log('uist balance after:', istBalanceAfter);
t.log('active vaults after:', activeVaultsAfter);

t.is(
istBalanceBefore + Number(mint),
Expand All @@ -247,15 +213,8 @@ test.serial('user can open a vault under debt limit', async t => {

test.serial('user cannot increased vault debt above debt limit', async t => {
const { vaultID, debt: debtBefore } = await getLastVaultFromAddress(GOV1ADDR);
t.log('vault debt before:', debtBefore);

const { availableDebtForMint, debtLimit, totalDebt } =
await getAvailableDebtForMint(VAULT_MANAGER);

const { adjustedToMintAmount } = await calculateMintFee(
availableDebtForMint,
VAULT_MANAGER,
);
const { availableDebtForMint } = await getAvailableDebtForMint(VAULT_MANAGER);

// The availableDebtForMint + mintFee will surpass the debt limit
const mint = Number(availableDebtForMint);
Expand All @@ -265,14 +224,11 @@ test.serial('user cannot increased vault debt above debt limit', async t => {
wantMinted: mint,
}),
{
message: new RegExp(
`Minting {"brand":"\\[Alleged: IST brand\\]","value":"\\[${adjustedToMintAmount.value}n\\]"} past {"brand":"\\[Alleged: IST brand\\]","value":"\\[${totalDebt}n\\]"} would hit total debt limit {"brand":"\\[Alleged: IST brand\\]","value":"\\[${debtLimit}n\\]"}`,
),
message: /Error: Minting.*would hit total debt limit/,
},
);

const { debt: debtAfter } = await getLastVaultFromAddress(GOV1ADDR);
t.log('vault debt after:', debtAfter);

t.is(debtAfter, debtBefore, 'The vault Debt should stay the same');
});
Expand All @@ -288,11 +244,9 @@ test.serial(
mint,
VAULT_MANAGER,
);
t.log('mint + fee:', adjustedToMintAmount.value);

const { vaultID, debt: debtAfterOpenVault } =
await getLastVaultFromAddress(GOV1ADDR);
t.log('vault debt after open:', debtAfterOpenVault);

t.is(
debtAfterOpenVault,
Expand All @@ -304,11 +258,9 @@ test.serial(

const { adjustedToMintAmount: adjustedToMintAmountAfter } =
await calculateMintFee(1n, VAULT_MANAGER);
t.log('wantMinted + fee:', adjustedToMintAmountAfter.value);

const { debt: debtAfterAdjustVault } =
await getLastVaultFromAddress(GOV1ADDR);
t.log('vault debt after adjust:', debtAfterAdjustVault);

t.is(
debtAfterAdjustVault,
Expand All @@ -335,14 +287,23 @@ test.serial('confirm that Oracle prices are being received', async t => {
await verifyPushedPrice(oraclesByBrand, BRANDS[0], 10, roundId);

const atomQuote = await getPriceQuote(BRANDS[0]);
t.log('price quote:', atomQuote);
t.is(atomQuote, '+10000000');
t.is(
atomQuote,
'+10000000',
'ATOM price quote does not match the expected value',
);

const vaultQuote = await getVaultPrices(ATOMManagerIndex);
t.log('vault quote:', vaultQuote.value[0].amountOut.value);

t.true(vaultQuote.value[0].amountIn.brand.includes(' ATOM '));
t.is(vaultQuote.value[0].amountOut.value, atomQuote);
t.true(
vaultQuote.value[0].amountIn.brand.includes(' ATOM '),
'ATOM price quote not found',
);
t.is(
vaultQuote.value[0].amountOut.value,
atomQuote,
'Vault price quote does not match the expected ATOM price quote',
);
});

test.serial(
Expand All @@ -354,18 +315,12 @@ test.serial(
*/
const user = await getUser('long-living-vault');

const {
vaultID,
debt: debtAfterOpenVault,
collateral: collateralBefore,
} = await getLastVaultFromAddress(user);
t.log('vault debt after open:', debtAfterOpenVault);
t.log('vault collateral before:', collateralBefore);
const { vaultID, collateral: collateralBefore } =
await getLastVaultFromAddress(user);

await adjustVault(user, vaultID, { wantCollateral: 1.0 });

const { collateral: collateralAfter } = await getLastVaultFromAddress(user);
t.log('vault collateral after:', collateralAfter);

t.is(
collateralBefore,
Expand All @@ -376,7 +331,6 @@ test.serial(
await closeVault(user, vaultID, 6.03);

const { state } = await getLastVaultFromAddress(user);
t.log('vault state:', state);

t.is(state, 'closed', 'The vault should be in the "closed" state.');
},
Expand All @@ -389,32 +343,27 @@ test.serial(
const collateral = '10.0';
await openVault(GOV1ADDR, mint, collateral);

const { debtLimit: debtLimitBefore, totalDebt: totalDebtBefore } =
const { totalDebt: totalDebtBefore } =
await getAvailableDebtForMint(VAULT_MANAGER);
t.log('debtLimit before adjusting parameter: ', debtLimitBefore);
t.log('totalDebt before adjusting parameter: ', totalDebtBefore);

const limit = (totalDebtBefore - 10_000_000n) / 1_000_000n;
await setDebtLimit(GOV1ADDR, limit);

const { debtLimit: debtLimitAfter, totalDebt: totalDebtAfter } =
await getAvailableDebtForMint(VAULT_MANAGER);
t.log('debtLimit after adjusting parameter: ', debtLimitAfter);
t.true(
debtLimitAfter < totalDebtAfter,
'debtLimit should be less than totalDebt',
);

const { vaultID, debt: vaultDebtBefore } =
await getLastVaultFromAddress(GOV1ADDR);
t.log('vault debt before pay off:', vaultDebtBefore);

await adjustVault(GOV1ADDR, vaultID, {
giveMinted: Number(vaultDebtBefore) / 1_000_000,
});

const { debt: vaultDebtAfter } = await getLastVaultFromAddress(GOV1ADDR);
t.log('vault debt after pay off:', vaultDebtAfter);

t.is(vaultDebtAfter, 0n, 'The vault Debt should have been erased');
},
Expand Down

0 comments on commit 1dd386a

Please sign in to comment.