DEV Community

Max Daunarovich for Flow Blockchain

Posted on • Edited on

5 4

Build on FLOW | Learn FCL - 3. How to Return Custom Value from Script

Overview

In this guide you will learn how to return value containing multiple other values that doesn’t share the same type. We will do this by defining and using custom Cadence Struct.

  • define custom Struct in the body of the script
  • create instance of aforementioned Struct and use it as return value of the script

💡 Learn better with video? Lucky for you there is a video you can follow along w/ this guide. Narrated by one and only Flow Developer Advocate Kimcodeashian!

Kimazing Content

Previously on "Learn FCL"

In the previous post, we learned how to pass arguments to our Cadence script. But if you recall from one of the examples - passMultipleDifferentTypes - we could only return a single value, even though we passed in multiple different types.

Today, I will show you how you can define a custom Struct in the body of your script and then use it as a return type.

Let’s begin! 💪

Step 1 - Installation

Add "@onflow/fcl": "1.0.0" as your dependency

Step 2 - Setup

Just like the last time, we will import the necessary methods and setup FCL:

import { query, config } from "@onflow/fcl";

const api = "https://rest-testnet.onflow.org";
config().put("accessNode.api", api);
Enter fullscreen mode Exit fullscreen mode

Step 3 - Implement fetchCustom method

It is allowed define new Structs in the body of the script. We will create one and call it Custom(use can use any name you want)

const fetchCustom = async (name) => {
  const cadence = `
    // We will define custom Struct with name Custom :)
    pub struct Custom {

      pub let number: Int
      pub let address: Address

      // underscore before name of the arguments will allow us to pass values
      // without specifying the name of the arguments, which is usefull when
      // you have only a few of them

      init(number: Int, address: Address){
        self.number = number
        self.address = address
      }
    }

    pub fun main():Custom {
        // in order to create instance of a struct you use it's name
        // and pass initialization arguments in parentheses
        let t = Custom(number: 42, address: 0x1337)
        return t
    }
  `;

  const custom = await query({ cadence });

  const { number, address } = custom;
  console.log(`number field is ${number}`)
  console.log(`address field is ${address}`)
};
Enter fullscreen mode Exit fullscreen mode

Finally

As always, let’s execute the code with IIFE:

(async () => {
  console.clear();
  await fetchCustom();
})();
Enter fullscreen mode Exit fullscreen mode

You should see the following result in the console:

number field is: 42
address field is: 0x0000000000001337
Enter fullscreen mode Exit fullscreen mode

Did you get lost 🤷‍♂️? No worries 😉 - checkout the full code on Codesandbox

Next time we will put our aquired skills to use and use .find deployed contract to convert .find identity name to address.

Until next time 👋

Resources

Other resources you might find useful:

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay