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 - Nested structs, undefined structs, ByteArray #2738

Open
starknetdev opened this issue Nov 29, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@starknetdev
Copy link
Contributor

Describe the bug
Some issues in my generated bindings other than those listed in #2715.

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:

Nested structs

It seems the bindings produced in models.gen.ts do not define types in the Schema when nested.

Current behavior

    AdventurerModel: {
      fieldOrder: ["adventurer_id", "adventurer"],
      adventurer_id: 0,
      adventurer: {
        fieldOrder: [
          "health",
          "xp",
          "gold",
          "beast_health",
          "stat_upgrades_available",
          "stats",
          "equipment",
          "battle_action_count",
          "mutated",
          "awaiting_item_specials",
        ],
        health: 0,
        xp: 0,
        gold: 0,
        beast_health: 0,
        stat_upgrades_available: 0,
        stats: Stats,
        equipment: Equipment,
        battle_action_count: 0,
        mutated: false,
        awaiting_item_specials: false,
      },
    },

Expected behavior

    AdventurerModel: {
      fieldOrder: ["adventurer_id", "adventurer"],
      adventurer_id: 0,
      adventurer: {
        fieldOrder: [
          "health",
          "xp",
          "gold",
          "beast_health",
          "stat_upgrades_available",
          "stats",
          "equipment",
          "battle_action_count",
          "mutated",
          "awaiting_item_specials",
        ],
        health: 0,
        xp: 0,
        gold: 0,
        beast_health: 0,
        stat_upgrades_available: 0,
        stats: {
          fieldOrder: [
            "strength",
            "dexterity",
            "vitality",
            "intelligence",
            "wisdom",
            "charisma",
            "luck",
          ],
          strength: 0,
          dexterity: 0,
          vitality: 0,
          intelligence: 0,
          wisdom: 0,
          charisma: 0,
          luck: 0,
        },
        equipment: {
          fieldOrder: [
            "weapon",
            "chest",
            "head",
            "waist",
            "foot",
            "hand",
            "neck",
            "ring",
          ],
          weapon: { fieldOrder: ["id", "xp"], id: 0, xp: 0 },
          chest: { fieldOrder: ["id", "xp"], id: 0, xp: 0 },
          head: { fieldOrder: ["id", "xp"], id: 0, xp: 0 },
          waist: { fieldOrder: ["id", "xp"], id: 0, xp: 0 },
          foot: { fieldOrder: ["id", "xp"], id: 0, xp: 0 },
          hand: { fieldOrder: ["id", "xp"], id: 0, xp: 0 },
          neck: { fieldOrder: ["id", "xp"], id: 0, xp: 0 },
          ring: { fieldOrder: ["id", "xp"], id: 0, xp: 0 },
        },
        battle_action_count: 0,
        mutated: false,
        awaiting_item_specials: false,
      },
    },

Undefined input structs

I have one case in my contracts.gen.ts where an input struct is not defined (it's not stored in a model).

Current behavior

  const tournament_mock_registerTokens = async (
    snAccount: Account,
    tokens: Array<Token>
  ) => {
    try {
      return await provider.execute(
        snAccount,
        {
          contractName: "tournament_mock",
          entrypoint: "register_tokens",
          calldata: [tokens],
        },
        "tournament"
      );
    } catch (error) {
      console.error(error);
    }
  };

Expected Behaviour

interface Token {
    token: string
    token_data_type: models.TokenDataType
}

const tournament_mock_registerTokens = async (
  snAccount: Account,
  tokens: Array<Token>
) => {
  try {
    return await provider.execute(
      snAccount,
      {
        contractName: "tournament_mock",
        entrypoint: "register_tokens",
        calldata: [tokens],
      },
      "tournament"
    );
  } catch (error) {
    console.error(error);
  }
};

ByteArray

Currently inputs for ByteArray are typed as string in contracts.gen.ts. This is correct for models.gen.ts as they are indexed as so, however for call inputs this results in deserialization errors.

Current behavior

  const tournament_mock_createTournament = async (
    snAccount: Account,
    name: BigNumberish,
    description: string,
    startTime: BigNumberish,
    endTime: BigNumberish,
    submissionPeriod: BigNumberish,
    winnersCount: BigNumberish,
    gatedType: models.Option,
    entryPremium: models.Option
  ) => {
    try {
      return await provider.execute(
        snAccount,
        {
          contractName: "tournament_mock",
          entrypoint: "create_tournament",
          calldata: [
            name,
            description,
            startTime,
            endTime,
            submissionPeriod,
            winnersCount,
            gatedType,
            entryPremium,
          ],
        },
        "tournament"
      );
    } catch (error) {
      console.error(error);
    }
  };

Expected behavior

  const tournament_mock_createTournament = async (
  account: Account,
  name: BigNumberish,
  description: ByteArray,
  startTime: BigNumberish,
  endTime: BigNumberish,
  submissionPeriod: BigNumberish,
  winnersCount: BigNumberish,
  gatedType: CairoOption<GatedTypeEnum>,
  entryPremium: CairoOption<Premium>
) => {
  try {
    return await provider.execute(
      account,
      {
        contractName: "tournament_mock",
        entrypoint: "create_tournament",
        calldata: [
          name,
          description,
          startTime,
          endTime,
          submissionPeriod,
          winnersCount,
          gatedType,
          entryPremium,
        ],
      },
      "tournament"
    );
  } catch (error) {
    console.error(error);
  }
};

Where ByteArray is generated by:

import { byteArray} from "starknet"

const byteArrayDescription = byteArray.byteArrayFromString(tournamentDescription)
@starknetdev starknetdev added the bug Something isn't working label Nov 29, 2024
@starknetdev starknetdev changed the title [BUG] Typescript bindings [BUG] Typescript bindings - Nested structs, undefined structs, ByteArray Nov 29, 2024
@starknetdev
Copy link
Contributor Author

starknetdev commented Dec 1, 2024

Last one I noticed, thought I'd add to this PR.

U256

Similar to ByteArray the model type is correct but the contracts.gen.ts needs to have a U256 type.

Current Behaviour

  const erc721_mock_approve = async (
    snAccount: Account,
    to: string,
    tokenId: BigNumberish
  ) => {
    try {
      return await provider.execute(
        snAccount,
        {
          contractName: "erc721_mock",
          entrypoint: "approve",
          calldata: [to, tokenId],
        },
        "tournament"
      );
    } catch (error) {
      console.error(error);
    }
  };

Expected Behaviour

interface U256 {
  low: BigNumberish;
  high: BigNumberish;
}

const erc721_mock_approve = async (
  account: Account,
  to: string,
  tokenId: U256
) => {
  try {
    return await provider.execute(
      account,
      {
        contractName: "erc721_mock",
        entrypoint: "approve",
        calldata: [to, tokenId],
      },
      "tournament"
    );
  } catch (error) {
    console.error(error);
  }
};

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

1 participant