How do you pass a struct into FCL?

Daniela
Daniela
  • Updated

To pass a struct into FCL (Flow Client Library), you can use the fcl.query function. This function sends Cadence scripts to the Flow blockchain and receives back decoded responses. The cadence key inside the object sent to the query function is where you can pass Cadence code, including structs.

Here's an example where a struct called Point is defined and used in a script:

import * as fcl from "@onflow/fcl";

const response = await fcl.query({
cadence: `
pub struct Point {
pub var x: Int
pub var y: Int

init(x: Int, y: Int) {
self.x = x
self.y = y
}
}

pub fun main(): [Point] {
return [Point(x: 1, y: 1), Point(x: 2, y: 2)]
}
`
});

console.log(response) // [{x:1, y:1}, {x:2, y:2}]

In this example, a struct called Point is defined with two properties x and y.

The main function then returns an array of Point structs. The fcl.query function sends this script to the Flow blockchain and the response is logged to the console.

Please note that this is a general example and might need adjustments based on your specific use case.

For more detailed information, refer to our FCL documentation.