| Crates.io | cainome |
| lib.rs | cainome |
| version | 0.10.0 |
| created_at | 2025-02-07 02:42:01.071807+00 |
| updated_at | 2025-09-03 16:38:14.056171+00 |
| description | Generate rust bindings for cairo contracts. |
| homepage | https://github.com/cartridge-gg/cainome |
| repository | https://github.com/cartridge-gg/cainome |
| max_upload_size | |
| id | 1546461 |
| size | 991,177 |
Cainome is a library to generate bindings from Cairo ABI.
Cainome architecture provides a flexible way to work with Cairo ABI for different languages (backends).
When you have to interact with a Cairo contract from Rust or Go, you can use Cainome to generate the bindings for you.
Cainome will totally abstract the Cairo serialization and deserialization, and you can focus on the logic around your contract.
use cainome::rs::abigen;
abigen!(MyContract, "/path/project.contract_class.json");
fn main() -> Result<()> {
// starknet-rs provider + contract address.
let contract = MyContract::new(contract_address, provider);
// Send transactions.
let tx_result = contract.my_func(Felt::ONE).send().await?;
// Call functions.
let res = contract.my_view().call().await?;
}
Add a //go:generate directive to your Go source file:
//go:generate go run -mod=mod github.com/cainome/src/bin/cli/plugins/builtins/golang --execution-version v3 --golang --golang-package mycontract --output-dir ./bindings ./contract.json
package main
import (
"context"
"mymodule/bindings/mycontract"
)
func main() {
// Your code using the generated bindings
}
Then run:
go generate ./...
This will automatically download and run cainome to generate your bindings. The cainome binary will be installed via cargo if not already present.
You can also set a specific version using the CAINOME_VERSION environment variable:
CAINOME_VERSION=v0.3.0 go generate ./...
For more advanced usage, environment variables, and troubleshooting, see the CLI documentation.
# Generate Go bindings using CLI
cainome --golang --golang-package mycontract --output-dir ./bindings /path/project.contract_class.json
package main
import (
"context"
"github.com/NethermindEth/starknet.go/account"
"github.com/NethermindEth/starknet.go/rpc"
"mycontract"
)
func main() {
// Setup StarkNet provider and account
provider := rpc.NewProvider("https://starknet-mainnet.public.blastapi.io")
// Create contract reader for view functions
reader := mycontract.NewReader(contractAddress, provider)
// Call view functions
result, err := reader.MyView(context.Background())
if err != nil {
panic(err)
}
// Create contract writer for transactions
writer := mycontract.NewWriter(contractAddress, account)
// Send transactions
txResult, err := writer.MyFunc(context.Background(), feltValue)
if err != nil {
panic(err)
}
}
For more details, refer to the different READMEs in the github repository.
src/bin/cli, the cainome CLI binary can be built using cargo build: README.src/lib.rs, the cainome library can be built using cargo build --lib.Tokens README.Felt buffer README.abigen macro to generate rust bindings README.TypeScript bindings (coming soon).Currently those crates are not published on crates.io, please consider using them with the release tags.
Cainome uses a plugin system that currently supports two main approaches:
protobuf, which can be written in any language--golang flag)Currently, to write a plugin you can take as example the RustPlugin.
src/bin/cli/plugins/builtins.rs crate), or in the module you've created at the previous step (use a folder in this case).
Writting a crate can be easier to re-use in other projects though.PluginInput as argument, where the tokens from the parser crate are available for each contract.
From these tokens, you can easily generate code that represent the ABI of the contract. In the case of rust, you can find in the rs crate
some examples of how types are handled.
You don't have to use syn crate as rs crate is doing. You can simply build strings.output_dir, so it is responsible of writing and organizing it's files.Cainome is a word combining Cairo and Genome. The idea of Cairo ABI being the DNA of our ecosystem,
and like the genome expresses several genes which turn into proteins, from an ABI we can generate several bindings in different languages.