R5 Network
WebsiteR5 LabsGitHub
  • Getting Started
    • Hello & Welcome!
  • About R5
    • Overview
    • R5 Components
    • Consensus Mechanism
    • zkNet (Privacy)
  • R5 Coin
  • R5 Tokenomics
  • Tutorials & Guides
    • Connect & Use R5
      • R5 Desktop Wallet
      • MetaMask
      • Rabby Wallet
      • Coinbase Wallet
    • zkNet Web Wallet
    • R5 Desktop Wallet
      • Interface Overview
      • Send a Transaction
      • Receive a Transaction
      • Backup Your Wallet
      • Retrieve Your Private Key
    • How To: Deploy a Node
    • How To: Mine R5
    • How To: GPU Mine R5
    • How To: Build R5 From Source
    • How To: Connect Local Nodes
  • For Developers
    • R5 SDK
      • R5 Relayer
      • R5 Console
      • JS Console
      • CLI Wallet
      • SCdev
      • SSL Proxy
    • Hardware Requirements
    • R5 Testnet
    • R5 Devnet
    • Local Networks
    • JSON-RPC API
      • admin
      • debug
      • ethash
      • miner
      • net
      • r5 (eth)
      • rpc
      • txpool
      • web3
    • Indexer API
    • zkNet API
    • Node Configuration
    • Ethash-R5
    • Smart Contracts
    • Wrapped R5 (Native)
    • Tokens & NFTs
  • Bug Bounty Program
  • Resources
    • Website
    • R5 Labs
    • R5 Labs GitHub
Powered by GitBook
On this page
  • Key Features
  • Example Smart Contract
  • Getting Started
  1. For Developers

Smart Contracts

R5 smart contracts are fully compatible with the EVM and can be developed using popular languages such as Solidity and Vyper. This document provides technical details on how to write, compile and deploy contracts on the R5 network, as well as an example contract to get you started.


Key Features

  • EVM Compatibility: R5 smart contracts run on a standard EVM, ensuring that bytecode produced by Solidity or Vyper compilers executes consistently. You can use familiar development tools and frameworks without modification.

  • Language Support: Develop contracts using Solidity or Vyper. The resulting bytecode is executed by the R5 Virtual Machine, supporting all EVM opcodes and gas calculations according to the R5 configuration.

  • Efficient Deployment: The R5 network provides a stable environment for deploying contracts. Once deployed, contracts can be interacted with using standard JSON‑RPC endpoints.

  • Full Control: Developers have access to a rich set of debugging and profiling tools that help optimise contract execution and monitor gas usage.


Example Smart Contract

Below is a simple Solidity contract that demonstrates a basic token implementation on the R5 network. This example uses Solidity version 0.8.x and includes standard ERC‑20 functions, such as transferring tokens and managing allowances.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract R5Token {
    string public name = "R5Token";
    string public symbol = "R5T";
    uint8 public decimals = 18;
    uint256 public totalSupply;

    // Mapping from addresses to balances.
    mapping(address => uint256) public balanceOf;
    // Mapping from addresses to allowances.
    mapping(address => mapping(address => uint256)) public allowance;

    // Events for logging transfers and approvals.
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    // Constructor sets the total supply and assigns it to the deployer.
    constructor(uint256 _initialSupply) {
        totalSupply = _initialSupply * 10 ** uint256(decimals);
        balanceOf[msg.sender] = totalSupply;
        emit Transfer(address(0), msg.sender, totalSupply);
    }
    
    // Transfer tokens from the sender to another address.
    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(_to != address(0), "Invalid recipient");
        require(balanceOf[msg.sender] >= _value, "Insufficient balance");
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
        return true;
    }
    
    // Approve an address to spend tokens on your behalf.
    function approve(address _spender, uint256 _value) public returns (bool success) {
        allowance[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
        return true;
    }
    
    // Transfer tokens using the approved allowance.
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(_to != address(0), "Invalid recipient");
        require(balanceOf[_from] >= _value, "Insufficient balance");
        require(allowance[_from][msg.sender] >= _value, "Allowance exceeded");
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        allowance[_from][msg.sender] -= _value;
        emit Transfer(_from, _to, _value);
        return true;
    }
}

Explanation

  • Deployment: Deploy the contract by specifying an initial supply. The total supply is assigned to the deployer's address at deployment.

  • Standard Operations: The contract includes basic token transfer functions (transfer), approval and allowance management (approve and transferFrom), and events to log transfers and approvals.

  • EVM Execution: The contract compiles to EVM bytecode and is executed by the R5 Virtual Machine, ensuring that all operations such as gas calculations and state changes follow the R5 network’s parameters.


Getting Started

  1. Compilation and Deployment: Use a Solidity or Vyper compiler (compatible with version 0.8.x or the version required by your project) along with tools like Remix, Truffle, or Hardhat to compile your contract. Deploy it on the R5 network using your preferred method (e.g. the R5 JS Console or deployment scripts).

  2. Interacting with Contracts: Once deployed, interact with your contract using JSON‑RPC endpoints provided by the R5 node. For example, call functions to transfer tokens or check an account's balance.

  3. Testing and Debugging: Leverage R5’s built-in debugging tools and the JS Console to trace transactions and monitor gas usage. These tools help ensure your contracts execute as expected in the R5 environment.

  4. Optimisation: Use profiling tools to optimise contract code and gas efficiency. Detailed logs and performance metrics are available to help refine your smart contract operations.

PreviousEthash-R5NextWrapped R5 (Native)

Last updated 2 months ago