@rootzero/contracts 0.6.2 → 0.7.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/Commands.sol +8 -16
- package/Core.sol +4 -2
- package/Cursors.sol +2 -2
- package/Events.sol +4 -2
- package/Queries.sol +10 -0
- package/blocks/Cursors.sol +168 -8
- package/blocks/Keys.sol +4 -1
- package/blocks/Schema.sol +32 -11
- package/blocks/Writers.sol +536 -95
- package/commands/Base.sol +20 -6
- package/commands/Provision.sol +5 -12
- package/commands/Stake.sol +1 -84
- package/commands/admin/AllowAssets.sol +7 -5
- package/commands/admin/DenyAssets.sol +7 -5
- package/commands/admin/Relocate.sol +2 -2
- package/core/Access.sol +2 -4
- package/core/CursorBase.sol +43 -0
- package/core/Host.sol +1 -1
- package/core/HostBound.sol +14 -0
- package/core/Operation.sol +41 -37
- package/events/Erc721.sol +20 -0
- package/events/Query.sol +20 -0
- package/events/Swap.sol +20 -0
- package/package.json +1 -1
- package/peer/AllowAssets.sol +4 -9
- package/peer/AssetPull.sol +41 -0
- package/peer/Base.sol +11 -0
- package/peer/DenyAssets.sol +4 -9
- package/peer/Pull.sol +8 -11
- package/peer/Push.sol +6 -3
- package/queries/Assets.sol +46 -0
- package/queries/Balances.sol +46 -0
- package/queries/Base.sol +34 -0
- package/queries/Positions.sol +49 -0
- package/utils/Ids.sol +68 -1
- package/utils/Layout.sol +2 -0
- package/commands/Borrow.sol +0 -89
- package/commands/Liquidate.sol +0 -99
- package/commands/Liquidity.sol +0 -180
- package/commands/Mint.sol +0 -54
- package/commands/Reclaim.sol +0 -55
- package/commands/Redeem.sol +0 -99
- package/commands/Repay.sol +0 -99
- package/commands/Swap.sol +0 -90
- package/commands/Unstake.sol +0 -58
- package/events/Quote.sol +0 -21
- /package/events/{HostAnnounced.sol → Host.sol} +0 -0
package/blocks/Writers.sol
CHANGED
|
@@ -60,32 +60,69 @@ library Writers {
|
|
|
60
60
|
writer = Writer({i: 0, end: len, dst: new bytes(len)});
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
/// @notice Append arbitrary bytes to the writer.
|
|
64
|
-
/// @param writer Destination writer; `i` is advanced by `data.length`.
|
|
65
|
-
/// @param data Bytes to append.
|
|
66
|
-
function append(Writer memory writer, bytes memory data) internal pure {
|
|
67
|
-
uint next = writer.i + data.length;
|
|
68
|
-
if (next > writer.dst.length) revert WriterOverflow();
|
|
69
|
-
// Copy `data` into `dst` starting at the current write position.
|
|
70
|
-
assembly ("memory-safe") {
|
|
71
|
-
mcopy(add(add(mload(add(writer, 0x40)), 0x20), mload(writer)), add(data, 0x20), mload(data))
|
|
72
|
-
}
|
|
73
|
-
writer.i = next;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
63
|
/// @notice Allocate a writer sized for exactly `count` BALANCE blocks (1:1 ratio).
|
|
77
64
|
/// @param count Number of balance blocks to allocate space for.
|
|
78
65
|
/// @return writer Allocated writer.
|
|
79
66
|
function allocBalances(uint count) internal pure returns (Writer memory writer) {
|
|
80
|
-
return
|
|
67
|
+
return alloc96s(count);
|
|
81
68
|
}
|
|
82
69
|
|
|
83
|
-
/// @notice Allocate a writer sized for `
|
|
84
|
-
///
|
|
85
|
-
/// @param count Number of input blocks; output capacity is doubled.
|
|
70
|
+
/// @notice Allocate a writer sized for exactly `count` AMOUNT blocks (1:1 ratio).
|
|
71
|
+
/// @param count Number of amount blocks to allocate space for.
|
|
86
72
|
/// @return writer Allocated writer.
|
|
87
|
-
function
|
|
88
|
-
return
|
|
73
|
+
function allocAmounts(uint count) internal pure returns (Writer memory writer) {
|
|
74
|
+
return alloc96s(count);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// @notice Allocate a writer sized for exactly `count` ASSET blocks (1:1 ratio).
|
|
78
|
+
/// @param count Number of asset blocks to allocate space for.
|
|
79
|
+
/// @return writer Allocated writer.
|
|
80
|
+
function allocAssets(uint count) internal pure returns (Writer memory writer) {
|
|
81
|
+
return alloc64s(count);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// @notice Allocate a writer sized for exactly `count` dynamic blocks with a shared payload length.
|
|
85
|
+
/// Each block reserves `Sizes.Header + payloadLen` bytes.
|
|
86
|
+
/// @param count Number of blocks to allocate space for.
|
|
87
|
+
/// @param payloadLen Payload byte length for each block.
|
|
88
|
+
/// @return writer Allocated writer.
|
|
89
|
+
function allocBytes(uint count, uint payloadLen) internal pure returns (Writer memory writer) {
|
|
90
|
+
return allocFromScaledCount(count, ALLOC_SCALE, Sizes.Header + payloadLen);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/// @notice Allocate a writer sized for exactly `count` 32-byte-payload blocks (1:1 ratio).
|
|
94
|
+
/// @param count Number of blocks to allocate space for.
|
|
95
|
+
/// @return writer Allocated writer.
|
|
96
|
+
function alloc32s(uint count) internal pure returns (Writer memory writer) {
|
|
97
|
+
return allocFromScaledCount(count, ALLOC_SCALE, Sizes.B32);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/// @notice Allocate a writer sized for exactly `count` 64-byte-payload blocks (1:1 ratio).
|
|
101
|
+
/// @param count Number of blocks to allocate space for.
|
|
102
|
+
/// @return writer Allocated writer.
|
|
103
|
+
function alloc64s(uint count) internal pure returns (Writer memory writer) {
|
|
104
|
+
return allocFromScaledCount(count, ALLOC_SCALE, Sizes.B64);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// @notice Allocate a writer sized for exactly `count` 96-byte-payload blocks (1:1 ratio).
|
|
108
|
+
/// @param count Number of blocks to allocate space for.
|
|
109
|
+
/// @return writer Allocated writer.
|
|
110
|
+
function alloc96s(uint count) internal pure returns (Writer memory writer) {
|
|
111
|
+
return allocFromScaledCount(count, ALLOC_SCALE, Sizes.B96);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/// @notice Allocate a writer sized for exactly `count` 128-byte-payload blocks (1:1 ratio).
|
|
115
|
+
/// @param count Number of blocks to allocate space for.
|
|
116
|
+
/// @return writer Allocated writer.
|
|
117
|
+
function alloc128s(uint count) internal pure returns (Writer memory writer) {
|
|
118
|
+
return allocFromScaledCount(count, ALLOC_SCALE, Sizes.B128);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// @notice Allocate a writer sized for exactly `count` 160-byte-payload blocks (1:1 ratio).
|
|
122
|
+
/// @param count Number of blocks to allocate space for.
|
|
123
|
+
/// @return writer Allocated writer.
|
|
124
|
+
function alloc160s(uint count) internal pure returns (Writer memory writer) {
|
|
125
|
+
return allocFromScaledCount(count, ALLOC_SCALE, Sizes.B160);
|
|
89
126
|
}
|
|
90
127
|
|
|
91
128
|
/// @notice Allocate a writer for BALANCE blocks with a custom output-to-input ratio.
|
|
@@ -94,21 +131,89 @@ library Writers {
|
|
|
94
131
|
/// (e.g. `ALLOC_SCALE` = 1:1, `2 * ALLOC_SCALE` = 2:1).
|
|
95
132
|
/// @return writer Allocated writer.
|
|
96
133
|
function allocScaledBalances(uint count, uint scaledRatio) internal pure returns (Writer memory writer) {
|
|
97
|
-
return
|
|
134
|
+
return allocScaled96s(count, scaledRatio);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/// @notice Allocate a writer for AMOUNT blocks with a custom output-to-input ratio.
|
|
138
|
+
/// @param count Number of input blocks.
|
|
139
|
+
/// @param scaledRatio Output count multiplier expressed in `ALLOC_SCALE` units
|
|
140
|
+
/// (e.g. `ALLOC_SCALE` = 1:1, `2 * ALLOC_SCALE` = 2:1).
|
|
141
|
+
/// @return writer Allocated writer.
|
|
142
|
+
function allocScaledAmounts(uint count, uint scaledRatio) internal pure returns (Writer memory writer) {
|
|
143
|
+
return allocScaled96s(count, scaledRatio);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/// @notice Allocate a writer for ASSET blocks with a custom output-to-input ratio.
|
|
147
|
+
/// @param count Number of input blocks.
|
|
148
|
+
/// @param scaledRatio Output count multiplier expressed in `ALLOC_SCALE` units
|
|
149
|
+
/// (e.g. `ALLOC_SCALE` = 1:1, `2 * ALLOC_SCALE` = 2:1).
|
|
150
|
+
/// @return writer Allocated writer.
|
|
151
|
+
function allocScaledAssets(uint count, uint scaledRatio) internal pure returns (Writer memory writer) {
|
|
152
|
+
return allocScaled64s(count, scaledRatio);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/// @notice Allocate a writer for dynamic blocks with a shared payload length and custom output ratio.
|
|
156
|
+
/// Each block reserves `Sizes.Header + payloadLen` bytes.
|
|
157
|
+
/// @param count Number of input blocks.
|
|
158
|
+
/// @param scaledRatio Output count multiplier expressed in `ALLOC_SCALE` units.
|
|
159
|
+
/// @param payloadLen Payload byte length for each block.
|
|
160
|
+
/// @return writer Allocated writer.
|
|
161
|
+
function allocScaledBytes(uint count, uint scaledRatio, uint payloadLen) internal pure returns (Writer memory writer) {
|
|
162
|
+
return allocFromScaledCount(count, scaledRatio, Sizes.Header + payloadLen);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// @notice Allocate a writer for 32-byte-payload blocks with a custom output-to-input ratio.
|
|
166
|
+
/// @param count Number of input blocks.
|
|
167
|
+
/// @param scaledRatio Output count multiplier expressed in `ALLOC_SCALE` units.
|
|
168
|
+
/// @return writer Allocated writer.
|
|
169
|
+
function allocScaled32s(uint count, uint scaledRatio) internal pure returns (Writer memory writer) {
|
|
170
|
+
return allocFromScaledCount(count, scaledRatio, Sizes.B32);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// @notice Allocate a writer for 64-byte-payload blocks with a custom output-to-input ratio.
|
|
174
|
+
/// @param count Number of input blocks.
|
|
175
|
+
/// @param scaledRatio Output count multiplier expressed in `ALLOC_SCALE` units.
|
|
176
|
+
/// @return writer Allocated writer.
|
|
177
|
+
function allocScaled64s(uint count, uint scaledRatio) internal pure returns (Writer memory writer) {
|
|
178
|
+
return allocFromScaledCount(count, scaledRatio, Sizes.B64);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/// @notice Allocate a writer for 96-byte-payload blocks with a custom output-to-input ratio.
|
|
182
|
+
/// @param count Number of input blocks.
|
|
183
|
+
/// @param scaledRatio Output count multiplier expressed in `ALLOC_SCALE` units.
|
|
184
|
+
/// @return writer Allocated writer.
|
|
185
|
+
function allocScaled96s(uint count, uint scaledRatio) internal pure returns (Writer memory writer) {
|
|
186
|
+
return allocFromScaledCount(count, scaledRatio, Sizes.B96);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/// @notice Allocate a writer for 128-byte-payload blocks with a custom output-to-input ratio.
|
|
190
|
+
/// @param count Number of input blocks.
|
|
191
|
+
/// @param scaledRatio Output count multiplier expressed in `ALLOC_SCALE` units.
|
|
192
|
+
/// @return writer Allocated writer.
|
|
193
|
+
function allocScaled128s(uint count, uint scaledRatio) internal pure returns (Writer memory writer) {
|
|
194
|
+
return allocFromScaledCount(count, scaledRatio, Sizes.B128);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/// @notice Allocate a writer for 160-byte-payload blocks with a custom output-to-input ratio.
|
|
198
|
+
/// @param count Number of input blocks.
|
|
199
|
+
/// @param scaledRatio Output count multiplier expressed in `ALLOC_SCALE` units.
|
|
200
|
+
/// @return writer Allocated writer.
|
|
201
|
+
function allocScaled160s(uint count, uint scaledRatio) internal pure returns (Writer memory writer) {
|
|
202
|
+
return allocFromScaledCount(count, scaledRatio, Sizes.B160);
|
|
98
203
|
}
|
|
99
204
|
|
|
100
205
|
/// @notice Allocate a writer sized for exactly `count` TRANSACTION blocks (1:1 ratio).
|
|
101
206
|
/// @param count Number of transaction blocks to allocate space for.
|
|
102
207
|
/// @return writer Allocated writer.
|
|
103
208
|
function allocTxs(uint count) internal pure returns (Writer memory writer) {
|
|
104
|
-
return
|
|
209
|
+
return alloc160s(count);
|
|
105
210
|
}
|
|
106
211
|
|
|
107
212
|
/// @notice Allocate a writer sized for exactly `count` CUSTODY blocks (1:1 ratio).
|
|
108
213
|
/// @param count Number of custody blocks to allocate space for.
|
|
109
214
|
/// @return writer Allocated writer.
|
|
110
215
|
function allocCustodies(uint count) internal pure returns (Writer memory writer) {
|
|
111
|
-
return
|
|
216
|
+
return alloc128s(count);
|
|
112
217
|
}
|
|
113
218
|
|
|
114
219
|
/// @notice Allocate a writer for CUSTODY blocks with a custom output-to-input ratio.
|
|
@@ -116,7 +221,7 @@ library Writers {
|
|
|
116
221
|
/// @param scaledRatio Output count multiplier in `ALLOC_SCALE` units.
|
|
117
222
|
/// @return writer Allocated writer.
|
|
118
223
|
function allocScaledCustodies(uint count, uint scaledRatio) internal pure returns (Writer memory writer) {
|
|
119
|
-
return
|
|
224
|
+
return allocScaled128s(count, scaledRatio);
|
|
120
225
|
}
|
|
121
226
|
|
|
122
227
|
/// @notice Core allocation routine used by all typed `alloc*` helpers.
|
|
@@ -138,30 +243,402 @@ library Writers {
|
|
|
138
243
|
}
|
|
139
244
|
|
|
140
245
|
// -------------------------------------------------------------------------
|
|
141
|
-
//
|
|
246
|
+
// Fixed-width write helpers
|
|
142
247
|
// -------------------------------------------------------------------------
|
|
143
248
|
|
|
144
|
-
/// @notice Write a
|
|
145
|
-
/// @param dst Destination buffer; must have at least `i + Sizes.
|
|
249
|
+
/// @notice Write a fixed-width 32-byte-payload block directly into `dst` at byte offset `i`.
|
|
250
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.B32` bytes.
|
|
146
251
|
/// @param i Write offset within `dst`.
|
|
147
|
-
/// @param
|
|
252
|
+
/// @param key Block type key.
|
|
253
|
+
/// @param a First payload word.
|
|
148
254
|
/// @return next Byte offset immediately after the written block.
|
|
149
|
-
function
|
|
150
|
-
next = i + Sizes.
|
|
255
|
+
function write32(bytes memory dst, uint i, bytes4 key, bytes32 a) internal pure returns (uint next) {
|
|
256
|
+
next = i + Sizes.B32;
|
|
257
|
+
if (next > dst.length) revert WriterOverflow();
|
|
258
|
+
uint header = toBlockHeader(key, 32);
|
|
259
|
+
assembly ("memory-safe") {
|
|
260
|
+
let p := add(add(dst, 0x20), i)
|
|
261
|
+
mstore(p, header)
|
|
262
|
+
mstore(add(p, 0x08), a)
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/// @notice Write an ABI-style boolean as a 32-byte scalar payload block.
|
|
267
|
+
/// Encodes `false` as `bytes32(0)` and `true` as `bytes32(uint(1))`.
|
|
268
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.B32` bytes.
|
|
269
|
+
/// @param i Write offset within `dst`.
|
|
270
|
+
/// @param key Block type key.
|
|
271
|
+
/// @param value Boolean value to encode.
|
|
272
|
+
/// @return next Byte offset immediately after the written block.
|
|
273
|
+
function writeBool(bytes memory dst, uint i, bytes4 key, bool value) internal pure returns (uint next) {
|
|
274
|
+
return write32(dst, i, key, value ? bytes32(uint(1)) : bytes32(0));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/// @notice Write a fixed-width 64-byte-payload block directly into `dst` at byte offset `i`.
|
|
278
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.B64` bytes.
|
|
279
|
+
/// @param i Write offset within `dst`.
|
|
280
|
+
/// @param key Block type key.
|
|
281
|
+
/// @param a First payload word.
|
|
282
|
+
/// @param b Second payload word.
|
|
283
|
+
/// @return next Byte offset immediately after the written block.
|
|
284
|
+
function write64(bytes memory dst, uint i, bytes4 key, bytes32 a, bytes32 b) internal pure returns (uint next) {
|
|
285
|
+
next = i + Sizes.B64;
|
|
151
286
|
if (next > dst.length) revert WriterOverflow();
|
|
152
|
-
uint header = toBlockHeader(
|
|
153
|
-
// Write 8-byte header then three 32-byte payload words.
|
|
287
|
+
uint header = toBlockHeader(key, 64);
|
|
154
288
|
assembly ("memory-safe") {
|
|
155
289
|
let p := add(add(dst, 0x20), i)
|
|
156
290
|
mstore(p, header)
|
|
157
|
-
mstore(add(p, 0x08),
|
|
158
|
-
mstore(add(p, 0x28),
|
|
159
|
-
mstore(add(p, 0x48), mload(add(value, 0x40)))
|
|
291
|
+
mstore(add(p, 0x08), a)
|
|
292
|
+
mstore(add(p, 0x28), b)
|
|
160
293
|
}
|
|
161
294
|
}
|
|
162
295
|
|
|
296
|
+
/// @notice Write a fixed-width 96-byte-payload block directly into `dst` at byte offset `i`.
|
|
297
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.B96` bytes.
|
|
298
|
+
/// @param i Write offset within `dst`.
|
|
299
|
+
/// @param key Block type key.
|
|
300
|
+
/// @param a First payload word.
|
|
301
|
+
/// @param b Second payload word.
|
|
302
|
+
/// @param c Third payload word.
|
|
303
|
+
/// @return next Byte offset immediately after the written block.
|
|
304
|
+
function write96(
|
|
305
|
+
bytes memory dst,
|
|
306
|
+
uint i,
|
|
307
|
+
bytes4 key,
|
|
308
|
+
bytes32 a,
|
|
309
|
+
bytes32 b,
|
|
310
|
+
bytes32 c
|
|
311
|
+
) internal pure returns (uint next) {
|
|
312
|
+
next = i + Sizes.B96;
|
|
313
|
+
if (next > dst.length) revert WriterOverflow();
|
|
314
|
+
uint header = toBlockHeader(key, 96);
|
|
315
|
+
assembly ("memory-safe") {
|
|
316
|
+
let p := add(add(dst, 0x20), i)
|
|
317
|
+
mstore(p, header)
|
|
318
|
+
mstore(add(p, 0x08), a)
|
|
319
|
+
mstore(add(p, 0x28), b)
|
|
320
|
+
mstore(add(p, 0x48), c)
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/// @notice Write a fixed-width 128-byte-payload block directly into `dst` at byte offset `i`.
|
|
325
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.B128` bytes.
|
|
326
|
+
/// @param i Write offset within `dst`.
|
|
327
|
+
/// @param key Block type key.
|
|
328
|
+
/// @param a First payload word.
|
|
329
|
+
/// @param b Second payload word.
|
|
330
|
+
/// @param c Third payload word.
|
|
331
|
+
/// @param d Fourth payload word.
|
|
332
|
+
/// @return next Byte offset immediately after the written block.
|
|
333
|
+
function write128(
|
|
334
|
+
bytes memory dst,
|
|
335
|
+
uint i,
|
|
336
|
+
bytes4 key,
|
|
337
|
+
bytes32 a,
|
|
338
|
+
bytes32 b,
|
|
339
|
+
bytes32 c,
|
|
340
|
+
bytes32 d
|
|
341
|
+
) internal pure returns (uint next) {
|
|
342
|
+
next = i + Sizes.B128;
|
|
343
|
+
if (next > dst.length) revert WriterOverflow();
|
|
344
|
+
uint header = toBlockHeader(key, 128);
|
|
345
|
+
assembly ("memory-safe") {
|
|
346
|
+
let p := add(add(dst, 0x20), i)
|
|
347
|
+
mstore(p, header)
|
|
348
|
+
mstore(add(p, 0x08), a)
|
|
349
|
+
mstore(add(p, 0x28), b)
|
|
350
|
+
mstore(add(p, 0x48), c)
|
|
351
|
+
mstore(add(p, 0x68), d)
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/// @notice Write a fixed-width 160-byte-payload block directly into `dst` at byte offset `i`.
|
|
356
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.B160` bytes.
|
|
357
|
+
/// @param i Write offset within `dst`.
|
|
358
|
+
/// @param key Block type key.
|
|
359
|
+
/// @param a First payload word.
|
|
360
|
+
/// @param b Second payload word.
|
|
361
|
+
/// @param c Third payload word.
|
|
362
|
+
/// @param d Fourth payload word.
|
|
363
|
+
/// @param e Fifth payload word.
|
|
364
|
+
/// @return next Byte offset immediately after the written block.
|
|
365
|
+
function write160(
|
|
366
|
+
bytes memory dst,
|
|
367
|
+
uint i,
|
|
368
|
+
bytes4 key,
|
|
369
|
+
bytes32 a,
|
|
370
|
+
bytes32 b,
|
|
371
|
+
bytes32 c,
|
|
372
|
+
bytes32 d,
|
|
373
|
+
bytes32 e
|
|
374
|
+
) internal pure returns (uint next) {
|
|
375
|
+
next = i + Sizes.B160;
|
|
376
|
+
if (next > dst.length) revert WriterOverflow();
|
|
377
|
+
uint header = toBlockHeader(key, 160);
|
|
378
|
+
assembly ("memory-safe") {
|
|
379
|
+
let p := add(add(dst, 0x20), i)
|
|
380
|
+
mstore(p, header)
|
|
381
|
+
mstore(add(p, 0x08), a)
|
|
382
|
+
mstore(add(p, 0x28), b)
|
|
383
|
+
mstore(add(p, 0x48), c)
|
|
384
|
+
mstore(add(p, 0x68), d)
|
|
385
|
+
mstore(add(p, 0x88), e)
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/// @notice Write a dynamic block with a fixed 32-byte head word.
|
|
390
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.B32 + tail.length` bytes.
|
|
391
|
+
/// @param i Write offset within `dst`.
|
|
392
|
+
/// @param key Block type key.
|
|
393
|
+
/// @param a Fixed head word.
|
|
394
|
+
/// @param tail Dynamic payload bytes appended after the head.
|
|
395
|
+
/// @return next Byte offset immediately after the written block.
|
|
396
|
+
function writeHead32(
|
|
397
|
+
bytes memory dst,
|
|
398
|
+
uint i,
|
|
399
|
+
bytes4 key,
|
|
400
|
+
bytes32 a,
|
|
401
|
+
bytes memory tail
|
|
402
|
+
) internal pure returns (uint next) {
|
|
403
|
+
next = i + Sizes.B32 + tail.length;
|
|
404
|
+
if (next > dst.length) revert WriterOverflow();
|
|
405
|
+
uint header = toBlockHeader(key, 32 + tail.length);
|
|
406
|
+
assembly ("memory-safe") {
|
|
407
|
+
let p := add(add(dst, 0x20), i)
|
|
408
|
+
mstore(p, header)
|
|
409
|
+
mstore(add(p, 0x08), a)
|
|
410
|
+
mcopy(add(p, 0x28), add(tail, 0x20), mload(tail))
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/// @notice Write a dynamic block with a fixed 64-byte head.
|
|
415
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.B64 + tail.length` bytes.
|
|
416
|
+
/// @param i Write offset within `dst`.
|
|
417
|
+
/// @param key Block type key.
|
|
418
|
+
/// @param a First fixed head word.
|
|
419
|
+
/// @param b Second fixed head word.
|
|
420
|
+
/// @param tail Dynamic payload bytes appended after the head.
|
|
421
|
+
/// @return next Byte offset immediately after the written block.
|
|
422
|
+
function writeHead64(
|
|
423
|
+
bytes memory dst,
|
|
424
|
+
uint i,
|
|
425
|
+
bytes4 key,
|
|
426
|
+
bytes32 a,
|
|
427
|
+
bytes32 b,
|
|
428
|
+
bytes memory tail
|
|
429
|
+
) internal pure returns (uint next) {
|
|
430
|
+
next = i + Sizes.B64 + tail.length;
|
|
431
|
+
if (next > dst.length) revert WriterOverflow();
|
|
432
|
+
uint header = toBlockHeader(key, 64 + tail.length);
|
|
433
|
+
assembly ("memory-safe") {
|
|
434
|
+
let p := add(add(dst, 0x20), i)
|
|
435
|
+
mstore(p, header)
|
|
436
|
+
mstore(add(p, 0x08), a)
|
|
437
|
+
mstore(add(p, 0x28), b)
|
|
438
|
+
mcopy(add(p, 0x48), add(tail, 0x20), mload(tail))
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/// @notice Write a dynamic block directly into `dst` at byte offset `i`.
|
|
443
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.Header + data.length` bytes.
|
|
444
|
+
/// @param i Write offset within `dst`.
|
|
445
|
+
/// @param key Block type key.
|
|
446
|
+
/// @param data Dynamic payload bytes.
|
|
447
|
+
/// @return next Byte offset immediately after the written block.
|
|
448
|
+
function write(bytes memory dst, uint i, bytes4 key, bytes memory data) internal pure returns (uint next) {
|
|
449
|
+
next = i + Sizes.Header + data.length;
|
|
450
|
+
if (next > dst.length) revert WriterOverflow();
|
|
451
|
+
uint header = toBlockHeader(key, data.length);
|
|
452
|
+
assembly ("memory-safe") {
|
|
453
|
+
let p := add(add(dst, 0x20), i)
|
|
454
|
+
mstore(p, header)
|
|
455
|
+
mcopy(add(p, 0x08), add(data, 0x20), mload(data))
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// -------------------------------------------------------------------------
|
|
460
|
+
// Typed write helpers
|
|
461
|
+
// -------------------------------------------------------------------------
|
|
462
|
+
|
|
463
|
+
/// @notice Write a BALANCE block directly into `dst` at byte offset `i`.
|
|
464
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.Amount` bytes.
|
|
465
|
+
/// @param i Write offset within `dst`.
|
|
466
|
+
/// @param value Balance fields to encode.
|
|
467
|
+
/// @return next Byte offset immediately after the written block.
|
|
468
|
+
function writeBalanceBlock(bytes memory dst, uint i, AssetAmount memory value) internal pure returns (uint next) {
|
|
469
|
+
return write96(dst, i, Keys.Balance, value.asset, value.meta, bytes32(value.amount));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/// @notice Write an AMOUNT block directly into `dst` at byte offset `i`.
|
|
473
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.Balance` bytes.
|
|
474
|
+
/// @param i Write offset within `dst`.
|
|
475
|
+
/// @param value Amount fields to encode.
|
|
476
|
+
/// @return next Byte offset immediately after the written block.
|
|
477
|
+
function writeAmountBlock(bytes memory dst, uint i, AssetAmount memory value) internal pure returns (uint next) {
|
|
478
|
+
return write96(dst, i, Keys.Amount, value.asset, value.meta, bytes32(value.amount));
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/// @notice Write an ASSET block directly into `dst` at byte offset `i`.
|
|
482
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.B64` bytes.
|
|
483
|
+
/// @param i Write offset within `dst`.
|
|
484
|
+
/// @param asset Asset identifier.
|
|
485
|
+
/// @param meta Asset metadata slot.
|
|
486
|
+
/// @return next Byte offset immediately after the written block.
|
|
487
|
+
function writeAssetBlock(bytes memory dst, uint i, bytes32 asset, bytes32 meta) internal pure returns (uint next) {
|
|
488
|
+
return write64(dst, i, Keys.Asset, asset, meta);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/// @notice Write a BOUNTY block directly into `dst` at byte offset `i`.
|
|
492
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.Bounty` bytes.
|
|
493
|
+
/// @param i Write offset within `dst`.
|
|
494
|
+
/// @param amount Relayer reward amount.
|
|
495
|
+
/// @param relayer Relayer account identifier.
|
|
496
|
+
/// @return next Byte offset immediately after the written block.
|
|
497
|
+
function writeBountyBlock(bytes memory dst, uint i, uint amount, bytes32 relayer) internal pure returns (uint next) {
|
|
498
|
+
return write64(dst, i, Keys.Bounty, bytes32(amount), relayer);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/// @notice Write a CUSTODY block directly into `dst` at byte offset `i`.
|
|
502
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.Custody` bytes.
|
|
503
|
+
/// @param i Write offset within `dst`.
|
|
504
|
+
/// @param value Custody fields to encode.
|
|
505
|
+
/// @return next Byte offset immediately after the written block.
|
|
506
|
+
function writeCustodyBlock(bytes memory dst, uint i, HostAmount memory value) internal pure returns (uint next) {
|
|
507
|
+
return write128(dst, i, Keys.Custody, bytes32(value.host), value.asset, value.meta, bytes32(value.amount));
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/// @notice Write a TRANSACTION block directly into `dst` at byte offset `i`.
|
|
511
|
+
/// @param dst Destination buffer; must have at least `i + Sizes.Transaction` bytes.
|
|
512
|
+
/// @param i Write offset within `dst`.
|
|
513
|
+
/// @param value Transfer record fields to encode.
|
|
514
|
+
/// @return next Byte offset immediately after the written block.
|
|
515
|
+
function writeTxBlock(bytes memory dst, uint i, Tx memory value) internal pure returns (uint next) {
|
|
516
|
+
return write160(
|
|
517
|
+
dst,
|
|
518
|
+
i,
|
|
519
|
+
Keys.Transaction,
|
|
520
|
+
bytes32(value.from),
|
|
521
|
+
bytes32(value.to),
|
|
522
|
+
value.asset,
|
|
523
|
+
value.meta,
|
|
524
|
+
bytes32(value.amount)
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// -------------------------------------------------------------------------
|
|
529
|
+
// Append helpers
|
|
530
|
+
// -------------------------------------------------------------------------
|
|
531
|
+
|
|
532
|
+
/// @notice Append arbitrary bytes to the writer.
|
|
533
|
+
/// @param writer Destination writer; `i` is advanced by `data.length`.
|
|
534
|
+
/// @param data Bytes to append.
|
|
535
|
+
function append(Writer memory writer, bytes memory data) internal pure {
|
|
536
|
+
uint next = writer.i + data.length;
|
|
537
|
+
if (next > writer.dst.length) revert WriterOverflow();
|
|
538
|
+
// Copy `data` into `dst` starting at the current write position.
|
|
539
|
+
assembly ("memory-safe") {
|
|
540
|
+
mcopy(add(add(mload(add(writer, 0x40)), 0x20), mload(writer)), add(data, 0x20), mload(data))
|
|
541
|
+
}
|
|
542
|
+
writer.i = next;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/// @notice Append a fixed-width 32-byte-payload block.
|
|
546
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.B32`.
|
|
547
|
+
/// @param key Block type key.
|
|
548
|
+
/// @param a First payload word.
|
|
549
|
+
function append32(Writer memory writer, bytes4 key, bytes32 a) internal pure {
|
|
550
|
+
writer.i = write32(writer.dst, writer.i, key, a);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/// @notice Append an ABI-style boolean as a 32-byte scalar payload block.
|
|
554
|
+
/// Encodes `false` as `bytes32(0)` and `true` as `bytes32(uint(1))`.
|
|
555
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.B32`.
|
|
556
|
+
/// @param key Block type key.
|
|
557
|
+
/// @param value Boolean value to encode.
|
|
558
|
+
function appendBool(Writer memory writer, bytes4 key, bool value) internal pure {
|
|
559
|
+
writer.i = writeBool(writer.dst, writer.i, key, value);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
/// @notice Append a fixed-width 64-byte-payload block.
|
|
564
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.B64`.
|
|
565
|
+
/// @param key Block type key.
|
|
566
|
+
/// @param a First payload word.
|
|
567
|
+
/// @param b Second payload word.
|
|
568
|
+
function append64(Writer memory writer, bytes4 key, bytes32 a, bytes32 b) internal pure {
|
|
569
|
+
writer.i = write64(writer.dst, writer.i, key, a, b);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/// @notice Append a fixed-width 96-byte-payload block.
|
|
573
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.B96`.
|
|
574
|
+
/// @param key Block type key.
|
|
575
|
+
/// @param a First payload word.
|
|
576
|
+
/// @param b Second payload word.
|
|
577
|
+
/// @param c Third payload word.
|
|
578
|
+
function append96(Writer memory writer, bytes4 key, bytes32 a, bytes32 b, bytes32 c) internal pure {
|
|
579
|
+
writer.i = write96(writer.dst, writer.i, key, a, b, c);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/// @notice Append a fixed-width 128-byte-payload block.
|
|
583
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.B128`.
|
|
584
|
+
/// @param key Block type key.
|
|
585
|
+
/// @param a First payload word.
|
|
586
|
+
/// @param b Second payload word.
|
|
587
|
+
/// @param c Third payload word.
|
|
588
|
+
/// @param d Fourth payload word.
|
|
589
|
+
function append128(Writer memory writer, bytes4 key, bytes32 a, bytes32 b, bytes32 c, bytes32 d) internal pure {
|
|
590
|
+
writer.i = write128(writer.dst, writer.i, key, a, b, c, d);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/// @notice Append a fixed-width 160-byte-payload block.
|
|
594
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.B160`.
|
|
595
|
+
/// @param key Block type key.
|
|
596
|
+
/// @param a First payload word.
|
|
597
|
+
/// @param b Second payload word.
|
|
598
|
+
/// @param c Third payload word.
|
|
599
|
+
/// @param d Fourth payload word.
|
|
600
|
+
/// @param e Fifth payload word.
|
|
601
|
+
function append160(
|
|
602
|
+
Writer memory writer,
|
|
603
|
+
bytes4 key,
|
|
604
|
+
bytes32 a,
|
|
605
|
+
bytes32 b,
|
|
606
|
+
bytes32 c,
|
|
607
|
+
bytes32 d,
|
|
608
|
+
bytes32 e
|
|
609
|
+
) internal pure {
|
|
610
|
+
writer.i = write160(writer.dst, writer.i, key, a, b, c, d, e);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/// @notice Append a dynamic block with a fixed 32-byte head word.
|
|
614
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.B32 + tail.length`.
|
|
615
|
+
/// @param key Block type key.
|
|
616
|
+
/// @param a Fixed head word.
|
|
617
|
+
/// @param tail Dynamic payload bytes appended after the head.
|
|
618
|
+
function appendHead32(Writer memory writer, bytes4 key, bytes32 a, bytes memory tail) internal pure {
|
|
619
|
+
writer.i = writeHead32(writer.dst, writer.i, key, a, tail);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/// @notice Append a dynamic block with a fixed 64-byte head.
|
|
623
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.B64 + tail.length`.
|
|
624
|
+
/// @param key Block type key.
|
|
625
|
+
/// @param a First fixed head word.
|
|
626
|
+
/// @param b Second fixed head word.
|
|
627
|
+
/// @param tail Dynamic payload bytes appended after the head.
|
|
628
|
+
function appendHead64(Writer memory writer, bytes4 key, bytes32 a, bytes32 b, bytes memory tail) internal pure {
|
|
629
|
+
writer.i = writeHead64(writer.dst, writer.i, key, a, b, tail);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/// @notice Append a dynamic block.
|
|
633
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.Header + data.length`.
|
|
634
|
+
/// @param key Block type key.
|
|
635
|
+
/// @param data Dynamic payload bytes.
|
|
636
|
+
function appendBytes(Writer memory writer, bytes4 key, bytes memory data) internal pure {
|
|
637
|
+
writer.i = write(writer.dst, writer.i, key, data);
|
|
638
|
+
}
|
|
639
|
+
|
|
163
640
|
/// @notice Append a BALANCE block using separate field values.
|
|
164
|
-
/// @param writer Destination writer; `i` is advanced by `Sizes.
|
|
641
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.Amount`.
|
|
165
642
|
/// @param asset Asset identifier.
|
|
166
643
|
/// @param meta Asset metadata slot.
|
|
167
644
|
/// @param amount Token amount.
|
|
@@ -176,6 +653,30 @@ library Writers {
|
|
|
176
653
|
writer.i = writeBalanceBlock(writer.dst, writer.i, value);
|
|
177
654
|
}
|
|
178
655
|
|
|
656
|
+
/// @notice Append an AMOUNT block using separate field values.
|
|
657
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.Amount`.
|
|
658
|
+
/// @param asset Asset identifier.
|
|
659
|
+
/// @param meta Asset metadata slot.
|
|
660
|
+
/// @param amount Token amount.
|
|
661
|
+
function appendAmount(Writer memory writer, bytes32 asset, bytes32 meta, uint amount) internal pure {
|
|
662
|
+
appendAmount(writer, AssetAmount(asset, meta, amount));
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/// @notice Append an AMOUNT block from a struct.
|
|
666
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.Balance`.
|
|
667
|
+
/// @param value Amount fields to encode.
|
|
668
|
+
function appendAmount(Writer memory writer, AssetAmount memory value) internal pure {
|
|
669
|
+
writer.i = writeAmountBlock(writer.dst, writer.i, value);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/// @notice Append an ASSET block.
|
|
673
|
+
/// @param writer Destination writer; `i` is advanced by `Sizes.B64`.
|
|
674
|
+
/// @param asset Asset identifier.
|
|
675
|
+
/// @param meta Asset metadata slot.
|
|
676
|
+
function appendAsset(Writer memory writer, bytes32 asset, bytes32 meta) internal pure {
|
|
677
|
+
writer.i = writeAssetBlock(writer.dst, writer.i, asset, meta);
|
|
678
|
+
}
|
|
679
|
+
|
|
179
680
|
/// @notice Append a BALANCE block only if `amount > 0`; silently skips zero amounts.
|
|
180
681
|
/// @param writer Destination writer.
|
|
181
682
|
/// @param asset Asset identifier.
|
|
@@ -192,25 +693,6 @@ library Writers {
|
|
|
192
693
|
if (value.amount > 0) appendBalance(writer, value);
|
|
193
694
|
}
|
|
194
695
|
|
|
195
|
-
/// @notice Write a BOUNTY block directly into `dst` at byte offset `i`.
|
|
196
|
-
/// @param dst Destination buffer; must have at least `i + Sizes.Bounty` bytes.
|
|
197
|
-
/// @param i Write offset within `dst`.
|
|
198
|
-
/// @param amount Relayer reward amount.
|
|
199
|
-
/// @param relayer Relayer account identifier.
|
|
200
|
-
/// @return next Byte offset immediately after the written block.
|
|
201
|
-
function writeBountyBlock(bytes memory dst, uint i, uint amount, bytes32 relayer) internal pure returns (uint next) {
|
|
202
|
-
next = i + Sizes.Bounty;
|
|
203
|
-
if (next > dst.length) revert WriterOverflow();
|
|
204
|
-
uint header = toBlockHeader(Keys.Bounty, 64);
|
|
205
|
-
// Write 8-byte header then amount and relayer.
|
|
206
|
-
assembly ("memory-safe") {
|
|
207
|
-
let p := add(add(dst, 0x20), i)
|
|
208
|
-
mstore(p, header)
|
|
209
|
-
mstore(add(p, 0x08), amount)
|
|
210
|
-
mstore(add(p, 0x28), relayer)
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
696
|
/// @notice Append a BOUNTY block to the writer.
|
|
215
697
|
/// @param writer Destination writer; `i` is advanced by `Sizes.Bounty`.
|
|
216
698
|
/// @param amount Relayer reward amount.
|
|
@@ -219,26 +701,6 @@ library Writers {
|
|
|
219
701
|
writer.i = writeBountyBlock(writer.dst, writer.i, amount, relayer);
|
|
220
702
|
}
|
|
221
703
|
|
|
222
|
-
/// @notice Write a CUSTODY block directly into `dst` at byte offset `i`.
|
|
223
|
-
/// @param dst Destination buffer; must have at least `i + Sizes.Custody` bytes.
|
|
224
|
-
/// @param i Write offset within `dst`.
|
|
225
|
-
/// @param value Custody fields to encode.
|
|
226
|
-
/// @return next Byte offset immediately after the written block.
|
|
227
|
-
function writeCustodyBlock(bytes memory dst, uint i, HostAmount memory value) internal pure returns (uint next) {
|
|
228
|
-
next = i + Sizes.Custody;
|
|
229
|
-
if (next > dst.length) revert WriterOverflow();
|
|
230
|
-
uint header = toBlockHeader(Keys.Custody, 128);
|
|
231
|
-
// Write 8-byte header then four 32-byte payload words.
|
|
232
|
-
assembly ("memory-safe") {
|
|
233
|
-
let p := add(add(dst, 0x20), i)
|
|
234
|
-
mstore(p, header)
|
|
235
|
-
mstore(add(p, 0x08), mload(value))
|
|
236
|
-
mstore(add(p, 0x28), mload(add(value, 0x20)))
|
|
237
|
-
mstore(add(p, 0x48), mload(add(value, 0x40)))
|
|
238
|
-
mstore(add(p, 0x68), mload(add(value, 0x60)))
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
704
|
/// @notice Append a CUSTODY block using separate field values.
|
|
243
705
|
/// @param writer Destination writer; `i` is advanced by `Sizes.Custody`.
|
|
244
706
|
/// @param host Host node ID.
|
|
@@ -256,27 +718,6 @@ library Writers {
|
|
|
256
718
|
writer.i = writeCustodyBlock(writer.dst, writer.i, value);
|
|
257
719
|
}
|
|
258
720
|
|
|
259
|
-
/// @notice Write a TRANSACTION block directly into `dst` at byte offset `i`.
|
|
260
|
-
/// @param dst Destination buffer; must have at least `i + Sizes.Transaction` bytes.
|
|
261
|
-
/// @param i Write offset within `dst`.
|
|
262
|
-
/// @param value Transfer record fields to encode.
|
|
263
|
-
/// @return next Byte offset immediately after the written block.
|
|
264
|
-
function writeTxBlock(bytes memory dst, uint i, Tx memory value) internal pure returns (uint next) {
|
|
265
|
-
next = i + Sizes.Transaction;
|
|
266
|
-
if (next > dst.length) revert WriterOverflow();
|
|
267
|
-
uint header = toBlockHeader(Keys.Transaction, 160);
|
|
268
|
-
// Write 8-byte header then five 32-byte payload words.
|
|
269
|
-
assembly ("memory-safe") {
|
|
270
|
-
let p := add(add(dst, 0x20), i)
|
|
271
|
-
mstore(p, header)
|
|
272
|
-
mstore(add(p, 0x08), mload(value))
|
|
273
|
-
mstore(add(p, 0x28), mload(add(value, 0x20)))
|
|
274
|
-
mstore(add(p, 0x48), mload(add(value, 0x40)))
|
|
275
|
-
mstore(add(p, 0x68), mload(add(value, 0x60)))
|
|
276
|
-
mstore(add(p, 0x88), mload(add(value, 0x80)))
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
721
|
/// @notice Append a TRANSACTION block from a struct.
|
|
281
722
|
/// @param writer Destination writer; `i` is advanced by `Sizes.Transaction`.
|
|
282
723
|
/// @param value Transfer record fields to encode.
|