Skip to content

Commit 3482ce4

Browse files
committed
refactor(vault-v2): introduce isUnknownOfFactoryError utility for improved error handling
1 parent 6e58392 commit 3482ce4

File tree

5 files changed

+29
-65
lines changed

5 files changed

+29
-65
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { BaseError, ContractFunctionRevertedError } from "viem";
2+
3+
/**
4+
* Checks if an error is a contract revert with the "UnknownOfFactory" error name.
5+
* Used to propagate factory validation errors instead of falling back to multicall.
6+
*/
7+
export function isUnknownOfFactoryError(error: unknown): boolean {
8+
if (!(error instanceof BaseError)) return false;
9+
10+
const revertError = error.walk(
11+
(err) => err instanceof ContractFunctionRevertedError,
12+
);
13+
14+
return (
15+
revertError instanceof ContractFunctionRevertedError &&
16+
revertError.data?.errorName === "UnknownOfFactory"
17+
);
18+
}

packages/blue-sdk-viem/src/fetch/vault-v2/VaultV2.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import {
1010
} from "@morpho-org/blue-sdk";
1111
import {
1212
type Address,
13-
BaseError,
1413
type Client,
15-
ContractFunctionRevertedError,
1614
type Hash,
1715
erc20Abi,
1816
zeroAddress,
@@ -24,6 +22,7 @@ import {
2422
vaultV2Abi,
2523
vaultV2FactoryAbi,
2624
} from "../../abis";
25+
import { isUnknownOfFactoryError } from "../../error";
2726
import { abi, code } from "../../queries/vault-v2/GetVaultV2";
2827
import type { DeploylessFetchParameters } from "../../types";
2928
import { fetchToken } from "../Token";
@@ -73,17 +72,7 @@ export async function fetchVaultV2(
7372
});
7473
} catch (error) {
7574
if (deployless === "force") throw error;
76-
77-
if (error instanceof BaseError) {
78-
const revertError = error.walk(
79-
(err) => err instanceof ContractFunctionRevertedError,
80-
);
81-
if (
82-
revertError instanceof ContractFunctionRevertedError &&
83-
revertError.data?.errorName === "UnknownOfFactory"
84-
)
85-
throw error;
86-
}
75+
if (isUnknownOfFactoryError(error)) throw error;
8776
// Fallback to multicall if deployless call fails.
8877
}
8978
}

packages/blue-sdk-viem/src/fetch/vault-v2/VaultV2MorphoMarketV1Adapter.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ import {
55
VaultV2MorphoMarketV1Adapter,
66
getChainAddresses,
77
} from "@morpho-org/blue-sdk";
8-
import {
9-
type Address,
10-
BaseError,
11-
type Client,
12-
ContractFunctionRevertedError,
13-
} from "viem";
8+
import type { Address, Client } from "viem";
149
import { getChainId, readContract } from "viem/actions";
1510
import {
1611
morphoMarketV1AdapterAbi,
1712
morphoMarketV1AdapterFactoryAbi,
1813
} from "../../abis";
14+
import { isUnknownOfFactoryError } from "../../error";
1915
import {
2016
abi,
2117
code,
@@ -56,18 +52,8 @@ export async function fetchVaultV2MorphoMarketV1Adapter(
5652
});
5753
} catch (error) {
5854
if (deployless === "force") throw error;
55+
if (isUnknownOfFactoryError(error)) throw error;
5956
// Fallback to multicall if deployless call fails.
60-
61-
if (error instanceof BaseError) {
62-
const revertError = error.walk(
63-
(err) => err instanceof ContractFunctionRevertedError,
64-
);
65-
if (
66-
revertError instanceof ContractFunctionRevertedError &&
67-
revertError.data?.errorName === "UnknownOfFactory"
68-
)
69-
throw error;
70-
}
7157
}
7258
}
7359

packages/blue-sdk-viem/src/fetch/vault-v2/VaultV2MorphoMarketV1AdapterV2.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ import {
77
getChainAddresses,
88
} from "@morpho-org/blue-sdk";
99
import { fromEntries } from "@morpho-org/morpho-ts";
10-
import {
11-
type Address,
12-
BaseError,
13-
type Client,
14-
ContractFunctionRevertedError,
15-
} from "viem";
10+
import type { Address, Client } from "viem";
1611
import { getChainId, readContract } from "viem/actions";
1712
import {
1813
morphoMarketV1AdapterV2Abi,
1914
morphoMarketV1AdapterV2FactoryAbi,
2015
} from "../../abis";
16+
import { isUnknownOfFactoryError } from "../../error";
2117
import {
2218
abi,
2319
code,
@@ -67,18 +63,8 @@ export async function fetchVaultV2MorphoMarketV1AdapterV2(
6763
});
6864
} catch (error) {
6965
if (deployless === "force") throw error;
66+
if (isUnknownOfFactoryError(error)) throw error;
7067
// Fallback to multicall if deployless call fails.
71-
72-
if (error instanceof BaseError) {
73-
const revertError = error.walk(
74-
(err) => err instanceof ContractFunctionRevertedError,
75-
);
76-
if (
77-
revertError instanceof ContractFunctionRevertedError &&
78-
revertError.data?.errorName === "UnknownOfFactory"
79-
)
80-
throw error;
81-
}
8268
}
8369
}
8470

packages/blue-sdk-viem/src/fetch/vault-v2/VaultV2MorphoVaultV1Adapter.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ import {
55
VaultV2MorphoVaultV1Adapter,
66
getChainAddresses,
77
} from "@morpho-org/blue-sdk";
8-
import {
9-
type Address,
10-
BaseError,
11-
type Client,
12-
ContractFunctionRevertedError,
13-
erc20Abi,
14-
} from "viem";
8+
import { type Address, type Client, erc20Abi } from "viem";
159
import { getChainId, readContract } from "viem/actions";
1610
import {
1711
morphoVaultV1AdapterAbi,
1812
morphoVaultV1AdapterFactoryAbi,
1913
} from "../../abis";
14+
import { isUnknownOfFactoryError } from "../../error";
2015
import {
2116
abi,
2217
code,
@@ -50,18 +45,8 @@ export async function fetchVaultV2MorphoVaultV1Adapter(
5045
return new VaultV2MorphoVaultV1Adapter({ ...adapter, address });
5146
} catch (error) {
5247
if (deployless === "force") throw error;
48+
if (isUnknownOfFactoryError(error)) throw error;
5349
// Fallback to multicall if deployless call fails.
54-
55-
if (error instanceof BaseError) {
56-
const revertError = error.walk(
57-
(err) => err instanceof ContractFunctionRevertedError,
58-
);
59-
if (
60-
revertError instanceof ContractFunctionRevertedError &&
61-
revertError.data?.errorName === "UnknownOfFactory"
62-
)
63-
throw error;
64-
}
6550
}
6651
}
6752

0 commit comments

Comments
 (0)