Skip to content

Commit

Permalink
test(simulation): make them pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Oct 8, 2024
1 parent 17dd903 commit dcf25a8
Show file tree
Hide file tree
Showing 49 changed files with 1,090 additions and 366 deletions.
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
},
"suspicious": {
"useGetterReturn": "off",
"noAssignInExpressions": "off"
"noAssignInExpressions": "off",
"noFallthroughSwitchClause": "off"
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/blue-api-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@morpho-org/blue-api-sdk",
"version": "2.0.0-alpha.6",
"author": "Morpho Association <contact@morpho.org>",
"type": "module",
"main": "src/index.ts",
"files": [
"lib"
Expand Down Expand Up @@ -31,6 +32,13 @@
},
"publishConfig": {
"main": "lib/index.js",
"types": "lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
},
"access": "public"
}
}
8 changes: 4 additions & 4 deletions packages/blue-api-sdk/src/converter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AccrualPosition,
AccrualVault,
Address,
type Address,
Market,
MarketConfig,
MathLib,
Expand All @@ -15,7 +15,7 @@ import {
} from "@morpho-org/blue-sdk";
import { Time, isDefined } from "@morpho-org/morpho-ts";

import {
import type {
Chain as BlueApiChain,
Market as BlueApiMarket,
MarketPosition as BlueApiMarketPosition,
Expand All @@ -28,7 +28,7 @@ import {
VaultAllocation as BlueApiVaultAllocation,
VaultState as BlueApiVaultState,
Maybe,
} from "./types";
} from "./types.js";

export interface PartialBlueApiTokenPrice {
priceUsd?: BlueApiToken["priceUsd"];
Expand Down Expand Up @@ -316,7 +316,7 @@ export class BlueSdkConverter {
return new AccrualVault(
{
...state,
config: this.getVaultConfig(dto),
...this.getVaultConfig(dto),
fee: this.options.parseNumber(state.fee, 18),
pendingOwner:
state.pendingOwner ?? "0x0000000000000000000000000000000000000000",
Expand Down
8 changes: 4 additions & 4 deletions packages/blue-api-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./types";
export * from "./converter";
export * from "./warnings";
export * from "./cache";
export * from "./types.js";
export * from "./converter.js";
export * from "./warnings.js";
export * from "./cache.js";
8 changes: 8 additions & 0 deletions packages/blue-sdk-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "2.0.0-alpha.6",
"author": "Morpho Association <contact@morpho.org>",
"license": "MIT",
"type": "module",
"main": "src/index.ts",
"files": [
"lib"
Expand Down Expand Up @@ -46,6 +47,13 @@
},
"publishConfig": {
"main": "lib/index.js",
"types": "lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
},
"access": "public"
}
}
16 changes: 8 additions & 8 deletions packages/blue-sdk-ethers/src/fetch/Vault.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Provider, resolveProperties } from "ethers";
import { type Provider, resolveProperties } from "ethers";
import { MetaMorpho__factory, PublicAllocator__factory } from "ethers-types";

import {
AccrualVault,
Address,
type Address,
ChainUtils,
MarketId,
type MarketId,
Vault,
VaultConfig,
VaultPublicAllocatorConfig,
type VaultConfig,
type VaultPublicAllocatorConfig,
getChainAddresses,
} from "@morpho-org/blue-sdk";
import { FetchOptions } from "../types";
import { fetchVaultConfig } from "./VaultConfig";
import { fetchVaultMarketAllocation } from "./VaultMarketAllocation";
import type { FetchOptions } from "../types.js";
import { fetchVaultConfig } from "./VaultConfig.js";
import { fetchVaultMarketAllocation } from "./VaultMarketAllocation.js";

export async function fetchVault(
address: Address,
Expand Down
4 changes: 2 additions & 2 deletions packages/blue-sdk-ethers/test/e2e/Vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { ZeroAddress } from "ethers";
import { MetaMorpho__factory, PublicAllocator__factory } from "ethers-types";
import { ethers } from "hardhat";

import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import type { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import { setNextBlockTimestamp } from "@nomicfoundation/hardhat-network-helpers/dist/src/helpers/time";

import { ChainId, MarketId, addresses } from "@morpho-org/blue-sdk";
import { ChainId, type MarketId, addresses } from "@morpho-org/blue-sdk";
import { setUp } from "@morpho-org/morpho-test";

import { Vault } from "../../src/augment/Vault";
Expand Down
34 changes: 18 additions & 16 deletions packages/blue-sdk-viem-bundler/src/helpers/operations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
Address,
type Address,
DEFAULT_SLIPPAGE_TOLERANCE,
DEFAULT_SUPPLY_TARGET_UTILIZATION,
MarketId,
type MarketId,
MarketUtils,
MathLib,
NATIVE_ADDRESS,
Expand All @@ -13,13 +13,13 @@ import {
permissionedWrapperTokens,
} from "@morpho-org/blue-sdk";
import {
Erc20Operations,
MaybeDraft,
Operation,
Operations,
PublicAllocatorOptions,
SimulationResult,
SimulationState,
type Erc20Operations,
type MaybeDraft,
type Operation,
type Operations,
type PublicAllocatorOptions,
type SimulationResult,
type SimulationState,
handleOperation,
handleOperations,
produceImmutable,
Expand All @@ -29,7 +29,7 @@ import { entries, getLast, getValue, keys } from "@morpho-org/morpho-ts";

import { maxUint256 } from "viem";
import { BundlerErrors } from "../errors";
import {
import type {
BundlerOperation,
CallbackBundlerOperation,
InputBundlerOperation,
Expand Down Expand Up @@ -273,13 +273,13 @@ export const populateSubBundle = (

const borrowedAssets =
mainOperation.type === "Blue_Borrow"
? mainOperation.args.assets ??
market.toBorrowAssets(mainOperation.args.shares)
? (mainOperation.args.assets ??
market.toBorrowAssets(mainOperation.args.shares))
: 0n;
const withdrawnAssets =
mainOperation.type === "Blue_Withdraw"
? mainOperation.args.assets ??
market.toSupplyAssets(mainOperation.args.shares)
? (mainOperation.args.assets ??
market.toSupplyAssets(mainOperation.args.shares))
: 0n;

const newTotalSupplyAssets = market.totalSupplyAssets - withdrawnAssets;
Expand Down Expand Up @@ -673,7 +673,7 @@ export const finalizeBundle = (
token = startData.getMarket(operation.args.id).config.collateralToken;
break;
case "MetaMorpho_Withdraw":
token = startData.getVault(operation.address).config.asset;
token = startData.getVault(operation.address).asset;
break;
default:
return;
Expand Down Expand Up @@ -836,7 +836,9 @@ export const populateBundle = (
);

return subBundleOperations;
} catch (error: any) {
} catch (error) {
if (!(error instanceof Error)) throw error;

throw new BundlerErrors.Bundle(error, index, inputOperation, steps);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const handleMetaMorphoDepositOperation: OperationHandler<
{
type: "Erc20_Transfer",
sender: address,
address: vault.config.asset,
address: vault.asset,
args: {
amount: assets,
from: sender,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { maxUint256, zeroAddress } from "viem";
import { maxUint256 } from "viem";

import { MathLib } from "@morpho-org/blue-sdk";

Expand Down Expand Up @@ -38,8 +38,6 @@ export const handleMetaMorphoReallocateOperation: OperationHandler<
handleBlueOperation(
{
type: "Blue_Withdraw",
// Bypass balance check because the vault's token balance is not stored
// and it is checked with invariant `totalWithdrawn == totalSupplied`.
sender: address,
args: {
id,
Expand Down Expand Up @@ -71,9 +69,7 @@ export const handleMetaMorphoReallocateOperation: OperationHandler<
handleBlueOperation(
{
type: "Blue_Supply",
// Bypass balance check because the vault's token balance is not stored
// and it is checked with invariant `totalWithdrawn == totalSupplied`.
sender: zeroAddress,
sender: address,
args: {
id,
assets: suppliedAssets,
Expand All @@ -93,4 +89,12 @@ export const handleMetaMorphoReallocateOperation: OperationHandler<
totalSupplied,
totalWithdrawn,
);

// Update totalAssets as soon as the vault is interacted with (onchain, it's a dynamic view function).
// But do not accrue vault fee!
const accruedVault = data
.tryGetAccrualVault(address)
?.accrueInterest(data.block.timestamp);
if (accruedVault != null)
data.getVault(address).totalAssets = accruedVault.totalAssets;
};
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const handleMetaMorphoWithdrawOperation: OperationHandler<
{
type: "Erc20_Transfer",
sender: address,
address: vault.config.asset,
address: vault.asset,
args: {
amount: assets,
from: address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ describe("Blue_AccrueInterest", () => {
abi: blueAbi,
functionName: "accrueInterest",
args: [
{
collateralToken: usdc_wstEth.collateralToken,
loanToken: usdc_wstEth.loanToken,
oracle: usdc_wstEth.oracle,
irm: usdc_wstEth.irm,
lltv: usdc_wstEth.lltv,
},
usdc_wstEth as Pick<
typeof usdc_wstEth,
"collateralToken" | "loanToken" | "oracle" | "irm" | "lltv"
>,
],
});

Expand Down
Loading

0 comments on commit dcf25a8

Please sign in to comment.