Skip to content

Abstractions for mapping arbitrary Rust types to Solidity ABI compatible types#2441

Merged
davidsemakula merged 43 commits intomasterfrom
semakula/sol-codec
Apr 1, 2025
Merged

Abstractions for mapping arbitrary Rust types to Solidity ABI compatible types#2441
davidsemakula merged 43 commits intomasterfrom
semakula/sol-codec

Conversation

@davidsemakula
Copy link
Collaborator

@davidsemakula davidsemakula commented Mar 21, 2025

Summary

Closes #2429
Closes #2428

  • [y] y/n | Does it introduce breaking changes?
  • [n] y/n | Is it dependent on a specific version of cargo-contract or pallet-revive?

Description

  • Adds SolEncode and SolDecode traits to ink_primitives as replacements for alloy_sol_types::SolValue in public interfaces (and analogs to scale::Encode and scale::Decode but for Solidity ABI encoding/decoding). SolEncode and SolDecode essentially describe how to map an arbitrary Rust/ink! type to a Solidity ABI type equivalent (i.e. a type that implements SolTypeEncode and/or SolTypeDecode) for Solidity ABI encoding/decoding
  • Adds SolTypeEncode and SolTypeDecode sealed traits which describe a Rust/ink! equivalent of a Solidity ABI type, and implement logic for Solidity ABI encoding/decoding. These traits are implemented for all Rust primitive types that have an equivalent Solidity ABI type, as well as Address / H160 and U256 ink! primitives. However SolTypeEncode is additionally implemented for references (and a few smart pointers) of types that implement SolTypeEncode (i.e. &T, &mut T, Box<T> and Cow<T> for T: SolTypeEncode). The rustdoc for both traits includes a table showing the mapping of Rust/ink! primitives to equivalent Solidity ABI types. All SolTypeEncode and SolTypeDecode types also implement SolEncode and SolDecode
  • Adds SolBytes<T> type as a newtype wrapper that encodes/decodes u8 sequences/collections as their equivalent Solidity bytes representations. T has a SolBytesType bound, and SolBytesType is a sealed trait that's only implemented for u8, [u8; N] where 1 <= N <= 32 and Vec<u8>
  • Adds sanity checks to ensure our SolTypeEncode, SolTypeDecode, SolEncode and SolDecode implementations match alloy_sol_types::SolValue equivalents
  • Removes all usages of alloy_sol_types outside ink_primitives and instead uses the new Solidity compatibility abstractions above defined in ink_primitives
  • Implements SolEncode and SolDecode for AccountId, Hash, H160 and H256 types from ink! primitives
  • Converts Address into a type alias for H160, and replaces H160 with Address in the public interfaces where H160 semantically represents an address

Related/future work - to be done in a follow up PR(s)

See also

Checklist before requesting a review

  • I have added an entry to CHANGELOG.md
  • I have commented on my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • Any dependent changes have been merged and published in downstream modules
@davidsemakula davidsemakula force-pushed the semakula/sol-codec branch 11 times, most recently from 3c83ded to 0424020 Compare March 28, 2025 08:38
@davidsemakula davidsemakula marked this pull request as ready for review March 29, 2025 10:24
@davidsemakula davidsemakula requested review from peterwht and removed request for ascjones March 29, 2025 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants