Skip to content

Commit

Permalink
Merge pull request #134 from morpho-org/refactor/market-config
Browse files Browse the repository at this point in the history
Refactor `MarketConfig` -> `MarketParams`
  • Loading branch information
Rubilmax authored Oct 21, 2024
2 parents dadc64e + 6185068 commit 16259f8
Show file tree
Hide file tree
Showing 57 changed files with 521 additions and 521 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
node-version-file: .nvmrc
cache: pnpm

- uses: foundry-rs/foundry-toolchain@v1
- uses: foundry-rs/foundry-toolchain@v1.2.0

- run: pnpm install

Expand Down
14 changes: 7 additions & 7 deletions packages/blue-api-sdk/src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AccrualVault,
type Address,
Market,
MarketConfig,
MarketParams,
MathLib,
Position,
TokenWithPrice,
Expand Down Expand Up @@ -41,14 +41,14 @@ export interface PartialBlueApiToken
name?: BlueApiToken["name"];
}

export interface PartialBlueApiMarketConfig
export interface PartialBlueApiMarketParams
extends Pick<BlueApiMarket, "oracleAddress" | "irmAddress" | "lltv"> {
collateralAsset: Maybe<Pick<BlueApiToken, "address">>;
loanAsset: Pick<BlueApiToken, "address">;
}

export interface PartialBlueApiMarket
extends PartialBlueApiMarketConfig,
extends PartialBlueApiMarketParams,
Pick<BlueApiMarket, "collateralPrice"> {
state: Maybe<
Pick<
Expand Down Expand Up @@ -173,8 +173,8 @@ export class BlueSdkConverter {
);
}

public getMarketConfig(dto: PartialBlueApiMarketConfig) {
return new MarketConfig({
public getMarketParams(dto: PartialBlueApiMarketParams) {
return new MarketParams({
collateralToken: this.options.parseAddress(
dto.collateralAsset?.address ?? ZERO_ADDRESS,
),
Expand All @@ -188,7 +188,7 @@ export class BlueSdkConverter {
public getMarket(dto: PartialBlueApiMarket) {
if (dto.state == null) return null;

const config = this.getMarketConfig(dto);
const params = this.getMarketParams(dto);
const fee = this.options.parseNumber(dto.state.fee, 18);
const price = dto.collateralPrice ?? 1n;

Expand All @@ -201,7 +201,7 @@ export class BlueSdkConverter {
: undefined;

return new Market({
config,
params,
totalSupplyAssets: dto.state.supplyAssets,
totalBorrowAssets: dto.state.borrowAssets,
totalSupplyShares: dto.state.supplyShares,
Expand Down
10 changes: 5 additions & 5 deletions packages/blue-sdk-ethers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Opt in classes augmentation to easily fetch an entire entity of the Morpho Blue
import "@morpho-org/blue-sdk-ethers/lib/augment/AccrualPosition";
import "@morpho-org/blue-sdk-ethers/lib/augment/Holding";
import "@morpho-org/blue-sdk-ethers/lib/augment/Market";
import "@morpho-org/blue-sdk-ethers/lib/augment/MarketConfig";
import "@morpho-org/blue-sdk-ethers/lib/augment/MarketParams";
import "@morpho-org/blue-sdk-ethers/lib/augment/Position";
import "@morpho-org/blue-sdk-ethers/lib/augment/Token";
import "@morpho-org/blue-sdk-ethers/lib/augment/VaultConfig";
Expand All @@ -60,14 +60,14 @@ import "@morpho-org/blue-sdk-ethers/lib/augment";

### Fetch the config of a specific market

Leverage the [`MarketConfig`](./src/market/MarketConfig.ts) class to fetch information on a given market's immutable configuration:
Leverage the [`MarketParams`](./src/market/MarketParams.ts) class to fetch information on a given market's immutable configuration:

```typescript
import { MarketId } from "@morpho-org/blue-sdk";
// /!\ Import MarketConfig from the augmentation file (or simply import the file)
import { MarketConfig } from "@morpho-org/blue-sdk-ethers/lib/augment/MarketConfig";
// /!\ Import MarketParams from the augmentation file (or simply import the file)
import { MarketParams } from "@morpho-org/blue-sdk-ethers/lib/augment/MarketParams";

const config = await MarketConfig.fetch(
const config = await MarketParams.fetch(
"0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc" as MarketId,
provider // Ethers provider.
);
Expand Down
12 changes: 0 additions & 12 deletions packages/blue-sdk-ethers/src/augment/MarketConfig.ts

This file was deleted.

12 changes: 12 additions & 0 deletions packages/blue-sdk-ethers/src/augment/MarketParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MarketParams } from "@morpho-org/blue-sdk";
import { fetchMarketParams } from "../fetch/index.js";

declare module "@morpho-org/blue-sdk" {
namespace MarketParams {
let fetch: typeof fetchMarketParams;
}
}

MarketParams.fetch = fetchMarketParams;

export { MarketParams };
2 changes: 1 addition & 1 deletion packages/blue-sdk-ethers/src/augment/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./Holding.js";
export * from "./Position.js";
export * from "./MarketConfig.js";
export * from "./MarketParams.js";
export * from "./Market.js";
export * from "./Token.js";
export * from "./User.js";
Expand Down
22 changes: 11 additions & 11 deletions packages/blue-sdk-ethers/src/fetch/Market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
import {
ChainUtils,
Market,
type MarketConfig,
type MarketId,
type MarketParams,
getChainAddresses,
} from "@morpho-org/blue-sdk";
import type { FetchOptions } from "../types.js";
import { fetchMarketConfig } from "./MarketConfig.js";
import { fetchMarketParams } from "./MarketParams.js";

export async function fetchMarket(
id: MarketId,
Expand All @@ -24,13 +24,13 @@ export async function fetchMarket(
chainId ?? (await runner.provider.getNetwork()).chainId,
);

const config = await fetchMarketConfig(id, runner, { chainId });
const config = await fetchMarketParams(id, runner, { chainId });

return fetchMarketFromConfig(config, runner, { chainId, overrides });
}

export async function fetchMarketFromConfig(
config: MarketConfig,
params: MarketParams,
runner: { provider: Provider },
{ chainId, overrides = {} }: FetchOptions = {},
) {
Expand All @@ -56,25 +56,25 @@ export async function fetchMarketFromConfig(
morpho,
// @ts-ignore incompatible commonjs type
runner,
).market(config.id, overrides),
config.oracle !== ZeroAddress
).market(params.id, overrides),
params.oracle !== ZeroAddress
? BlueOracle__factory.connect(
config.oracle,
params.oracle,
// @ts-ignore incompatible commonjs type
runner,
).price(overrides)
: 0n,
config.irm === adaptiveCurveIrm
params.irm === adaptiveCurveIrm
? await AdaptiveCurveIrm__factory.connect(
config.irm,
params.irm,
// @ts-ignore incompatible commonjs type
runner,
).rateAtTarget(config.id, overrides)
).rateAtTarget(params.id, overrides)
: undefined,
]);

return new Market({
config,
params,
totalSupplyAssets,
totalBorrowAssets,
totalSupplyShares,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import {
type Address,
type ChainId,
ChainUtils,
MarketConfig,
type MarketId,
UnknownMarketConfigError,
MarketParams,
UnknownMarketParamsError,
_try,
getChainAddresses,
} from "@morpho-org/blue-sdk";

export async function fetchMarketConfig(
export async function fetchMarketParams(
id: MarketId,
runner: { provider: Provider },
{ chainId }: { chainId?: ChainId } = {},
) {
let config = _try(() => MarketConfig.get(id), UnknownMarketConfigError);
let config = _try(() => MarketParams.get(id), UnknownMarketParamsError);

if (!config) {
chainId = ChainUtils.parseSupportedChainId(
Expand All @@ -35,7 +35,7 @@ export async function fetchMarketConfig(
blockTag: "latest",
});

config = new MarketConfig({
config = new MarketParams({
lltv: marketParams.lltv,
loanToken: marketParams.loanToken as Address,
collateralToken: marketParams.collateralToken as Address,
Expand Down
4 changes: 2 additions & 2 deletions packages/blue-sdk-ethers/src/fetch/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
AccrualPosition,
type Address,
ChainUtils,
type MarketConfig,
type MarketId,
type MarketParams,
Position,
getChainAddresses,
} from "@morpho-org/blue-sdk";
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function fetchAccrualPosition(

export async function fetchAccrualPositionFromConfig(
user: Address,
config: MarketConfig,
config: MarketParams,
runner: { provider: Provider },
options: FetchOptions = {},
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/blue-sdk-ethers/src/fetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./Holding.js";
export * from "./Position.js";
export * from "./MarketConfig.js";
export * from "./MarketParams.js";
export * from "./Market.js";
export * from "./Token.js";
export * from "./User.js";
Expand Down
18 changes: 9 additions & 9 deletions packages/blue-sdk-ethers/test/e2e/Market.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainId, MarketConfig, addresses } from "@morpho-org/blue-sdk";
import { ChainId, MarketParams, addresses } from "@morpho-org/blue-sdk";

import { markets } from "@morpho-org/morpho-test";
import { randomAddress } from "@morpho-org/test";
Expand All @@ -13,7 +13,7 @@ const { usdc_wstEth, usdc_idle, eth_wstEth } = markets[ChainId.EthMainnet];
describe("augment/Market", () => {
test("should fetch market data", async ({ wallet }) => {
const expectedData = new Market({
config: usdc_wstEth,
params: usdc_wstEth,
totalSupplyAssets: 32212092216793n,
totalSupplyShares: 31693536738210306937n,
totalBorrowAssets: 30448219939637n,
Expand All @@ -31,7 +31,7 @@ describe("augment/Market", () => {

test("should fetch price and rate if idle market", async ({ wallet }) => {
const expectedData = new Market({
config: usdc_idle,
params: usdc_idle,
totalSupplyAssets: 0n,
totalSupplyShares: 0n,
totalBorrowAssets: 0n,
Expand All @@ -57,13 +57,13 @@ describe("augment/Market", () => {
functionName: "owner",
});

const config = new MarketConfig({
const params = new MarketParams({
...eth_wstEth,
irm: randomAddress(),
});

await client.setCode({
address: config.irm,
address: params.irm,
bytecode: (await client.getCode({
address: adaptiveCurveIrm,
}))!,
Expand All @@ -75,7 +75,7 @@ describe("augment/Market", () => {
address: morpho,
abi: blueAbi,
functionName: "enableIrm",
args: [config.irm],
args: [params.irm],
});

const timestamp = (await client.timestamp()) + 3n;
Expand All @@ -86,11 +86,11 @@ describe("augment/Market", () => {
address: morpho,
abi: blueAbi,
functionName: "createMarket",
args: [{ ...config }],
args: [{ ...params }],
});

const expectedData = new Market({
config,
params,
totalSupplyAssets: 0n,
totalSupplyShares: 0n,
totalBorrowAssets: 0n,
Expand All @@ -101,7 +101,7 @@ describe("augment/Market", () => {
rateAtTarget: undefined,
});

const value = await Market.fetch(config.id, wallet);
const value = await Market.fetch(params.id, wallet);

expect(value).toStrictEqual(expectedData);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { zeroAddress } from "viem";

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

import { MarketConfig } from "../../src/augment/MarketConfig.js";
import { MarketParams } from "../../src/augment/MarketParams.js";
import { test } from "./setup.js";

import { markets } from "@morpho-org/morpho-test";
import { describe, expect } from "vitest";

const { usdc_wstEth } = markets[ChainId.EthMainnet];

describe("augment/MarketConfig", () => {
describe("augment/MarketParams", () => {
test("should fetch config from cache", async ({ wallet }) => {
const market = await MarketConfig.fetch(usdc_wstEth.id, wallet);
const market = await MarketParams.fetch(usdc_wstEth.id, wallet);

expect(market).toStrictEqual(usdc_wstEth);
});
Expand All @@ -28,7 +28,7 @@ describe("augment/MarketConfig", () => {
liquidationIncentiveFactor: 1150000000000000000n,
};

const market = await MarketConfig.fetch(
const market = await MarketParams.fetch(
"0x58e212060645d18eab6d9b2af3d56fbc906a92ff5667385f616f662c70372284" as MarketId,
wallet,
);
Expand Down
8 changes: 4 additions & 4 deletions packages/blue-sdk-viem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Opt in classes augmentation to easily fetch an entire entity of the Morpho Blue
import "@morpho-org/blue-sdk-viem/lib/augment/AccrualPosition";
import "@morpho-org/blue-sdk-viem/lib/augment/Holding";
import "@morpho-org/blue-sdk-viem/lib/augment/Market";
import "@morpho-org/blue-sdk-viem/lib/augment/MarketConfig";
import "@morpho-org/blue-sdk-viem/lib/augment/MarketParams";
import "@morpho-org/blue-sdk-viem/lib/augment/Position";
import "@morpho-org/blue-sdk-viem/lib/augment/Token";
import "@morpho-org/blue-sdk-viem/lib/augment/VaultConfig";
Expand All @@ -60,14 +60,14 @@ import "@morpho-org/blue-sdk-viem/lib/augment";

### Fetch the config of a specific market

Leverage the [`MarketConfig`](./src/market/MarketConfig.ts) class to fetch information on a given market's immutable configuration:
Leverage the [`MarketParams`](./src/market/MarketParams.ts) class to fetch information on a given market's immutable configuration:

```typescript
import { MarketId } from "@morpho-org/blue-sdk";
// /!\ Import AccrualPosition from the augmentation file (or simply import the file)
import { MarketConfig } from "@morpho-org/blue-sdk-viem/lib/augment/MarketConfig";
import { MarketParams } from "@morpho-org/blue-sdk-viem/lib/augment/MarketParams";

const config = await MarketConfig.fetch(
const config = await MarketParams.fetch(
"0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc" as MarketId,
client // viem client.
);
Expand Down
12 changes: 0 additions & 12 deletions packages/blue-sdk-viem/src/augment/MarketConfig.ts

This file was deleted.

Loading

0 comments on commit 16259f8

Please sign in to comment.