DEV Community

Cover image for Announcing the Sword compiler
Sword
Sword

Posted on

Announcing the Sword compiler

In celebration of Bitcoin's 12th birthday on January 3rd at 19:15, we release the Sword compiler as open source. Sword is a declarative language for expressing fully-collateralized financial contracts on the blockchain. Sword makes timed, conditional transfers of tokenized assets easy.

Sword is highly domain-specific. Its language design solves problems we have not seen solved elsewhere:

  • Negative balances not allowed! To activate a derivative contract, full collateral for the assets being settled must be provided. When an asset's quantity depends on some condition that is not known during deployment, a bound is provided. This means that a derivative contract can always pay out.

This constraint makes derivatives practically executable.

  • Holding a position has been tokenized! When a derivative contract gets deployed, a number of party tokens are created for each possible payout. Those party tokens can be traded while the derivative contract finalizes, and they can be converted to the underlying, settled asset when the position matures.

This feature provides a clear path towards market adoption.

Sword is very small. It features a functioning Ethereum back-end, but can be extended to most newer blockchains. Sword is also quite efficient. It compiles directly to EVM and features optimizations that are not available in a language like Solidity.

A quick example

Using an if-within control structure and a price oracle, we can formulate a contract:

if obs(bool, BTCUSD_EXCEEDS, 40000) within days(7) then
    transfer(DAI, 1)
else
    transfer(DAI, 2)
Enter fullscreen mode Exit fullscreen mode

... which means: If the USD price of one Bitcoin exceeds $40,000 within one week of contract activation, transfer 1 DAI to party #1, otherwise transfer 1 DAI to party #2. To activate this contract, 1 DAI must be provided. After at most one week, holders of either party token #1 or #2 can cash out.

Something about then and now

Sword saw some big highlights in 2020:

  • Safe arithmetic derived from OpenZeppelin, but hand-coded in EVM.
  • Positions as tokens was first implemented using external ERC20 token contracts.
  • We then hand-coded an ERC1155 multi-party token interface in EVM instead.
  • A party token #0 was devised to ensure that assets don't get stuck.
  • Integration test coverage improved significantly.

Several of these improvements were made as contributions by students who subsequently took their first Haskell course. We are proud to say that the barrier to entry is low enough to welcome people with limited experience in Haskell, compilers and blockchain.

In 2021 we'd like to keep this momentum:

  • Target another blockchain; which one?
  • More safety in the translation; higher test coverage, more sophisticated testing techniques.
  • More efficiency; some low-hanging fruit was taken, we've had fun hand-optimizing EVM code, but there are many more opportunities. Writing a compiler for the blockchain is truly a use-case that justifies efficient code.
  • Improve the language design and tooling; some choices lead to edge cases, others lead to elegance. There are still ways to make the language both simpler and easier to use.

Now that Sword is open source, you're invited! The issue tracker is going through a transition period to become more readable to newcomers, but we have 99 problems and we're happy to share them!

Top comments (0)