Skip to content

Commit

Permalink
Merge pull request #224 from morpho-org/fix/eip712-domain
Browse files Browse the repository at this point in the history
fix(eip5267): add domain to vault and fix token cache
  • Loading branch information
Rubilmax authored Jan 6, 2025
2 parents 5091fcd + ebc1e1b commit 98ae41d
Show file tree
Hide file tree
Showing 25 changed files with 390 additions and 237 deletions.
34 changes: 26 additions & 8 deletions packages/blue-sdk-ethers/src/fetch/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ChainId,
ChainUtils,
ConstantWrappedToken,
Eip5267Domain,
ExchangeRateWrappedToken,
NATIVE_ADDRESS,
Token,
Expand Down Expand Up @@ -158,24 +159,41 @@ export async function fetchToken(
const erc20 = ERC20Metadata__factory.connect(address, chainId, runner);
const erc20Permit = ERC20Permit__factory.connect(address, runner);

const [decimals, symbol, name, eip712Domain] = 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).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,
eip712Domain: eip712Domain && {
chainId: eip712Domain.chainId,
name: eip712Domain.name,
verifyingContract: eip712Domain.verifyingContract as Address,
version: eip712Domain.version,
},
eip5267Domain,
};

const { wstEth, stEth } = getChainAddresses(chainId);
Expand Down
64 changes: 24 additions & 40 deletions packages/blue-sdk-ethers/src/fetch/VaultConfig.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,39 @@
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,
const mm = MetaMorpho__factory.connect(
address,
// @ts-ignore incompatible commonjs type
runner,
);

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

// 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 [token, asset, decimalsOffset] = await Promise.all([
fetchToken(address, runner, options),
mm.asset() as Promise<Address>,
mm.DECIMALS_OFFSET(overrides),
]);

config = new VaultConfig(
{
address,
asset,
symbol,
name,
decimalsOffset,
},
chainId,
);
}

return config;
return new VaultConfig(
{
...token,
asset,
decimalsOffset,
},
options.chainId,
);
}
2 changes: 1 addition & 1 deletion packages/blue-sdk-ethers/src/signatures/permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getPermitMessage = (
): SignatureMessage => {
const { usdc, dai } = getChainAddresses(chainId);

const domain = erc20.eip712Domain ?? {
const domain = erc20.eip5267Domain?.eip712Domain ?? {
name: erc20.name,
version: erc20.address === usdc ? "2" : "1",
chainId,
Expand Down
15 changes: 10 additions & 5 deletions packages/blue-sdk-ethers/test/e2e/Token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { test } from "./setup";

import {
ChainId,
Eip5267Domain,
ExchangeRateWrappedToken,
addresses,
} from "@morpho-org/blue-sdk";
import { randomAddress } from "@morpho-org/test";
import { zeroHash } from "viem";
import { Token } from "../../src/augment/Token";

const { mkr, usdc, stEth, wstEth } = addresses[ChainId.EthMainnet];
Expand Down Expand Up @@ -63,23 +65,26 @@ describe("augment/Token", () => {
expect(value).toStrictEqual(expectedData);
});

test("should fetch token data with eip712Domain", async ({ wallet }) => {
test("should fetch token data with eip5267Domain", async ({ wallet }) => {
const steakUSDC = "0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB";
const expectedData = new Token({
address: steakUSDC,
decimals: 18,
symbol: "steakUSDC",
name: "Steakhouse USDC",
eip712Domain: {
eip5267Domain: new Eip5267Domain({
fields: "0x0f",
name: "Steakhouse USDC",
verifyingContract: steakUSDC,
version: "1",
chainId: 1n,
},
verifyingContract: steakUSDC,
salt: zeroHash,
extensions: [],
}),
});

const value = await Token.fetch(expectedData.address, wallet);

expect(value).toStrictEqual(expectedData);
expect(value).toEqual(expectedData);
});
});
20 changes: 17 additions & 3 deletions packages/blue-sdk-ethers/test/e2e/Vault.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { describe, expect } from "vitest";
import { test } from "./setup";

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

import { vaults } from "@morpho-org/morpho-test";
import { zeroAddress } from "viem";
import { zeroAddress, zeroHash } from "viem";
import { Vault } from "../../src/augment/Vault";
import { metaMorphoAbi, publicAllocatorAbi } from "./abis";

Expand Down Expand Up @@ -71,10 +76,19 @@ describe("augment/Vault", () => {
lastTotalAssets: 26129569140552n,
totalAssets: 26138940196162n,
totalSupply: 25752992371062043744406063n,
eip5267Domain: new Eip5267Domain({
fields: "0x0f",
name: "Steakhouse USDC",
version: "1",
chainId: 1n,
verifyingContract: steakUsdc.address,
salt: zeroHash,
extensions: [],
}),
});

const value = await Vault.fetch(steakUsdc.address, wallet);

expect(value).toStrictEqual(expectedData);
expect(value).toEqual(expectedData);
});
});
32 changes: 6 additions & 26 deletions packages/blue-sdk-viem/contracts/GetToken.sol
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import {IERC20Permit} from "./interfaces/IERC20Permit.sol";
import {IERC20Permit, Eip5267Domain} from "./interfaces/IERC20Permit.sol";
import {IERC20} from "./interfaces/IERC20.sol";
import {IWstEth} from "./interfaces/IWstEth.sol";

struct Eip712Domain {
string name;
string version;
address verifyingContract;
uint256 chainId;
}

struct TokenResponse {
uint256 decimals;
bool hasSymbol;
string symbol;
bool hasName;
string name;
uint256 stEthPerWstEth;
Eip712Domain eip712Domain;
bool hasEip712Domain;
Eip5267Domain eip5267Domain;
bool hasEip5267Domain;
}

contract GetToken {
Expand All @@ -41,22 +34,9 @@ contract GetToken {

if (isWstEth) res.stEthPerWstEth = IWstEth(address(token)).stEthPerToken();

try IERC20Permit(address(token)).eip712Domain() returns (
bytes1 /* fields */,
string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
bytes32 /* salt */,
uint256[] memory /* extensions */
) {
res.hasEip712Domain = true;
res.eip712Domain = Eip712Domain({
name: name,
version: version,
verifyingContract: verifyingContract,
chainId: chainId
});
try IERC20Permit(address(token)).eip712Domain() returns (Eip5267Domain memory eip5267Domain) {
res.hasEip5267Domain = true;
res.eip5267Domain = eip5267Domain;
} catch {}
}
}
5 changes: 4 additions & 1 deletion packages/blue-sdk-viem/contracts/GetVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.0;

import {IMorpho, Id, MarketParams} from "./interfaces/IMorpho.sol";
import {Eip5267Domain} from "./interfaces/IERC20Permit.sol";
import {IMetaMorpho, PendingUint192, PendingAddress} from "./interfaces/IMetaMorpho.sol";
import {IPublicAllocator} from "./interfaces/IPublicAllocator.sol";

Expand All @@ -11,6 +12,7 @@ struct VaultConfig {
string name;
uint256 decimals;
uint256 decimalsOffset;
Eip5267Domain eip5267Domain;
}

struct PublicAllocatorConfig {
Expand Down Expand Up @@ -50,7 +52,8 @@ contract GetVault {
symbol: vault.symbol(),
name: vault.name(),
decimals: vault.decimals(),
decimalsOffset: vault.DECIMALS_OFFSET()
decimalsOffset: vault.DECIMALS_OFFSET(),
eip5267Domain: vault.eip712Domain()
});

res.owner = vault.owner();
Expand Down
26 changes: 13 additions & 13 deletions packages/blue-sdk-viem/contracts/interfaces/IERC20Permit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ pragma solidity >=0.5.0;

import {IERC20} from "./IERC20.sol";

struct Eip5267Domain {
bytes1 fields;
string name;
string version;
uint256 chainId;
address verifyingContract;
bytes32 salt;
uint256[] extensions;
}

/**
* @dev Interface of the ERC-20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[ERC-2612].
Expand Down Expand Up @@ -84,18 +94,8 @@ interface IERC20Permit is IERC20 {
function DOMAIN_SEPARATOR() external view returns (bytes32);

/**
* @dev See {IERC-5267}.
* @dev returns the fields and values that describe the domain separator used by this contract for EIP-712
* signature.
*/
function eip712Domain()
external
view
returns (
bytes1 fields,
string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
bytes32 salt,
uint256[] memory extensions
);
function eip712Domain() external view returns (Eip5267Domain memory);
}
2 changes: 1 addition & 1 deletion packages/blue-sdk-viem/src/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7865,7 +7865,7 @@ export const permissionedErc20WrapperAbi = [
},
] as const;

export const eip712DomainAbi = [
export const erc5267Abi = [
{
inputs: [],
name: "eip712Domain",
Expand Down
Loading

0 comments on commit 98ae41d

Please sign in to comment.