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

Scripts

PreviousModulesNextScript Arguments

Last updated 4 years ago

Was this helpful?

As already mentioned, dfinance supports transaction scripting. It means users can compile and execute scripts. Different between modules here is that you can't publish script and use it again in the future, each script executing by new transaction every time.

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

Write a script

Let's write a basic script, accepts two arguments, a and b values, and then using module math make a sum from these two numbers and then fire events.

script {
   use 0x1::Event;
   use {{sender}}::Math;

   fun main(account: &signer, a: u64, b: u64) {
      let sum = Math::add(a, b);
      Event::emit(account, sum);
   }
}

Replace {{sender}} with the address you used during publish of the module in the previous part of current documentation.

The script accepts two arguments in function "main", then calculate sum with provided arguments, and fire event with this sum. Both arguments are u64 integers.

Compile the script using dncli:

dncli q vm compile <script file> <address> --to-file <output file>

And then execute with arguments:

dncli tx vm execute <output file> 15 20 --from <my address>

You can verify execution with querying transaction by id.

There will be even fired event, that will contain "keep" status and the resulting sum, like:

[
   {
      "type":"contract_events",
      "attributes":[
         {
            "key":"sender_address",
            "value":"wallet1qjgqxwk55p9ejlupmeza0r02hyextys9rrthgg"
         },
         {
            "key":"source",
            "value":"script"
         },
         {
            "key":"type",
            "value":"u64"
         },
         {
            "key":"data",
            "value":"2300000000000000"
         }
      ]
   },
   {
      "type":"contract_status",
      "attributes":[
         {
            "key":"status",
            "value":"keep"
         }
      ]
   },
   {
      "type":"message",
      "attributes":[
         {
            "key":"action",
            "value":"execute_script"
         },
         {
            "key":"sender",
            "value":"wallet1qjgqxwk55p9ejlupmeza0r02hyextys9rrthgg"
         }
      ]
   },
   {
      "type":"transfer",
      "attributes":[
         {
            "key":"recipient",
            "value":"wallet17xpfvakm2amg962yls6f84z3kell8c5la07d0l"
         },
         {
            "key":"amount",
            "value":"1xfi"
         }
      ]
   }
]
scripts