Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Typescript bindings fieldOrder #2748

Closed
starknetdev opened this issue Dec 2, 2024 · 3 comments
Closed

[BUG] Typescript bindings fieldOrder #2748

starknetdev opened this issue Dec 2, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@starknetdev
Copy link
Contributor

Describe the bug
fieldOrder is added to all models and struct types stored within the models in models.gen.ts which I assume is necessary for querying torii. The current problem is that contracts.gen.ts references these for inputs, but fieldOrder should not be present otherwise there are deserialization errors.

To Reproduce

  • Clone tournament_component
  • run, cd tournaments && cd contracts && sozo build --typescript
  • inspect, contracts/bindings/typescript/models.gen.ts + contracts.gen.ts

or find the file here:

Current Behaviour
models.gen.ts

export interface Adventurer {
  fieldOrder: string[];
  ...fields
}

contracts.gen.ts

  const loot_survivor_mock_setAdventurer = async (
    adventurerId: BigNumberish,
    adventurer: models.Adventurer
  ) => {
    try {
      return await provider.call("tournament", {
        contractName: "loot_survivor_mock",
        entrypoint: "set_adventurer",
        calldata: [adventurerId, adventurer],
      });
    } catch (error) {
      console.error(error);
    }
  };

Expected behavior

The binding can add a type that removes fieldOrder (and from nested objects), or handle this is in the generation process.

models.gen.ts

export interface Adventurer {
  fieldOrder: string[];
  ...fields
}

// Utility type to recursively remove fieldOrder from all nested objects
type RemoveFieldOrder<T> = T extends object
  ? Omit<
      {
        [K in keyof T]: T[K] extends object ? RemoveFieldOrder<T[K]> : T[K];
      },
      'fieldOrder'
    >
  : T;

// Create a new type without fieldOrder
type InputAdventurer = RemoveFieldOrder<Adventurer>;

contracts.gen.ts

  const loot_survivor_mock_setAdventurer = async (
    adventurerId: BigNumberish,
    adventurer: models.InputAdventurer
  ) => {
    try {
      return await provider.call("tournament", {
        contractName: "loot_survivor_mock",
        entrypoint: "set_adventurer",
        calldata: [adventurerId, adventurer],
      });
    } catch (error) {
      console.error(error);
    }
  };
@starknetdev starknetdev added the bug Something isn't working label Dec 2, 2024
@MartianGreed
Copy link
Contributor

Thank's for this valuable feedbac, will add this to current PR

@rsodre
Copy link

rsodre commented Dec 5, 2024

or maybe fieldOrder can be removed from all models, and use a generic type when needed, like this:

export interface Score {
  points: number;
};

export type ModelWithFieldOrder<T> = T & {
  fieldOrder: string[];
};

const _score: Score = {
  points: 0,
};
const _score_with_field_order: ModelWithFieldOrder<Score> = {
  fieldOrder: ['points'],
  ..._score,
};

@glihm
Copy link
Collaborator

glihm commented Dec 6, 2024

Merged in #2773.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants