> For the complete documentation index, see [llms.txt](https://enderphan.kubertu.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://enderphan.kubertu.com/smartcontract/writing-a-safe-smart-contract.md).

# Writing a safe smart contract

## Avoid Race-Condition

Always either update the state or throw

```
contract C {
  bool bought;
  ...
  function buy(string name) {
    if (bought) throw;
    bought = true;
    ...
  }
}
```

use the state-machine pattern with modifiers

even if only one user: UI buttons are easily clicked twice
