Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soilking committed Oct 16, 2024
1 parent 19be5d9 commit f918f17
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
33 changes: 15 additions & 18 deletions projects/subgraph-basin/tests/Exchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { dayFromTimestamp, hourFromTimestamp } from "../../subgraph-core/utils/D
import { BigDecimal, BigInt } from "@graphprotocol/graph-ts";
import { BEAN_ERC20 } from "../../subgraph-core/constants/raw/BeanstalkEthConstants";
import { initL1Version } from "./entity-mocking/MockVersion";
import { assertBDClose } from "../../subgraph-core/tests/Assert";

describe("Well Entity: Exchange Tests", () => {
beforeEach(() => {
Expand Down Expand Up @@ -66,15 +67,13 @@ describe("Well Entity: Exchange Tests", () => {
let transferAmounts = updatedStore.cumulativeTransferVolumeReservesUSD;

assert.stringEquals("0", tradeAmounts[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.times(BigDecimal.fromString("1.5")).toString(), tradeAmounts[1].toString());
assert.stringEquals(BEAN_USD_AMOUNT.times(BigDecimal.fromString("2.5")).toString(), transferAmounts[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.times(BigDecimal.fromString("3.5")).toString(), transferAmounts[1].toString());
assert.stringEquals(WETH_USD_AMOUNT.times(BigDecimal.fromString("1.5")).toString(), updatedStore.cumulativeTradeVolumeUSD.toString());
assert.stringEquals(
BEAN_USD_AMOUNT.times(BigDecimal.fromString("2.5"))
.plus(WETH_USD_AMOUNT.times(BigDecimal.fromString("3.5")))
.toString(),
updatedStore.cumulativeTransferVolumeUSD.toString()
assertBDClose(WETH_USD_AMOUNT.times(BigDecimal.fromString("1.5")), tradeAmounts[1]);
assertBDClose(BEAN_USD_AMOUNT.times(BigDecimal.fromString("2.5")), transferAmounts[0]);
assertBDClose(WETH_USD_AMOUNT.times(BigDecimal.fromString("3.5")), transferAmounts[1]);
assertBDClose(WETH_USD_AMOUNT.times(BigDecimal.fromString("1.5")), updatedStore.cumulativeTradeVolumeUSD);
assertBDClose(
BEAN_USD_AMOUNT.times(BigDecimal.fromString("2.5")).plus(WETH_USD_AMOUNT.times(BigDecimal.fromString("3.5"))),
updatedStore.cumulativeTransferVolumeUSD
);
});
test("Previous day snapshot entity created", () => {
Expand Down Expand Up @@ -123,16 +122,14 @@ describe("Well Entity: Exchange Tests", () => {
let tradeAmounts = updatedStore.cumulativeTradeVolumeReservesUSD;
let transferAmounts = updatedStore.cumulativeTransferVolumeReservesUSD;

assert.stringEquals(BEAN_USD_AMOUNT.times(BigDecimal.fromString("1.5")).toString(), tradeAmounts[0].toString());
assertBDClose(BEAN_USD_AMOUNT.times(BigDecimal.fromString("1.5")), tradeAmounts[0]);
assert.stringEquals("0", tradeAmounts[1].toString());
assert.stringEquals(BEAN_USD_AMOUNT.times(BigDecimal.fromString("3.5")).toString(), transferAmounts[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.times(BigDecimal.fromString("2.5")).toString(), transferAmounts[1].toString());
assert.stringEquals(BEAN_USD_AMOUNT.times(BigDecimal.fromString("1.5")).toString(), updatedStore.cumulativeTradeVolumeUSD.toString());
assert.stringEquals(
BEAN_USD_AMOUNT.times(BigDecimal.fromString("3.5"))
.plus(WETH_USD_AMOUNT.times(BigDecimal.fromString("2.5")))
.toString(),
updatedStore.cumulativeTransferVolumeUSD.toString()
assertBDClose(BEAN_USD_AMOUNT.times(BigDecimal.fromString("3.5")), transferAmounts[0]);
assertBDClose(WETH_USD_AMOUNT.times(BigDecimal.fromString("2.5")), transferAmounts[1]);
assertBDClose(BEAN_USD_AMOUNT.times(BigDecimal.fromString("1.5")), updatedStore.cumulativeTradeVolumeUSD);
assertBDClose(
BEAN_USD_AMOUNT.times(BigDecimal.fromString("3.5")).plus(WETH_USD_AMOUNT.times(BigDecimal.fromString("2.5"))),
updatedStore.cumulativeTransferVolumeUSD
);
});
});
Expand Down
53 changes: 27 additions & 26 deletions projects/subgraph-basin/tests/Liquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { toAddress } from "../../subgraph-core/utils/Bytes";
import { mockWellLpTokenUnderlying } from "../../subgraph-core/tests/event-mocking/Tokens";
import { deprecated_calcLiquidityVolume } from "../src/utils/legacy/CP2";
import { loadOrCreateWellFunction } from "../src/entities/WellComponents";
import { assertBDClose } from "../../subgraph-core/tests/Assert";

const BI_2 = BigInt.fromU32(2);
const BI_3 = BigInt.fromU32(3);
Expand Down Expand Up @@ -59,9 +60,9 @@ describe("Well Entity: Liquidity Event Tests", () => {
let updatedStore = loadWell(WELL);
let endingBalances = updatedStore.reservesUSD;

assert.stringEquals(BEAN_USD_AMOUNT.toString(), endingBalances[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.toString(), endingBalances[1].toString());
assert.stringEquals(WETH_USD_AMOUNT.times(BigDecimal.fromString("2")).toString(), updatedStore.totalLiquidityUSD.toString());
assertBDClose(BEAN_USD_AMOUNT, endingBalances[0]);
assertBDClose(WETH_USD_AMOUNT, endingBalances[1]);
assertBDClose(WETH_USD_AMOUNT.times(BigDecimal.fromString("2")), updatedStore.totalLiquidityUSD);
});
test("Liquidity Token balance", () => {
assert.fieldEquals(WELL_ENTITY_TYPE, WELL.toHexString(), "lpTokenSupply", WELL_LP_AMOUNT.toString());
Expand All @@ -77,8 +78,8 @@ describe("Well Entity: Liquidity Event Tests", () => {

assert.bigIntEquals(BEAN_SWAP_AMOUNT, transferReserves[0]);
assert.bigIntEquals(WETH_SWAP_AMOUNT, transferReserves[1]);
assert.stringEquals(BEAN_USD_AMOUNT.toString(), transferReservesUSD[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.toString(), transferReservesUSD[1].toString());
assertBDClose(BEAN_USD_AMOUNT, transferReservesUSD[0]);
assertBDClose(WETH_USD_AMOUNT, transferReservesUSD[1]);
});
});

Expand All @@ -102,9 +103,9 @@ describe("Well Entity: Liquidity Event Tests", () => {
let endingBalances = updatedStore.reservesUSD;

// Bean balance is still only one unit of BEAN_USD_AMOUNT because the price was cut in half on the second deposit
assert.stringEquals(BEAN_USD_AMOUNT.toString(), endingBalances[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.toString(), endingBalances[1].toString());
assert.stringEquals(BEAN_USD_AMOUNT.plus(WETH_USD_AMOUNT).toString(), updatedStore.totalLiquidityUSD.toString());
assertBDClose(BEAN_USD_AMOUNT, endingBalances[0]);
assertBDClose(WETH_USD_AMOUNT, endingBalances[1]);
assertBDClose(BEAN_USD_AMOUNT.plus(WETH_USD_AMOUNT), updatedStore.totalLiquidityUSD);
});
test("Liquidity Token balance", () => {
assert.fieldEquals(WELL_ENTITY_TYPE, WELL.toHexString(), "lpTokenSupply", WELL_LP_AMOUNT.times(BI_2).toString());
Expand All @@ -120,8 +121,8 @@ describe("Well Entity: Liquidity Event Tests", () => {

assert.bigIntEquals(BEAN_SWAP_AMOUNT.times(BI_2), transferReserves[0]);
assert.bigIntEquals(WETH_SWAP_AMOUNT, transferReserves[1]);
assert.stringEquals(BEAN_USD_AMOUNT.times(BigDecimal.fromString("1.5")).toString(), transferReservesUSD[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.toString(), transferReservesUSD[1].toString());
assertBDClose(BEAN_USD_AMOUNT.times(BigDecimal.fromString("1.5")), transferReservesUSD[0]);
assertBDClose(WETH_USD_AMOUNT, transferReservesUSD[1]);
});
});

Expand All @@ -144,9 +145,9 @@ describe("Well Entity: Liquidity Event Tests", () => {
let updatedStore = loadWell(WELL);
let endingBalances = updatedStore.reservesUSD;

assert.stringEquals(BEAN_USD_AMOUNT.toString(), endingBalances[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.toString(), endingBalances[1].toString());
assert.stringEquals(WETH_USD_AMOUNT.times(BigDecimal.fromString("2")).toString(), updatedStore.totalLiquidityUSD.toString());
assertBDClose(BEAN_USD_AMOUNT, endingBalances[0]);
assertBDClose(WETH_USD_AMOUNT, endingBalances[1]);
assertBDClose(WETH_USD_AMOUNT.times(BigDecimal.fromString("2")), updatedStore.totalLiquidityUSD);
});
test("Liquidity Token balance", () => {
assert.fieldEquals(WELL_ENTITY_TYPE, WELL.toHexString(), "lpTokenSupply", WELL_LP_AMOUNT.plus(BI_10).toString());
Expand All @@ -162,8 +163,8 @@ describe("Well Entity: Liquidity Event Tests", () => {

assert.bigIntEquals(BEAN_SWAP_AMOUNT, transferReserves[0]);
assert.bigIntEquals(WETH_SWAP_AMOUNT, transferReserves[1]);
assert.stringEquals(BEAN_USD_AMOUNT.toString(), transferReservesUSD[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.toString(), transferReservesUSD[1].toString());
assertBDClose(BEAN_USD_AMOUNT, transferReservesUSD[0]);
assertBDClose(WETH_USD_AMOUNT, transferReservesUSD[1]);
});
});

Expand All @@ -188,9 +189,9 @@ describe("Well Entity: Liquidity Event Tests", () => {
let endingBalances = updatedStore.reservesUSD;

// WETH was doubled from the initial, so the bean price has also doubled
assert.stringEquals(BEAN_USD_AMOUNT.toString(), endingBalances[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.toString(), endingBalances[1].toString());
assert.stringEquals(BEAN_USD_AMOUNT.plus(WETH_USD_AMOUNT).toString(), updatedStore.totalLiquidityUSD.toString());
assertBDClose(BEAN_USD_AMOUNT, endingBalances[0]);
assertBDClose(WETH_USD_AMOUNT, endingBalances[1]);
assertBDClose(BEAN_USD_AMOUNT.plus(WETH_USD_AMOUNT), updatedStore.totalLiquidityUSD);
});
test("Liquidity Token balance", () => {
assert.fieldEquals(WELL_ENTITY_TYPE, WELL.toHexString(), "lpTokenSupply", WELL_LP_AMOUNT.plus(BI_10).toString());
Expand All @@ -206,8 +207,8 @@ describe("Well Entity: Liquidity Event Tests", () => {

assert.bigIntEquals(BEAN_SWAP_AMOUNT.div(BI_2), transferReserves[0]);
assert.bigIntEquals(WETH_SWAP_AMOUNT, transferReserves[1]);
assert.stringEquals(BEAN_USD_AMOUNT.div(BD_2).toString(), transferReservesUSD[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.toString(), transferReservesUSD[1].toString());
assertBDClose(BEAN_USD_AMOUNT.div(BD_2), transferReservesUSD[0]);
assertBDClose(WETH_USD_AMOUNT, transferReservesUSD[1]);
});
});

Expand Down Expand Up @@ -240,8 +241,8 @@ describe("Well Entity: Liquidity Event Tests", () => {

assert.bigIntEquals(BEAN_SWAP_AMOUNT.times(BI_2), transferReserves[0]);
assert.bigIntEquals(WETH_SWAP_AMOUNT.times(BI_2), transferReserves[1]);
assert.stringEquals(BEAN_USD_AMOUNT.times(BD_2).toString(), transferReservesUSD[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.times(BD_2).toString(), transferReservesUSD[1].toString());
assertBDClose(BEAN_USD_AMOUNT.times(BD_2), transferReservesUSD[0]);
assertBDClose(WETH_USD_AMOUNT.times(BD_2), transferReservesUSD[1]);
});
});

Expand Down Expand Up @@ -275,8 +276,8 @@ describe("Well Entity: Liquidity Event Tests", () => {

assert.bigIntEquals(BEAN_SWAP_AMOUNT.times(BI_3), transferReserves[0]);
assert.bigIntEquals(WETH_SWAP_AMOUNT.times(BI_2), transferReserves[1]);
assert.stringEquals(BEAN_USD_AMOUNT.times(BD_3).toString(), transferReservesUSD[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.times(BD_2).toString(), transferReservesUSD[1].toString());
assertBDClose(BEAN_USD_AMOUNT.times(BD_3).truncate(2), transferReservesUSD[0]);
assertBDClose(WETH_USD_AMOUNT.times(BD_2).truncate(2), transferReservesUSD[1]);
});
});

Expand Down Expand Up @@ -310,8 +311,8 @@ describe("Well Entity: Liquidity Event Tests", () => {

assert.bigIntEquals(BEAN_SWAP_AMOUNT.times(BI_2), transferReserves[0]);
assert.bigIntEquals(WETH_SWAP_AMOUNT.times(BI_3), transferReserves[1]);
assert.stringEquals(BEAN_USD_AMOUNT.times(BD_2).toString(), transferReservesUSD[0].toString());
assert.stringEquals(WETH_USD_AMOUNT.times(BD_3).toString(), transferReservesUSD[1].toString());
assertBDClose(BEAN_USD_AMOUNT.times(BD_2), transferReservesUSD[0]);
assertBDClose(WETH_USD_AMOUNT.times(BD_3), transferReservesUSD[1]);
});
});
test("Liquidity Volume Calculation", () => {
Expand Down
7 changes: 7 additions & 0 deletions projects/subgraph-core/tests/Assert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { assert } from "matchstick-as/assembly/index";
import { BigDecimal } from "@graphprotocol/graph-ts";

export function assertBDClose(expected: BigDecimal, actual: BigDecimal): void {
const diff = actual.minus(expected);
assert.assertTrue(diff < BigDecimal.fromString("0.1"));
}

0 comments on commit f918f17

Please sign in to comment.