docs
  • Introduction
  • Getting started
    • Meet the network using CLI
    • Your first transaction
    • Run smart contract
    • Run your own node
    • Ledger Support
  • Architecture
    • Dnode
    • Dncli
    • XFI & Other coins
    • Fees & Gas
    • Addresses
  • Staking
    • Delegate sXFI & LPT
    • Become a validator
    • Rewards & Inflation
    • Slashing
    • More
  • Move VM
    • Modules
    • Scripts
    • Script Arguments
    • Standard Library
    • Events
    • Resources
    • Move Book
    • More
  • Oracles
    • Query Price
  • PegZone
    • Deposit
    • Usage
    • Withdraw
  • Useful Resources
    • Dfinance Website
    • Wallet
    • Move Book
    • Block Explorer
    • Swagger UI
    • Community
    • VSCode Move IDE
Powered by GitBook
On this page

Was this helpful?

  1. Move VM

Modules

PreviousMove VMNextScripts

Last updated 4 years ago

Was this helpful?

There are two types of smart contracts in dfinance: module and script.

Difference between them that module is published into blockchain storage and is stored under the publisher account, while script is simply a transaction-as-script and can only operate with existing modules.

The Move Book also has a section about in Move language.

Write a module

Let's see an example of a small module that will just add two numbers (a and b):

module Math {
    public fun add(a: u64, b: u64): u64 {
        a + b
    }
}

Let's compile this module using dncli. Compiler requires sender's address as it's included into bytecode. This address will then be verified on module publish.

dncli q vm compile <path-to-mvir> <address> --to-file <output file>

Replace variables in this pattern with your own and you will get a compiled module in the specified output file. When it's done, you can publish your module:

dncli tx vm publish <output file> --from <account>

Check your transaction by querying its id, which was returned in the output.

dncli q tx <id>

If you see a contract_status event, with status keep inside, everything published fine!

When it's done your module is be published under your address, and you and other users can access it in their modules or scripts:

use {{address}}::Math;

Just replace {{address}} with yours and you can use this module.

modules