Define a Resource

In this page, we're defining a resource object and giving it a custom label, "Hello World!".

1

Defining the Resource Object

Let's start by creating a new file that will contain our resource definition.

HelloWorld/
touch Resource.juvix

Next, we preface our Resource.juvix file with the standard module and imports.

Resource.juvix
-- At the start of a Juvix file, we must declare a module with the filename.
module Resource;

-- We import the standard library prelude.
import Stdlib.Prelude open;

-- We import Anoma libraries to access core functionality like `anomaEncode`.
import Anoma open;
import Anoma.Builtin.System open;

We now start defining our resource which we call mkHelloWorldResource by giving it values for logic, label, value, quantity, ephemeral, nonce, randSeed and nullifierKeyCommitment.

Let's start by putting default values for logic and value while assigning TODO to label for now.

Resource.juvix
-- ### Module, imports ###

--- A logic function always returning `true`.
--- @param publicInputs Public inputs to the logic function (not used).
--- @param privateInputs Private inputs to the logic function (not used).
--- @return Whether the logic funtion is valid or not.
logic (publicInputs : Logic.Instance) (privateInputs : Logic.Witness) : Bool := true;

--- Constructs a resource object.
--- @param nonce A nonce ensuring a unique resource commitment hash.
--- @param ephemeral The resource's ephemerality (default: `false`).
--- @return The constructed resource object.
mkHelloWorldResource (nonce : Nonce) {ephemeral : Bool := false} : Resource :=
  mkResource@{
    logic;
    label := TODO; -- We put a TODO here for now.
    value := 0;    -- We don't put any data in here in this example.
  };

What's happening here is that we assign true to the logic, implying that this Resource will not have any logic constraints. Similarly, we assign zero for value which is an arbitrary default.

Let's continue adding missing parameters before getting back to label.

Resource.juvix
-- ### Module, imports, and logic ###

mkHelloWorldResource (nonce : Nonce) {ephemeral : Bool := false} : Resource :=
  mkResource@{
    logic;
    label := TODO;
    value := 0; 
    quantity := 1;
    nonce := nonceToNat nonce;
    ephemeral;
    randSeed := 0;
    nullifierKeyCommitment := 0;
  };

In the above code, we

  • assign quantity of 1

  • pass function parameter nonce

  • pass function parameter ephemeral

  • assign default parameter 0 to randSeed

  • assign default parameter 0 to nullifierKeyCommitment

These default values are artifacts of the current devnet implementation.

2

Add the Label

Let's now add our custom label in Resource.juvix.

Resource.juvix
-- ### Module, imports, and logic / value helper functions ###

label : Nat := anomaEncode "Hello World!";

mkHelloWorldResource (nonce : Nonce) {ephemeral : Bool := false} : Resource :=
  mkResource@{
    logic;
    label; -- We removed the `:= TODO` so that the label defined above is used.
    value := 0;
    quantity := 1;
    nonce := nonceToNat nonce;
    ephemeral;
    randSeed := 0;
    nullifierKeyCommitment := 0;
  };

At this point, our Resource.juvix file is complete

See the complete Resource.juvix file.
Resource.juvix
module Resource;

import Stdlib.Prelude open;
import Anoma open;
import Anoma.Builtin.System open;

--- A logic function always returning `true`.
--- @param publicInputs Public inputs to the logic function (not used).
--- @param privateInputs Private inputs to the logic function (not used).
--- @return Whether the logic funtion is valid or not.
logic (publicInputs : Logic.Instance) (privateInputs : Logic.Witness) : Bool :=
  true;

--- label takes a string and applies anomaEncode to it
--- @return object of type Nat
label : Nat := anomaEncode "Hello world!";

--- Constructs a resource object.
--- @param nonce A nonce ensuring a unique resource commitment hash.
--- @param ephemeral The resource's ephemerality (default: `false`).
--- @return Whether the logic funtion is valid or not.
mkHelloWorldResource (nonce : Nonce) {ephemeral : Bool := false} : Resource :=
  mkResource@{
    logic;
    label;
    value := 0;
    quantity := 1;
    nonce := nonceToNat nonce;
    ephemeral;
    randSeed := 0;
    nullifierKeyCommitment := 0;
  };

Next, we're going to build the transaction function which will be used to initialize the resource object via transaction we manually prepare with our code.

Last updated