Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/lerna-lite/publish-3…
Browse files Browse the repository at this point in the history
….7.1
  • Loading branch information
Rubilmax authored Jul 29, 2024
2 parents efc2082 + 9a15248 commit a40d383
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 381 deletions.
2 changes: 1 addition & 1 deletion packages/blue-sdk-ethers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@morpho-org/blue-sdk-ethers",
"version": "1.1.2",
"version": "1.1.3",
"author": "Morpho Association <[email protected]>",
"license": "MIT",
"main": "src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/blue-sdk-viem/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@morpho-org/blue-sdk-viem",
"version": "1.1.2",
"version": "1.1.4",
"author": "Morpho Association <[email protected]>",
"license": "MIT",
"main": "src/index.ts",
Expand Down
111 changes: 43 additions & 68 deletions packages/blue-sdk-viem/src/fetch/Holding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,8 @@ import {
permissionedWrapperTokens,
} from "@morpho-org/blue-sdk";
import { fromEntries } from "@morpho-org/morpho-ts";
import {
Account,
Address,
Chain,
PublicClient,
RpcSchema,
Transport,
erc20Abi,
maxUint256,
} from "viem";
import { Address, Client, erc20Abi, maxUint256 } from "viem";
import { getBalance, getChainId, readContract } from "viem/actions";
import {
erc2612Abi,
permissionedErc20WrapperAbi,
Expand All @@ -29,22 +21,17 @@ import {
} from "../abis";
import { ViewOverrides } from "../types";

export async function fetchHolding<
transport extends Transport,
chain extends Chain | undefined,
account extends Account | undefined,
rpcSchema extends RpcSchema | undefined,
>(
export async function fetchHolding(
user: Address,
token: Address,
client: PublicClient<transport, chain, account, rpcSchema>,
client: Client,
{
chainId,
overrides = {},
}: { chainId?: ChainId; overrides?: ViewOverrides } = {},
) {
chainId = ChainUtils.parseSupportedChainId(
chainId ?? (await client.getChainId()),
chainId ?? (await getChainId(client)),
);

const chainAddresses = getChainAddresses(chainId);
Expand All @@ -66,13 +53,9 @@ export async function fetchHolding<
},
]),
),
balance: await client.getBalance({ ...overrides, address: user }),
balance: await getBalance(client, { ...overrides, address: user }),
});

// const erc20 = ERC20__factory.connect(token, runner);
// const permit2 = Permit2__factory.connect(chainAddresses.permit2, runner);
// const erc2612 = ERC2612__factory.connect(token, runner);

const [
balance,
erc20Allowances,
Expand All @@ -81,7 +64,7 @@ export async function fetchHolding<
whitelistControllerAggregator,
hasErc20WrapperPermission,
] = await Promise.all([
client.readContract({
readContract(client, {
...overrides,
abi: erc20Abi,
address: token,
Expand All @@ -93,7 +76,7 @@ export async function fetchHolding<
async (label) =>
[
label,
await client.readContract({
await readContract(client, {
...overrides,
abi: erc20Abi,
address: token,
Expand All @@ -108,50 +91,44 @@ export async function fetchHolding<
async (label) =>
[
label,
await client
.readContract({
...overrides,
abi: permit2Abi,
address: chainAddresses.permit2 as Address,
functionName: "allowance",
args: [user, token, chainAddresses[label] as Address],
})
.then(([amount, expiration, nonce]) => ({
amount,
expiration: BigInt(expiration),
nonce: BigInt(nonce),
})),
await readContract(client, {
...overrides,
abi: permit2Abi,
address: chainAddresses.permit2 as Address,
functionName: "allowance",
args: [user, token, chainAddresses[label] as Address],
}).then(([amount, expiration, nonce]) => ({
amount,
expiration: BigInt(expiration),
nonce: BigInt(nonce),
})),
] as const,
),
),
client
.readContract({
...overrides,
abi: erc2612Abi,
address: token,
functionName: "nonces",
args: [user],
})
.catch(() => undefined),
readContract(client, {
...overrides,
abi: erc2612Abi,
address: token,
functionName: "nonces",
args: [user],
}).catch(() => undefined),
permissionedBackedTokens[chainId].has(token)
? client.readContract({
? readContract(client, {
...overrides,
abi: wrappedBackedTokenAbi,
address: token,
functionName: "whitelistControllerAggregator",
})
: undefined,
client
.readContract({
...overrides,
abi: permissionedErc20WrapperAbi,
address: token,
functionName: "hasPermission",
args: [user],
})
.catch(() =>
permissionedWrapperTokens[chainId].has(token) ? false : undefined,
),
readContract(client, {
...overrides,
abi: permissionedErc20WrapperAbi,
address: token,
functionName: "hasPermission",
args: [user],
}).catch(() =>
permissionedWrapperTokens[chainId].has(token) ? false : undefined,
),
]);

const holding = new Holding({
Expand All @@ -165,15 +142,13 @@ export async function fetchHolding<
});

if (whitelistControllerAggregator)
holding.canTransfer = await client
.readContract({
...overrides,
abi: whitelistControllerAggregatorV2Abi,
address: whitelistControllerAggregator,
functionName: "isWhitelisted",
args: [user],
})
.catch(() => undefined);
holding.canTransfer = await readContract(client, {
...overrides,
abi: whitelistControllerAggregatorV2Abi,
address: whitelistControllerAggregator,
functionName: "isWhitelisted",
args: [user],
}).catch(() => undefined);

return holding;
}
40 changes: 11 additions & 29 deletions packages/blue-sdk-viem/src/fetch/Market.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
Account,
Address,
Chain,
ParseAccount,
PublicClient,
RpcSchema,
Transport,
zeroAddress,
} from "viem";
import { Address, Client, zeroAddress } from "viem";

import {
ChainId,
Expand All @@ -18,47 +9,38 @@ import {
getChainAddresses,
} from "@morpho-org/blue-sdk";

import { getChainId, readContract } from "viem/actions";
import { adaptiveCurveIrmAbi, blueAbi, blueOracleAbi } from "../abis";
import { ViewOverrides } from "../types";
import { fetchMarketConfig } from "./MarketConfig";

export async function fetchMarket<
transport extends Transport,
chain extends Chain | undefined,
account extends Account | undefined,
rpcSchema extends RpcSchema | undefined,
>(
export async function fetchMarket(
id: MarketId,
client: PublicClient<transport, chain, ParseAccount<account>, rpcSchema>,
client: Client,
{
chainId,
overrides = {},
}: { chainId?: ChainId; overrides?: ViewOverrides } = {},
) {
chainId = ChainUtils.parseSupportedChainId(
chainId ?? (await client.getChainId()),
chainId ?? (await getChainId(client)),
);

const config = await fetchMarketConfig(id, client, { chainId });

return fetchMarketFromConfig(config, client, { chainId, overrides });
}

export async function fetchMarketFromConfig<
transport extends Transport,
chain extends Chain | undefined,
account extends Account | undefined,
rpcSchema extends RpcSchema | undefined,
>(
export async function fetchMarketFromConfig(
config: MarketConfig,
client: PublicClient<transport, chain, ParseAccount<account>, rpcSchema>,
client: Client,
{
chainId,
overrides = {},
}: { chainId?: ChainId; overrides?: ViewOverrides } = {},
) {
chainId = ChainUtils.parseSupportedChainId(
chainId ?? (await client.getChainId()),
chainId ?? (await getChainId(client)),
);

const { morpho, adaptiveCurveIrm } = getChainAddresses(chainId);
Expand All @@ -75,23 +57,23 @@ export async function fetchMarketFromConfig<
price,
rateAtTarget,
] = await Promise.all([
client.readContract({
readContract(client, {
...overrides,
address: morpho as Address,
abi: blueAbi,
functionName: "market",
args: [config.id],
}),
config.oracle !== zeroAddress
? client.readContract({
? readContract(client, {
...overrides,
address: config.oracle as Address,
abi: blueOracleAbi,
functionName: "price",
})
: 0n,
config.irm === adaptiveCurveIrm
? await client.readContract({
? await readContract(client, {
...overrides,
address: adaptiveCurveIrm as Address,
abi: adaptiveCurveIrmAbi,
Expand Down
24 changes: 6 additions & 18 deletions packages/blue-sdk-viem/src/fetch/MarketConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,27 @@ import {
_try,
getChainAddresses,
} from "@morpho-org/blue-sdk";
import {
Account,
Address,
Chain,
ParseAccount,
PublicClient,
RpcSchema,
Transport,
} from "viem";
import { Address, Client } from "viem";
import { getChainId, readContract } from "viem/actions";
import { blueAbi } from "../abis";

export async function fetchMarketConfig<
transport extends Transport,
chain extends Chain | undefined,
account extends Account | undefined,
rpcSchema extends RpcSchema | undefined,
>(
export async function fetchMarketConfig(
id: MarketId,
client: PublicClient<transport, chain, ParseAccount<account>, rpcSchema>,
client: Client,
{ chainId }: { chainId?: ChainId } = {},
) {
let config = _try(() => MarketConfig.get(id), UnknownMarketConfigError);

if (!config) {
chainId = ChainUtils.parseSupportedChainId(
chainId ?? (await client.getChainId()),
chainId ?? (await getChainId(client)),
);

const { morpho } = getChainAddresses(chainId);

const [loanToken, collateralToken, oracle, irm, lltv] =
// Always fetch at latest block because config is immutable.
await client.readContract({
await readContract(client, {
address: morpho as Address,
abi: blueAbi,
functionName: "idToMarketParams",
Expand Down
Loading

0 comments on commit a40d383

Please sign in to comment.