Skip to content
41 changes: 4 additions & 37 deletions packages/blue-sdk-viem/src/fetch/vault-v2/VaultV2Adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import {
import type { Address, Client } from "viem";
import { getChainId, readContract } from "viem/actions";
import { morphoVaultV1AdapterFactoryAbi } from "../../abis";
import { morphoMarketV1AdapterFactoryAbi } from "../../abis";
import type { DeploylessFetchParameters } from "../../types";
import { fetchVaultV2MorphoMarketV1Adapter } from "./VaultV2MorphoMarketV1Adapter";
import { fetchAccrualVaultV2MorphoMarketV1Adapter } from "./VaultV2MorphoMarketV1Adapter";
import {
fetchAccrualVaultV2MorphoVaultV1Adapter,
fetchVaultV2MorphoVaultV1Adapter,
Expand All @@ -22,10 +19,9 @@ export async function fetchVaultV2Adapter(
parameters.chainId ??= await getChainId(client);
parameters.deployless ??= true;

const { morphoVaultV1AdapterFactory, morphoMarketV1AdapterFactory } =
getChainAddresses(parameters.chainId);
const { morphoVaultV1AdapterFactory } = getChainAddresses(parameters.chainId);

const [isMorphoVaultV1Adapter, isMorphoMarketV1Adapter] = await Promise.all([
const [isMorphoVaultV1Adapter] = await Promise.all([
morphoVaultV1AdapterFactory
? readContract(client, {
...parameters,
Expand All @@ -35,23 +31,11 @@ export async function fetchVaultV2Adapter(
args: [address],
})
: false,
morphoMarketV1AdapterFactory
? readContract(client, {
...parameters,
address: morphoMarketV1AdapterFactory,
abi: morphoMarketV1AdapterFactoryAbi,
functionName: "isMorphoMarketV1Adapter",
args: [address],
})
: false,
]);

if (isMorphoVaultV1Adapter)
return fetchVaultV2MorphoVaultV1Adapter(address, client, parameters);

if (isMorphoMarketV1Adapter)
return fetchVaultV2MorphoMarketV1Adapter(address, client, parameters);

throw new UnsupportedVaultV2AdapterError(address);
}

Expand All @@ -63,10 +47,9 @@ export async function fetchAccrualVaultV2Adapter(
parameters.chainId ??= await getChainId(client);
parameters.deployless ??= true;

const { morphoVaultV1AdapterFactory, morphoMarketV1AdapterFactory } =
getChainAddresses(parameters.chainId);
const { morphoVaultV1AdapterFactory } = getChainAddresses(parameters.chainId);

const [isMorphoVaultV1Adapter, isMorphoMarketV1Adapter] = await Promise.all([
const [isMorphoVaultV1Adapter] = await Promise.all([
morphoVaultV1AdapterFactory
? readContract(client, {
...parameters,
Expand All @@ -76,26 +59,10 @@ export async function fetchAccrualVaultV2Adapter(
args: [address],
})
: false,
morphoMarketV1AdapterFactory
? readContract(client, {
...parameters,
address: morphoMarketV1AdapterFactory,
abi: morphoMarketV1AdapterFactoryAbi,
functionName: "isMorphoMarketV1Adapter",
args: [address],
})
: false,
]);

if (isMorphoVaultV1Adapter)
return fetchAccrualVaultV2MorphoVaultV1Adapter(address, client, parameters);

if (isMorphoMarketV1Adapter)
return fetchAccrualVaultV2MorphoMarketV1Adapter(
address,
client,
parameters,
);

throw new UnsupportedVaultV2AdapterError(address);
}
255 changes: 127 additions & 128 deletions packages/blue-sdk-viem/test/VaultV2Adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
AccrualVaultV2,
CapacityLimitReason,
MathLib,
VaultV2MorphoMarketV1Adapter,
VaultV2MorphoVaultV1Adapter,
} from "@morpho-org/blue-sdk";
import {
Expand Down Expand Up @@ -32,16 +31,16 @@ const expectedDataVaultV1Adapter = new VaultV2MorphoVaultV1Adapter({
});

// VaultV2 with liquidity adapter marketV1
const vaultV2Address2 = "0x678b8851DFcA08E40F3e31C8ABd08dE3E8E14b64";
const vaultV2AdapterMarketV1Address =
"0x83831b31f225B3DD0e96C69D683606bE399Dc757";
// const vaultV2Address2 = "0x678b8851DFcA08E40F3e31C8ABd08dE3E8E14b64";
// const vaultV2AdapterMarketV1Address =
// "0x83831b31f225B3DD0e96C69D683606bE399Dc757";

const expectedDataMarketV1Adapter = new VaultV2MorphoMarketV1Adapter({
address: vaultV2AdapterMarketV1Address,
parentVault: vaultV2Address2,
skimRecipient: zeroAddress,
marketParamsList: [],
});
// const expectedDataMarketV1Adapter = new VaultV2MorphoMarketV1Adapter({
// address: vaultV2AdapterMarketV1Address,
// parentVault: vaultV2Address2,
// skimRecipient: zeroAddress,
// marketParamsList: [],
// });

describe("VaultV2Adapter", () => {
describe("should fetch vaultV1 adapter", () => {
Expand Down Expand Up @@ -70,31 +69,31 @@ describe("VaultV2Adapter", () => {
});
});

describe("should fetch marketV1 adapter", () => {
vaultV2Test("with deployless reads", async ({ client }) => {
const value = await fetchVaultV2Adapter(
vaultV2AdapterMarketV1Address,
client,
{
deployless: true,
},
);

expect(value).toStrictEqual(expectedDataMarketV1Adapter);
});

vaultV2Test("with multicall", async ({ client }) => {
const value = await fetchVaultV2Adapter(
vaultV2AdapterMarketV1Address,
client,
{
deployless: false,
},
);

expect(value).toStrictEqual(expectedDataMarketV1Adapter);
});
});
// describe("should fetch marketV1 adapter", () => {
// vaultV2Test("with deployless reads", async ({ client }) => {
// const value = await fetchVaultV2Adapter(
// vaultV2AdapterMarketV1Address,
// client,
// {
// deployless: true,
// },
// );

// expect(value).toStrictEqual(expectedDataMarketV1Adapter);
// });

// vaultV2Test("with multicall", async ({ client }) => {
// const value = await fetchVaultV2Adapter(
// vaultV2AdapterMarketV1Address,
// client,
// {
// deployless: false,
// },
// );

// expect(value).toStrictEqual(expectedDataMarketV1Adapter);
// });
// });
});

describe("LiquidityAdapter vaultV1", () => {
Expand Down Expand Up @@ -249,99 +248,99 @@ describe("LiquidityAdapter vaultV1", () => {
});
});

describe("LiquidityAdapter marketV1", () => {
describe("maxDeposit function", () => {
vaultV2Test("should be limited by balance", async ({ client }) => {
await client.writeContract({
account: "0x707D44b65BA91C42f212e8bB61f71cc69fBf8fd7",
address: vaultV2Address2,
abi: vaultV2Abi,
functionName: "setCurator",
args: [curator],
});
const data = encodeFunctionData({
abi: vaultV2Abi,
functionName: "setIsAllocator",
args: [allocator, true],
});
await client.writeContract({
account: curator,
address: vaultV2Address2,
abi: vaultV2Abi,
functionName: "submit",
args: [data],
});
await client.writeContract({
account: "0x707D44b65BA91C42f212e8bB61f71cc69fBf8fd7",
address: vaultV2Address2,
abi: vaultV2Abi,
functionName: "setIsAllocator",
args: [allocator, true],
});
await client.writeContract({
account: allocator,
address: vaultV2Address2,
abi: vaultV2Abi,
functionName: "setLiquidityAdapterAndData",
args: [vaultV2AdapterMarketV1Address, "0x"],
});
const accrualVaultV2 = await fetchAccrualVaultV2(vaultV2Address, client);

const assets = parseUnits("100000", 6); // 100K assets
const result = accrualVaultV2.maxDeposit(assets);
expect(result).toStrictEqual({
value: assets,
limiter: CapacityLimitReason.balance,
});
});
});

describe("maxWithdraw function", () => {
vaultV2Test("should be limited by balance", async ({ client }) => {
await client.writeContract({
account: "0x707D44b65BA91C42f212e8bB61f71cc69fBf8fd7",
address: vaultV2Address2,
abi: vaultV2Abi,
functionName: "setCurator",
args: [curator],
});
const data = encodeFunctionData({
abi: vaultV2Abi,
functionName: "setIsAllocator",
args: [allocator, true],
});
await client.writeContract({
account: curator,
address: vaultV2Address2,
abi: vaultV2Abi,
functionName: "submit",
args: [data],
});
await client.writeContract({
account: "0x707D44b65BA91C42f212e8bB61f71cc69fBf8fd7",
address: vaultV2Address2,
abi: vaultV2Abi,
functionName: "setIsAllocator",
args: [allocator, true],
});
await client.writeContract({
account: allocator,
address: vaultV2Address2,
abi: vaultV2Abi,
functionName: "setLiquidityAdapterAndData",
args: [vaultV2AdapterMarketV1Address, "0x"],
});
const accrualVaultV2 = await fetchAccrualVaultV2(vaultV2Address, client);

const shares = parseUnits("100000", 6); // 100K shares
const result = accrualVaultV2.maxWithdraw(shares);
expect(result).toStrictEqual({
value: 0n,
limiter: CapacityLimitReason.balance,
});
});
});
});
// describe("LiquidityAdapter marketV1", () => {
// describe("maxDeposit function", () => {
// vaultV2Test("should be limited by balance", async ({ client }) => {
// await client.writeContract({
// account: "0x707D44b65BA91C42f212e8bB61f71cc69fBf8fd7",
// address: vaultV2Address2,
// abi: vaultV2Abi,
// functionName: "setCurator",
// args: [curator],
// });
// const data = encodeFunctionData({
// abi: vaultV2Abi,
// functionName: "setIsAllocator",
// args: [allocator, true],
// });
// await client.writeContract({
// account: curator,
// address: vaultV2Address2,
// abi: vaultV2Abi,
// functionName: "submit",
// args: [data],
// });
// await client.writeContract({
// account: "0x707D44b65BA91C42f212e8bB61f71cc69fBf8fd7",
// address: vaultV2Address2,
// abi: vaultV2Abi,
// functionName: "setIsAllocator",
// args: [allocator, true],
// });
// await client.writeContract({
// account: allocator,
// address: vaultV2Address2,
// abi: vaultV2Abi,
// functionName: "setLiquidityAdapterAndData",
// args: [vaultV2AdapterMarketV1Address, "0x"],
// });
// const accrualVaultV2 = await fetchAccrualVaultV2(vaultV2Address, client);

// const assets = parseUnits("100000", 6); // 100K assets
// const result = accrualVaultV2.maxDeposit(assets);
// expect(result).toStrictEqual({
// value: assets,
// limiter: CapacityLimitReason.balance,
// });
// });
// });

// describe("maxWithdraw function", () => {
// vaultV2Test("should be limited by balance", async ({ client }) => {
// await client.writeContract({
// account: "0x707D44b65BA91C42f212e8bB61f71cc69fBf8fd7",
// address: vaultV2Address2,
// abi: vaultV2Abi,
// functionName: "setCurator",
// args: [curator],
// });
// const data = encodeFunctionData({
// abi: vaultV2Abi,
// functionName: "setIsAllocator",
// args: [allocator, true],
// });
// await client.writeContract({
// account: curator,
// address: vaultV2Address2,
// abi: vaultV2Abi,
// functionName: "submit",
// args: [data],
// });
// await client.writeContract({
// account: "0x707D44b65BA91C42f212e8bB61f71cc69fBf8fd7",
// address: vaultV2Address2,
// abi: vaultV2Abi,
// functionName: "setIsAllocator",
// args: [allocator, true],
// });
// await client.writeContract({
// account: allocator,
// address: vaultV2Address2,
// abi: vaultV2Abi,
// functionName: "setLiquidityAdapterAndData",
// args: [vaultV2AdapterMarketV1Address, "0x"],
// });
// const accrualVaultV2 = await fetchAccrualVaultV2(vaultV2Address, client);

// const shares = parseUnits("100000", 6); // 100K shares
// const result = accrualVaultV2.maxWithdraw(shares);
// expect(result).toStrictEqual({
// value: 0n,
// limiter: CapacityLimitReason.balance,
// });
// });
// });
// });

describe("LiquidityAdapter zero address", () => {
vaultV2Test(
Expand Down
Loading