Skip to content

Commit

Permalink
feat: add read functions
Browse files Browse the repository at this point in the history
  • Loading branch information
iherger committed Apr 29, 2024
1 parent c6d6b44 commit 08332b2
Showing 1 changed file with 197 additions and 1 deletion.
198 changes: 197 additions & 1 deletion packages/sdk/src/Portfolio/Integrations/MorphoBlue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { type Hex, decodeAbiParameters, encodeAbiParameters } from "viem";
import { type Address, type Hex, type PublicClient, decodeAbiParameters, encodeAbiParameters } from "viem";
import { readContract } from "viem/actions";
import { Viem } from "../../Utils.js";
import * as ExternalPositionManager from "../../_internal/ExternalPositionManager.js";

export type Action = (typeof Action)[keyof typeof Action];
Expand Down Expand Up @@ -221,3 +223,197 @@ export function repayDecode(encoded: Hex): RepayArgs {
repayAmount,
};
}

//--------------------------------------------------------------------------------------------
// EXTERNAL READ FUNCTIONS
//--------------------------------------------------------------------------------------------

const morphoBlueAbi = [
{
type: "function",
name: "idToMarketParams",
inputs: [
{
name: "_id",
type: "bytes32",
internalType: "bytes32",
},
],
outputs: [
{
name: "marketParams_",
type: "tuple",
internalType: "struct IMorphoBlue.MarketParams",
components: [
{
name: "loanToken",
type: "address",
internalType: "address",
},
{
name: "collateralToken",
type: "address",
internalType: "address",
},
{
name: "oracle",
type: "address",
internalType: "address",
},
{
name: "irm",
type: "address",
internalType: "address",
},
{
name: "lltv",
type: "uint256",
internalType: "uint256",
},
],
},
],
stateMutability: "view",
},
{
type: "function",
name: "market",
inputs: [
{
name: "_id",
type: "bytes32",
internalType: "bytes32",
},
],
outputs: [
{
name: "market_",
type: "tuple",
internalType: "struct IMorphoBlue.Market",
components: [
{
name: "totalSupplyAssets",
type: "uint128",
internalType: "uint128",
},
{
name: "totalSupplyShares",
type: "uint128",
internalType: "uint128",
},
{
name: "totalBorrowAssets",
type: "uint128",
internalType: "uint128",
},
{
name: "totalBorrowShares",
type: "uint128",
internalType: "uint128",
},
{
name: "lastUpdate",
type: "uint128",
internalType: "uint128",
},
{
name: "fee",
type: "uint128",
internalType: "uint128",
},
],
},
],
stateMutability: "view",
},
{
type: "function",
name: "position",
inputs: [
{
name: "_id",
type: "bytes32",
internalType: "bytes32",
},
{
name: "_user",
type: "address",
internalType: "address",
},
],
outputs: [
{
name: "position_",
type: "tuple",
internalType: "struct IMorphoBlue.Position",
components: [
{
name: "supplyShares",
type: "uint256",
internalType: "uint256",
},
{
name: "borrowShares",
type: "uint128",
internalType: "uint128",
},
{
name: "collateral",
type: "uint128",
internalType: "uint128",
},
],
},
],
stateMutability: "view",
},
] as const;

export function getMarketParamsFromId(
client: PublicClient,
args: Viem.ContractCallParameters<{
morphoBlue: Address;
marketId: Address;
}>,
) {
return readContract(client, {
...Viem.extractBlockParameters(args),
abi: morphoBlueAbi,
functionName: "idToMarketParams",
address: args.morphoBlue,
args: [args.marketId],
});
}

export function getMarket(
client: PublicClient,
args: Viem.ContractCallParameters<{
morphoBlue: Address;
marketId: Address;
}>,
) {
return readContract(client, {
...Viem.extractBlockParameters(args),
abi: morphoBlueAbi,
functionName: "market",
address: args.morphoBlue,
args: [args.marketId],
});
}

export function getPosition(
client: PublicClient,
args: Viem.ContractCallParameters<{
morphoBlue: Address;
marketId: Address;
user: Address;
}>,
) {
return readContract(client, {
...Viem.extractBlockParameters(args),
abi: morphoBlueAbi,
functionName: "position",
address: args.morphoBlue,
args: [args.marketId, args.user],
});
}

0 comments on commit 08332b2

Please sign in to comment.