Join our
Discord!
LogoLogo
SupraScan ExplorerStarKey WalletDiscord
MoveVM
  • Network
  • Oracles
  • Automation
  • SupraNova
  • Guides
MoveVM
  • Overview
  • Getting Started
    • Install Supra CLI with Docker
    • Create a Supra Account
    • Create a Move Package
      • Initialize a Package
      • Write a Module
      • Compile and Publish
      • Interact with a Package
    • Create a dApp with StarKey
  • Network Information
  • Token Standards
  • Learn Move 101
    • Getting Started with Move
    • Unsigned Integers in Move
    • Math Operations in Move
    • Using Vectors in Move
    • Reading Resource Data with borrow_global
    • Passing Data in Move: Value vs. Reference
    • Adding Elements with vector::push_back
    • Emitting Events with event::emit
  • Move Book
    • Getting Started
      • Modules and Scripts
      • Move Tutorial
    • Primitive Types
      • Integers
      • Bool
      • Address
      • Vector
      • Signer
      • References
      • Tuples and Unit
    • Basic Concepts
      • Local Variables and Scope
      • Equality
      • Abort and Assert
      • Conditionals
      • While, For, and Loop
      • Functions
      • Structs and Resources
      • Constants
      • Generics
      • Type Abilities
      • Uses and Aliases
      • Friends
      • Packages
      • Package Upgrades
      • Unit Tests
    • Global Storage
      • Structure
      • Operators
    • Reference
      • Standard Library
      • Coding Conventions
  • TypeScript SDK
    • Guides
      • Create Supra Accounts
      • Publish a Package
    • Documentation
    • Repository
  • Rest API
    • Mainnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Consensus
      • Events
      • Tables
    • Testnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Events
      • Tables
  • Developer Resources
    • Supra Dapp Templates
    • Supra Move VS Code Extension
  • Links
    • Supra DevHub
    • SupraScan Block Explorer
    • StarKey Wallet
    • Live Data Feeds
    • Whitepapers
    • Security Audits
    • Supra's Official GitHub
Powered by GitBook
On this page
Edit on GitHub
  1. Learn Move 101

Reading Resource Data with borrow_global

borrow_global allows you to read an existing resource struct from a specific address without needing a signer, but the function must declare that it accesses the resource using acquires ResourceName.

PreviousUsing Vectors in MoveNextPassing Data in Move: Value vs. Reference

For the latest RPC URL, please refer to the page.

borrow_global: read an existing resource struct

We just need to call borrow_global with the right resource type and address. The address can be passed into the function as an argument or referred to specifically with @ such as @0xcafe. A signer is not required here. If the resource is not found at the address, the function will error when the code is run on the blockchain. The function that calls borrow_global also needs to declare that it does so by adding “acquires ResourceName” at the end.

Let’s write a new function named get_Gendna_digits that returns the Gendna_digits field of the DinosaurGendna resource stored at 0xcafe:

module 0xcafe::Dinosaur_nest {
    use std::vector;

    struct DinosaurGendna has key {
        Gendna_digits: u64,
        Gendna_modulus: u64,
    }

    struct Dinosaur has store {
        Gendna: u64,
    }

    struct DinosaurSwarm has key {
        Dinosaurs: vector<Dinosaur>,
    }

    fun init_module(cafe_signer: &signer) {
        let Gendna_digits = 10;
        let Gendna_modulus = 10 ^ Gendna_digits;
        move_to(cafe_signer, DinosaurGendna {
            Gendna_digits,
            Gendna_modulus: (Gendna_modulus as u256),
        });
        move_to(cafe_signer, DinosaurSwarm {
            Dinosaurs: vector[],
        });
    }

    public fun spawn_Dinosaur(Gendna: u64): Dinosaur {
        Dinosaur {
            Gendna,
        }
    }

public fun get_Gendna_digits(): u64 acquires DinosaurGendna {
borrow_global<DinosaurGendna>(@0xcafe).Gendna_digits
  }
}
Supra Network Information