Skip to content

Commit 3c11e1d

Browse files
committed
move UDVT to types/TypedSlot.sol
1 parent f19086c commit 3c11e1d

File tree

5 files changed

+121
-113
lines changed

5 files changed

+121
-113
lines changed

contracts/utils/TransientSlot.sol

Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
pragma solidity ^0.8.24;
55

6+
import {TypedSlot} from "./types/TypedSlot.sol";
7+
68
/**
79
* @dev Library for reading and writing primitive types to specific storage slots. This is a variant of {StorageSlot}
810
* that supports transient storage.
@@ -12,6 +14,7 @@ pragma solidity ^0.8.24;
1214
* Example usage:
1315
* ```solidity
1416
* contract ReentrancyGuard {
17+
* using TypedSlot for bytes32;
1518
* using TransientSlot for *;
1619
*
1720
* bytes32 internal constant _REENTRANCY_SLOT = 0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
@@ -27,22 +30,10 @@ pragma solidity ^0.8.24;
2730
* ```
2831
*/
2932
library TransientSlot {
30-
/**
31-
* @dev UDVT that represent a slot holding a address.
32-
*/
33-
type AddressSlotType is bytes32;
34-
35-
/**
36-
* @dev Cast an arbitrary slot to a AddressSlotType.
37-
*/
38-
function asAddressSlot(bytes32 slot) internal pure returns (AddressSlotType) {
39-
return AddressSlotType.wrap(slot);
40-
}
41-
4233
/**
4334
* @dev Load the value held at location `slot` in transient storage.
4435
*/
45-
function tload(AddressSlotType slot) internal view returns (address value) {
36+
function tload(TypedSlot.AddressSlotType slot) internal view returns (address value) {
4637
/// @solidity memory-safe-assembly
4738
assembly {
4839
value := tload(slot)
@@ -52,29 +43,17 @@ library TransientSlot {
5243
/**
5344
* @dev Store `value` at location `slot` in transient storage.
5445
*/
55-
function tstore(AddressSlotType slot, address value) internal {
46+
function tstore(TypedSlot.AddressSlotType slot, address value) internal {
5647
/// @solidity memory-safe-assembly
5748
assembly {
5849
tstore(slot, value)
5950
}
6051
}
6152

62-
/**
63-
* @dev UDVT that represent a slot holding a bool.
64-
*/
65-
type BooleanSlotType is bytes32;
66-
67-
/**
68-
* @dev Cast an arbitrary slot to a BooleanSlotType.
69-
*/
70-
function asBooleanSlot(bytes32 slot) internal pure returns (BooleanSlotType) {
71-
return BooleanSlotType.wrap(slot);
72-
}
73-
7453
/**
7554
* @dev Load the value held at location `slot` in transient storage.
7655
*/
77-
function tload(BooleanSlotType slot) internal view returns (bool value) {
56+
function tload(TypedSlot.BooleanSlotType slot) internal view returns (bool value) {
7857
/// @solidity memory-safe-assembly
7958
assembly {
8059
value := tload(slot)
@@ -84,29 +63,17 @@ library TransientSlot {
8463
/**
8564
* @dev Store `value` at location `slot` in transient storage.
8665
*/
87-
function tstore(BooleanSlotType slot, bool value) internal {
66+
function tstore(TypedSlot.BooleanSlotType slot, bool value) internal {
8867
/// @solidity memory-safe-assembly
8968
assembly {
9069
tstore(slot, value)
9170
}
9271
}
9372

94-
/**
95-
* @dev UDVT that represent a slot holding a bytes32.
96-
*/
97-
type Bytes32SlotType is bytes32;
98-
99-
/**
100-
* @dev Cast an arbitrary slot to a Bytes32SlotType.
101-
*/
102-
function asBytes32Slot(bytes32 slot) internal pure returns (Bytes32SlotType) {
103-
return Bytes32SlotType.wrap(slot);
104-
}
105-
10673
/**
10774
* @dev Load the value held at location `slot` in transient storage.
10875
*/
109-
function tload(Bytes32SlotType slot) internal view returns (bytes32 value) {
76+
function tload(TypedSlot.Bytes32SlotType slot) internal view returns (bytes32 value) {
11077
/// @solidity memory-safe-assembly
11178
assembly {
11279
value := tload(slot)
@@ -116,29 +83,17 @@ library TransientSlot {
11683
/**
11784
* @dev Store `value` at location `slot` in transient storage.
11885
*/
119-
function tstore(Bytes32SlotType slot, bytes32 value) internal {
86+
function tstore(TypedSlot.Bytes32SlotType slot, bytes32 value) internal {
12087
/// @solidity memory-safe-assembly
12188
assembly {
12289
tstore(slot, value)
12390
}
12491
}
12592

126-
/**
127-
* @dev UDVT that represent a slot holding a uint256.
128-
*/
129-
type Uint256SlotType is bytes32;
130-
131-
/**
132-
* @dev Cast an arbitrary slot to a Uint256SlotType.
133-
*/
134-
function asUint256Slot(bytes32 slot) internal pure returns (Uint256SlotType) {
135-
return Uint256SlotType.wrap(slot);
136-
}
137-
13893
/**
13994
* @dev Load the value held at location `slot` in transient storage.
14095
*/
141-
function tload(Uint256SlotType slot) internal view returns (uint256 value) {
96+
function tload(TypedSlot.Uint256SlotType slot) internal view returns (uint256 value) {
14297
/// @solidity memory-safe-assembly
14398
assembly {
14499
value := tload(slot)
@@ -148,29 +103,17 @@ library TransientSlot {
148103
/**
149104
* @dev Store `value` at location `slot` in transient storage.
150105
*/
151-
function tstore(Uint256SlotType slot, uint256 value) internal {
106+
function tstore(TypedSlot.Uint256SlotType slot, uint256 value) internal {
152107
/// @solidity memory-safe-assembly
153108
assembly {
154109
tstore(slot, value)
155110
}
156111
}
157112

158-
/**
159-
* @dev UDVT that represent a slot holding a int256.
160-
*/
161-
type Int256SlotType is bytes32;
162-
163-
/**
164-
* @dev Cast an arbitrary slot to a Int256SlotType.
165-
*/
166-
function asInt256Slot(bytes32 slot) internal pure returns (Int256SlotType) {
167-
return Int256SlotType.wrap(slot);
168-
}
169-
170113
/**
171114
* @dev Load the value held at location `slot` in transient storage.
172115
*/
173-
function tload(Int256SlotType slot) internal view returns (int256 value) {
116+
function tload(TypedSlot.Int256SlotType slot) internal view returns (int256 value) {
174117
/// @solidity memory-safe-assembly
175118
assembly {
176119
value := tload(slot)
@@ -180,7 +123,7 @@ library TransientSlot {
180123
/**
181124
* @dev Store `value` at location `slot` in transient storage.
182125
*/
183-
function tstore(Int256SlotType slot, int256 value) internal {
126+
function tstore(TypedSlot.Int256SlotType slot, int256 value) internal {
184127
/// @solidity memory-safe-assembly
185128
assembly {
186129
tstore(slot, value)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: MIT
2+
// This file was procedurally generated from scripts/generate/templates/TypedSlot.js.
3+
4+
pragma solidity ^0.8.20;
5+
6+
/**
7+
* @dev Library for representing bytes32 slot as typed objects.
8+
*/
9+
library TypedSlot {
10+
/**
11+
* @dev UDVT that represent a slot holding a address.
12+
*/
13+
type AddressSlotType is bytes32;
14+
15+
/**
16+
* @dev Cast an arbitrary slot to a AddressSlotType.
17+
*/
18+
function asAddressSlot(bytes32 slot) internal pure returns (AddressSlotType) {
19+
return AddressSlotType.wrap(slot);
20+
}
21+
22+
/**
23+
* @dev UDVT that represent a slot holding a bool.
24+
*/
25+
type BooleanSlotType is bytes32;
26+
27+
/**
28+
* @dev Cast an arbitrary slot to a BooleanSlotType.
29+
*/
30+
function asBooleanSlot(bytes32 slot) internal pure returns (BooleanSlotType) {
31+
return BooleanSlotType.wrap(slot);
32+
}
33+
34+
/**
35+
* @dev UDVT that represent a slot holding a bytes32.
36+
*/
37+
type Bytes32SlotType is bytes32;
38+
39+
/**
40+
* @dev Cast an arbitrary slot to a Bytes32SlotType.
41+
*/
42+
function asBytes32Slot(bytes32 slot) internal pure returns (Bytes32SlotType) {
43+
return Bytes32SlotType.wrap(slot);
44+
}
45+
46+
/**
47+
* @dev UDVT that represent a slot holding a uint256.
48+
*/
49+
type Uint256SlotType is bytes32;
50+
51+
/**
52+
* @dev Cast an arbitrary slot to a Uint256SlotType.
53+
*/
54+
function asUint256Slot(bytes32 slot) internal pure returns (Uint256SlotType) {
55+
return Uint256SlotType.wrap(slot);
56+
}
57+
58+
/**
59+
* @dev UDVT that represent a slot holding a int256.
60+
*/
61+
type Int256SlotType is bytes32;
62+
63+
/**
64+
* @dev Cast an arbitrary slot to a Int256SlotType.
65+
*/
66+
function asInt256Slot(bytes32 slot) internal pure returns (Int256SlotType) {
67+
return Int256SlotType.wrap(slot);
68+
}
69+
}

scripts/generate/run.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ for (const [file, template] of Object.entries({
3636
'utils/structs/EnumerableSet.sol': './templates/EnumerableSet.js',
3737
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
3838
'utils/structs/Checkpoints.sol': './templates/Checkpoints.js',
39+
'utils/types/TypedSlot.sol': './templates/TypedSlot.js',
3940
'utils/SlotDerivation.sol': './templates/SlotDerivation.js',
4041
'utils/StorageSlot.sol': './templates/StorageSlot.js',
4142
'utils/TransientSlot.sol': './templates/TransientSlot.js',

scripts/generate/templates/TransientSlot.js

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const { TYPES } = require('./Slot.opts');
44
const header = `\
55
pragma solidity ^0.8.24;
66
7+
import {TypedSlot} from "./types/TypedSlot.sol";
8+
79
/**
810
* @dev Library for reading and writing primitive types to specific storage slots. This is a variant of {StorageSlot}
911
* that supports transient storage.
@@ -13,6 +15,7 @@ pragma solidity ^0.8.24;
1315
* Example usage:
1416
* \`\`\`solidity
1517
* contract ReentrancyGuard {
18+
* using TypedSlot for bytes32;
1619
* using TransientSlot for *;
1720
*
1821
* bytes32 internal constant _REENTRANCY_SLOT = 0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
@@ -29,47 +32,11 @@ pragma solidity ^0.8.24;
2932
*/
3033
`;
3134

32-
const udvt = ({ type, name }) => `\
33-
/**
34-
* @dev UDVT that represent a slot holding a ${type}.
35-
*/
36-
type ${name}SlotType is bytes32;
37-
38-
/**
39-
* @dev Cast an arbitrary slot to a ${name}SlotType.
40-
*/
41-
function as${name}Slot(bytes32 slot) internal pure returns (${name}SlotType) {
42-
return ${name}SlotType.wrap(slot);
43-
}
44-
`;
45-
46-
// const storage = ({ type, name }) => `\
47-
// /**
48-
// * @dev Load the value held at location \`slot\` in (normal) storage.
49-
// */
50-
// function sload(${name}SlotType slot) internal view returns (${type} value) {
51-
// /// @solidity memory-safe-assembly
52-
// assembly {
53-
// value := sload(slot)
54-
// }
55-
// }
56-
//
57-
// /**
58-
// * @dev Store \`value\` at location \`slot\` in (normal) storage.
59-
// */
60-
// function sstore(${name}SlotType slot, ${type} value) internal {
61-
// /// @solidity memory-safe-assembly
62-
// assembly {
63-
// sstore(slot, value)
64-
// }
65-
// }
66-
// `;
67-
6835
const transient = ({ type, name }) => `\
6936
/**
7037
* @dev Load the value held at location \`slot\` in transient storage.
7138
*/
72-
function tload(${name}SlotType slot) internal view returns (${type} value) {
39+
function tload(TypedSlot.${name}SlotType slot) internal view returns (${type} value) {
7340
/// @solidity memory-safe-assembly
7441
assembly {
7542
value := tload(slot)
@@ -79,7 +46,7 @@ function tload(${name}SlotType slot) internal view returns (${type} value) {
7946
/**
8047
* @dev Store \`value\` at location \`slot\` in transient storage.
8148
*/
82-
function tstore(${name}SlotType slot, ${type} value) internal {
49+
function tstore(TypedSlot.${name}SlotType slot, ${type} value) internal {
8350
/// @solidity memory-safe-assembly
8451
assembly {
8552
tstore(slot, value)
@@ -91,10 +58,6 @@ function tstore(${name}SlotType slot, ${type} value) internal {
9158
module.exports = format(
9259
header.trimEnd(),
9360
'library TransientSlot {',
94-
TYPES.filter(type => type.isValueType).flatMap(type => [
95-
udvt(type),
96-
// storage(type), // disabled for now
97-
transient(type),
98-
]),
61+
TYPES.filter(type => type.isValueType).map(type => transient(type)),
9962
'}',
10063
);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const format = require('../format-lines');
2+
const { TYPES } = require('./Slot.opts');
3+
4+
const header = `\
5+
pragma solidity ^0.8.20;
6+
7+
/**
8+
* @dev Library for representing bytes32 slot as typed objects.
9+
*/
10+
`;
11+
12+
const udvt = ({ type, name }) => `\
13+
/**
14+
* @dev UDVT that represent a slot holding a ${type}.
15+
*/
16+
type ${name}SlotType is bytes32;
17+
18+
/**
19+
* @dev Cast an arbitrary slot to a ${name}SlotType.
20+
*/
21+
function as${name}Slot(bytes32 slot) internal pure returns (${name}SlotType) {
22+
return ${name}SlotType.wrap(slot);
23+
}
24+
`;
25+
26+
// GENERATE
27+
module.exports = format(
28+
header.trimEnd(),
29+
'library TypedSlot {',
30+
TYPES.filter(type => type.isValueType).map(type => udvt(type)),
31+
'}',
32+
);

0 commit comments

Comments
 (0)