0.1.2 • Published 2 years ago

unfold-lang v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Unfold

Unfold is a lightweight, domain-specific language for querying crypto wallets. It currently supports various operations, such as checking the ETH, ERC20, or ERC721 balance, on a single wallet address.

The primary benefit of Unfold is its simplicity in usage. You can learn the entire grammar in < 5 minutes, making it a perfect low-code environment for tokengating your storefront or community. Moreover, every runtime is sandboxed and isolated from the rest of the environment. This prevents malicious users from injecting Javascript into your websites.

Sounds interesting? Check out the demo!

Want to try it out? I've set up a playground just for you!

Installation

Unfold is available on npm. Just run the following command in your project.

You can also manually install Unfold. This is useful if you want to make significant changes to the interpreter architecture. Just clone the repo (or manually install it) and run npm install in the directory.

Set Up

After installation, you can get started in < 15 lines of code.

Under the Hood

Every time any Unfold code is written, a separate runtime instance needs to be created to execute the code.

To maintain generalizability to other L1/L2 chains, Unfold runtimes expect two inputs: the written code and a wallet context. Wallet contexts are responsible for providing methods and attributes that interact with the chain, such as address, queryNativeToken(), queryToken(tokenType, tokenAddress), and setChain(chainId).

The lexing/parsing of Unfold is handled by nearleyjs, which returns an abstract syntax tree in JSON. This tree is passed off to simple interpreter written in vanilla Javascript.

An example of an AST (for the first example in example_queries.txt) is shown below.

Writing Scripts

Every Unfold script is composed of statements. The Unfold runtime expects at most one statement per line. Unfold supports the usual comparators and conditional operations, as well as boolean values.

The syntax for an conditional statement is as follows.

A condition includes all of the usual comparators, logical symbols, and boolean values. Conditions can compare numbers (i.e. amount of USDC in a wallet) or addresses (i.e. for whitelisting).

A directive can be a return statement, a token assignment, a chain change, or another conditional statement.

The only type of variable in Unfold is a token. A token can be defined as follows.

TokenName can be any string. This identifier is stored a global state that can be reused by future statements.

TokenObject can be created by either specifying ERC20 or ERC721 with a contract address.

There is also a special TokenObject ETH that queries the amount of Ether in a given wallet address.

Any condition or value can be returned from the Unfold runtime through the return command.

Comments are prefaced with a pound symbol #. A comment must occupy its own line with no other code.

Roadmap

I'm not actively working on this project, but I'll be adding features as I need them.

I'll happily merge new contexts for other L1s, for example, Solana (with the help of @solana/web3js). I want this project to be as extensible and general-purpose as possible.

Adding new primitives to the grammar would be helpful. I'm currently thinking about lists, dicts, etc. Adding types like these would increase complexity by quite a bit (adding for loops, for example), but maybe the tradeoff is worth it.

A cool idea I've been having is to extend the grammar to call methods on contracts directly. Definitely an interesting exercise in language design!

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago