Skip to content

Commit

Permalink
feat: sort ABI items (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Oct 30, 2024
1 parent 07cbe85 commit 10db14e
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 260 deletions.
10 changes: 9 additions & 1 deletion crates/sema/src/ty/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl<'gcx> Gcx<'gcx> {
/// Reference: <https://docs.soliditylang.org/en/develop/abi-spec.html>
pub fn contract_abi(self, id: hir::ContractId) -> Vec<json::AbiItem<'static>> {
let mut items = Vec::<json::AbiItem<'static>>::new();

let c = self.hir.contract(id);
if let Some(ctor) = c.ctor {
let json::Function { inputs, state_mutability, .. } = self.function_abi(ctor);
Expand All @@ -47,7 +48,14 @@ impl<'gcx> Gcx<'gcx> {
_ => {}
}
}
// items.sort_by(|a, b| a.name().cmp(&b.name()));

// https://github.com/ethereum/solidity/blob/87d86bfba64d8b88537a4a85c1d71f521986b614/libsolidity/interface/ABI.cpp#L43-L47
fn cmp_key<'a>(item: &'a json::AbiItem<'_>) -> impl Ord + use<'a> {
// TODO: Use `json_type` instead of `debug_name`.
(item.debug_name(), item.name())
}
items.sort_by(|a, b| cmp_key(a).cmp(&cmp_key(b)));

items
}

Expand Down
280 changes: 140 additions & 140 deletions tests/ui/abi/basic.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,77 @@
"stateMutability": "nonpayable"
},
{
"type": "fallback",
"stateMutability": "nonpayable"
"type": "error",
"name": "Er",
"inputs": [
{
"name": "a",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "c",
"type": "bool[]",
"internalType": "bool[]"
},
{
"name": "x",
"type": "string",
"internalType": "string"
},
{
"name": "u",
"type": "uint256",
"internalType": "C.UDVT"
}
]
},
{
"type": "receive",
"stateMutability": "payable"
"type": "event",
"name": "Ev",
"inputs": [
{
"name": "a",
"type": "uint256",
"indexed": false,
"internalType": "uint256"
},
{
"name": "b",
"type": "uint256",
"indexed": true,
"internalType": "uint256"
},
{
"name": "c",
"type": "bool[]",
"indexed": false,
"internalType": "bool[]"
},
{
"name": "x",
"type": "string",
"indexed": false,
"internalType": "string"
},
{
"name": "u",
"type": "uint256",
"indexed": false,
"internalType": "C.UDVT"
},
{
"name": "u2",
"type": "uint256",
"indexed": true,
"internalType": "C.UDVT"
}
],
"anonymous": false
},
{
"type": "fallback",
"stateMutability": "nonpayable"
},
{
"type": "function",
Expand Down Expand Up @@ -332,73 +397,8 @@
"stateMutability": "nonpayable"
},
{
"type": "event",
"name": "Ev",
"inputs": [
{
"name": "a",
"type": "uint256",
"indexed": false,
"internalType": "uint256"
},
{
"name": "b",
"type": "uint256",
"indexed": true,
"internalType": "uint256"
},
{
"name": "c",
"type": "bool[]",
"indexed": false,
"internalType": "bool[]"
},
{
"name": "x",
"type": "string",
"indexed": false,
"internalType": "string"
},
{
"name": "u",
"type": "uint256",
"indexed": false,
"internalType": "C.UDVT"
},
{
"name": "u2",
"type": "uint256",
"indexed": true,
"internalType": "C.UDVT"
}
],
"anonymous": false
},
{
"type": "error",
"name": "Er",
"inputs": [
{
"name": "a",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "c",
"type": "bool[]",
"internalType": "bool[]"
},
{
"name": "x",
"type": "string",
"internalType": "string"
},
{
"name": "u",
"type": "uint256",
"internalType": "C.UDVT"
}
]
"type": "receive",
"stateMutability": "payable"
}
],
"hashes": {
Expand Down Expand Up @@ -484,6 +484,75 @@
],
"stateMutability": "payable"
},
{
"type": "error",
"name": "Er",
"inputs": [
{
"name": "a",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "c",
"type": "bool[]",
"internalType": "bool[]"
},
{
"name": "x",
"type": "string",
"internalType": "string"
},
{
"name": "u",
"type": "uint256",
"internalType": "C.UDVT"
}
]
},
{
"type": "event",
"name": "Ev",
"inputs": [
{
"name": "a",
"type": "uint256",
"indexed": false,
"internalType": "uint256"
},
{
"name": "b",
"type": "uint256",
"indexed": true,
"internalType": "uint256"
},
{
"name": "c",
"type": "bool[]",
"indexed": false,
"internalType": "bool[]"
},
{
"name": "x",
"type": "string",
"indexed": false,
"internalType": "string"
},
{
"name": "u",
"type": "uint256",
"indexed": false,
"internalType": "C.UDVT"
},
{
"name": "u2",
"type": "uint256",
"indexed": true,
"internalType": "C.UDVT"
}
],
"anonymous": false
},
{
"type": "function",
"name": "f1",
Expand Down Expand Up @@ -799,75 +868,6 @@
}
],
"stateMutability": "nonpayable"
},
{
"type": "event",
"name": "Ev",
"inputs": [
{
"name": "a",
"type": "uint256",
"indexed": false,
"internalType": "uint256"
},
{
"name": "b",
"type": "uint256",
"indexed": true,
"internalType": "uint256"
},
{
"name": "c",
"type": "bool[]",
"indexed": false,
"internalType": "bool[]"
},
{
"name": "x",
"type": "string",
"indexed": false,
"internalType": "string"
},
{
"name": "u",
"type": "uint256",
"indexed": false,
"internalType": "C.UDVT"
},
{
"name": "u2",
"type": "uint256",
"indexed": true,
"internalType": "C.UDVT"
}
],
"anonymous": false
},
{
"type": "error",
"name": "Er",
"inputs": [
{
"name": "a",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "c",
"type": "bool[]",
"internalType": "bool[]"
},
{
"name": "x",
"type": "string",
"internalType": "string"
},
{
"name": "u",
"type": "uint256",
"internalType": "C.UDVT"
}
]
}
],
"hashes": {
Expand Down
Loading

0 comments on commit 10db14e

Please sign in to comment.