Skip to content

Commit

Permalink
fix: contract created operation with input message (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio authored Jan 11, 2024
1 parent bb17944 commit 08a85b4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-berries-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": patch
---

Fix operations contract created with input of type message
10 changes: 10 additions & 0 deletions packages/providers/src/transaction-summary/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { Input, InputCoin, InputContract, InputMessage } from '@fuel-ts/transactions';
import { InputType } from '@fuel-ts/transactions';

/** @hidden */
export function getInputsByTypes<T = Input>(inputs: Input[], types: Array<InputType>) {
return inputs.filter((i) => types.includes(i.type)) as T[];
}

/** @hidden */
export function getInputsByType<T = Input>(inputs: Input[], type: InputType) {
return inputs.filter((i) => i.type === type) as T[];
Expand All @@ -17,6 +22,11 @@ export function getInputsMessage(inputs: Input[]) {
return getInputsByType<InputMessage>(inputs, InputType.Message);
}

/** @hidden */
export function getInputsCoinAndMessage(inputs: Input[]) {
return getInputsByTypes<InputCoin | InputMessage>(inputs, [InputType.Coin, InputType.Message]);
}

/** @hidden */
export function getInputsContract(inputs: Input[]) {
return getInputsByType<InputContract>(inputs, InputType.Contract);
Expand Down
24 changes: 24 additions & 0 deletions packages/providers/src/transaction-summary/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,30 @@ describe('operations', () => {
expect(operations.length).toEqual(1);
expect(operations[0]).toStrictEqual(expected);
});

it('should getOperations return contract created operations with input type message', () => {
const expected: Operation = {
from: {
address: '0x06300e686a5511c7ba0399fc68dcbe0ca2d8f54f7e6afea73c505dd3bcacf33b',
type: 1,
},
name: OperationName.contractCreated,
to: {
address: '0xef066899413ef8dc7c3073a50868bafb3d039d9bad8006c2635b7f0efa992553',
type: 0,
},
};
const operations = getOperations({
transactionType: TransactionType.Create,
inputs: [MOCK_INPUT_MESSAGE],
outputs: [MOCK_OUTPUT_CONTRACT_CREATED, MOCK_OUTPUT_CHANGE],
receipts: [],
maxInputs: bn(255),
});

expect(operations.length).toEqual(1);
expect(operations[0]).toStrictEqual(expected);
});
});

describe('addOperation', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/providers/src/transaction-summary/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
getInputFromAssetId,
getInputAccountAddress,
getInputContractFromIndex,
getInputsCoin,
getInputsContract,
getInputsCoinAndMessage,
} from './input';
import {
getOutputsChange,
Expand Down Expand Up @@ -427,7 +427,7 @@ export function getPayProducerOperations(outputs: Output[]): Operation[] {
/** @hidden */
export function getContractCreatedOperations({ inputs, outputs }: InputOutputParam): Operation[] {
const contractCreatedOutputs = getOutputsContractCreated(outputs);
const input = getInputsCoin(inputs)[0];
const input = getInputsCoinAndMessage(inputs)[0];
const fromAddress = getInputAccountAddress(input);
const contractCreatedOperations = contractCreatedOutputs.reduce((prev, contractCreatedOutput) => {
const operations = addOperation(prev, {
Expand Down

0 comments on commit 08a85b4

Please sign in to comment.