Skip to content

Commit

Permalink
fix: re-add concat function that accepts BytesLike (#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizAsFight authored Oct 23, 2023
1 parent 2e170af commit 1bd2a4f
Show file tree
Hide file tree
Showing 23 changed files with 148 additions and 114 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-actors-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/utils": patch
---

`concatBytes` now accept `BytesLike` as input.
5 changes: 4 additions & 1 deletion internal/check-imports/src/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { FUEL_NETWORK_URL } from '@fuel-ts/wallet/configs';
// TODO: Add `launchNode` and `launchNodeAndGetWallets` here
import { generateTestWallet, seedTestWallet } from '@fuel-ts/wallet/test-utils';
import { english, Language } from '@fuel-ts/wordlists';
import { ScriptRequest, chunkAndPadBytes, normalizeString, concatBytes } from 'fuels';
import { ScriptRequest, chunkAndPadBytes, normalizeString, concatBytes, concat, arrayify, hexlify } from 'fuels';

const { log } = console;

Expand Down Expand Up @@ -142,6 +142,9 @@ log(InputCoinCoder);
log(chunkAndPadBytes);
log(normalizeString);
log(concatBytes);
log(concat);
log(arrayify);
log(hexlify);

/**
* versions
Expand Down
4 changes: 2 additions & 2 deletions packages/abi-coder/src/coders/byte.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorCode } from '@fuel-ts/errors';
import { bn } from '@fuel-ts/math';
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';

import { WORD_SIZE } from '../constants';
import type { Uint8ArrayWithDynamicData } from '../utilities';
Expand Down Expand Up @@ -50,7 +50,7 @@ export class ByteCoder extends Coder<number[], Uint8Array> {
data.push(new Uint8Array(paddingLength));
}

return concatBytes(data);
return concat(data);
}

decode(data: Uint8Array, offset: number): [Uint8Array, number] {
Expand Down
4 changes: 2 additions & 2 deletions packages/abi-coder/src/coders/enum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { toNumber } from '@fuel-ts/math';
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';
import type { RequireExactlyOne } from 'type-fest';

import { concatWithDynamicData } from '../utilities';
Expand Down Expand Up @@ -50,7 +50,7 @@ export class EnumCoder<TCoders extends Record<string, Coder>> extends Coder<
const caseIndex = Object.keys(this.coders).indexOf(value);

const padding = new Uint8Array(this.#encodedValueSize - valueCoder.encodedLength);
return concatBytes([this.#caseIndexCoder.encode(caseIndex), padding, encodedValue]);
return concat([this.#caseIndexCoder.encode(caseIndex), padding, encodedValue]);
}

encode(value: InputValueOf<TCoders>): Uint8Array {
Expand Down
4 changes: 2 additions & 2 deletions packages/abi-coder/src/coders/stdString.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bn } from '@fuel-ts/math';
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';
import { toUtf8Bytes, toUtf8String } from 'ethers';

import { WORD_SIZE } from '../constants';
Expand Down Expand Up @@ -46,7 +46,7 @@ export class StdStringCoder extends Coder<string, string> {
data.push(new Uint8Array(paddingLength));
}

return concatBytes(data);
return concat(data);
}

decode(data: Uint8Array, offset: number): [string, number] {
Expand Down
4 changes: 2 additions & 2 deletions packages/abi-coder/src/coders/string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ErrorCode } from '@fuel-ts/errors';
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';
import { toUtf8Bytes, toUtf8String } from 'ethers';

import { Coder } from './abstract-coder';
Expand All @@ -23,7 +23,7 @@ export class StringCoder<TLength extends number = number> extends Coder<string,

const encoded = toUtf8Bytes(value);
const padding = new Uint8Array(this.#paddingLength);
return concatBytes([encoded, padding]);
return concat([encoded, padding]);
}

decode(data: Uint8Array, offset: number): [string, number] {
Expand Down
34 changes: 11 additions & 23 deletions packages/abi-coder/src/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';

import type { Uint8ArrayWithDynamicData } from './utilities';
import { unpackDynamicData, concatWithDynamicData } from './utilities';
Expand All @@ -9,7 +9,7 @@ describe('Abi Coder Utilities', () => {
const data2 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const data3 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const data4 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 16]);
const EXPECTED = concatBytes([data1, data2, data3, data4]);
const EXPECTED = concat([data1, data2, data3, data4]);

const RESULT = concatWithDynamicData([data1, data2, data3, data4]);
expect(RESULT).toEqual(EXPECTED);
Expand All @@ -21,7 +21,7 @@ describe('Abi Coder Utilities', () => {
const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const someData = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 16]);
const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([pointer, capacity, length, someData]);
const EXPECTED: Uint8ArrayWithDynamicData = concat([pointer, capacity, length, someData]);
EXPECTED.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 36]) };

const RESULT = concatWithDynamicData([pointer, capacity, length, someData]);
Expand All @@ -36,7 +36,7 @@ describe('Abi Coder Utilities', () => {
const pointer = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 24]);
const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([
const EXPECTED: Uint8ArrayWithDynamicData = concat([
pointer,
capacity,
length,
Expand All @@ -49,7 +49,7 @@ describe('Abi Coder Utilities', () => {
3: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 36]),
};

const arrayWithVectorData: Uint8ArrayWithDynamicData = concatBytes([pointer, capacity, length]);
const arrayWithVectorData: Uint8ArrayWithDynamicData = concat([pointer, capacity, length]);
arrayWithVectorData.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 36]) };

const RESULT = concatWithDynamicData([arrayWithVectorData, arrayWithVectorData]);
Expand All @@ -65,7 +65,7 @@ describe('Abi Coder Utilities', () => {
const pointer = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 24]);
const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([
const EXPECTED: Uint8ArrayWithDynamicData = concat([
pointer,
capacity,
length,
Expand All @@ -82,21 +82,9 @@ describe('Abi Coder Utilities', () => {
6: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 37]),
};

const arrayWithVectorData1: Uint8ArrayWithDynamicData = concatBytes([
pointer,
capacity,
length,
]);
const arrayWithVectorData2: Uint8ArrayWithDynamicData = concatBytes([
pointer,
capacity,
length,
]);
const arrayWithVectorData3: Uint8ArrayWithDynamicData = concatBytes([
pointer,
capacity,
length,
]);
const arrayWithVectorData1: Uint8ArrayWithDynamicData = concat([pointer, capacity, length]);
const arrayWithVectorData2: Uint8ArrayWithDynamicData = concat([pointer, capacity, length]);
const arrayWithVectorData3: Uint8ArrayWithDynamicData = concat([pointer, capacity, length]);
arrayWithVectorData1.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 33]) };
arrayWithVectorData2.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 35]) };
arrayWithVectorData3.dynamicData = { 0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 37]) };
Expand Down Expand Up @@ -124,7 +112,7 @@ describe('Abi Coder Utilities', () => {
const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const someData = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 16]);
const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([
const EXPECTED: Uint8ArrayWithDynamicData = concat([
pointerA,
capacity,
length,
Expand Down Expand Up @@ -169,7 +157,7 @@ describe('Abi Coder Utilities', () => {
const capacity = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const length = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4]);
const data = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 16]);
const EXPECTED: Uint8ArrayWithDynamicData = concatBytes([
const EXPECTED: Uint8ArrayWithDynamicData = concat([
otherData,
pointer,
capacity,
Expand Down
6 changes: 3 additions & 3 deletions packages/abi-coder/src/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';
import { getBytesCopy, type BytesLike } from 'ethers';

import { U64Coder } from './coders/u64';
Expand Down Expand Up @@ -61,7 +61,7 @@ export function unpackDynamicData(
dataOffset: number
): Uint8Array {
if (!results.dynamicData) {
return concatBytes([results]);
return concat([results]);
}

let cumulativeDynamicByteLength = 0;
Expand All @@ -83,7 +83,7 @@ export function unpackDynamicData(
dataOffset + vData.byteLength + cumulativeDynamicByteLength
)
: vData;
updatedResults = concatBytes([updatedResults, dataToAppend]);
updatedResults = concat([updatedResults, dataToAppend]);

cumulativeDynamicByteLength += dataToAppend.byteLength;
});
Expand Down
16 changes: 8 additions & 8 deletions packages/abi-coder/test/interface.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { BN } from '@fuel-ts/math';
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';

import { NumberCoder, WORD_SIZE, Interface } from '../src';
import type { JsonAbiConfigurable } from '../src/json-abi';
Expand Down Expand Up @@ -28,7 +28,7 @@ import {
} from './utils/constants';

function encodeVectorFully(encodedData: Uint8Array[] | Uint8Array, offset: number) {
const data = encodedData instanceof Uint8Array ? encodedData : concatBytes(encodedData);
const data = encodedData instanceof Uint8Array ? encodedData : concat(encodedData);
const dataLength = data.length / 8;
const length = new NumberCoder('u8').encode(dataLength);
const capacity = length;
Expand All @@ -37,7 +37,7 @@ function encodeVectorFully(encodedData: Uint8Array[] | Uint8Array, offset: numbe
return {
offset,
length: dataLength,
vec: concatBytes([o, length, capacity]),
vec: concat([o, length, capacity]),
data,
};
}
Expand Down Expand Up @@ -589,7 +589,7 @@ describe('Abi interface', () => {
const data1Vec2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[1][0]]);
const data2Vec2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[1][1]]);
const data3Vec2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[1][2]]);
const expectedBytes = concatBytes([
const expectedBytes = concat([
// top level vector
pointer,
capacity,
Expand Down Expand Up @@ -629,7 +629,7 @@ describe('Abi interface', () => {

const data1 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[0][0]]);
const data2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input[0][1]]);
const expectedBytes = concatBytes([pointer, capacity, length, data1, data2]);
const expectedBytes = concat([pointer, capacity, length, data1, data2]);

return expectedBytes;
},
Expand All @@ -655,7 +655,7 @@ describe('Abi interface', () => {
const data2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[1]]);
const data3 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[2]]);
const data4 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[3]]);
const expectedBytes = concatBytes([
const expectedBytes = concat([
enumCaseOne,
pointer,
capacity,
Expand Down Expand Up @@ -690,7 +690,7 @@ describe('Abi interface', () => {
const data2 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[1]]);
const data3 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[2]]);
const data4 = Uint8Array.from([0, 0, 0, 0, 0, 0, 0, input.vec[3]]);
const expectedBytes = concatBytes([
const expectedBytes = concat([
u8,
pointer,
capacity,
Expand All @@ -715,7 +715,7 @@ describe('Abi interface', () => {
const encodedVal =
encodedValue instanceof Function ? encodedValue(value, offset) : encodedValue;
const expectedEncoded =
encodedVal instanceof Uint8Array ? encodedVal : concatBytes(encodedVal);
encodedVal instanceof Uint8Array ? encodedVal : concat(encodedVal);

expect(encoded).toEqual(expectedEncoded);

Expand Down
3 changes: 3 additions & 0 deletions packages/fuels/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ describe('index.js', () => {
expect(fuels.ScriptResultDecoderError).toBeTruthy();
expect(fuels.Script).toBeTruthy();
expect(fuels.FunctionInvocationScope).toBeTruthy();
expect(fuels.arrayify).toBeTruthy();
expect(fuels.hexlify).toBeTruthy();
expect(fuels.concat).toBeTruthy();
});
});
8 changes: 4 additions & 4 deletions packages/program/src/contract-call-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
TransactionResultReturnReceipt,
} from '@fuel-ts/providers';
import { ReceiptType } from '@fuel-ts/transactions';
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';
import * as asm from '@fuels/vm-asm';
import { getBytesCopy } from 'ethers';

Expand Down Expand Up @@ -160,7 +160,7 @@ const scriptResultDecoder =
const nextReturnData: TransactionResultReturnDataReceipt = filtered[
index + 1
] as TransactionResultReturnDataReceipt;
return concatBytes([encodedScriptReturn, getBytesCopy(nextReturnData.data)]);
return concat([encodedScriptReturn, getBytesCopy(nextReturnData.data)]);
}

return [encodedScriptReturn];
Expand Down Expand Up @@ -288,12 +288,12 @@ export const getContractCallScript = (
scriptData.push(args);

// move offset for next call
segmentOffset = dataOffset + concatBytes(scriptData).byteLength;
segmentOffset = dataOffset + concat(scriptData).byteLength;
}

// get asm instructions
const script = getInstructions(paramOffsets, outputInfos);
const finalScriptData = concatBytes(scriptData);
const finalScriptData = concat(scriptData);
return { data: finalScriptData, script };
},
() => [new Uint8Array()]
Expand Down
4 changes: 2 additions & 2 deletions packages/program/src/instruction-set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';
import type * as asm from '@fuels/vm-asm';
import { hexlify } from 'ethers';

Expand Down Expand Up @@ -29,7 +29,7 @@ export class InstructionSet {
}

toBytes(): Uint8Array {
return concatBytes(
return concat(
this.#operations.reduce((instructions, line) => {
instructions.push(line.to_bytes());
return instructions;
Expand Down
4 changes: 2 additions & 2 deletions packages/transactions/src/coders/byte-array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Coder } from '@fuel-ts/abi-coder';
import { concatBytes } from '@fuel-ts/utils';
import { concat } from '@fuel-ts/utils';
import { hexlify, getBytesCopy } from 'ethers';
import type { BytesLike } from 'ethers';

Expand Down Expand Up @@ -33,7 +33,7 @@ export class ByteArrayCoder extends Coder<BytesLike, string> {
parts.push(new Uint8Array(this.#paddingLength));
}

return concatBytes(parts);
return concat(parts);
}

decode(data: Uint8Array, offset: number): [string, number] {
Expand Down
Loading

0 comments on commit 1bd2a4f

Please sign in to comment.