hackethberlin/README.md

69 lines
3.3 KiB
Markdown
Raw Normal View History

2018-09-08 22:35:41 +02:00
# Crotalinae
2018-09-09 08:54:12 +02:00
Metaprogramming for Ethereum Smart Contracts expressed in Scala's Type System.
2018-09-08 22:35:41 +02:00
2018-09-09 09:26:17 +02:00
- Unlock Smart Contracts **adoption with a JVM**-based language: code them _from_ Scala or _in_ Scala
2018-09-09 08:21:04 +02:00
- Smart Contracts code generator is itself **a strictly typed program**
- Write a Contract using structs and definitions of **Crotalinae DSL**, and if it compiles, you're **safe**
- **Export** Smart Contract in [Vyper](https://github.com/ethereum/vyper) as a single plaintext and check it visually if needed
2018-09-09 08:54:12 +02:00
- _(WIP)_ Code directly in Scala: **Scala source code** is translated to Crotalinae DSL using macros
2018-09-08 22:35:41 +02:00
2018-09-09 08:48:12 +02:00
## Tech dive
2018-09-08 22:35:41 +02:00
2018-09-09 08:48:12 +02:00
[Scala language](https://www.scala-lang.org/) is a functional programming language for JVM.
We've noticed that we may generate Smart Contracts in a functional way: function definition is a profunctor (_Intuitively it is a bifunctor where the first argument is contravariant and the second argument is covariant._), arguments and structs are _products_, and so on.
2018-09-08 22:35:41 +02:00
2018-09-09 08:54:12 +02:00
Scala's strictly typed product exists in form of a [shapeless heterogenous list](https://github.com/milessabin/shapeless). As arguments or contract data may be referenced by name, we also use Record pattern and reflect these names in the type system.
2018-09-08 22:35:41 +02:00
2018-09-09 08:48:12 +02:00
To go beyond the data and function definitions, we use [Free Monad](https://typelevel.org/cats/datatypes/freemonad.html) from a category theory library for Scala named [Cats](https://github.com/typelevel/cats). It lets us _represent stateful computations as data, and run them_.
2018-09-08 22:35:41 +02:00
2018-09-09 08:48:12 +02:00
Running, in our case, means code generation. It is done with a _natural transformation_ from _Crotalinae DSL_ into a _Writer Monad_, which is then converted to a text.
Contracts are exported in [Vyper](https://github.com/ethereum/vyper) language.
It's simple, very readable and comprehensive, and we really like it!
2018-09-09 08:54:12 +02:00
However, it lacks tools for code reuse: you often need to copy-paste where, say in Solidity, you may call a function from a library.
With _Crotalinae_ you still have _Vyper_ sources at the end, but may take advantages of the functional composition to re-use code in different contracts instead of copy-pasting it.
2018-09-08 22:35:41 +02:00
## Example
This _crazy_ Scala code:
```scala
val f = `@public` @:
2018-09-09 10:09:24 +02:00
sumArgs.funcDef("sumSome", uint256) { args ⇒
2018-09-08 22:35:41 +02:00
for {
c ← 'c :=: `++`(args.ref('a), args.ref('b))
d ← 'd :=: `++`(args.ref('b), c)
2018-09-09 10:09:24 +02:00
_ ← d :=: c
2018-09-08 22:35:41 +02:00
sum ← `++`(args.ref('a), d).toReturn
} yield sum
}
println(f.toVyper)
```
Compiles into this:
```python
@public
2018-09-09 10:09:24 +02:00
def sumSome(a: uint256, b: uint256) -> uint256:
2018-09-08 22:35:41 +02:00
c = a + b
d = b + c
2018-09-09 10:09:24 +02:00
d = c
2018-09-08 22:35:41 +02:00
return a + d
```
2018-09-09 11:12:12 +02:00
The more sophisticated [Auction example](https://github.com/fluencelabs/hackethberlin/blob/master/src/main/scala/fluence/Auction.scala) is deployed on [Rinkeby](https://rinkeby.etherscan.io/address/0xf24A7726eaF1337A2E8826579EA381705fe64164). Also, you may try it out in [scastie](https://scastie.scala-lang.org/9fdPcByrQM6X7zONR7YX9g). Hooray!
2018-09-08 22:35:41 +02:00
## Future plans
Of course, building EVM code from Vyper, from Scala DSL, using Free Monads and Shapeless Records, is not enough.
2018-09-09 11:05:57 +02:00
So we're working on Macro Programming support as well, so you can write your contracts in a less crazy, more usual Scala and still get all safety advantages. It will add one more layer of Scala code: Scala -> Scala AST (macro) -> Crotalinae DSL -> Vyper.
2018-09-08 22:35:41 +02:00
So stay tuned!