fuels-ts
fuels-ts copied to clipboard
Improve encoder for Vector<bytes> to support uInt8Array
Motivation
As seen in this forum topic, the encoder for Vector
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;
}
}