onflow / flixkit-go

Apache License 2.0
0 stars 7 forks source link

Generate fcl-js binding files #8

Open bthaile opened 1 year ago

bthaile commented 1 year ago

Given a interactive template (flix) need to create binding files. Task should address local development.

In current Cadence vs code project structure, bindings files can be generated to "bindings" folder by default. For CLI configuration user passes in "Cadence" folder and all transactions and scripts will be discovered and templates generated to "templates" directory and binding generated to "bindings" folder.

Tasks:

Sub Tasks:

Note:

Reference: Cadut uses handlebar templates to generate javascript files.

Binding should have a hash template interface (arguments) and verify the hash has not changed at runtime.

Example javascript binding that uses a URL to flix

import * as fcl from @onflow/fcl

const params =  { "amount": "UFix64", "to": "Address"}
const templateURL = "https://flix.flow.com/v1/templates?name=transfer-flow"

export async function TemplateName({to, amount }) {
  // check argument hash
  let hashMatches = false;
  let hasError = false;
  try {
        const response = await fetch(templateURL);
        // Check if the request was successful
        if (response.ok) {
          const templateData = await response.json();  // Convert response to JSON
          doesMatches = matchParams(templateData.parameters, params)
        } else {
          throw new Error(`Failed to fetch: ${response.status} ${response.statusText}`);
        }
   } catch (error) {
      console.error(error)  // need better way to convey errors than console log and eat
      hasError = true
  } 
  if (!doesMatches) return 
   return request({to, amount});
}

async function request({ to, amount }) {
  const transactionId = await fcl.mutate({
    template: templateURL,
    args: (arg, t) => ([ arg(to, t.Address), arg(amount, t.UFix64)])
  })

  return transactionId
}
chasefleming commented 1 year ago

I'm not sure TS or JS bindings should be generated in this library. I'm wondering if this should be a separate lib (maybe even a TS lib).

bjartek commented 1 year ago

Go has the ability to parse the program and get out information from it so i think go is the right choixe here. The code should also support generating go bindings.

bthaile commented 1 year ago

@chasefleming I was thinking this flixkit-go would be all things flix.