In a Cadence contract I have a function that has the ability to return the desired optional struct given an address
and a UInt64
.
import C from 0x___
pub fun main(address: Address, id: UInt64): C.ReadOnly? {
return C.fetc(address: address, id: id)
}
From javascript (where CODE
is the above) I can call the above like so:
await fcl.send([
fcl.script(CODE),
fcl.args([
fcl.arg("0x____", t.Address),
fcl.arg(72, t.UInt64)
])
]).then(fcl.decode)
I am trying to turn the above into a batch fetching, where given a list of address
, id
pairs I can return a list of the above results. Which is where I run into my issue that I need help with.
An important additional constraint to keep in mind is there is no guarantee that the address
and id
are unique. Can the smart people here think of a nice way of doing this?
The following cadence is invalid (mixed-type array) but should be a way to convey what I am trying to do
const WANT = [
{ key: "a", value: [addr1, id1] },
{ key: "b", value: [addr2, id2] },
{ key: "c", value: [addr2, id2] },
{ key: "d", value: [addr3, id3] },
]
const CODE = fcl.cdc`
import C from 0x____
pub fun main(want: {String: [Address, UInt64]}): {String: C.ReadOnly?} {
let result: {String: C.ReadOnly?} = {}
for let w in want.keys {
result[w] = C.fetch(address: w[0], id: w[1])
}
return result
}
`
await fcl.send([
fcl.script(CODE),
fcl.args([
fcl.arg(WANT, t.Dictionary({ key: t.String, value: t.Array([t.Address, t.Unit64]) }))
])
]).then(fcl.decode) // Would return something like { a: thing, b: thing, c: null, d: thing }