Waves Smart Contracts. First experirence

Nicolay Lipnevich
3 min readJul 31, 2018

In April 2018 Waves team introduced their non turing complete smart contracts. Waves is promising and extremely fast developing blockchain. As a system architect I have to investigate and try new technologies in order to be able to choose best technology stack for projects. And with start of Waves hackathon I decided to dive into. Below you can find detailed information about smart contract that won third place reward on the hackathon. Hope this information will be usefull for developers who want to implement own smart contracts

Smart Rewarding Idea

Idea of ​​the project in the automatic distribution of salaries of software developers using the githab in accordance to part of work performed. The main challenge of such projects is the need to trust the program that distributes the money. Waves Smart Contract make it secure and reliable, where a user without programming skills can check correctness of the distribution of funds before transferring money

Stack of technologies

Html and a Java Script (Node.js Waves library), open source on GitHub and a live demo on Firebase Hosting

Process

  1. User has to choose a GitHub repository and the total amount of salaries
  2. Payment (approximately 0.01 Waves or 0.02 dollars) for the storing data in the blockchain and creating smart contract should be done
  3. User has to check the correctness of the data and then transfers funds to the verified address
  4. The funds will be distributed among the developers

Development

The best entry point for a novice developer is the demo console, there is a clear example and the console for immediate testing your code. During development, it is better to use a test blockchain, where you can get 10 free test Waves in a single click. Although Google still does not find many examples due to the novelty of technology Smart Contracts are well documented and all the necessary information can be found in the official description

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

Smart Contract

Smart contract is connected to a single account and validate each action on it (except for incoming money transferring):

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 }

Let’s analyze it row by row

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

First of all we are storing wallet public key as a constant to be sure that only wallet owner (Smart Rewarding Project in our case) may transfer funds for the account

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

It is main construction of each Waves smart contart tells us that this contract restrict any transaction other than the transfer of funds (TransferTransaction). So it is not possible to update any account data (DataTransaction) or update smart contract (SetScript) after this smart contract creation

let employerAddress = addressFromPublicKey(tx.senderPk);

With standart library method addressFromPublicKey we are getting sender account address

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

With standart library method addressFromRecipient we are getting recipient account address

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

Here method getLong give us data stored in account of sender that is actually amount of salary for developer. The contract can access the data already stored in Waves blockchain (and can NOT access data located on some website or a third-party server)

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

Here we check that amount of money correct and check signature. True means that transaction allowed and false means that transaction forbidden. Thus, with any transfer of money from this account, it is checked that the money will go to the wallet of the developer and in the amount already stored in the blockchain in an easily readable key pair format (address as key and amount as value), which makes it possible not to trust the program, but check the data by yourself in 5 -10 seconds

Online project demo on the test block available here
Here you can see the project source

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

--

--