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

feat: return deep clone on transaction request from method #3097

Merged
merged 8 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-melons-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

feat: return deep clone on transaction request `from` method
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BN } from '@fuel-ts/math';
import type { TransactionBlob } from '@fuel-ts/transactions';
import { clone } from 'ramda';

import type { GasCosts } from '../provider';
import { calculateMetadataGasForTxBlob } from '../utils';
Expand All @@ -17,10 +18,7 @@ export interface BlobTransactionRequestLike extends BaseTransactionRequestLike {

export class BlobTransactionRequest extends BaseTransactionRequest {
static from(obj: BlobTransactionRequestLike) {
if (obj instanceof this) {
return obj;
}
return new this(obj);
return new this(clone(obj));
maschad marked this conversation as resolved.
Show resolved Hide resolved
}

/** Type of the transaction */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { bn, type BN } from '@fuel-ts/math';
import type { TransactionCreate } from '@fuel-ts/transactions';
import { TransactionType, OutputType } from '@fuel-ts/transactions';
import { arrayify, hexlify } from '@fuel-ts/utils';
import { clone } from 'ramda';

import type { GqlGasCosts } from '../__generated__/operations';
import { calculateMetadataGasForTxCreate } from '../utils/gas';
Expand Down Expand Up @@ -32,10 +33,7 @@ export interface CreateTransactionRequestLike extends BaseTransactionRequestLike
*/
export class CreateTransactionRequest extends BaseTransactionRequest {
static from(obj: CreateTransactionRequestLike) {
if (obj instanceof this) {
return obj;
}
return new this(obj);
return new this(clone(obj));
}

/** Type of the transaction */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { BN, BigNumberish } from '@fuel-ts/math';
import type { TransactionScript } from '@fuel-ts/transactions';
import { InputType, OutputType, TransactionType } from '@fuel-ts/transactions';
import { arrayify, hexlify } from '@fuel-ts/utils';
import { clone } from 'ramda';

import type { ChainInfo, GasCosts } from '../provider';
import { calculateMetadataGasForTxScript, getMaxGas } from '../utils/gas';
Expand Down Expand Up @@ -39,10 +40,7 @@ export interface ScriptTransactionRequestLike extends BaseTransactionRequestLike
*/
export class ScriptTransactionRequest extends BaseTransactionRequest {
static from(obj: ScriptTransactionRequestLike) {
if (obj instanceof this) {
return obj;
}
return new this(obj);
return new this(clone(obj));
maschad marked this conversation as resolved.
Show resolved Hide resolved
}

/** Type of the transaction */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { bn } from '@fuel-ts/math';
import { InputType } from '@fuel-ts/transactions';

import { BlobTransactionRequest } from './blob-transaction-request';
import { CreateTransactionRequest } from './create-transaction-request';
import { ScriptTransactionRequest } from './script-transaction-request';
Expand Down Expand Up @@ -42,3 +45,88 @@ describe('isTransactionTypeBlob', () => {
expect(isTransactionTypeCreate(request)).toBe(false);
});
});

describe('transactionRequestify', () => {
maschad marked this conversation as resolved.
Show resolved Hide resolved
it('from method should return a cloned script transaction request', () => {
const baseRequest = new ScriptTransactionRequest();
const newRequest = ScriptTransactionRequest.from(baseRequest);
newRequest.inputs.push({
amount: bn(1),
id: '0x',
owner: '0x',
txPointer: '0x',
witnessIndex: 0,
type: InputType.Coin,
assetId: '0x',
});
baseRequest.inputs.push({
amount: bn(10),
id: '0x123',
owner: '0x456',
txPointer: '0x789',
witnessIndex: 1,
type: InputType.Coin,
assetId: '0xabc',
});
expect(newRequest).to.not.equal(baseRequest);
expect(newRequest.inputs).to.not.equal(baseRequest.inputs);
expect(newRequest.inputs).toHaveLength(1);
expect(baseRequest.inputs).toHaveLength(1);
});

it('from method should return a cloned create transaction request', () => {
const baseRequest = new CreateTransactionRequest({});
const newRequest = CreateTransactionRequest.from(baseRequest);
newRequest.inputs.push({
amount: bn(1),
id: '0x',
owner: '0x',
txPointer: '0x',
witnessIndex: 0,
type: InputType.Coin,
assetId: '0x',
});
baseRequest.inputs.push({
amount: bn(10),
id: '0x123',
owner: '0x456',
txPointer: '0x789',
witnessIndex: 1,
type: InputType.Coin,
assetId: '0xabc',
});

expect(newRequest).to.not.equal(baseRequest);
expect(newRequest.inputs).to.not.equal(baseRequest.inputs);
expect(newRequest.inputs).toHaveLength(1);
expect(baseRequest.inputs).toHaveLength(1);
});

it('from method should return a cloned blob transaction request', () => {
const baseRequest = new BlobTransactionRequest({ blobId: '0x' });
const newRequest = BlobTransactionRequest.from(baseRequest);
newRequest.inputs.push({
amount: bn(1),
id: '0x',
owner: '0x',
txPointer: '0x',
witnessIndex: 0,
type: InputType.Coin,
assetId: '0x',
});
baseRequest.inputs.push({
amount: bn(10),
id: '0x123',
owner: '0x456',
txPointer: '0x789',
witnessIndex: 1,
type: InputType.Coin,
assetId: '0xabc',
});

expect(newRequest).to.not.equal(baseRequest);
expect(newRequest.inputs).to.not.equal(baseRequest.inputs);
expect(newRequest.inputs).toHaveLength(1);
expect(baseRequest.inputs).toHaveLength(1);
});
});