Smart Contracts in Waves. First experience
By: Alejandro Escandon
Tags: Wavesplatform, Smart Contracts, Waves, Ride, Web3

Translated from Смартконтракты Waves. Первый опыт

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

A little later, when the Waves Hackathon was announced, it was time to dive into the new technology. Below you will find detailed information about the smart contract that won the 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 correctness of the distribution of funds before transferring money

Used technology

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

Process

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

Development

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

The Waves API library for Node.js already works well in browsers, but unfortunately, it is NOT yet compatible with Google Cloud Functions or React Native (the Waves developers are working to fix it soon).

Smart Contract

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

Analysis of each line of code of the smart contract

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

Here we save the public key of the wallet (your JavaScript must contain the publicKey variable created), 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 structure of any smart contract. Waves tells us that this contract prohibits any activity case _ => false except transfer of funds case tx: TransferTransaction . Therefore, after creating a contract, it is impossible to change the data of the developers who will receive the money (through DataTransaction) or change the smart contract itself (through SetScript).

let employerAddress = addressFromPublicKey(tx.senderPk);

Here, the built-in library’s base addressFromPublicKey method is used to obtain 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(getInteger(employerAddress, dateKey));

The base getInteger function gives us the data stored in the blockchain at the address of the request creator, the employer, which is the sum of the developer’s salary, it get data from the employer account state. 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 the signature. True means that the transaction will be allowed, and false means that the transaction will be denied. Thus, any transfer of money from this account is checked and the money will be sent only to the developer’s wallet and to the amount already stored in the blockchain in the format of an easily readable pair of keys (address as key and amount as value), which allows to trust the dapp.

Online demonstration of the project on a testnet waves platform, 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.

Want to learn more, go to the online free course Mastering Web3 with Waves.