Skip to content

Conversation

ernest-nowacki
Copy link
Contributor

Evolution of helpers, the last one is what you can use if this PR gets merged.

No helpers, everything inline

const onCronTrigger = async (env: Environment<Config>): Promise<void> => {
  env.logger?.log("Hello, Calculator! Workflow triggered.");

  const aggregatedValue = await runInNodeMode(async () => {
    const http = new cre.capabilities.HTTPClient();
    const resp = await http.sendRequest({
      url: env.config?.apiUrl,
      method: "GET",
    });

    const bodyStr = new TextDecoder().decode(resp.body);
    const num = Number.parseFloat(bodyStr.trim());

    return create(SimpleConsensusInputsSchema, {
      observation: observationValue(val.float64(num)),
      descriptors: consensusDescriptorMedian,
    });
  });

  sendResponseValue(val.mapValue({ Result: aggregatedValue }));
};

No helpers, split logic into smaller functions

// This is where you fetch your offchain data
const fetchMathResult = async (config: Config) => {
  const response = await cre.utils.fetch({
    url: config.apiUrl,
  });
  return Number.parseFloat(response.body.trim());
};

// This is how you guide nodes how to agree on a result
const fetchAggregatedResult = async (config: Config) =>
  cre.runInNodeMode(async () => {
    const result = await fetchMathResult(config);
    return cre.utils.consensus.getAggregatedValue(
      cre.utils.val.float64(result),
      "median"
    );
  });

// This is your handler which will perform the desired action
const onCronTrigger = async (env: Environment<Config>) => {
  const aggregatedValue = await fetchAggregatedResult(env.config);
  cre.sendResponseValue(cre.utils.val.mapValue({ Result: aggregatedValue }));
};

Helpers + smaller functions

const fetchMathResult = useMedianConsensus(async (config: Config) => {
  const response = await cre.utils.fetch({
    url: config.apiUrl,
  });
  return Number.parseFloat(response.body.trim());
}, "float64");

const onCronTrigger = async (env: Environment<Config>) => {
  const aggregatedValue = await fetchMathResult(env.config);
  cre.sendResponseValue(cre.utils.val.mapValue({ Result: aggregatedValue }));
};

Copy link

👋 ernest-nowacki, thanks for creating this pull request!

To help reviewers, please consider creating future PRs as drafts first. This allows you to self-review and make any final changes before notifying the team.

Once you're ready, you can mark it as "Ready for review" to request feedback. Thanks!

export { AggregationType } from "@cre/generated/sdk/v1alpha/sdk_pb";

const consensusAggregators = [
"median",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these exist as generated constants from the protos?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protos exposes these as ENUM with numeric values which is not perfect :S but yeah it's called AggregationType I believe.

// main is the entry point for the workflow
export async function main() {
try {
const runner = await cre.newRunner<Config>({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think workflows are starting to look a lot better and really take shape.

@ernest-nowacki ernest-nowacki merged commit 6eaff58 into main Aug 22, 2025
5 checks passed
@ernest-nowacki ernest-nowacki deleted the feat/decorators-1 branch August 22, 2025 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants