> For the complete documentation index, see [llms.txt](https://bloqly.gitbook.io/bloqly/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bloqly.gitbook.io/bloqly/smart-contracts/javascript-smart-contracts.md).

# JavaScript smart contracts

Bloqly engine is responsible for validating and storing states of documents. Each document state change is represented by new `value` property of a transaction.

It is possible that a bad client can create a transaction which doesn't respect the typical state diagram of a document.

For example, let's imagine there is a document, representing a child enrollment form in a school. It can be in statuses 'RECEIVED', 'IN\_PROGRESS', 'CONFIRMED', 'REJECTED'.

Without blockchain validating these statuses, it is possible to create 'CONFIRMED' status right away.

In order to address this issue, Bloqly introduces state-free functional smart contracts which can be written in JavaScript and other scripting languages (Python, Ruby etc).

Smart contract code representing the provided example:

```javascript
const STATUSES = ['RECEIVED', 'IN_PROGRESS', 'CONFIRMED', 'REJECTED'];

const INITIAL_STATUS = 'RECEIVED';

const STATUSES_CHANGE = {
    'RECEIVED': ['IN_PROGRESS', 'RECEIVED'],
    'IN_PROGRESS': ['CONFIRMED', 'REJECTED'],
    'CONFIRMED': [],
    'REJECTED': [],
};

function confirm(value, prev, space, key) {

    const prevValue = prev || INITIAL_STATUS;

    const statusExists = STATUSES.indexOf(value) !== -1;

    const nextStatusAllowed = STATUSES_CHANGE[prevValue].indexOf(value) !== -1;

    return statusExists && nextStatusAllowed;
}
```

Method `confirm` is called each time new transaction received. In the case it returns `false`, no data will be stored in blockchain.
