Skip to main content

What are Smart Contracts?


A smart contract is a computer program or a transaction protocol which is intended to automatically execute, control or document legally relevant events and actions according to the terms of a contract or an agreement. The objectives of smart contracts are the reduction of need in trusted intermediators, arbitrations and enforcement costs, fraud losses, as well as the reduction of malicious and accidental exceptions.

In short, smart contracts are executable logic run on chain. Decentralized and on-chain execution makes the logic unstoppable, unbreakable and irreversible. This makes the execution trusted and the validity secured by means of cryptography.

Smart contracts can represent generalized logic ranging from financial applications to organisational structures.

Here is a great video explanation:

This channel is a first stop for blockchain learners. It is highly recommended that you explore the rest of the playlist.

In our context, CosmWasm is the framework that provides code and tooling to develop smart contracts and infrastructure to run them on chain. For now smart contracts are written using Rust.

Here is a short code snippet exemplifying the smart contract execution logic to help understand the concept better.

/// This logic is used to transfer tokens/coins from one account to another
pub fn execute_transfer(
deps: DepsMut,
_env: Env,
info: MessageInfo,
recipient: String,
amount: Uint128,
) -> Result<Response, ContractError> {
// Amount validation
if amount == Uint128::zero() {
return Err(ContractError::InvalidZeroAmount {});

let rcpt_addr = deps.api.addr_validate(&recipient)?;

// Update the stored balances
|balance: Option<Uint128>| -> StdResult<_> {
|balance: Option<Uint128>| -> StdResult<_> { Ok(balance.unwrap_or_default() + amount) },

// Returning a response back to indicate the execution is successful
let res = Response::new()
.add_attribute("action", "transfer")
.add_attribute("from", info.sender)
.add_attribute("to", recipient)
.add_attribute("amount", amount);

As you can see smart contracts are not magic. They are very similar to any other programmed logic.

Differences from traditional back-end development are fundamental concepts from cryptography and other disciplines, such as: hash, address, merkle root, key-value database.

Of course, there are other differences—consensus, network security and message propagation to name a few. However, these are more advanced topics and will not be covered here.