Solidity
A Solidity encoding and decoding framework for Rust.
// Basic usage using the built in `Encode` derive macro.
// (Requires the `derive` feature.)
// Basic usage using serde. (Requires the `serde` feature).
// Note: Serde only supports a subset of the types that Solidity supports.
// If you need to support more types you'll have to use the `Encode` derive
// macro, or use the `solid::Builder` manually.
// Use the `#[solid(constructor)]` attribute to declare a struct as a constructor.
// This is important because constructors do not have the function name prefix,
// unlike all other functions. Usually the struct name is used as the function
// name. To rename the function use the `#[solid(name = "<function_name>")]`
// where `<function_name>` is the name of your function.
// ie. `#[solid(name = "transfer")]`.
// Basic usage with the built in `Decode` derive macro.
// (Requires the `derive` feature.)
// Note: `Uint256` and all other `Int`/`Uint` types are simple
// wrappers around `[u8; 32]`. The point of them is to support all
// `int`/`uint` Solidity types.
// Basic usage with serde's `Deserialize` derive macro.
// (Requires the `serde` feature.)
// Note: Serde only supports a subset of the types that Solidity supports.
// If you need to support more types you'll have to use the `Encode` derive
// macro, or use the `solid::Builder` manually.
// Support for composite types and `Vec`
// If you want to manually build the contract you can use the provided `Builder`
let function = new
.name
.push
.push
.push
.build;
num_bigint Support
If you'd like support for num_bigint
enable the bigint
feature.
// Note: BigInt is variable sized and encodes to `int256`.
// To encode to `uint256` use the `BigUint` struct.
// Also, BigInt supports numbers larger than the max value a uint256 can store, so the value
// will be truncated to 32 bytes before it's encoded.
ethereum_types Support
If you'd like support for ethereum_types
enable the ethereum_types
feature.
// Support for Address, U256, U128 from `ethereum_types` crate.
Install
# Cargo.toml
# Default features which includes `derive`, and `serde`
= "0.1.4"
# num_bigint support
= { = "0.1.4", = false, = [ "derive", "serde", "bigint" ] }
# ethereum_types support
= { = "0.1.4", = false, = [ "derive", "serde", "ethereum_types" ] }
Using cargo-edit
Features
- derive: Add support for the
Encode
andDecode
derive macros. (Recommended) - derse: Add support for
serde
sSerialize
andDeserialize
derive macros, andto_bytes
function. - bigint: Add suport for
num_bigint
crate. - ethereum_types: Add support for
ethereum_types
crate. - nightly: Experimental const generic support.
cargo-solid Subcommand
cargo-solid is a cargo subcommand that allows you to generate the encoding functions and decodable struct definitions for named return types of associated Solidity methods. example
Subcommand Install
The following command generates the solidity abi for a contract.
Then run the following command to generate the rust definition.
The name of the output file will the be same name as the input file with the extension set to .rs
, and will be
located in the local src
directory. You can freely move the file if you would like, but either way you will
still need to add the file as a module:
As of version cargo-solidv0.1.4 you can also generate the files into a directory such as src/generated
, and
there is also support for generating code using nightly definitions of BytesFix
and Int
.