Skip to content

Commit

Permalink
Fix Tests (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalecks authored Nov 28, 2023
2 parents f2434fb + 8f21b1c commit cd5df2a
Show file tree
Hide file tree
Showing 27 changed files with 514 additions and 256 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/ci.sdk-core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ jobs:
- name: Install Dependencies
if: steps.node-modules-cache.outputs.cache-hit != 'true'
run: yarn install --immutable
- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: wasm32-unknown-unknown
profile: minimal
override: true
- name: Install Rust (Stable)
run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- name: Install Anvil
uses: foundry-rs/foundry-toolchain@v1
with:
Expand Down
9 changes: 2 additions & 7 deletions .github/workflows/ci.sdk-wells.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ jobs:
run: yarn install --immutable
- name: Generate Typedefs
run: yarn sdk:generate
- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: wasm32-unknown-unknown
profile: minimal
override: true
- name: Install Rust (Stable)
run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- name: Install Anvil
uses: foundry-rs/foundry-toolchain@v1
with:
Expand Down
13 changes: 3 additions & 10 deletions .github/workflows/ci.sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,15 @@ jobs:
- name: Install Dependencies
if: steps.node-modules-cache.outputs.cache-hit != 'true'
run: yarn install --immutable
- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: wasm32-unknown-unknown
profile: minimal
override: true
- name: Install Rust (Stable)
run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- name: Install Anvil
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
# TODO: Cache Anvil RPC calls between runs to speed up tests
- name: Launch Anvil
run: anvil --fork-url $ANVIL_FORK_URL --chain-id 1337 &
env:
ANVIL_FORK_URL: ${{ secrets.ANVIL_FORK_URL }}
run: yarn anvil4tests &
- name: Build All
run: yarn build
- run: yarn sdk:test
Expand Down
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { JestConfigWithTsJest } from "ts-jest";

const jestConfig: JestConfigWithTsJest = {
testSequencer: "<rootDir>/testSequencer.js",
runner: "jest-serial-runner",
projects: [
{
// @ts-ignore
preset: "ts-jest",
displayName: "sdk-wells",
rootDir: "projects/sdk-wells",

testMatch: ["<rootDir>/src/**/?(*.)+(spec|test).[jt]s?(x)", "<rootDir>/test/**/?(*.)+(spec|test).[jt]s?(x)"],
moduleNameMapper: {
"@beanstalk/sdk-wells/(.*)$": "<rootDir>/src/$1",
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@types/prettier": "^2",
"husky": "8.0.1",
"jest": "29.2.2",
"jest-serial-runner": "1.2.1",
"lint-staged": "13.1.0",
"prettier": "2.7.1",
"ts-jest": "29.0.3",
Expand Down Expand Up @@ -43,6 +44,7 @@
"ui:build": "yarn workspace ui build",
"ui:test": "yarn workspace ui test",
"test:browser": "yarn workspace tests test:browser",
"ex": "yarn workspace @beanstalk/examples x"
"ex": "yarn workspace @beanstalk/examples x",
"anvil4tests": "anvil --fork-url https://eth-mainnet.g.alchemy.com/v2/Kk7ktCQL5wz4v4AG8bR2Gun8TAASQ-qi --chain-id 1337 --fork-block-number 18629000"
}
}
14 changes: 14 additions & 0 deletions projects/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { sunrise } from "./commands/sunrise.js";
import { setPrice } from "./commands/setprice.js";
import { help } from "./commands/help.js";
import { mineBlocks } from "./commands/mine.js";
import { setDeltaB } from "./commands/setDeltaB.js";
import { getDeltaB } from "./commands/getDeltaB.js";

main().catch((e) => {
console.log("FAILED:");
Expand All @@ -28,6 +30,7 @@ async function main() {
const args = commandLineArgs(commands, { partial: true });

const { sdk, chain, stop } = await setupSDK(args);
sdk.DEBUG = false;

switch (args.command) {
case "balance":
Expand All @@ -45,6 +48,17 @@ async function main() {
case "mine":
await mineBlocks(sdk, args.amount);
break;
case "setpriceabove":
case "setpriceover":
await setDeltaB(sdk, chain, "up", args.amount);
break;
case "setpricebelow":
case "setpriceunder":
await setDeltaB(sdk, chain, "down", args.amount);
break;
case "deltab":
await getDeltaB(sdk);
break;
case "help":
default:
await help();
Expand Down
7 changes: 7 additions & 0 deletions projects/cli/src/commands/getDeltaB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BeanstalkSDK } from "@beanstalk/sdk";
import chalk from "chalk";

export const getDeltaB = async (sdk: BeanstalkSDK) => {
const deltaB = await sdk.bean.getDeltaB();
console.log(`${chalk.bold.whiteBright("DeltaB: ")} ${chalk.greenBright(deltaB.toHuman())}`);
};
16 changes: 15 additions & 1 deletion projects/cli/src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,22 @@ export const help = () => {
content: [
{ name: "{bold.greenBright balance}", summary: "Display balance(s). Optionally specify account or token" },
{ name: "{bold.greenBright setbalance}", summary: "Set balance(s). Optionally specify account, token, or amount" },
{ name: "{bold.greenBright setprice}", summary: "Set BEAN price by setting liquidity. Defaults to 20M BEAN and 20M 3CRV. Parameters are millions. See examples below" },
{
name: "{bold.greenBright setprice}",
summary: "Set BEAN price by setting liquidity. Defaults to 20M BEAN and 20M 3CRV. Parameters are millions. See examples below"
},
{ name: "{bold.greenBright sunrise}", summary: "Calls the sunrise() function" },
{ name: "{bold.greenBright mine}", summary: "Mines a block (or more)" },
{ name: "{bold.greenBright deltab}", summary: "Display current DeltaB" },
{
name: "{bold.greenBright setpriceover}",
summary: "Buys enough BEANs to set DeltaB over 0, and the price over 1.00. Multiplier may be applied"
},
{
name: "{bold.greenBright setpriceunder}",
summary: "Sells enough BEANs to set DeltaB under 0, and the price under 1.00. Multiplier may be applied"
},

{ name: "{bold.greenBright help}", summary: "You're looking at it :)" }
]
},
Expand Down
18 changes: 18 additions & 0 deletions projects/cli/src/commands/setDeltaB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BeanstalkSDK } from "@beanstalk/sdk";
import { BlockchainUtils } from "@beanstalk/sdk/dist/types/utils/TestUtils";
import chalk from "chalk";
import { table } from "table";

export const setDeltaB = async (sdk: BeanstalkSDK, chain: BlockchainUtils, direction: string, amount) => {
const multiplier = amount === "50000" ? 2 : amount;
if (direction === "down") {
await chain.setPriceUnder1(multiplier);
} else if (direction === "up") {
await chain.setPriceOver1(multiplier);
} else {
console.log(`unknown input: ${direction}. Expected "up", or "down"`);
}

const deltaB = await sdk.bean.getDeltaB();
console.log(`${chalk.bold.whiteBright("New DeltaB: ")} ${chalk.greenBright(deltaB.toHuman())}`);
};
25 changes: 25 additions & 0 deletions projects/examples/src/deltaB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { TokenValue } from "@beanstalk/sdk-core";
import { account as _account, impersonate, chain } from "./setup";
import { defaultAbiCoder } from "ethers/lib/utils";

main().catch((e) => {
console.log("FAILED:");
console.log(e);
});

async function main() {
const account = process.argv[3] || _account;
const { sdk, stop } = await impersonate(account);
sdk.DEBUG = false;

let deltaB = await sdk.bean.getDeltaB();

if (deltaB.gte(TokenValue.ZERO)) {
await chain.setPriceUnder1();
} else {
await chain.setPriceOver1();
}
// chain.mine()
deltaB = await sdk.bean.getDeltaB();
console.log("New DeltaB: ", deltaB.toHuman());
}
1 change: 1 addition & 0 deletions projects/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@graphql-codegen/typescript-graphql-request": "^4.5.6",
"@graphql-codegen/typescript-operations": "^2.5.4",
"@graphql-codegen/typescript-resolvers": "2.7.4",
"@jest/test-sequencer": "29.7.0",
"@nomiclabs/hardhat-ethers": "^2.1.1",
"@rollup/plugin-commonjs": "23.0.2",
"@rollup/plugin-json": "5.0.1",
Expand Down
36 changes: 0 additions & 36 deletions projects/sdk/src/classes/Token/Token.test.ts

This file was deleted.

6 changes: 1 addition & 5 deletions projects/sdk/src/lib/farm/presets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ describe("Facet: Pipeline", () => {
let snapshot: number;

beforeEach(async () => {
snapshot = await test.snapshot();
test.resetFork();
farm = sdk.farm.create();
await test.sendBean(account, sdk.tokens.BEAN.amount(100));
});

afterEach(async () => {
await test.revert(snapshot);
});

describe("loading without approval", () => {
it.skip("throws", async () => {
// Setup
Expand Down
30 changes: 18 additions & 12 deletions projects/sdk/src/lib/silo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { getTestUtils } from "../utils/TestUtils/provider";

import { Token } from "../classes/Token";
import { TokenSiloBalance } from "./silo/types";
import { calculateGrownStalkSeeds } from "./silo/utils";
import { TokenValue } from "@beanstalk/sdk-core";
import { BF_MULTISIG } from "src/utils/TestUtils/addresses";
import { Silo } from "src/lib/silo";
Expand All @@ -19,29 +18,37 @@ const timer = async (fn: Promise<any>, label: string) => {
};

/// Constants
const account1 = "0x9a00beffa3fc064104b71f6b7ea93babdc44d9da"; // whale
const account2 = "0x0"; // zero addy
// const account1 = "0x9a00beffa3fc064104b71f6b7ea93babdc44d9da"; // whale
const account2 = "0x0000000000000000000000000000000000000000"; // zero addy

/// Setup
const { sdk, account } = getTestUtils();
const { sdk, account, utils } = getTestUtils();

/// Tests
beforeAll(async () => {
await utils.resetFork();
const amount = sdk.tokens.BEAN.amount("100000");
await utils.setBalance(sdk.tokens.BEAN, account, amount);
await sdk.tokens.BEAN.approveBeanstalk(amount);

await sdk.silo.deposit(sdk.tokens.BEAN, sdk.tokens.BEAN, amount, 0.1, account);
});
describe("Silo Balance loading", () => {
describe("getBalance", function () {
it("returns an empty object", async () => {
const balance = await sdk.silo.getBalance(sdk.tokens.BEAN, account2, { source: DataSource.SUBGRAPH });
const balance = await sdk.silo.getBalance(sdk.tokens.BEAN, account2, { source: DataSource.LEDGER });
chaiExpect(balance.amount.eq(0)).to.be.true;
});
it("loads an account with deposits (fuzzy)", async () => {
const balance = await sdk.silo.getBalance(sdk.tokens.BEAN, BF_MULTISIG, { source: DataSource.SUBGRAPH });
chaiExpect(balance.amount.gt(10_000)).to.be.true; // FIXME
const balance = await sdk.silo.getBalance(sdk.tokens.BEAN, account, { source: DataSource.LEDGER });
chaiExpect(balance.amount.toHuman()).to.eq("100000");
});

// FIX: discrepancy in graph results
it.skip("source: ledger === subgraph", async function () {
const [ledger, subgraph]: TokenSiloBalance[] = await Promise.all([
timer(sdk.silo.getBalance(sdk.tokens.BEAN, account1, { source: DataSource.LEDGER }), "Ledger result time"),
timer(sdk.silo.getBalance(sdk.tokens.BEAN, account1, { source: DataSource.SUBGRAPH }), "Subgraph result time")
timer(sdk.silo.getBalance(sdk.tokens.BEAN, account, { source: DataSource.LEDGER }), "Ledger result time"),
timer(sdk.silo.getBalance(sdk.tokens.BEAN, account, { source: DataSource.SUBGRAPH }), "Subgraph result time")
]);

// We cannot compare .deposited.bdv as the ledger results come from prod
Expand All @@ -59,8 +66,8 @@ describe("Silo Balance loading", () => {
// @todo pick several accounts and loop
beforeAll(async () => {
[ledger, subgraph] = await Promise.all([
timer(sdk.silo.getBalances(account1, { source: DataSource.LEDGER }), "Ledger result time"),
timer(sdk.silo.getBalances(account1, { source: DataSource.SUBGRAPH }), "Subgraph result time")
timer(sdk.silo.getBalances(account, { source: DataSource.LEDGER }), "Ledger result time"),
timer(sdk.silo.getBalances(account, { source: DataSource.SUBGRAPH }), "Subgraph result time")
]);
});

Expand Down Expand Up @@ -170,7 +177,6 @@ describe("Deposit Permits", function () {
});

describe("Silo mowMultiple", () => {
const account = account1;
const whitelistedToken = sdk.tokens.BEAN;
const whitelistedToken2 = sdk.tokens.BEAN_CRV3_LP;
const nonWhitelistedToken = sdk.tokens.DAI;
Expand Down
5 changes: 3 additions & 2 deletions projects/sdk/src/lib/silo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumber, ContractTransaction } from "ethers";
import type { PayableOverrides } from "ethers";
import { Token } from "src/classes/Token";
import { BeanstalkSDK, DataSource } from "./BeanstalkSDK";
import { EventProcessor } from "src/lib/events/processor";
Expand Down Expand Up @@ -156,8 +157,8 @@ export class Silo {
* @param fromAmount Amount to convert
* @returns Promise of Transaction
*/
async convert(fromToken: Token, toToken: Token, fromAmount: TokenValue) {
return this.siloConvert.convert(fromToken, toToken, fromAmount);
async convert(fromToken: Token, toToken: Token, fromAmount: TokenValue, slippage: number = 0.1, overrides: PayableOverrides = {}) {
return this.siloConvert.convert(fromToken, toToken, fromAmount, slippage, overrides);
}

/**
Expand Down
Loading

0 comments on commit cd5df2a

Please sign in to comment.