Skip to content

Commit 3a4e51c

Browse files
authored
Add i256 type (#42)
1 parent 586425c commit 3a4e51c

7 files changed

Lines changed: 982 additions & 24 deletions

File tree

CLAUDE.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub trait StaticEncodedLen: SolEncode {
137137
| `uint8`..`uint128` | `u8`..`u128` | yes | yes | `impl_static_type!` | |
138138
| `uint256` | `U256` (ruint) | yes | yes | `impl_static_type!` | |
139139
| `int8`..`int128` | `i8`..`i128` | yes | yes | `impl_static_type!` | Sign-extended encoding |
140-
| `int256` | `I256` | **no** | **no** | **missing** | Macro codegen references `I256` but type doesn't exist |
140+
| `int256` | `I256` | yes | yes | `impl_static_type!` | Newtype around `U256` with two's-complement signed ops |
141141
| `bool` | `bool` | yes | yes | `impl_static_type!` | |
142142
| `address` | `Address` | yes | yes | `impl_static_type!` | Wrapper around `[u8; 20]` |
143143
| `bytesN` | `[u8; N]` | yes | yes | blanket impl | SOL_NAME = `"bytesN"`, left-aligned encoding |
@@ -159,7 +159,6 @@ The `SolArrayElement` marker trait controls which types can be used as elements
159159

160160
### Known Gaps
161161

162-
- **`I256`**: The decode codegen (`decode.rs`) emits `::pvm_contract_types::I256::from_be_slice(...)` for `Int(256)`, but `I256` is never defined. Any contract using `int256` will fail to compile.
163162
- **`&[u8]`**: No trait impl for byte slices. The macro compensates with inline codegen for no-alloc `bytes` decoding.
164163

165164
### Custom Types via `#[derive(SolType)]`

crates/pvm-contract-macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ pub fn fallback(_attr: TokenStream, item: TokenStream) -> TokenStream {
646646
/// | `u32` | `uint32` | 32 bytes |
647647
/// | `u16` | `uint16` | 32 bytes |
648648
/// | `u8` | `uint8` | 32 bytes |
649+
/// | `I256` | `int256` | 32 bytes |
649650
/// | `i128` | `int128` | 32 bytes |
650651
/// | `i64` | `int64` | 32 bytes |
651652
/// | `i32` | `int32` | 32 bytes |

crates/pvm-contract-macros/src/signature/types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ impl SolType {
135135
| "::pvm_contract::Address" => Some(SolType::Address),
136136
"U256" | "ruint::aliases::U256" => Some(SolType::Uint(256)),
137137
"u256" => Some(SolType::Uint(256)),
138+
"I256" | "pvm_contract_types::I256" | "::pvm_contract_types::I256" => {
139+
Some(SolType::Int(256))
140+
}
141+
"i256" => Some(SolType::Int(256)),
138142
"u128" => Some(SolType::Uint(128)),
139143
"u64" => Some(SolType::Uint(64)),
140144
"u32" => Some(SolType::Uint(32)),
@@ -318,6 +322,14 @@ mod tests {
318322
let ty: syn::Type = syn::parse_str("i128").unwrap();
319323
let sol = SolType::from_rust_type(&ty).unwrap();
320324
assert_eq!(sol, SolType::Int(128));
325+
326+
let ty: syn::Type = syn::parse_str("I256").unwrap();
327+
let sol = SolType::from_rust_type(&ty).unwrap();
328+
assert_eq!(sol, SolType::Int(256));
329+
330+
let ty: syn::Type = syn::parse_str("pvm_contract_types::I256").unwrap();
331+
let sol = SolType::from_rust_type(&ty).unwrap();
332+
assert_eq!(sol, SolType::Int(256));
321333
}
322334

323335
#[test]

crates/pvm-contract-types/proptest-regressions/tests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
# It is recommended to check this file in to source control so that
66
# everyone who runs the test benefits from these saved cases.
77
cc b3613914836697ae21ef98e5dbf2bec0023fb909cb99621462a513a3446024c4 # shrinks to x = 0, y = 0, name = ""
8+
cc 7189bd9b310d2762ea2ecd592d0e1bb466bf7cd19ee8df563f8536c0580675a3 # shrinks to a_limbs = [0, 0, 0, 9223372036854775808], shift = 1

0 commit comments

Comments
 (0)