Skip to content

Commit 1ff6a56

Browse files
committed
Improve JsonAbi printer indentation handling for globals and nested items
1 parent b52de13 commit 1ff6a56

File tree

8 files changed

+235
-225
lines changed

8 files changed

+235
-225
lines changed

crates/json-abi/src/to_sol.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ pub(crate) struct SolPrinter<'a> {
9292

9393
/// Configuration.
9494
config: ToSolConfig,
95+
96+
/// Current indentation level.
97+
indent_level: usize,
9598
}
9699

97100
impl Deref for SolPrinter<'_> {
@@ -112,15 +115,17 @@ impl DerefMut for SolPrinter<'_> {
112115

113116
impl<'a> SolPrinter<'a> {
114117
pub(crate) fn new(s: &'a mut String, name: &'a str, config: ToSolConfig) -> Self {
115-
Self { s, name, print_param_location: false, config }
118+
Self { s, name, print_param_location: false, config, indent_level: 0 }
116119
}
117120

118121
pub(crate) fn print(&mut self, abi: &'a JsonAbi) {
119122
abi.to_sol_root(self);
120123
}
121124

122125
fn indent(&mut self) {
123-
self.push_str(" ");
126+
for _ in 0..self.indent_level {
127+
self.push_str(" ");
128+
}
124129
}
125130

126131
/// Normalizes `s` as a Rust identifier and pushes it to the buffer.
@@ -177,11 +182,13 @@ impl JsonAbi {
177182
out.push_str(name);
178183
out.push_str(" {\n");
179184
let prev = core::mem::replace(&mut out.name, name);
185+
out.indent_level += 1;
180186
for it in its {
181187
out.indent();
182188
it.to_sol(out);
183189
out.push('\n');
184190
}
191+
out.indent_level -= 1;
185192
out.name = prev;
186193
out.push_str("}\n\n");
187194
}
@@ -195,6 +202,7 @@ impl JsonAbi {
195202
out.push('{');
196203
out.push('\n');
197204

205+
out.indent_level += 1;
198206
if one_contract {
199207
for (name, its) in &its.other {
200208
if its.is_empty() {
@@ -220,6 +228,7 @@ impl JsonAbi {
220228
out.pop(); // trailing newline
221229

222230
out.push('}');
231+
out.indent_level -= 1;
223232
if !its.globals.is_empty() {
224233
out.push('\n');
225234
fmt!(its.globals);
@@ -399,12 +408,13 @@ impl ToSol for It<'_> {
399408
out.push_str("struct ");
400409
out.push_ident(self.name);
401410
out.push_str(" {\n");
411+
out.indent_level += 1;
402412
for component in components {
403-
out.indent();
404413
out.indent();
405414
component.to_sol(out);
406415
out.push_str(";\n");
407416
}
417+
out.indent_level -= 1;
408418
out.indent();
409419
out.push('}');
410420
}

crates/json-abi/tests/abi/BlurExchange.sol

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,4 @@
11
interface BlurExchange {
2-
type Side is uint8;
3-
type SignatureVersion is uint8;
4-
struct Execution {
5-
Input sell;
6-
Input buy;
7-
}
8-
struct Fee {
9-
uint16 rate;
10-
address payable recipient;
11-
}
12-
struct Input {
13-
Order order;
14-
uint8 v;
15-
bytes32 r;
16-
bytes32 s;
17-
bytes extraSignature;
18-
SignatureVersion signatureVersion;
19-
uint256 blockNumber;
20-
}
21-
struct Order {
22-
address trader;
23-
Side side;
24-
address matchingPolicy;
25-
address collection;
26-
uint256 tokenId;
27-
uint256 amount;
28-
address paymentToken;
29-
uint256 price;
30-
uint256 listingTime;
31-
uint256 expirationTime;
32-
Fee[] fees;
33-
uint256 salt;
34-
bytes extraParams;
35-
}
36-
372
event AdminChanged(address previousAdmin, address newAdmin);
383
event BeaconUpgraded(address indexed beacon);
394
event Closed();
@@ -95,4 +60,39 @@ interface BlurExchange {
9560
function transferOwnership(address newOwner) external;
9661
function upgradeTo(address newImplementation) external;
9762
function upgradeToAndCall(address newImplementation, bytes memory data) external payable;
98-
}
63+
}
64+
type Side is uint8;
65+
type SignatureVersion is uint8;
66+
struct Execution {
67+
Input sell;
68+
Input buy;
69+
}
70+
struct Fee {
71+
uint16 rate;
72+
address payable recipient;
73+
}
74+
struct Input {
75+
Order order;
76+
uint8 v;
77+
bytes32 r;
78+
bytes32 s;
79+
bytes extraSignature;
80+
SignatureVersion signatureVersion;
81+
uint256 blockNumber;
82+
}
83+
struct Order {
84+
address trader;
85+
Side side;
86+
address matchingPolicy;
87+
address collection;
88+
uint256 tokenId;
89+
uint256 amount;
90+
address paymentToken;
91+
uint256 price;
92+
uint256 listingTime;
93+
uint256 expirationTime;
94+
Fee[] fees;
95+
uint256 salt;
96+
bytes extraParams;
97+
}
98+

crates/json-abi/tests/abi/Bootstrap.sol

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ library ModuleManager {
66
}
77

88
interface Bootstrap {
9-
type CallType is bytes1;
10-
struct BootstrapConfig {
11-
address module;
12-
bytes data;
13-
}
14-
159
error AccountAccessUnauthorized();
1610
error CannotRemoveLastValidator();
1711
error HookAlreadyInstalled(address currentHook);
@@ -34,4 +28,10 @@ interface Bootstrap {
3428
function getValidatorsPaginated(address cursor, uint256 size) external view returns (address[] memory array, address next);
3529
function initMSA(BootstrapConfig[] memory _valdiators, BootstrapConfig[] memory _executors, BootstrapConfig memory _hook, BootstrapConfig[] memory _fallbacks) external;
3630
function singleInitMSA(address validator, bytes memory data) external;
37-
}
31+
}
32+
type CallType is bytes1;
33+
struct BootstrapConfig {
34+
address module;
35+
bytes data;
36+
}
37+

crates/json-abi/tests/abi/DelegationManager.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ library ISignatureUtils {
2828
}
2929

3030
interface DelegationManager {
31-
type DelegatedShares is uint256;
32-
3331
error ActivelyDelegated();
3432
error AllocationDelaySet();
3533
error CallerCannotUndelegate();
@@ -128,4 +126,6 @@ interface DelegationManager {
128126
function undelegate(address staker) external returns (bytes32[] memory withdrawalRoots);
129127
function unpause(uint256 newPausedStatus) external;
130128
function updateOperatorMetadataURI(string memory metadataURI) external;
131-
}
129+
}
130+
type DelegatedShares is uint256;
131+

crates/json-abi/tests/abi/Fastlane.sol

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,4 @@
11
interface Fastlane {
2-
type statusType is uint8;
3-
struct Bid {
4-
address validatorAddress;
5-
address opportunityAddress;
6-
address searcherContractAddress;
7-
address searcherPayableAddress;
8-
uint256 bidAmount;
9-
}
10-
struct Status {
11-
uint128 activeAtAuction;
12-
uint128 inactiveAtAuction;
13-
statusType kind;
14-
}
15-
struct ValidatorBalanceCheckpoint {
16-
uint256 pendingBalanceAtlastBid;
17-
uint256 outstandingBalance;
18-
uint128 lastWithdrawnAuction;
19-
uint128 lastBidReceivedAuction;
20-
}
21-
struct ValidatorPreferences {
22-
uint256 minAutoshipAmount;
23-
address validatorPayableAddress;
24-
}
25-
262
event AuctionEnded(uint128 indexed auction_number);
273
event AuctionStarted(uint128 indexed auction_number);
284
event AuctionStarterSet(address indexed starter);
@@ -94,4 +70,28 @@ interface Fastlane {
9470
function transferOwnership(address newOwner) external;
9571
function withdrawStuckERC20(address _tokenAddress) external;
9672
function withdrawStuckNativeToken(uint256 amount) external;
97-
}
73+
}
74+
type statusType is uint8;
75+
struct Bid {
76+
address validatorAddress;
77+
address opportunityAddress;
78+
address searcherContractAddress;
79+
address searcherPayableAddress;
80+
uint256 bidAmount;
81+
}
82+
struct Status {
83+
uint128 activeAtAuction;
84+
uint128 inactiveAtAuction;
85+
statusType kind;
86+
}
87+
struct ValidatorBalanceCheckpoint {
88+
uint256 pendingBalanceAtlastBid;
89+
uint256 outstandingBalance;
90+
uint128 lastWithdrawnAuction;
91+
uint128 lastBidReceivedAuction;
92+
}
93+
struct ValidatorPreferences {
94+
uint256 minAutoshipAmount;
95+
address validatorPayableAddress;
96+
}
97+

0 commit comments

Comments
 (0)