Skip to content

Commit

Permalink
Merge pull request #223 from morpho-org/fix/fetch-eip712-domain-in-token
Browse files Browse the repository at this point in the history
Fix/fetch eip712 domain in token
  • Loading branch information
Rubilmax authored Jan 6, 2025
2 parents 11d2961 + db34ca8 commit 3f4f6af
Show file tree
Hide file tree
Showing 36 changed files with 540 additions and 262 deletions.
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
43 changes: 30 additions & 13 deletions packages/blue-sdk-ethers/src/fetch/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
decodeBytes32String,
isHexString,
} from "ethers";
import { WStEth__factory } from "ethers-types";
import { ERC20Permit__factory, WStEth__factory } from "ethers-types";
import { ERC20__factory } from "ethers-types";
import type {
ERC20,
Expand All @@ -18,6 +18,7 @@ import {
ChainId,
ChainUtils,
ConstantWrappedToken,
Eip5267Domain,
ExchangeRateWrappedToken,
NATIVE_ADDRESS,
Token,
Expand Down Expand Up @@ -79,7 +80,6 @@ export namespace Bytes32ERC20__factory {
export const abi = _bytes32ERC20Abi;

export function createInterface() {
// @ts-ignore incompatible commonjs type
return new Interface(_bytes32ERC20Abi) as ERC20Interface;
}

Expand Down Expand Up @@ -116,11 +116,7 @@ export namespace ERC20Metadata__factory {
if (isBytes32ERC20Metadata(address, chainId))
return Bytes32ERC20__factory.connect(address, runner);

const erc20 = ERC20__factory.connect(
address,
// @ts-ignore incompatible commonjs type
runner,
);
const erc20 = ERC20__factory.connect(address, runner);

const name = erc20.name.bind(erc20);
erc20.name = Object.assign(
Expand Down Expand Up @@ -156,30 +152,51 @@ export async function fetchToken(
if (address === NATIVE_ADDRESS) return Token.native(chainId);

const erc20 = ERC20Metadata__factory.connect(address, chainId, runner);
const erc20Permit = ERC20Permit__factory.connect(address, runner);

const [decimals, symbol, name] = await Promise.all([
const [decimals, symbol, name, eip5267Domain] = await Promise.all([
erc20.decimals(overrides).catch(() => undefined),
erc20.symbol(overrides).catch(() => undefined),
erc20.name(overrides).catch(() => undefined),
erc20Permit
.eip712Domain(overrides)
.then(
({
fields,
name,
version,
chainId,
verifyingContract,
salt,
extensions,
}) =>
new Eip5267Domain({
fields: fields as `0x${string}`,
name,
version,
chainId,
verifyingContract: verifyingContract as Address,
salt: salt as `0x${string}`,
extensions,
}),
)
.catch(() => undefined),
]);

const token = {
address,
name,
symbol,
decimals,
eip5267Domain,
};

const { wstEth, stEth } = getChainAddresses(chainId);

switch (address) {
case wstEth: {
if (stEth) {
const wstEthToken = WStEth__factory.connect(
wstEth!,
// @ts-ignore incompatible commonjs type
runner,
);
const wstEthToken = WStEth__factory.connect(wstEth!, runner);
const stEthPerWstEth = await wstEthToken.stEthPerToken(overrides);
return new ExchangeRateWrappedToken(token, stEth, stEthPerWstEth);
}
Expand Down
6 changes: 1 addition & 5 deletions packages/blue-sdk-ethers/src/fetch/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ export async function fetchUser(
);

const { morpho, bundler } = getChainAddresses(chainId);
const blue = MorphoBlue__factory.connect(
morpho,
// @ts-ignore incompatible commonjs type
runner,
);
const blue = MorphoBlue__factory.connect(morpho, runner);

const [isBundlerAuthorized, morphoNonce] = await Promise.all([
blue.isAuthorized(address, bundler, overrides),
Expand Down
27 changes: 5 additions & 22 deletions packages/blue-sdk-ethers/src/fetch/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ChainUtils,
type MarketId,
Vault,
type VaultConfig,
type VaultPublicAllocatorConfig,
getChainAddresses,
} from "@morpho-org/blue-sdk";
Expand All @@ -24,29 +23,13 @@ export async function fetchVault(
options.chainId ?? (await runner.provider.getNetwork()).chainId,
);

const config = await fetchVaultConfig(address, runner, options);
const chainAddresses = getChainAddresses(options.chainId);
const mm = MetaMorpho__factory.connect(address, runner);

return fetchVaultFromConfig(address, config, runner, options);
}

export async function fetchVaultFromConfig(
address: Address,
config: VaultConfig,
runner: { provider: Provider },
{ chainId, overrides = {} }: FetchOptions = {},
) {
chainId = ChainUtils.parseSupportedChainId(
chainId ?? (await runner.provider.getNetwork()).chainId,
);

const chainAddresses = getChainAddresses(chainId);
const mm = MetaMorpho__factory.connect(
address,
// @ts-ignore incompatible commonjs type
runner,
);
const { overrides = {} } = options;

const [
config,
curator,
owner,
guardian,
Expand All @@ -64,6 +47,7 @@ export async function fetchVaultFromConfig(
withdrawQueueSize,
hasPublicAllocator,
] = await Promise.all([
fetchVaultConfig(address, runner, options),
mm.curator(overrides) as Promise<Address>,
mm.owner(overrides) as Promise<Address>,
mm.guardian(overrides) as Promise<Address>,
Expand Down Expand Up @@ -94,7 +78,6 @@ export async function fetchVaultFromConfig(
if (hasPublicAllocator) {
const publicAllocator = PublicAllocator__factory.connect(
chainAddresses.publicAllocator!,
// @ts-ignore incompatible commonjs type
runner,
);

Expand Down
62 changes: 21 additions & 41 deletions packages/blue-sdk-ethers/src/fetch/VaultConfig.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,35 @@
import type { Provider } from "ethers";
import { MetaMorpho__factory } from "ethers-types";

import {
type Address,
type ChainId,
ChainUtils,
UnknownVaultConfigError,
VaultConfig,
_try,
} from "@morpho-org/blue-sdk";
import { type Address, ChainUtils, VaultConfig } from "@morpho-org/blue-sdk";
import type { FetchOptions } from "../types";
import { fetchToken } from "./Token";

export async function fetchVaultConfig(
address: Address,
runner: { provider: Provider },
{ chainId }: { chainId?: ChainId } = {},
options: FetchOptions = {},
) {
chainId = ChainUtils.parseSupportedChainId(
chainId ?? (await runner.provider.getNetwork()).chainId,
options.chainId = ChainUtils.parseSupportedChainId(
options.chainId ?? (await runner.provider.getNetwork()).chainId,
);

let config = _try(
() => VaultConfig.get(address, chainId),
UnknownVaultConfigError,
);

if (!config) {
const mm = MetaMorpho__factory.connect(
address,
// @ts-ignore incompatible commonjs type
runner,
);
const mm = MetaMorpho__factory.connect(address, runner);

// always fetch at latest block because config is immutable
const [asset, symbol, name, decimalsOffset] = await Promise.all([
mm.asset() as Promise<Address>,
mm.symbol(),
mm.name(),
mm.DECIMALS_OFFSET(),
]);
const { overrides = {} } = options;

config = new VaultConfig(
{
address,
asset,
symbol,
name,
decimalsOffset,
},
chainId,
);
}
const [token, asset, decimalsOffset] = await Promise.all([
fetchToken(address, runner, options), // TODO: avoid fetching decimals
mm.asset() as Promise<Address>,
mm.DECIMALS_OFFSET(overrides),
]);

return config;
return new VaultConfig(
{
...token,
asset,
decimalsOffset,
},
options.chainId,
);
}
6 changes: 1 addition & 5 deletions packages/blue-sdk-ethers/src/fetch/VaultMarketConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ export async function fetchVaultMarketConfig(
);
options.overrides ??= {};

const mm = MetaMorpho__factory.connect(
vault,
// @ts-ignore incompatible commonjs type
runner,
);
const mm = MetaMorpho__factory.connect(vault, runner);

const [{ cap, removableAt, enabled }, pendingCap, publicAllocatorConfig] =
await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export async function fetchVaultMarketPublicAllocatorConfig(

const [maxIn, maxOut] = await PublicAllocator__factory.connect(
publicAllocator,
// @ts-ignore incompatible commonjs type
runner,
).flowCaps(vault, marketId, overrides);

Expand Down
Loading

0 comments on commit 3f4f6af

Please sign in to comment.