https://github.com/lit-protocol/lit-precompiles
https://github.com/lit-protocol/lit-precompiles
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/lit-protocol/lit-precompiles
- Owner: LIT-Protocol
- Created: 2024-06-06T21:09:24.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-03T21:21:57.000Z (over 1 year ago)
- Last Synced: 2025-03-03T21:34:11.289Z (over 1 year ago)
- Language: Rust
- Size: 350 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: licenses/Apache-2.0
Awesome Lists containing this project
README
# Lit Precompiles
Precompiles used by the Lit Protocol network are implemented as Arbitrum Stylus contracts.
Build using `build.sh`, and then you can deploy using `cargo stylus deploy`. You may have to run `cargo clean` first, to remove any old builds.
## Repo Structure
- `core`: Core precompiles for both P256 and K256.
- `p256`: P256 precompiles.
- `k256`: K256 precompiles.
Note: this is not a proper Rust workspace, but it is a monorepo. You must add each crate to your IDE workspace manually to have rust-analyzer work. You must CD into each crate to build or deploy it.
## Quick Start
Install [Rust](https://www.rust-lang.org/tools/install), and then install the Stylus CLI tool with Cargo
```bash
cargo install --force cargo-stylus cargo-stylus-check
```
Add the `wasm32-unknown-unknown` build target to your Rust compiler:
```
rustup target add wasm32-unknown-unknown
```
You should now have it available as a Cargo subcommand:
```bash
cargo stylus --help
```
### Checking Stylus Contracts
From each of the Stylus project directories in `src`, run `cargo stylus check`.
### Arbitrum Testnet Information
All Arbitrum testnet information, including faucets and RPC endpoints can be found [here](https://docs.arbitrum.io/stylus/reference/testnet-information).
### ABI Export
You can export the Solidity ABI for your program by using the `cargo stylus` tool as follows:
```bash
cargo stylus export-abi
```
which outputs:
```js
/**
* This file was automatically generated by Stylus and represents a Rust program.
* For more information, please see [The Stylus SDK](https://github.com/OffchainLabs/stylus-sdk-rs).
*/
interface Counter {
function setNumber(uint256 new_number) external;
function increment() external;
}
```
Exporting ABIs uses a feature that is enabled by default in your Cargo.toml:
```toml
[features]
export-abi = ["stylus-sdk/export-abi"]
```
## Deploying
You can use the `cargo stylus` command to also deploy your program to the Stylus testnet. We can use the tool to first check
our program compiles to valid WASM for Stylus and will succeed a deployment onchain without transacting. By default, this will use the Stylus testnet public RPC endpoint. See here for [Stylus testnet information](https://docs.arbitrum.io/stylus/reference/testnet-information)
```bash
cargo stylus check
```
If successful, you should see:
```bash
Finished release [optimized] target(s) in 1.88s
Reading WASM file at stylus-hello-world/target/wasm32-unknown-unknown/release/stylus-hello-world.wasm
Compressed WASM size: 8.9 KB
Program succeeded Stylus onchain activation checks with Stylus version: 1
```
Next, we can estimate the gas costs to deploy and activate our program before we send our transaction. Check out the [cargo-stylus](https://github.com/OffchainLabs/cargo-stylus) README to see the different wallet options for this step:
```bash
cargo stylus deploy \
--private-key-path= \
--estimate-gas-only
```
You will then see the estimated gas cost for deploying before transacting:
```bash
Deploying program to address e43a32b54e48c7ec0d3d9ed2d628783c23d65020
Estimated gas for deployment: 1874876
```
The above only estimates gas for the deployment tx by default. To estimate gas for activation, first deploy your program using `--mode=deploy-only`, and then run `cargo stylus deploy` with the `--estimate-gas-only` flag, `--mode=activate-only`, and specify `--activate-program-address`.
Here's how to deploy:
```bash
cargo stylus deploy \
--private-key-path=
```
The CLI will send 2 transactions to deploy and activate your program onchain.
```bash
Compressed WASM size: 8.9 KB
Deploying program to address 0x457b1ba688e9854bdbed2f473f7510c476a3da09
Estimated gas: 1973450
Submitting tx...
Confirmed tx 0x42db…7311, gas used 1973450
Activating program at address 0x457b1ba688e9854bdbed2f473f7510c476a3da09
Estimated gas: 14044638
Submitting tx...
Confirmed tx 0x0bdb…3307, gas used 14044638
```
Once both steps are successful, you can interact with your program as you would with any Ethereum smart contract.
## Calling Your Program
This template includes an example of how to call and transact with your program in Rust using [ethers-rs](https://github.com/gakonst/ethers-rs) under the `examples/counter.rs`. However, your programs are also Ethereum ABI equivalent if using the Stylus SDK. **They can be called and transacted with using any other Ethereum tooling.**
By using the program address from your deployment step above, and your wallet, you can attempt to call the counter program and increase its value in storage:
```rs
abigen!(
Counter,
r#"[
function number() external view returns (uint256)
function setNumber(uint256 number) external
function increment() external
]"#
);
let counter = Counter::new(address, client);
let num = counter.number().call().await;
println!("Counter number value = {:?}", num);
let _ = counter.increment().send().await?.await?;
println!("Successfully incremented counter via a tx");
let num = counter.number().call().await;
println!("New counter number value = {:?}", num);
```
To run it, set the following env vars or place them in a `.env` file this project, then:
```
STYLUS_PROGRAM_ADDRESS=
PRIV_KEY_PATH=
RPC_URL=https://stylus-testnet.arbitrum.io/rpc
```
Next, run:
```
cargo run --example counter --target=
```
Where you can find `YOUR_ARCHITECTURE` by running `rustc -vV | grep host`. For M1 Apple computers, for example, this is `aarch64-apple-darwin` and for most Linux x86 it is `x86_64-unknown-linux-gnu`
## Build Options
By default, the cargo stylus tool will build your project for WASM using sensible optimizations, but you can control how this gets compiled by seeing the full README for [cargo stylus](https://github.com/OffchainLabs/cargo-stylus). If you wish to optimize the size of your compiled WASM, see the different options available [here](https://github.com/OffchainLabs/cargo-stylus/blob/main/OPTIMIZING_BINARIES.md).
## Peeking Under the Hood
The [stylus-sdk](https://github.com/OffchainLabs/stylus-sdk-rs) contains many features for writing Stylus programs in Rust. It also provides helpful macros to make the experience for Solidity developers easier. These macros expand your code into pure Rust code that can then be compiled to WASM. If you want to see what the `stylus-hello-world` boilerplate expands into, you can use `cargo expand` to see the pure Rust code that will be deployed onchain.
First, run `cargo install cargo-expand` if you don't have the subcommand already, then:
```
cargo expand --all-features --release --target=
```
Where you can find `YOUR_ARCHITECTURE` by running `rustc -vV | grep host`. For M1 Apple computers, for example, this is `aarch64-apple-darwin`.
## License
This project is fully open source, including an Apache-2.0 or MIT license at your choosing under your own copyright.