13 releases (2 stable)

new 2.0.0 Nov 13, 2024
1.0.0 Jan 9, 2024
0.11.0 Jan 9, 2024
0.10.0 Dec 25, 2023

#632 in Magic Beans

Download history 365/week @ 2024-07-29 300/week @ 2024-08-05 277/week @ 2024-08-12 158/week @ 2024-08-19 297/week @ 2024-08-26 324/week @ 2024-09-02 417/week @ 2024-09-09 301/week @ 2024-09-16 384/week @ 2024-09-23 412/week @ 2024-09-30 537/week @ 2024-10-07 353/week @ 2024-10-14 461/week @ 2024-10-21 489/week @ 2024-10-28 240/week @ 2024-11-04 666/week @ 2024-11-11

1,868 downloads per month
Used in zilliqa-rs

MIT license

240KB
4.5K SLoC

Rust 3.5K SLoC // 0.0% comments LALRPOP 1K SLoC // 0.4% comments

Scilla Parser

This repository contains a Rust parser for the Scilla smart contract language. Scilla is the smart contract language used in the Zilliqa blockchain.

Install

Add the following to your Cargo.toml:

[dependencies]
scilla_parser = "1.0.0"

Alternatively, You can run this command:

cargo add scilla_parser

This will add the scilla_parser dependency to Cargo.toml as specified in the installation instructions above.

Usage

This library parses a .scilla file. There are two options:

  1. Use Contract::parse and pass a contract path.
  2. Parse a string (slice) containing a scilla contract.

To parse a Scilla file:

Here is the code to parse SendZil.scilla contract:

    let contract_path = PathBuf::from("tests/contracts/SendZil.scilla");
    let contract = Contract::parse(&contract_path).unwrap();

    assert_eq!(
        contract,
        Contract {
            name: "SendZil".to_string(),
            init_params: FieldList::default(),
            fields: FieldList(vec![
                Field::new("test_field", Type::Uint256),
                Field::new("bool", Type::Bool),
                Field::new("empty_bool", Type::Option(Box::new(Type::Bool))),
                Field::new("some_int", Type::Option(Box::new(Type::Int32))),
                Field::new(
                    "pair",
                    Type::Pair(Box::new(Type::String), Box::new(Type::Uint32))
                ),
                Field::new("list", Type::List(Box::new(Type::Int32))),
            ]),
            transitions: TransitionList(vec![
                Transition::new_without_param("acceptZil"),
                Transition::new(
                    "updateTestField",
                    FieldList(vec![Field::new("val", Type::Uint256)])
                ),
                Transition::new_without_param("dontAcceptZil"),
                Transition::new(
                    "fundUserWithTag",
                    FieldList(vec![
                        Field::new("user", Type::ByStr20),
                        Field::new("amount", Type::Uint128)
                    ])
                ),
                Transition::new(
                    "fundUser",
                    FieldList(vec![
                        Field::new("user", Type::ByStr20),
                        Field::new("amount", Type::Uint128)
                    ])
                ),
                Transition::new(
                    "fundContract",
                    FieldList(vec![
                        Field::new("contract_address", Type::ByStr20),
                        Field::new("amount", Type::Uint128)
                    ])
                ),
                Transition::new(
                    "callOtherContract",
                    FieldList(vec![
                        Field::new("contract_address", Type::ByStr20),
                        Field::new("tag", Type::String),
                        Field::new("value", Type::Uint256)
                    ])
                ),
            ])
        }
    );

To parse a string containing a scilla contract:

    let contract_code: &str = "contract HelloWorld";
    let contract: Contract = contract_code.parse().unwrap();

For more examples, take a look at the tests.

Dependencies

~2.5–5.5MB
~89K SLoC