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

Improve encoder for Vector<bytes> to support uInt8Array #1471

Closed
cold-briu opened this issue Nov 30, 2023 · 2 comments
Closed

Improve encoder for Vector<bytes> to support uInt8Array #1471

cold-briu opened this issue Nov 30, 2023 · 2 comments
Assignees
Labels
feat Issue is a feature

Comments

@cold-briu
Copy link
Contributor

cold-briu commented Nov 30, 2023

Motivation

As seen in this forum topic, the encoder for Vector expects a regular array and will throw an error when being used with uInt8Array.

This requires the user to manually transform the uInt8Array into a common array.

One thing that we can do when trying to encode Vector of bytes or any similar type that will also applied to this, is to validate if it's a UInt8Array and then transform it to regular Array. But we need to validate this approach first.

Usage example

// current
const hexStrArr = ['0x123, 0xABC', ...]
const parsedData = hexStrArr.map((v) => Array.from(arrayify(v)))
await contract.functions.my_fn(parsedData)

// expected
const hexStrArr = ['0x123, 0xABC', ...]
const parsedData = hexStrArr.map((v) => arrayify(v))
await contract.functions.my_fn(parsedData)

Possible implementations

IDK, but it would be here

export class VectorType extends ArrayType {
  public static swayType = 'struct Vec';

  public name = 'vector';

  static MATCH_REGEX: RegExp = /^struct Vec/m;
  static IGNORE_REGEX: RegExp = /^struct RawVec$/m;

  static isSuitableFor(params: { type: string }) {
    const isAMatch = VectorType.MATCH_REGEX.test(params.type);
    const shouldBeIgnored = VectorType.IGNORE_REGEX.test(params.type);
    return isAMatch && !shouldBeIgnored;
  }

  public parseComponentsAttributes(_params: { types: IType[] }) {
    this.attributes = {
      inputLabel: `Vec`,
      outputLabel: `Vec`,
    };
    return this.attributes;
  }
}
@arboleya arboleya added feat Issue is a feature refactor labels Dec 7, 2023
@arboleya
Copy link
Member

@danielbate Will this still be relevant, or should it be closed in favor of #1672?

@danielbate
Copy link
Member

Closing in favour of #1672

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

No branches or pull requests

4 participants