Categories
blockchain tips and tricks

Restrict Access to Some Functions

Most smart contracts will need access restrictions on functions that only the owner of the contract should be able to invoke. A commonly used pattern for this is the onlyOwner pattern. This is where a custom modifier requires that the caller of the function is equal to the owner address.

To save writing your own logic for this, Openzeppelin provides a contract called Ownable,

pragma solidity ^0.5.5;

import "@openzeppelin/contracts/ownership/Ownable.sol";

contract OwnableContract is Ownable {

  function restrictedFunction() public onlyOwner returns (uint) {
    return 99;
  }

  function openFunction() public returns (uint) {
    return 1;
  }

}
Categories
blockchain tips and tricks

Does It Do Any Maths?

If you expect your smart contract to perform mathematical calculations, refrain from using arithmetic operators like plus(+), minus(-), multiply(*), divide(/), and modulus (%). Without proper checks, they have the potential to introduce underflow and overflow vulnerabilities.

Use Openzeppelin’s SafeMath library for unsigned integer operations.https://betterprogramming.pub/media/ac434b4d8b7dddfea73535fafd717396.

pragma solidity ^0.5.5;

import "@openzeppelin/contracts/math/SafeMath.sol";

contract BasicSafeMath {

  using SafeMath for uint;
  
  function doSomeMath(uint _a, uint _b) public returns (uint) {
    return _a.sub(_b);
  }
}
Categories
blockchain tips and tricks

Chose Solidity Version

Find out the latest stable version of Solidity and familiarise yourself with its syntax. If you’re extending libraries, make sure your chosen version isn’t ahead of the version that the libraries support.

For example, Openzeppelin contracts library version 0.2.5 supports Solidity version 0.5.5, but not 0.6.x.

Categories
blockchain tips and tricks

Chose Your Development Environment

If you’ve had experience developing software already, you’ll likely have a preferred IDE to code in. Multiple IDEs support Solidity development. My favourite is VS Code, a free text editor created by Microsoft, with the Solidity extension.

If you’re less experienced and don’t want to have to worry about setting up your environment, Ethereum provides a great online IDE called Remix. It allows you to start coding, compiling, and deploying immediately.

Categories
blockchain tips and tricks

Name It

Having a clear purpose should make naming your smart contract easy. If you have experience developing in object-oriented languages, name it as you would a class.

Names should be clear, descriptive, and succinct. It should have as few words as possible, ideally one or two. It should describe what the contract is, what it does, and indicate what it’s responsible for.

Categories
blockchain tips and tricks

Check if It Has Been Done Before

Never spend the time rewriting something that already exists. If what you’re trying to create is a different flavour of an existing concept, consider extending work that has been done by others.

Don’t reinvent the wheel.

Openzeppelin provides a library of contracts that implement EIPs like ERC-20, and ERC-721. Utilise these by extending their functionality instead of writing your own from scratch.

Categories
blockchain tips and tricks

Write Better Smart Contracts

Define the Purpose

Before starting, make sure you have a clear understanding of what your smart contract is supposed to do. What value does it add? How should it be used? Consider the question of whether or not it even needs to be a blockchain application. Would it be better implemented on more well-established technology platforms?

Have a clear view of the purpose of your smart contract.

Categories
blockchain tips and tricks

Watch out for Scammers

With no central exchange and little regulation in place, the bitcoin market is, unfortunately, an ideal environment for scammers.

Fake exchanges and wallets are common, as are phishing scams where fraudulent emails direct you to scam sites identical to the platforms you use to capture sensitive data.

Ponzi schemes, which work on a pyramid structure, offer increased returns for investors that recruit other investors and should be avoided at all costs.

Malware is, of course, another issue and without proper security measures around your bitcoin wallet, you leave yourself open to the threat of having your account emptied.

These are just some of the scams to watch out for and, as bitcoin trading is such an open, complicated and relatively new market, new threats pop up regularly.

Categories
blockchain tips and tricks

Get a Handle on Your Emotions

Alongside bitcoin trading tips involving risk management and strategy, there are also emotional factors that you need to be aware of, most notably, the fear of missing out (FOMO).

Due to bitcoin’s extreme volatility, it is not unusual to see dramatic price spikes in a very short space of time and it can be tempting to buy-in to an upward trend, fearing you’ll miss out on major profit if you don’t.

However, this goes against the most basic rule of trading – buy low, sell high.

If bitcoin has skyrocketed, it’s likely you’ve already missed the advantageous point of the upward trend, and you’ll end up paying a premium for an asset that will inevitably decrease in value, placing you at a significant loss.

Falling victim to FOMO is one of the main reasons so many beginner traders fail. Be wary and accept that some profitable opportunities just weren’t meant for you.

Categories
blockchain tips and tricks

Buy and Hold Bitcoin

As discussed earlier, there are several types of bitcoin trading strategies. The buy and hold approach is a passive strategy where positions are held anywhere from weeks to years.

There are multiple benefits to this:

Buying and holding bitcoin allows you to bypass its short-term volatility. It’s not unusual to see significant movement throughout any given day which can mean your stop loss and take profit targets are easily met, throwing you out of your trade.

This, in turn, can lead to overtrading, and since opening a new position is costly, overtrading can seriously eat into your profits.

As a passive trader, you can keep your position open and potentially earn a good profit with little time commitment, but you still need to have a robust risk-management strategy in place, with carefully considered stop-loss orders.