Qtum Documentation
  • Qtum Features and Advances
    • Qtum Ordinals
      • Chapter 1: Introduction to ordinals
      • Chapter 2: Understanding Inscriptions in Depth
      • Chapter 3: The Intricacies of Ordinals on the Blockchain
      • Chapter 4: Engineering Ordinals within Qtum's Architecture
      • Chapter 5: Prerequisites for Engaging with Ordinals on Qtum
      • Chapter 6: The Procedure for Inscribing a Satoshi with Ordinals
      • Inscribing Messages on Qtum Testnet
    • Qtum-BIP38
  • Qtum Wallet
    • Qtum-Core Wallet Documentation
      • Qtum Documentation
      • Using Qtum Wallet
      • Linux Repositories
      • Qtum on Raspberry Pi
      • How to Update Qtum
      • Qtum Wallet Best Practices
      • Qtum Wallet Commands
      • Adding Nodes To Qtum Wallet
      • Encrypt & Unlock Qtum Wallet
      • Recovery wallet data with salvagewallet
      • Testnet User Guide
      • Bech32 Addresses support
      • How to Add Options (Config)
      • How to use Bootstrap
  • Staking and Nodes
    • Secure Staking With Qtum Star Network
    • Staking Documentation
      • Qtum Staking
      • Offline Staking
      • Offline Staking Address Delegation - Undelegation Transaction Details
      • Qtum on AWS ec2
      • Staking with a VPS
      • Staking with a Raspberry Pi
  • Other Wallets
    • Wallets Supporting Qtum
      • Electrum
      • Qtum Web Wallet
      • Qtum Lightning Network
  • RPC CALLS AND API
    • QTUM RPC CALLS
    • QTUM RPC API
  • Qtum Deployment
    • Guidance on Qtum Deployment
    • Building Qtum on Linux
    • Guidance for Exchange Deployment
  • Smart Contracts and QRC20 Tokens
    • QRC20 Token integration
    • QRC20 Integration Technical Guide
    • Raw QRC20 Transaction implementation guide
  • WEB3
    • Web3 Compatibility Layer
    • Janus Docker Container
    • Truffle
    • Differences between EVM chains
  • QNEKT
    • QNEKT
    • Why Fork Metamask ?
    • Sideloading Qnekt
    • Setting up QNEKT
    • Working with Testnet Coins on QNEKT
    • Connecting QNEKT to regtest
    • QNEKT Regtest with react-box
  • Research
    • Confidential Assets
    • Data Analytics
Powered by GitBook
On this page
  • QRC20 Integration Technical Guide
  • Intro
  • Running QTUMD
  • Interacting With QRC20
  • Getting Token Balance
  • Withdraw
  • Generate a deposit address
  • Deposit and Witdraw Logs
  • Checking Confirmations
  1. Smart Contracts and QRC20 Tokens

QRC20 Integration Technical Guide

PreviousQRC20 Token integrationNextRaw QRC20 Transaction implementation guide

Last updated 1 year ago

QRC20 Integration Technical Guide

This document explains the basics of interacting with an QRC20 contract with the qtumd CLI command tool.

The full example in PHP: .

Intro

We recommend that an exchange use one main address to store tokens for all users. Below MAIN_QRC_ADDRESS is the main address.

The address of the QRC20 token contract is TOKEN_CONTRACT_ADDRESS.

  • gas limit is DEFAULT_GAS_LIMIT, recommended value is 250000

  • gas price is DEFAULT_GAS_PRICE,recommended value is 0.00000040

TOKEN_DECIMALS is the decimals of the token, which varies per token. Here we set it 8.

We'll use the following utility functions:

  • - Convert base58 address to hash160 address.

  • left-pad hex data to 32 bytes with 0.

  • multiply the nominal token amount by the number of decimal places.

Running QTUMD

You should remember to enable the indexing service when starting qtumd, using the flags -logevents -txindex.

Interacting With QRC20

In the CLI examples below, please substitute {} with actual addresses and values.

Getting Token Balance

  • $userAddress is the deposit address

qtum-cli callcontract \
    {TOKEN_CONTRACT_ADDRESS} \
    70a08231{to32bytesArg(addressToHash160($userAddress))}

In the JSON output look for executionResult.output. This is the token balance.

Withdraw

  • $userAddress is the withdraw address

  • $amount the withdraw amount in unit token

qtum-cli sendtocontract \
    {TOKEN_CONTRACT_ADDRESS} \
    a9059cbb{to32bytesArg(addressToHash160($userAddress))}{to32bytesArg(addDecimals($amount)) \
    0 \
    {DEFAULT_GAS_LIMIT} \
    {DEFAULT_GAS_PRICE} \
    {MAIN_QRC_ADDRESS}

The command returns the txid of this transaction. You may use it to find information about this transaction (e.g. number of confirmations).

Generate a deposit address

For an exchange, a user may use the same address for both QTUM and other QRC20 tokens.

Use this command to generate a deposit address:

qtum-cli getnewaddress

Deposit and Witdraw Logs

  • $startingBlock is where you want to start looking (inclursive)

    • You can start looking from 0, but for better efficiency, you should remember where to start looking.

qtum-cli searchlogs \
    STARTING_BLOCK \
    999999999 \
    '{ "addresses": ["TOKEN_CONTRACT_ADDRESS"]}' \
    '{"topics": ["ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}'

The event type filtered is the Transfer event:

event Transfer(address indexed _from, address indexed _to, uint256 _value)

Look through the logs to filter by to or from addresses.

Checking Confirmations

Given a transaction id $txid:

qtum-cli gettransaction $txid

Use the property confirmations from output.

Qrc20.php
addressToHash160
to32bytesArg
addDecimals