@morpho-org/midnight-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +252 -0
- package/lib/cjs/abis.d.ts +3170 -0
- package/lib/cjs/abis.js +4073 -0
- package/lib/cjs/api/MidnightApi.d.ts +532 -0
- package/lib/cjs/api/MidnightApi.js +713 -0
- package/lib/cjs/api/helpers.d.ts +20 -0
- package/lib/cjs/api/helpers.js +189 -0
- package/lib/cjs/api/index.d.ts +2 -0
- package/lib/cjs/api/index.js +21 -0
- package/lib/cjs/api/types.d.ts +453 -0
- package/lib/cjs/api/types.js +2 -0
- package/lib/cjs/constants.d.ts +168 -0
- package/lib/cjs/constants.js +187 -0
- package/lib/cjs/errors.d.ts +253 -0
- package/lib/cjs/errors.js +309 -0
- package/lib/cjs/fetch/Market.d.ts +65 -0
- package/lib/cjs/fetch/Market.js +117 -0
- package/lib/cjs/fetch/Offer.d.ts +74 -0
- package/lib/cjs/fetch/Offer.js +119 -0
- package/lib/cjs/fetch/Position.d.ts +85 -0
- package/lib/cjs/fetch/Position.js +148 -0
- package/lib/cjs/fetch/Ratifier.d.ts +38 -0
- package/lib/cjs/fetch/Ratifier.js +57 -0
- package/lib/cjs/fetch/index.d.ts +5 -0
- package/lib/cjs/fetch/index.js +21 -0
- package/lib/cjs/fetch/types.d.ts +57 -0
- package/lib/cjs/fetch/types.js +2 -0
- package/lib/cjs/fetch/utils.d.ts +8 -0
- package/lib/cjs/fetch/utils.js +18 -0
- package/lib/cjs/index.d.ts +18 -0
- package/lib/cjs/index.js +57 -0
- package/lib/cjs/market/Market.d.ts +613 -0
- package/lib/cjs/market/Market.js +503 -0
- package/lib/cjs/market/MarketUtils.d.ts +189 -0
- package/lib/cjs/market/MarketUtils.js +303 -0
- package/lib/cjs/market/Position.d.ts +424 -0
- package/lib/cjs/market/Position.js +425 -0
- package/lib/cjs/market/PositionUtils.d.ts +115 -0
- package/lib/cjs/market/PositionUtils.js +194 -0
- package/lib/cjs/market/index.d.ts +4 -0
- package/lib/cjs/market/index.js +20 -0
- package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
- package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
- package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
- package/lib/cjs/math/TakeAmountsLib.js +199 -0
- package/lib/cjs/math/TickLib.d.ts +200 -0
- package/lib/cjs/math/TickLib.js +272 -0
- package/lib/cjs/math/index.d.ts +3 -0
- package/lib/cjs/math/index.js +19 -0
- package/lib/cjs/offers/Offer.d.ts +534 -0
- package/lib/cjs/offers/Offer.js +343 -0
- package/lib/cjs/offers/OfferUtils.d.ts +565 -0
- package/lib/cjs/offers/OfferUtils.js +659 -0
- package/lib/cjs/offers/index.d.ts +2 -0
- package/lib/cjs/offers/index.js +18 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/queries/GetPosition.d.ts +55 -0
- package/lib/cjs/queries/GetPosition.js +74 -0
- package/lib/cjs/queries/index.d.ts +1 -0
- package/lib/cjs/queries/index.js +37 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
- package/lib/cjs/signatures/Group.d.ts +178 -0
- package/lib/cjs/signatures/Group.js +193 -0
- package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
- package/lib/cjs/signatures/GroupUtils.js +127 -0
- package/lib/cjs/signatures/Payload.d.ts +283 -0
- package/lib/cjs/signatures/Payload.js +570 -0
- package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
- package/lib/cjs/signatures/RatifierUtils.js +76 -0
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
- package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
- package/lib/cjs/signatures/Tree.d.ts +241 -0
- package/lib/cjs/signatures/Tree.js +263 -0
- package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
- package/lib/cjs/signatures/TreeUtils.js +461 -0
- package/lib/cjs/signatures/index.d.ts +7 -0
- package/lib/cjs/signatures/index.js +23 -0
- package/lib/esm/abis.d.ts +3170 -0
- package/lib/esm/abis.js +4070 -0
- package/lib/esm/api/MidnightApi.d.ts +532 -0
- package/lib/esm/api/MidnightApi.js +709 -0
- package/lib/esm/api/helpers.d.ts +20 -0
- package/lib/esm/api/helpers.js +180 -0
- package/lib/esm/api/index.d.ts +2 -0
- package/lib/esm/api/index.js +2 -0
- package/lib/esm/api/types.d.ts +453 -0
- package/lib/esm/api/types.js +1 -0
- package/lib/esm/constants.d.ts +168 -0
- package/lib/esm/constants.js +184 -0
- package/lib/esm/errors.d.ts +253 -0
- package/lib/esm/errors.js +291 -0
- package/lib/esm/fetch/Market.d.ts +65 -0
- package/lib/esm/fetch/Market.js +113 -0
- package/lib/esm/fetch/Offer.d.ts +74 -0
- package/lib/esm/fetch/Offer.js +116 -0
- package/lib/esm/fetch/Position.d.ts +85 -0
- package/lib/esm/fetch/Position.js +144 -0
- package/lib/esm/fetch/Ratifier.d.ts +38 -0
- package/lib/esm/fetch/Ratifier.js +54 -0
- package/lib/esm/fetch/index.d.ts +5 -0
- package/lib/esm/fetch/index.js +5 -0
- package/lib/esm/fetch/types.d.ts +57 -0
- package/lib/esm/fetch/types.js +1 -0
- package/lib/esm/fetch/utils.d.ts +8 -0
- package/lib/esm/fetch/utils.js +12 -0
- package/lib/esm/index.d.ts +18 -0
- package/lib/esm/index.js +18 -0
- package/lib/esm/market/Market.d.ts +613 -0
- package/lib/esm/market/Market.js +498 -0
- package/lib/esm/market/MarketUtils.d.ts +189 -0
- package/lib/esm/market/MarketUtils.js +300 -0
- package/lib/esm/market/Position.d.ts +424 -0
- package/lib/esm/market/Position.js +420 -0
- package/lib/esm/market/PositionUtils.d.ts +115 -0
- package/lib/esm/market/PositionUtils.js +191 -0
- package/lib/esm/market/index.d.ts +4 -0
- package/lib/esm/market/index.js +4 -0
- package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
- package/lib/esm/math/ConsumableUnitsLib.js +74 -0
- package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
- package/lib/esm/math/TakeAmountsLib.js +196 -0
- package/lib/esm/math/TickLib.d.ts +200 -0
- package/lib/esm/math/TickLib.js +269 -0
- package/lib/esm/math/index.d.ts +3 -0
- package/lib/esm/math/index.js +3 -0
- package/lib/esm/offers/Offer.d.ts +534 -0
- package/lib/esm/offers/Offer.js +339 -0
- package/lib/esm/offers/OfferUtils.d.ts +565 -0
- package/lib/esm/offers/OfferUtils.js +656 -0
- package/lib/esm/offers/index.d.ts +2 -0
- package/lib/esm/offers/index.js +2 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/queries/GetPosition.d.ts +55 -0
- package/lib/esm/queries/GetPosition.js +71 -0
- package/lib/esm/queries/index.d.ts +1 -0
- package/lib/esm/queries/index.js +1 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
- package/lib/esm/signatures/Group.d.ts +178 -0
- package/lib/esm/signatures/Group.js +189 -0
- package/lib/esm/signatures/GroupUtils.d.ts +191 -0
- package/lib/esm/signatures/GroupUtils.js +124 -0
- package/lib/esm/signatures/Payload.d.ts +283 -0
- package/lib/esm/signatures/Payload.js +564 -0
- package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
- package/lib/esm/signatures/RatifierUtils.js +73 -0
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
- package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
- package/lib/esm/signatures/Tree.d.ts +241 -0
- package/lib/esm/signatures/Tree.js +259 -0
- package/lib/esm/signatures/TreeUtils.d.ts +573 -0
- package/lib/esm/signatures/TreeUtils.js +458 -0
- package/lib/esm/signatures/index.d.ts +7 -0
- package/lib/esm/signatures/index.js +7 -0
- package/package.json +69 -0
- package/src/index.ts +19 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Address, Client, Hash } from "viem";
|
|
2
|
+
import { AccrualPosition, Position } from "../market/index.js";
|
|
3
|
+
import type { MidnightFetchParams } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Fetches a Midnight position by id and user.
|
|
6
|
+
*
|
|
7
|
+
* The Solidity storage getter does not return the fixed collateral array, so
|
|
8
|
+
* this helper reads each collateral slot before returning the position.
|
|
9
|
+
* The deployless path is used by default. If deployless is disabled or
|
|
10
|
+
* unavailable, the direct fallback can fan out to 130 RPC calls unless the viem
|
|
11
|
+
* client has `batch.multicall` enabled.
|
|
12
|
+
*
|
|
13
|
+
* Reads `eth_chainId` only when the viem client has no configured chain id. In
|
|
14
|
+
* deployless mode, reads the deployless `GetPosition.query(midnight, marketId,
|
|
15
|
+
* user)` helper. If deployless is disabled or falls back, reads
|
|
16
|
+
* `Midnight.position(marketId, user)` and each
|
|
17
|
+
* `Midnight.collateral(marketId, user, index)` slot for `index` 0 through 127.
|
|
18
|
+
*
|
|
19
|
+
* @param client - Viem client used for the reads.
|
|
20
|
+
* @param params.marketId - Market id whose position to read.
|
|
21
|
+
* @param params.user - Position owner address.
|
|
22
|
+
* @param params.deployless - Optional deployless mode; defaults to `true` with fallback, or `"force"` without fallback.
|
|
23
|
+
* @param params.account - Optional account used as the `from` field for the reads.
|
|
24
|
+
* @param params.blockNumber - Optional block number used for the reads.
|
|
25
|
+
* @param params.blockTag - Optional block tag used for the reads.
|
|
26
|
+
* @param params.stateOverride - Optional state override set used for the reads.
|
|
27
|
+
* @returns Normalized position object.
|
|
28
|
+
* @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
|
|
29
|
+
* @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { fetchPosition } from "@morpho-org/midnight-sdk";
|
|
33
|
+
* import { createPublicClient, http } from "viem";
|
|
34
|
+
* import { base } from "viem/chains";
|
|
35
|
+
*
|
|
36
|
+
* const client = createPublicClient({ chain: base, transport: http() });
|
|
37
|
+
* const position = await fetchPosition(client, {
|
|
38
|
+
* marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
|
|
39
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
40
|
+
* });
|
|
41
|
+
* console.log(position.debt);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function fetchPosition(client: Client, params: MidnightFetchParams & {
|
|
45
|
+
readonly marketId: Hash;
|
|
46
|
+
readonly user: Address;
|
|
47
|
+
}): Promise<Position>;
|
|
48
|
+
/**
|
|
49
|
+
* Fetches a Midnight position paired with its hydrated market.
|
|
50
|
+
*
|
|
51
|
+
* Reads the full inventory of `fetchPosition` and `fetchMarket` at the same
|
|
52
|
+
* call parameters: `eth_chainId` only when needed,
|
|
53
|
+
* `Midnight.toMarket(marketId)`, `Midnight.marketState(marketId)`, and either
|
|
54
|
+
* deployless `GetPosition.query(midnight, marketId, user)` or direct
|
|
55
|
+
* `Midnight.position(marketId, user)` plus all collateral slots.
|
|
56
|
+
*
|
|
57
|
+
* @param client - Viem client used for the reads.
|
|
58
|
+
* @param params.marketId - Market id whose position and market state to read.
|
|
59
|
+
* @param params.user - Position owner address.
|
|
60
|
+
* @param params.deployless - Optional deployless mode for the position read; defaults to `true` with fallback, or `"force"` without fallback.
|
|
61
|
+
* @param params.account - Optional account used as the `from` field for the reads.
|
|
62
|
+
* @param params.blockNumber - Optional block number used for the reads.
|
|
63
|
+
* @param params.blockTag - Optional block tag used for the reads.
|
|
64
|
+
* @param params.stateOverride - Optional state override set used for the reads.
|
|
65
|
+
* @returns Accrual position instance.
|
|
66
|
+
* @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
|
|
67
|
+
* @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { fetchAccrualPosition } from "@morpho-org/midnight-sdk";
|
|
71
|
+
* import { createPublicClient, http } from "viem";
|
|
72
|
+
* import { base } from "viem/chains";
|
|
73
|
+
*
|
|
74
|
+
* const client = createPublicClient({ chain: base, transport: http() });
|
|
75
|
+
* const position = await fetchAccrualPosition(client, {
|
|
76
|
+
* marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
|
|
77
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
78
|
+
* });
|
|
79
|
+
* console.log(position.market.id);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function fetchAccrualPosition(client: Client, params: MidnightFetchParams & {
|
|
83
|
+
readonly marketId: Hash;
|
|
84
|
+
readonly user: Address;
|
|
85
|
+
}): Promise<AccrualPosition>;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { getChainAddress } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { readContract } from "viem/actions";
|
|
3
|
+
import { midnightAbi } from "../abis.js";
|
|
4
|
+
import { MAX_COLLATERALS } from "../constants.js";
|
|
5
|
+
import { AccrualPosition, Position } from "../market/index.js";
|
|
6
|
+
import { abi as getPositionAbi, code as getPositionCode, } from "../queries/GetPosition.js";
|
|
7
|
+
import { fetchMarket } from "./Market.js";
|
|
8
|
+
import { callParameters, resolveChainId, shouldUseDeployless, } from "./utils.js";
|
|
9
|
+
/**
|
|
10
|
+
* Fetches a Midnight position by id and user.
|
|
11
|
+
*
|
|
12
|
+
* The Solidity storage getter does not return the fixed collateral array, so
|
|
13
|
+
* this helper reads each collateral slot before returning the position.
|
|
14
|
+
* The deployless path is used by default. If deployless is disabled or
|
|
15
|
+
* unavailable, the direct fallback can fan out to 130 RPC calls unless the viem
|
|
16
|
+
* client has `batch.multicall` enabled.
|
|
17
|
+
*
|
|
18
|
+
* Reads `eth_chainId` only when the viem client has no configured chain id. In
|
|
19
|
+
* deployless mode, reads the deployless `GetPosition.query(midnight, marketId,
|
|
20
|
+
* user)` helper. If deployless is disabled or falls back, reads
|
|
21
|
+
* `Midnight.position(marketId, user)` and each
|
|
22
|
+
* `Midnight.collateral(marketId, user, index)` slot for `index` 0 through 127.
|
|
23
|
+
*
|
|
24
|
+
* @param client - Viem client used for the reads.
|
|
25
|
+
* @param params.marketId - Market id whose position to read.
|
|
26
|
+
* @param params.user - Position owner address.
|
|
27
|
+
* @param params.deployless - Optional deployless mode; defaults to `true` with fallback, or `"force"` without fallback.
|
|
28
|
+
* @param params.account - Optional account used as the `from` field for the reads.
|
|
29
|
+
* @param params.blockNumber - Optional block number used for the reads.
|
|
30
|
+
* @param params.blockTag - Optional block tag used for the reads.
|
|
31
|
+
* @param params.stateOverride - Optional state override set used for the reads.
|
|
32
|
+
* @returns Normalized position object.
|
|
33
|
+
* @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
|
|
34
|
+
* @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { fetchPosition } from "@morpho-org/midnight-sdk";
|
|
38
|
+
* import { createPublicClient, http } from "viem";
|
|
39
|
+
* import { base } from "viem/chains";
|
|
40
|
+
*
|
|
41
|
+
* const client = createPublicClient({ chain: base, transport: http() });
|
|
42
|
+
* const position = await fetchPosition(client, {
|
|
43
|
+
* marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
|
|
44
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
45
|
+
* });
|
|
46
|
+
* console.log(position.debt);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export async function fetchPosition(client, params) {
|
|
50
|
+
const chainId = await resolveChainId(client);
|
|
51
|
+
const midnight = getChainAddress(chainId, "midnight");
|
|
52
|
+
if (shouldUseDeployless(params)) {
|
|
53
|
+
try {
|
|
54
|
+
const position = await readContract(client, {
|
|
55
|
+
...callParameters(params),
|
|
56
|
+
abi: getPositionAbi,
|
|
57
|
+
code: getPositionCode,
|
|
58
|
+
functionName: "query",
|
|
59
|
+
args: [midnight, params.marketId, params.user],
|
|
60
|
+
});
|
|
61
|
+
const collateral = position.collateral;
|
|
62
|
+
return new Position({
|
|
63
|
+
credit: position.credit,
|
|
64
|
+
pendingFee: position.pendingFee,
|
|
65
|
+
lastLossFactor: position.lastLossFactor,
|
|
66
|
+
lastAccrual: position.lastAccrual,
|
|
67
|
+
debt: position.debt,
|
|
68
|
+
collateralBitmap: position.collateralBitmap,
|
|
69
|
+
collateral: collateral.map((assets) => BigInt(assets)),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (params.deployless === "force")
|
|
74
|
+
throw error;
|
|
75
|
+
// Fallback to direct reads if deployless execution is unavailable.
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const position = await readContract(client, {
|
|
79
|
+
...callParameters(params),
|
|
80
|
+
address: midnight,
|
|
81
|
+
abi: midnightAbi,
|
|
82
|
+
functionName: "position",
|
|
83
|
+
args: [params.marketId, params.user],
|
|
84
|
+
});
|
|
85
|
+
const collateral = await Promise.all(Array.from({ length: Number(MAX_COLLATERALS) }, (_, collateralIndex) => {
|
|
86
|
+
return readContract(client, {
|
|
87
|
+
...callParameters(params),
|
|
88
|
+
address: midnight,
|
|
89
|
+
abi: midnightAbi,
|
|
90
|
+
functionName: "collateral",
|
|
91
|
+
args: [params.marketId, params.user, BigInt(collateralIndex)],
|
|
92
|
+
});
|
|
93
|
+
}));
|
|
94
|
+
return new Position({
|
|
95
|
+
credit: position[0],
|
|
96
|
+
pendingFee: position[1],
|
|
97
|
+
lastLossFactor: position[2],
|
|
98
|
+
lastAccrual: position[3],
|
|
99
|
+
debt: position[4],
|
|
100
|
+
collateralBitmap: position[5],
|
|
101
|
+
collateral: [...collateral],
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Fetches a Midnight position paired with its hydrated market.
|
|
106
|
+
*
|
|
107
|
+
* Reads the full inventory of `fetchPosition` and `fetchMarket` at the same
|
|
108
|
+
* call parameters: `eth_chainId` only when needed,
|
|
109
|
+
* `Midnight.toMarket(marketId)`, `Midnight.marketState(marketId)`, and either
|
|
110
|
+
* deployless `GetPosition.query(midnight, marketId, user)` or direct
|
|
111
|
+
* `Midnight.position(marketId, user)` plus all collateral slots.
|
|
112
|
+
*
|
|
113
|
+
* @param client - Viem client used for the reads.
|
|
114
|
+
* @param params.marketId - Market id whose position and market state to read.
|
|
115
|
+
* @param params.user - Position owner address.
|
|
116
|
+
* @param params.deployless - Optional deployless mode for the position read; defaults to `true` with fallback, or `"force"` without fallback.
|
|
117
|
+
* @param params.account - Optional account used as the `from` field for the reads.
|
|
118
|
+
* @param params.blockNumber - Optional block number used for the reads.
|
|
119
|
+
* @param params.blockTag - Optional block tag used for the reads.
|
|
120
|
+
* @param params.stateOverride - Optional state override set used for the reads.
|
|
121
|
+
* @returns Accrual position instance.
|
|
122
|
+
* @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
|
|
123
|
+
* @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id.
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* import { fetchAccrualPosition } from "@morpho-org/midnight-sdk";
|
|
127
|
+
* import { createPublicClient, http } from "viem";
|
|
128
|
+
* import { base } from "viem/chains";
|
|
129
|
+
*
|
|
130
|
+
* const client = createPublicClient({ chain: base, transport: http() });
|
|
131
|
+
* const position = await fetchAccrualPosition(client, {
|
|
132
|
+
* marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
|
|
133
|
+
* user: "0x0000000000000000000000000000000000009000",
|
|
134
|
+
* });
|
|
135
|
+
* console.log(position.market.id);
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export async function fetchAccrualPosition(client, params) {
|
|
139
|
+
const [position, market] = await Promise.all([
|
|
140
|
+
fetchPosition(client, params),
|
|
141
|
+
fetchMarket(client, params),
|
|
142
|
+
]);
|
|
143
|
+
return new AccrualPosition(position, market);
|
|
144
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Address, Client } from "viem";
|
|
2
|
+
import { type RatifierInfo } from "../signatures/RatifierUtils.js";
|
|
3
|
+
import type { MidnightCallParameters } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Fetches maker bytecode and classifies the ratifier route for new offers.
|
|
6
|
+
*
|
|
7
|
+
* The `bytecode` read is the result of `eth_getCode` for `params.maker` at the
|
|
8
|
+
* requested block. It is not returned to callers; it only tells the SDK whether
|
|
9
|
+
* the maker is an EOA or EIP-7702 account that can use Ecrecover signatures, or
|
|
10
|
+
* a deployed-code account that should use Setter root approval. Put the returned
|
|
11
|
+
* `ratifier` address on `Offer.create`, then use the returned `type` to choose
|
|
12
|
+
* `EcrecoverRatifierUtils.ratify` or `SetterRatifierUtils.ratify` after the tree
|
|
13
|
+
* has been built.
|
|
14
|
+
*
|
|
15
|
+
* Reads `eth_chainId` only when the viem client has no configured chain id,
|
|
16
|
+
* then reads `eth_getCode` for `params.maker`.
|
|
17
|
+
*
|
|
18
|
+
* @param client - Viem client used for the bytecode read.
|
|
19
|
+
* @param params.maker - Maker address whose bytecode determines the ratifier route.
|
|
20
|
+
* @param params.blockNumber - Optional block number used for the bytecode read.
|
|
21
|
+
* @param params.blockTag - Optional block tag used for the bytecode read.
|
|
22
|
+
* @returns Ratifier information.
|
|
23
|
+
* @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
|
|
24
|
+
* @throws {UnknownAddressError} when the registry has no configured ratifier address for the client chain id.
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { fetchRatifierInfo } from "@morpho-org/midnight-sdk";
|
|
28
|
+
*
|
|
29
|
+
* const maker = "0x7b093658BE7f90B63D7c359e8f408e503c2D9401";
|
|
30
|
+
* const info = await fetchRatifierInfo(client, { maker });
|
|
31
|
+
* // Pass info.ratifier to Offer.create(...), then use info.type after
|
|
32
|
+
* // Tree.create([...]) to choose the Ecrecover or Setter ratifier flow.
|
|
33
|
+
* console.log(info.type, info.ratifier);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function fetchRatifierInfo(client: Client, params: {
|
|
37
|
+
readonly maker: Address;
|
|
38
|
+
} & MidnightCallParameters): Promise<RatifierInfo>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { getChainAddress } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { getBytecode } from "viem/actions";
|
|
3
|
+
import { RatifierUtils, } from "../signatures/RatifierUtils.js";
|
|
4
|
+
import { resolveChainId } from "./utils.js";
|
|
5
|
+
/**
|
|
6
|
+
* Fetches maker bytecode and classifies the ratifier route for new offers.
|
|
7
|
+
*
|
|
8
|
+
* The `bytecode` read is the result of `eth_getCode` for `params.maker` at the
|
|
9
|
+
* requested block. It is not returned to callers; it only tells the SDK whether
|
|
10
|
+
* the maker is an EOA or EIP-7702 account that can use Ecrecover signatures, or
|
|
11
|
+
* a deployed-code account that should use Setter root approval. Put the returned
|
|
12
|
+
* `ratifier` address on `Offer.create`, then use the returned `type` to choose
|
|
13
|
+
* `EcrecoverRatifierUtils.ratify` or `SetterRatifierUtils.ratify` after the tree
|
|
14
|
+
* has been built.
|
|
15
|
+
*
|
|
16
|
+
* Reads `eth_chainId` only when the viem client has no configured chain id,
|
|
17
|
+
* then reads `eth_getCode` for `params.maker`.
|
|
18
|
+
*
|
|
19
|
+
* @param client - Viem client used for the bytecode read.
|
|
20
|
+
* @param params.maker - Maker address whose bytecode determines the ratifier route.
|
|
21
|
+
* @param params.blockNumber - Optional block number used for the bytecode read.
|
|
22
|
+
* @param params.blockTag - Optional block tag used for the bytecode read.
|
|
23
|
+
* @returns Ratifier information.
|
|
24
|
+
* @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
|
|
25
|
+
* @throws {UnknownAddressError} when the registry has no configured ratifier address for the client chain id.
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { fetchRatifierInfo } from "@morpho-org/midnight-sdk";
|
|
29
|
+
*
|
|
30
|
+
* const maker = "0x7b093658BE7f90B63D7c359e8f408e503c2D9401";
|
|
31
|
+
* const info = await fetchRatifierInfo(client, { maker });
|
|
32
|
+
* // Pass info.ratifier to Offer.create(...), then use info.type after
|
|
33
|
+
* // Tree.create([...]) to choose the Ecrecover or Setter ratifier flow.
|
|
34
|
+
* console.log(info.type, info.ratifier);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export async function fetchRatifierInfo(client, params) {
|
|
38
|
+
const chainId = await resolveChainId(client);
|
|
39
|
+
const ecrecoverRatifier = getChainAddress(chainId, "ecrecoverRatifier");
|
|
40
|
+
const setterRatifier = getChainAddress(chainId, "setterRatifier");
|
|
41
|
+
const bytecode = await getBytecode(client, {
|
|
42
|
+
...(params.blockNumber != null
|
|
43
|
+
? { blockNumber: params.blockNumber }
|
|
44
|
+
: params.blockTag != null
|
|
45
|
+
? { blockTag: params.blockTag }
|
|
46
|
+
: {}),
|
|
47
|
+
address: params.maker,
|
|
48
|
+
});
|
|
49
|
+
return RatifierUtils.getRatifierInfo({
|
|
50
|
+
bytecode,
|
|
51
|
+
ecrecoverRatifier,
|
|
52
|
+
setterRatifier,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Account, Address, BlockTag, StateOverride } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* Shared viem call parameters accepted by Midnight fetch helpers.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import type { MidnightCallParameters } from "@morpho-org/midnight-sdk";
|
|
8
|
+
*
|
|
9
|
+
* const params: MidnightCallParameters = { blockTag: "latest" };
|
|
10
|
+
* console.log(params.blockTag);
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export interface MidnightCallParameters {
|
|
14
|
+
/** Account used as the `from` field for the read. */
|
|
15
|
+
readonly account?: Account | Address;
|
|
16
|
+
/** Block number used for the read. */
|
|
17
|
+
readonly blockNumber?: bigint;
|
|
18
|
+
/** Block tag used for the read. */
|
|
19
|
+
readonly blockTag?: BlockTag;
|
|
20
|
+
/** State override set used for the read. */
|
|
21
|
+
readonly stateOverride?: StateOverride;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Deployless read mode accepted by deployless-capable Midnight fetch helpers.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import type { DeploylessFetchParameters } from "@morpho-org/midnight-sdk";
|
|
29
|
+
*
|
|
30
|
+
* const params: DeploylessFetchParameters = { deployless: "force" };
|
|
31
|
+
* console.log(params.deployless);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export interface DeploylessFetchParameters extends MidnightCallParameters {
|
|
35
|
+
/**
|
|
36
|
+
* If `true`, deployless-capable fetchers use deployless reads and fall back to direct reads if they fail.
|
|
37
|
+
*
|
|
38
|
+
* If `"force"`, deployless-capable fetchers use deployless reads without fallback.
|
|
39
|
+
*
|
|
40
|
+
* If `false`, deployless-capable fetchers use direct reads.
|
|
41
|
+
*
|
|
42
|
+
* Default is `true` for fetchers that implement deployless reads.
|
|
43
|
+
*/
|
|
44
|
+
readonly deployless?: boolean | "force";
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Shared viem fetch parameters for Midnight helpers.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import type { MidnightFetchParams } from "@morpho-org/midnight-sdk";
|
|
52
|
+
*
|
|
53
|
+
* const params: MidnightFetchParams = { blockTag: "latest" };
|
|
54
|
+
* console.log(params.blockTag);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export type MidnightFetchParams = DeploylessFetchParameters;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Client } from "viem";
|
|
2
|
+
import type { DeploylessFetchParameters, MidnightCallParameters } from "./types.js";
|
|
3
|
+
/** @internal Extracts viem call parameters from Midnight fetch inputs. */
|
|
4
|
+
export declare const callParameters: (params: MidnightCallParameters) => MidnightCallParameters;
|
|
5
|
+
/** @internal Returns the configured deployless mode, defaulting to enabled. */
|
|
6
|
+
export declare const shouldUseDeployless: (params: DeploylessFetchParameters) => boolean | "force";
|
|
7
|
+
/** @internal Returns the configured viem client chain id or fetches it from RPC. */
|
|
8
|
+
export declare const resolveChainId: (client: Client) => Promise<number>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { getChainId } from "viem/actions";
|
|
2
|
+
/** @internal Extracts viem call parameters from Midnight fetch inputs. */
|
|
3
|
+
export const callParameters = (params) => ({
|
|
4
|
+
account: params.account,
|
|
5
|
+
blockNumber: params.blockNumber,
|
|
6
|
+
blockTag: params.blockTag,
|
|
7
|
+
stateOverride: params.stateOverride,
|
|
8
|
+
});
|
|
9
|
+
/** @internal Returns the configured deployless mode, defaulting to enabled. */
|
|
10
|
+
export const shouldUseDeployless = (params) => params.deployless ?? true;
|
|
11
|
+
/** @internal Returns the configured viem client chain id or fetches it from RPC. */
|
|
12
|
+
export const resolveChainId = async (client) => client.chain?.id ?? (await getChainId(client));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from "./abis.js";
|
|
2
|
+
export * from "./constants.js";
|
|
3
|
+
export * from "./errors.js";
|
|
4
|
+
export * from "./fetch/index.js";
|
|
5
|
+
export * from "./market/index.js";
|
|
6
|
+
export * from "./math/index.js";
|
|
7
|
+
export * from "./offers/index.js";
|
|
8
|
+
export * from "./signatures/index.js";
|
|
9
|
+
/**
|
|
10
|
+
* Namespace for Midnight mempool payload utilities.
|
|
11
|
+
*
|
|
12
|
+
* Use `Payload.encode` to turn ratified offer items into wire bytes for mempool
|
|
13
|
+
* publication or raw payload API validation, and `Payload.decode` to inspect
|
|
14
|
+
* published maker offers, ratifier data, or attribution-tagged payloads.
|
|
15
|
+
* Encoded payloads are always `Hex` strings ready to be included in onchain
|
|
16
|
+
* mempool submission calldata.
|
|
17
|
+
*/
|
|
18
|
+
export * as Payload from "./signatures/Payload.js";
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from "./abis.js";
|
|
2
|
+
export * from "./constants.js";
|
|
3
|
+
export * from "./errors.js";
|
|
4
|
+
export * from "./fetch/index.js";
|
|
5
|
+
export * from "./market/index.js";
|
|
6
|
+
export * from "./math/index.js";
|
|
7
|
+
export * from "./offers/index.js";
|
|
8
|
+
export * from "./signatures/index.js";
|
|
9
|
+
/**
|
|
10
|
+
* Namespace for Midnight mempool payload utilities.
|
|
11
|
+
*
|
|
12
|
+
* Use `Payload.encode` to turn ratified offer items into wire bytes for mempool
|
|
13
|
+
* publication or raw payload API validation, and `Payload.decode` to inspect
|
|
14
|
+
* published maker offers, ratifier data, or attribution-tagged payloads.
|
|
15
|
+
* Encoded payloads are always `Hex` strings ready to be included in onchain
|
|
16
|
+
* mempool submission calldata.
|
|
17
|
+
*/
|
|
18
|
+
export * as Payload from "./signatures/Payload.js";
|