Skip to content

Commit

Permalink
Merge branch 'main' into feature/integ-1208-liquidate-all-positions-w…
Browse files Browse the repository at this point in the history
…ith-bad-debt
  • Loading branch information
Rubilmax authored Jan 6, 2025
2 parents 108ac15 + ea5e032 commit 38e8622
Show file tree
Hide file tree
Showing 56 changed files with 1,151 additions and 4,462 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@
"@biomejs/biome": "^1.9.4",
"@commitlint/cli": "^19.6.0",
"@commitlint/config-conventional": "^19.6.0",
"@conventional-changelog/git-client": "^1.0.1",
"@vitest/coverage-v8": "^2.1.8",
"conventional-changelog-conventionalcommits": "^8.0.0",
"conventional-changelog-writer": "^8.0.0",
"conventional-recommended-bump": "^10.0.0",
"dotenv-cli": "^7.4.4",
"happy-dom": "^15.11.7",
"happy-dom": "^16.2.0",
"husky": "^9.1.7",
"lint-staged": "^15.2.10",
"lint-staged": "^15.2.11",
"semver": "^7.6.3",
"typescript": "^5.7.2",
"viem": "^2.21.54",
"viem": "^2.22.2",
"vitest": "^2.1.8"
},
"lint-staged": {
Expand Down
126 changes: 76 additions & 50 deletions packages/blue-api-sdk/src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MathLib,
Position,
Token,
Vault,
VaultConfig,
VaultMarketAllocation,
VaultMarketConfig,
Expand Down Expand Up @@ -95,15 +96,7 @@ export interface PartialApiPublicAllocatorConfig
}

export interface PartialApiVaultAllocation
extends Pick<
ApiVaultAllocation,
| "supplyQueueIndex"
| "supplyShares"
| "supplyCap"
| "pendingSupplyCap"
| "pendingSupplyCapValidAt"
| "removableAt"
> {
extends Pick<ApiVaultAllocation, "supplyQueueIndex"> {
market: PartialApiMarket & Pick<ApiMarket, "uniqueKey">;
}

Expand Down Expand Up @@ -135,6 +128,25 @@ export interface PartialApiVault
publicAllocatorConfig: Maybe<PartialApiPublicAllocatorConfig>;
}

export interface PartialApiAccrualVaultAllocation
extends PartialApiVaultAllocation,
Pick<
ApiVaultAllocation,
| "supplyShares"
| "supplyCap"
| "pendingSupplyCap"
| "pendingSupplyCapValidAt"
| "removableAt"
> {}

export interface PartialApiAccrualVaultState extends PartialApiVaultState {
allocation: Maybe<PartialApiAccrualVaultAllocation[]>;
}

export interface PartialApiAccrualVault extends PartialApiVault {
state: Maybe<PartialApiAccrualVaultState>;
}

export interface ConverterOptions {
parseAddress: (value: string) => Address;
parseNumber: (value: number, decimals: number) => bigint;
Expand Down Expand Up @@ -180,6 +192,8 @@ export class BlueSdkConverter {
});
}

public getMarket(dto: PartialApiMarket & { state: null }): null;
public getMarket(dto: PartialApiMarket): Market;
public getMarket(dto: PartialApiMarket) {
if (dto.state == null) return null;

Expand Down Expand Up @@ -208,6 +222,12 @@ export class BlueSdkConverter {
});
}

public getAccrualPosition(
dto: PartialApiMarketAccrualPosition & { market: { state: null } },
): null;
public getAccrualPosition(
dto: PartialApiMarketAccrualPosition,
): AccrualPosition;
public getAccrualPosition(dto: PartialApiMarketAccrualPosition) {
const market = this.getMarket(dto.market);
if (market == null) return null;
Expand All @@ -232,20 +252,44 @@ export class BlueSdkConverter {
);
}

public getVault(dto: PartialApiVault & { state: null }): null;
public getVault(dto: PartialApiVault): Vault;
public getVault({ state, publicAllocatorConfig, ...dto }: PartialApiVault) {
if (state == null) return null;

return new Vault({
...state,
...this.getVaultConfig(dto),
fee: this.options.parseNumber(state.fee ?? 0, 18),
pendingOwner: state.pendingOwner ?? ZERO_ADDRESS,
pendingTimelock: {
value: state.pendingTimelock ?? 0n,
validAt: state.pendingTimelockValidAt ?? 0n,
},
pendingGuardian: {
value: state.pendingGuardian ?? ZERO_ADDRESS,
validAt: state.pendingGuardianValidAt ?? 0n,
},
lastTotalAssets: state.totalAssets,
supplyQueue:
state.allocation
?.filter((allocation) => isDefined(allocation.supplyQueueIndex))
.sort(
(allocationA, allocationB) =>
allocationA.supplyQueueIndex! - allocationB.supplyQueueIndex!,
)
.map((allocation) => allocation.market.uniqueKey) ?? [],
withdrawQueue:
state.allocation?.map(({ market }) => market.uniqueKey) ?? [],
publicAllocatorConfig: publicAllocatorConfig ?? undefined,
});
}

public getVaultMarketAllocation(
vault: Address,
dto: PartialApiVaultAllocation,
dto: PartialApiAccrualVaultAllocation,
publicAllocatorConfig?: Maybe<PartialApiPublicAllocatorConfig>,
) {
const position = this.getAccrualPosition({
user: { address: vault },
market: dto.market,
supplyShares: dto.supplyShares,
borrowShares: 0n,
collateral: 0n,
});
if (!position) return;

return new VaultMarketAllocation({
config: this.getVaultMarketConfig(
vault,
Expand All @@ -254,13 +298,19 @@ export class BlueSdkConverter {
({ market: { uniqueKey } }) => uniqueKey === dto.market.uniqueKey,
),
),
position,
position: this.getAccrualPosition({
user: { address: vault },
market: dto.market,
supplyShares: dto.supplyShares,
borrowShares: 0n,
collateral: 0n,
}),
});
}

public getVaultMarketConfig(
vault: Address,
dto: PartialApiVaultAllocation,
dto: PartialApiAccrualVaultAllocation,
flowCaps?: Maybe<PartialApiPublicAllocatorFlowCaps>,
) {
return new VaultMarketConfig({
Expand Down Expand Up @@ -291,38 +341,14 @@ export class BlueSdkConverter {
});
}

public getAccrualVault({
state,
publicAllocatorConfig,
...dto
}: PartialApiVault) {
public getAccrualVault(dto: PartialApiAccrualVault & { state: null }): null;
public getAccrualVault(dto: PartialApiAccrualVault): AccrualVault;
public getAccrualVault(dto: PartialApiAccrualVault) {
const { state, publicAllocatorConfig } = dto;
if (state == null) return null;

return new AccrualVault(
{
...state,
...this.getVaultConfig(dto),
fee: this.options.parseNumber(state.fee, 18),
pendingOwner: state.pendingOwner ?? ZERO_ADDRESS,
pendingTimelock: {
value: state.pendingTimelock ?? 0n,
validAt: state.pendingTimelockValidAt ?? 0n,
},
pendingGuardian: {
value: state.pendingGuardian ?? ZERO_ADDRESS,
validAt: state.pendingGuardianValidAt ?? 0n,
},
lastTotalAssets: state.totalAssets,
supplyQueue:
state.allocation
?.filter((allocation) => isDefined(allocation.supplyQueueIndex))
.sort(
(allocationA, allocationB) =>
allocationA.supplyQueueIndex! - allocationB.supplyQueueIndex!,
)
.map((allocation) => allocation.market.uniqueKey) ?? [],
publicAllocatorConfig: publicAllocatorConfig || undefined,
},
this.getVault(dto),
state.allocation
?.map((allocation) =>
this.getVaultMarketAllocation(
Expand Down
2 changes: 1 addition & 1 deletion packages/blue-sdk-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"ethers": "^6.13.4",
"ethers-types": "^3.17.3",
"typescript": "^5.7.2",
"viem": "^2.21.54",
"viem": "^2.22.2",
"vitest": "^2.1.8"
},
"publishConfig": {
Expand Down
4 changes: 1 addition & 3 deletions packages/blue-sdk-ethers/src/augment/Vault.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { AccrualVault, Vault } from "@morpho-org/blue-sdk";

import { fetchAccrualVault, fetchVault, fetchVaultFromConfig } from "../fetch";
import { fetchAccrualVault, fetchVault } from "../fetch";

declare module "@morpho-org/blue-sdk" {
namespace Vault {
let fetch: typeof fetchVault;
let fetchFromConfig: typeof fetchVaultFromConfig;
}

namespace AccrualVault {
Expand All @@ -14,7 +13,6 @@ declare module "@morpho-org/blue-sdk" {
}

Vault.fetch = fetchVault;
Vault.fetchFromConfig = fetchVaultFromConfig;
AccrualVault.fetch = fetchAccrualVault;

export { Vault, AccrualVault };
26 changes: 4 additions & 22 deletions packages/blue-sdk-ethers/src/fetch/Holding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,9 @@ export async function fetchHolding(
balance: await runner.provider.getBalance(user, overrides.blockTag),
});

const erc20 = ERC20__factory.connect(
token,
// @ts-ignore incompatible commonjs type
runner,
);
const permit2 = Permit2__factory.connect(
chainAddresses.permit2,
// @ts-ignore incompatible commonjs type
runner,
);
const erc2612 = ERC2612__factory.connect(
token,
// @ts-ignore incompatible commonjs type
runner,
);
const erc20 = ERC20__factory.connect(token, runner);
const permit2 = Permit2__factory.connect(chainAddresses.permit2, runner);
const erc2612 = ERC2612__factory.connect(token, runner);

const [
balance,
Expand Down Expand Up @@ -107,15 +95,10 @@ export async function fetchHolding(
permissionedBackedTokens[chainId].has(token)
? WrappedBackedToken__factory.connect(
token,
// @ts-ignore incompatible commonjs type
runner,
).whitelistControllerAggregator(overrides)
: undefined,
PermissionedERC20Wrapper__factory.connect(
token,
// @ts-ignore incompatible commonjs type
runner,
)
PermissionedERC20Wrapper__factory.connect(token, runner)
.hasPermission(user, overrides)
.catch(() => !permissionedWrapperTokens[chainId].has(token)),
]);
Expand All @@ -134,7 +117,6 @@ export async function fetchHolding(
holding.canTransfer =
await BackedWhitelistControllerAggregatorV2__factory.connect(
whitelistControllerAggregator,
// @ts-ignore incompatible commonjs type
runner,
)
.isWhitelisted(user, overrides)
Expand Down
13 changes: 2 additions & 11 deletions packages/blue-sdk-ethers/src/fetch/Market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,15 @@ export async function fetchMarketFromConfig(
price,
rateAtTarget,
] = await Promise.all([
MorphoBlue__factory.connect(
morpho,
// @ts-ignore incompatible commonjs type
runner,
).market(params.id, overrides),
MorphoBlue__factory.connect(morpho, runner).market(params.id, overrides),
params.oracle !== ZeroAddress
? BlueOracle__factory.connect(
params.oracle,
// @ts-ignore incompatible commonjs type
runner,
)
? BlueOracle__factory.connect(params.oracle, runner)
.price(overrides)
.catch(() => undefined)
: undefined,
params.irm === adaptiveCurveIrm
? await AdaptiveCurveIrm__factory.connect(
params.irm,
// @ts-ignore incompatible commonjs type
runner,
).rateAtTarget(params.id, overrides)
: undefined,
Expand Down
1 change: 0 additions & 1 deletion packages/blue-sdk-ethers/src/fetch/MarketParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export async function fetchMarketParams(

const marketParams = await MorphoBlue__factory.connect(
morpho,
// @ts-ignore incompatible commonjs type
runner,
).idToMarketParams(id, {
// Always fetch at latest block because config is immutable.
Expand Down
10 changes: 5 additions & 5 deletions packages/blue-sdk-ethers/src/fetch/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export async function fetchPosition(
const { morpho } = getChainAddresses(chainId);

const { supplyShares, borrowShares, collateral } =
await MorphoBlue__factory.connect(
morpho,
// @ts-ignore incompatible commonjs type
runner,
).position(marketId, user, overrides);
await MorphoBlue__factory.connect(morpho, runner).position(
marketId,
user,
overrides,
);

return new Position({
user,
Expand Down
Loading

0 comments on commit 38e8622

Please sign in to comment.