📜 ⬆️ ⬇️

Smart Contracts Waves. First experience

image

In April 2018, the Waves team unveiled their non-turing-complete smart contracts.

A little later, when the Hackathon Waves was announced, I decided it was time to dive into the new technology. Under the cut you will find detailed information about the smart contract that won third place on the hackathon.
')
The article will be useful for developers to implement their own smart contracts and get acquainted with the technology.

Idea


The idea of ​​the project is to automatically distribute the salaries of software developers using Github, in proportion to the work performed. The main task that the smart contract solves is that it saves us from having to trust the money-distributing program, allows even the user without programming skills to check the allocation of funds before transferring money

Technology stack


Html and JavaScript (Node.js Waves library ), open source on GitHub and demonstration of the project on Firebase Hosting

Process


  1. The user chooses the GitHub repository and determines the total amount of salaries.
  2. The user makes a payment in 0.01 Waves (approximately $ 0.02) to save data in the blockchain and create a smart contract
  3. The user checks the accuracy of the data and then transfers the amount of salaries to the address of the smart contract
  4. Salaries are distributed among developers

Development


The best entry point for a novice developer is a demo console containing an example and a console for testing code. During development, it is better to use a test blockchain , where you can get 10 free test waves with one click . And although Google still does not find many examples because of the new technology, smart contracts are well documented and contain all the necessary information.

The NodeJs library (1.4.0) already works well in browsers (except for creating Alias), but, unfortunately, it is NOT yet compatible with Google Functions or React Native (the Waves developers promise to fix it soon).

Smart contract


A smart contract is created for one account and checks every action with this account (except for incoming money transfers):

let signature = base58'${currentWallet.keyPair.publicKey}'; match tx { case tx:TransferTransaction => { let employerAddress = addressFromPublicKey(tx.senderPk); let dateKey = toBase58String(addressFromRecipient(tx.recipient).bytes); let salary = extract(getLong(employerAddress, dateKey)); if((salary == tx.amount) && sigVerify(tx.bodyBytes, tx.proofs[0], signature)) then true else false } case _ => false } 

Line by line smart contract parsing


 let signature = base58'${currentWallet.keyPair.publicKey}'; 

Here we save the public key of the wallet (your JavaScript must contain the currentWallet variable created using the Waves.Seed.create () command), so that only the smart wallet contract account (Smart Rewarding Project in our case) can transfer funds.

 match tx { case tx:TransferTransaction => {some validations} case _ => false } 

This is the basic construction of any smart contract. Waves tells us that this contract prohibits any activity except transferring funds (TransferTransaction). Thus, after creating a contract, it is impossible to change data about developers (DataTransaction) or to change the smart contract itself (SetScript).

 let employerAddress = addressFromPublicKey(tx.senderPk); 

Here, the built-in library's base addressFromPublicKey method is used to get the address of the request creator from its public key.

 let dateKey = toBase58String(addressFromRecipient(tx.recipient).bytes); 

The base method addressFromRecipient gets the address of the recipient of funds

 let salary = extract(getLong(employerAddress, dateKey)); 

The base getLong method gives us the data stored in the blockchain at the address of the request creator, which is the sum of the developer’s salary. The contract can access data stored on the Waves blockchain (and can NOT access data located on any website or a third-party server).

 if((salary == tx.amount) && sigVerify(tx.bodyBytes, tx.proofs[0], signature)) then true else false 

Here is the verification of the amount and signature. True means that the transaction will be allowed, and false means that the transaction will be denied. Thus, for any transfer of money from this account, it is checked that money will be sent only to the developer’s wallet and to the amount already stored in the block chain in the format of an easily readable pair of keys (address as key and amount as value), which allows credibility of the program, check all data yourself for 5-10 seconds before the transfer of salaries.

Online demonstration of the project on a test blockchain, available here.
Here you can see the project code.

I hope that the article was informative and will be useful to you for writing your first smart contract.

Source: https://habr.com/ru/post/424111/


All Articles