Bytom
Published in

Bytom

Introduction to Bytom 2.0 Contract Language

What is smart contract language

Smart contract language is a programming language for writing blockchain smart contracts. In order to be more friendly to developers and easier to use, current smart contract languages ​​generally refer to existing programming languages. For example, Solidity refers to Java Script. Influenced by languages ​​such as C++ and Python. The contract language of Bytom 2.0 adopts a Golang-like design.

Why Bytom 2.0 introduces a new language

Equity, the contract language of Bytom 1.0, implements Turing completeness on UTXO and has good programming ability, but it is quite different from the current programming language in terms of grammar, morphology, and program structure. It takes a long time to get started and has a learning curve. Steep, not conducive for developers to enter.

In order to improve these shortcomings of the Equity language, Bytom2.0’s contract language draws on the design of Golang, and is basically consistent with the Go language in terms of program structure, declarations, function definitions, type definitions, etc., so that you have Go programming experience (or have certain Developers with programming experience) can program without barriers. Thereby it is more conducive to the promotion and expansion of the contract

Language features

1. Go-like language design
2. Static type (variable type is known at compile time)
3. UTXO operation introduces struct, assetId introduces global variables
4. UTXO introduces the lock built-in method
Follow-up support:
Libraries (you can create reusable code called from different contracts — — just like static functions in static classes in other object-oriented programming languages).

Program structure

The contract procedure consists of the following elements:

1.Declaration part: declare the introduced global library, the current version is not available
2.Top-level structure: It is composed of statements, and the program is executed from the top-level structure of the contract source file.
3.Function definition (declaration): declaration defines the function
4.Type definition: structure, enumeration, execute the same function.

Simple example:

func t1(a int64, b int64) int64 {var c int64 = 12345678if a > b {
c = a
} else {
c = b
}
return c
}

Grammar rules

1. Source file character encoding

Use the same character encoding as the processor. Unix is ​​UTF-8, and Windows is GB2312. Except for comments and defined string literals, other non-ASCII characters cannot be used.

2. Keywords

The following words are used as keywords and cannot be used as variable names, function names, and structure names.

if、else、type、struct、var、goto、for、range、break、continue、func、nil、
bool、string、const、amount、asset、hash、publickey、signature、program

3. Blank character processing

Spaces, tabs, and newlines are used to distinguish different identifiers. The newline in the statement of the function indicates the end of the current statement.
4. Notes

Same as golang, `/*xxx*/` block comment, `//` line comment.
5. Identifier

Identifiers used for variable names, function names, structure names, etc., rules:
The first letter must be an English uppercase and lowercase letter or an underscore, `A-Za-z_`.
The second letter can start with English uppercase and lowercase letters, underscores or numbers, `A-Za-z_0–9`.

6, literal

bool literal: true false
Shaping literal: [1–9][0–9]*)|0
Floating point numbers: [0–9]+\.[0–9]+
String literal: [A-Za-z_][A-Za-z_0–9]*

7. Data type

bool type
Integer type
Floating point type
String type
Structure type
Enumerated type
amount type
asset type
hash type
publicKey type
signature type
program type
built-in method

8. Expression

expression operator concatenated expression of willingness
Unary expression
Literal
Identifier
enumerate

Case

// stateData:  [creater, nftAsset, owner, marginAsset, marginAmount]var (
CreaterScript hex = "63726561746572536372697074"
NftAsset asset = "a0a71c215764e342d10d003be6369baf4145d9c7977f7b8f6bf446e628d8b8b8"
OwnerScirpt hex = "6f776e6572536369727074"
MarginAsset asset = "a0a71c215764e342d10d003be6369baf4145d9c7977f7b8f6bf446e628d8b8b8"
MarginAmount amount = 200000000
)
// alt stack [creater,, nftAsset, owner, marginAsset, marginAmount]
// data statck [newMarginAsset, newMarginAmount, selecter]
func editMargin(marginAsset asset, marginAmount amount) {
MarginAmount = marginAmount
MarginAsset = marginAsset
lock(1, NftAsset, "")
lock(MarginAmount, MarginAsset, "")
}
// alt stack [creater, taxRate, nftAsset, owner, marginAsset, marginAmount]
// data statck [buyer, payAmount, marginAmount, selecter]
func buy(buyer hex, payAmount amount, marginAmount amount) {
var MarginFold int64 = 10
// []byte("platformScript")
var PlatformScript hex = "706c6174666f726d536372697074"
platformFee := int64(payAmount) / 100
ownerGot := int64(payAmount) - platformFee - createrTax
verify(ownerGot < int64(MarginAmount)*MarginFold)
lock(amount(platformFee), MarginAsset, PlatformScript)
lock(amount(createrTax), MarginAsset, CreaterScript)
lock(amount(ownerGot), MarginAsset, OwnerScirpt)
OwnerScirpt = buyer
MarginAmount = marginAmount
lock(1, NftAsset, "")
lock(MarginAmount, MarginAsset, "")
}

The above is a case of NFT purchase contract
1. First define a global structure, put NFT related information into
2. Define a purchase function. According to the needs of the user, take the NFT and quantity to be purchased as input, calculate the purchase fee and platform handling fee, and then transfer the ownership of the NFT to the buyer’s script

Future outlook

The current Bytom 2.0 contract language is still in the stage of improvement, and some of the above features have not yet been fully determined, and will be further improved according to the subsequent overall technical route and direction.

First, more language features will be added, such as the introduction of libraries, the introduction of inheritance capabilities, etc.
In the future, it can be further compatible with the Ethereum contract system, and implement mainstream virtual machine architectures such as EVM/eWASM based on the UTXO account model, so that DeFi applications, ecosystem components and tools on external ecosystems (such as Ethereum) are compatible with Bytom 2.0. Need to modify or only need small changes, further reducing the developer’s learning cost and migration difficulty.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store