From 0571534cede968fb1fc21e71697cbae6b309b5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Wed, 4 Sep 2024 14:27:56 -0300 Subject: [PATCH 01/14] remove use of receipt coders at assembleReceiptByType --- .../account/src/providers/utils/receipts.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/account/src/providers/utils/receipts.ts b/packages/account/src/providers/utils/receipts.ts index c9b4575b2eb..ea3d181773a 100644 --- a/packages/account/src/providers/utils/receipts.ts +++ b/packages/account/src/providers/utils/receipts.ts @@ -16,14 +16,9 @@ import type { ReceiptTransfer, ReceiptTransferOut, } from '@fuel-ts/transactions'; -import { - ReceiptBurnCoder, - ReceiptMessageOutCoder, - ReceiptMintCoder, - ReceiptType, -} from '@fuel-ts/transactions'; +import { getMintedAssetId, InputMessageCoder, ReceiptType } from '@fuel-ts/transactions'; import { FAILED_TRANSFER_TO_ADDRESS_SIGNAL } from '@fuel-ts/transactions/configs'; -import { arrayify } from '@fuel-ts/utils'; +import { arrayify, hexlify } from '@fuel-ts/utils'; import type { GqlReceiptFragment } from '../__generated__/operations'; import { GqlReceiptType } from '../__generated__/operations'; @@ -212,12 +207,12 @@ export function assembleReceiptByType(receipt: GqlReceiptFragment) { const data = receipt.data ? arrayify(receipt.data) : Uint8Array.from([]); const digest = hexOrZero(receipt.digest); - const messageId = ReceiptMessageOutCoder.getMessageId({ + const messageId = InputMessageCoder.getMessageId({ sender, recipient, nonce, amount, - data, + data: hexlify(data), }); const receiptMessageOut: ReceiptMessageOut = { @@ -237,7 +232,7 @@ export function assembleReceiptByType(receipt: GqlReceiptFragment) { case GqlReceiptType.Mint: { const contractId = hexOrZero(receipt.id || receipt.contractId); const subId = hexOrZero(receipt.subId); - const assetId = ReceiptMintCoder.getAssetId(contractId, subId); + const assetId = getMintedAssetId(contractId, subId); const mintReceipt: ReceiptMint = { type: ReceiptType.Mint, @@ -255,7 +250,7 @@ export function assembleReceiptByType(receipt: GqlReceiptFragment) { case GqlReceiptType.Burn: { const contractId = hexOrZero(receipt.id || receipt.contractId); const subId = hexOrZero(receipt.subId); - const assetId = ReceiptBurnCoder.getAssetId(contractId, subId); + const assetId = getMintedAssetId(contractId, subId); const burnReceipt: ReceiptBurn = { type: ReceiptType.Burn, From 380c5e79468d0451c8586b5409c7fd5a89f0fddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Wed, 4 Sep 2024 14:28:14 -0300 Subject: [PATCH 02/14] remove all receipt coders --- packages/transactions/src/coders/receipt.ts | 820 +------------------- 1 file changed, 8 insertions(+), 812 deletions(-) diff --git a/packages/transactions/src/coders/receipt.ts b/packages/transactions/src/coders/receipt.ts index 9e8f54881e0..92e0cce4b50 100644 --- a/packages/transactions/src/coders/receipt.ts +++ b/packages/transactions/src/coders/receipt.ts @@ -1,13 +1,8 @@ -/* eslint-disable max-classes-per-file */ -import { Coder, BigNumberCoder, B256Coder, NumberCoder } from '@fuel-ts/abi-coder'; -import { ErrorCode, FuelError } from '@fuel-ts/errors'; import { sha256 } from '@fuel-ts/hasher'; import type { AssetId } from '@fuel-ts/interfaces'; import type { BN } from '@fuel-ts/math'; import { arrayify, concat } from '@fuel-ts/utils'; -import { ByteArrayCoder } from './byte-array'; - export enum ReceiptType /* u8 */ { Call = 0, Return = 1, @@ -46,68 +41,6 @@ export type ReceiptCall = { is: BN; }; -export class ReceiptCallCoder extends Coder { - constructor() { - super('ReceiptCall', 'struct ReceiptCall', 0); - } - - encode(value: ReceiptCall): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.from)); - parts.push(new B256Coder().encode(value.to)); - parts.push(new BigNumberCoder('u64').encode(value.amount)); - parts.push(new B256Coder().encode(value.assetId)); - parts.push(new BigNumberCoder('u64').encode(value.gas)); - parts.push(new BigNumberCoder('u64').encode(value.param1)); - parts.push(new BigNumberCoder('u64').encode(value.param2)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptCall, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const from = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const to = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const amount = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const assetId = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const gas = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const param1 = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const param2 = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - return [ - { - type: ReceiptType.Call, - from, - to, - amount, - assetId, - gas, - param1, - param2, - pc, - is, - }, - o, - ]; - } -} - export type ReceiptReturn = { type: ReceiptType.Return; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -120,48 +53,6 @@ export type ReceiptReturn = { is: BN; }; -export class ReceiptReturnCoder extends Coder { - constructor() { - super('ReceiptReturn', 'struct ReceiptReturn', 0); - } - - encode(value: ReceiptReturn): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.id)); - parts.push(new BigNumberCoder('u64').encode(value.val)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptReturn, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const id = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - return [ - { - type: ReceiptType.Return, - id, - val, - pc, - is, - }, - o, - ]; - } -} - export type ReceiptReturnData = { type: ReceiptType.ReturnData; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -178,56 +69,6 @@ export type ReceiptReturnData = { is: BN; }; -export class ReceiptReturnDataCoder extends Coder { - constructor() { - super('ReceiptReturnData', 'struct ReceiptReturnData', 0); - } - - encode(value: ReceiptReturnData): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.id)); - parts.push(new BigNumberCoder('u64').encode(value.ptr)); - parts.push(new BigNumberCoder('u64').encode(value.len)); - parts.push(new B256Coder().encode(value.digest)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptReturnData, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const id = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const ptr = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const len = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const digest = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - return [ - { - type: ReceiptType.ReturnData, - id, - ptr, - len, - digest, - pc, - is, - }, - o, - ]; - } -} - export type ReceiptPanic = { type: ReceiptType.Panic; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -242,52 +83,6 @@ export type ReceiptPanic = { contractId: string; }; -export class ReceiptPanicCoder extends Coder { - constructor() { - super('ReceiptPanic', 'struct ReceiptPanic', 0); - } - - encode(value: ReceiptPanic): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.id)); - parts.push(new BigNumberCoder('u64').encode(value.reason)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - parts.push(new B256Coder().encode(value.contractId)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptPanic, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const id = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const reason = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const contractId = decoded; - - return [ - { - type: ReceiptType.Panic, - id, - reason, - pc, - is, - contractId, - }, - o, - ]; - } -} - export type ReceiptRevert = { type: ReceiptType.Revert; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -300,48 +95,6 @@ export type ReceiptRevert = { is: BN; }; -export class ReceiptRevertCoder extends Coder { - constructor() { - super('ReceiptRevert', 'struct ReceiptRevert', 0); - } - - encode(value: ReceiptRevert): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.id)); - parts.push(new BigNumberCoder('u64').encode(value.val)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptRevert, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const id = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - return [ - { - type: ReceiptType.Revert, - id, - val, - pc, - is, - }, - o, - ]; - } -} - export type ReceiptLog = { type: ReceiptType.Log; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -360,60 +113,6 @@ export type ReceiptLog = { is: BN; }; -export class ReceiptLogCoder extends Coder { - constructor() { - super('ReceiptLog', 'struct ReceiptLog', 0); - } - - encode(value: ReceiptLog): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.id)); - parts.push(new BigNumberCoder('u64').encode(value.val0)); - parts.push(new BigNumberCoder('u64').encode(value.val1)); - parts.push(new BigNumberCoder('u64').encode(value.val2)); - parts.push(new BigNumberCoder('u64').encode(value.val3)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptLog, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const id = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val0 = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val1 = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val2 = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val3 = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - return [ - { - type: ReceiptType.Log, - id, - val0, - val1, - val2, - val3, - pc, - is, - }, - o, - ]; - } -} - export type ReceiptLogData = { type: ReceiptType.LogData; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -434,64 +133,6 @@ export type ReceiptLogData = { is: BN; }; -export class ReceiptLogDataCoder extends Coder { - constructor() { - super('ReceiptLogData', 'struct ReceiptLogData', 0); - } - - encode(value: ReceiptLogData): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.id)); - parts.push(new BigNumberCoder('u64').encode(value.val0)); - parts.push(new BigNumberCoder('u64').encode(value.val1)); - parts.push(new BigNumberCoder('u64').encode(value.ptr)); - parts.push(new BigNumberCoder('u64').encode(value.len)); - parts.push(new B256Coder().encode(value.digest)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptLogData, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const id = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val0 = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val1 = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const ptr = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const len = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const digest = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - return [ - { - type: ReceiptType.LogData, - id, - val0, - val1, - ptr, - len, - digest, - pc, - is, - }, - o, - ]; - } -} - export type ReceiptTransfer = { type: ReceiptType.Transfer; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -508,56 +149,6 @@ export type ReceiptTransfer = { is: BN; }; -export class ReceiptTransferCoder extends Coder { - constructor() { - super('ReceiptTransfer', 'struct ReceiptTransfer', 0); - } - - encode(value: ReceiptTransfer): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.from)); - parts.push(new B256Coder().encode(value.to)); - parts.push(new BigNumberCoder('u64').encode(value.amount)); - parts.push(new B256Coder().encode(value.assetId)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptTransfer, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const from = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const to = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const amount = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const assetId = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - return [ - { - type: ReceiptType.Transfer, - from, - to, - amount, - assetId, - pc, - is, - }, - o, - ]; - } -} - export type ReceiptTransferOut = { type: ReceiptType.TransferOut; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -574,56 +165,6 @@ export type ReceiptTransferOut = { is: BN; }; -export class ReceiptTransferOutCoder extends Coder { - constructor() { - super('ReceiptTransferOut', 'struct ReceiptTransferOut', 0); - } - - encode(value: ReceiptTransferOut): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.from)); - parts.push(new B256Coder().encode(value.to)); - parts.push(new BigNumberCoder('u64').encode(value.amount)); - parts.push(new B256Coder().encode(value.assetId)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptTransferOut, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const from = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const to = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const amount = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const assetId = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - return [ - { - type: ReceiptType.TransferOut, - from, - to, - amount, - assetId, - pc, - is, - }, - o, - ]; - } -} - export type ReceiptScriptResult = { type: ReceiptType.ScriptResult; /** Result variant with embedded `PanicReason` in first 8 bits and `instr` (u64) */ @@ -632,40 +173,6 @@ export type ReceiptScriptResult = { gasUsed: BN; }; -export class ReceiptScriptResultCoder extends Coder { - constructor() { - super('ReceiptScriptResult', 'struct ReceiptScriptResult', 0); - } - - encode(value: ReceiptScriptResult): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new BigNumberCoder('u64').encode(value.result)); - parts.push(new BigNumberCoder('u64').encode(value.gasUsed)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptScriptResult, number] { - let decoded; - let o = offset; - - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const result = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const gasUsed = decoded; - - return [ - { - type: ReceiptType.ScriptResult, - result, - gasUsed, - }, - o, - ]; - } -} - export type ReceiptMessageOut = { type: ReceiptType.MessageOut; /** Hexadecimal string representation of the 256-bit (32-byte) message ID */ @@ -684,74 +191,6 @@ export type ReceiptMessageOut = { data: Uint8Array; }; -export class ReceiptMessageOutCoder extends Coder { - constructor() { - super('ReceiptMessageOut', 'struct ReceiptMessageOut', 0); - } - - static getMessageId( - value: Pick - ): string { - const parts: Uint8Array[] = []; - - parts.push(new ByteArrayCoder(32).encode(value.sender)); - parts.push(new ByteArrayCoder(32).encode(value.recipient)); - parts.push(new ByteArrayCoder(32).encode(value.nonce)); - parts.push(new BigNumberCoder('u64').encode(value.amount)); - parts.push(arrayify(value.data || '0x')); - - return sha256(concat(parts)); - } - - encode(value: Omit): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.sender)); - parts.push(new B256Coder().encode(value.recipient)); - parts.push(new BigNumberCoder('u64').encode(value.amount)); - parts.push(new B256Coder().encode(value.nonce)); - parts.push(new NumberCoder('u16', { padToWordSize: true }).encode(value.data.length)); - parts.push(new B256Coder().encode(value.digest)); - parts.push(new ByteArrayCoder(value.data.length).encode(value.data)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptMessageOut, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const sender = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const recipient = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const amount = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const nonce = decoded; - [decoded, o] = new NumberCoder('u16', { padToWordSize: true }).decode(data, o); - const len = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const digest = decoded; - [decoded, o] = new ByteArrayCoder(len).decode(data, o); - const messageData = arrayify(decoded); - - const receiptMessageOut: ReceiptMessageOut = { - type: ReceiptType.MessageOut, - messageId: '', - sender, - recipient, - amount, - nonce, - digest, - data: messageData, - }; - receiptMessageOut.messageId = ReceiptMessageOutCoder.getMessageId(receiptMessageOut); - - return [receiptMessageOut, o]; - } -} - export type ReceiptMint = { type: ReceiptType.Mint; @@ -768,69 +207,6 @@ export type ReceiptMint = { is: BN; }; -export const getMintedAssetId = (contractId: string, subId: string): string => { - const contractIdBytes = arrayify(contractId); - const subIdBytes = arrayify(subId); - - return sha256(concat([contractIdBytes, subIdBytes])); -}; - -export const createAssetId = (contractId: string, subId: string): AssetId => ({ - bits: getMintedAssetId(contractId, subId), -}); - -export class ReceiptMintCoder extends Coder { - constructor() { - super('ReceiptMint', 'struct ReceiptMint', 0); - } - - static getAssetId(contractId: string, subId: string): string { - return getMintedAssetId(contractId, subId); - } - - encode(value: ReceiptMint): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.subId)); - parts.push(new B256Coder().encode(value.contractId)); - parts.push(new BigNumberCoder('u64').encode(value.val)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptMint, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const subId = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const contractId = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - const assetId = ReceiptMintCoder.getAssetId(contractId, subId); - - const receiptMint: ReceiptMint = { - type: ReceiptType.Mint, - subId, - contractId, - val, - pc, - is, - assetId, - }; - - return [receiptMint, o]; - } -} - export type ReceiptBurn = { type: ReceiptType.Burn; @@ -847,58 +223,6 @@ export type ReceiptBurn = { is: BN; }; -export class ReceiptBurnCoder extends Coder { - constructor() { - super('ReceiptBurn', 'struct ReceiptBurn', 0); - } - - static getAssetId(contractId: string, subId: string): string { - return getMintedAssetId(contractId, subId); - } - - encode(value: ReceiptBurn): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new B256Coder().encode(value.subId)); - parts.push(new B256Coder().encode(value.contractId)); - parts.push(new BigNumberCoder('u64').encode(value.val)); - parts.push(new BigNumberCoder('u64').encode(value.pc)); - parts.push(new BigNumberCoder('u64').encode(value.is)); - - return concat(parts); - } - - decode(data: Uint8Array, offset: number): [ReceiptBurn, number] { - let decoded; - let o = offset; - - [decoded, o] = new B256Coder().decode(data, o); - const subId = decoded; - [decoded, o] = new B256Coder().decode(data, o); - const contractId = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const val = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const pc = decoded; - [decoded, o] = new BigNumberCoder('u64').decode(data, o); - const is = decoded; - - const assetId = ReceiptMintCoder.getAssetId(contractId, subId); - - const receiptBurn: ReceiptBurn = { - type: ReceiptType.Burn, - subId, - contractId, - val, - pc, - is, - assetId, - }; - - return [receiptBurn, o]; - } -} - export type Receipt = | ReceiptCall | ReceiptReturn @@ -914,141 +238,13 @@ export type Receipt = | ReceiptMint | ReceiptBurn; -export class ReceiptCoder extends Coder { - constructor() { - super('Receipt', 'struct Receipt', 0); - } - - encode(value: Receipt): Uint8Array { - const parts: Uint8Array[] = []; - - parts.push(new NumberCoder('u8', { padToWordSize: true }).encode(value.type)); - - const { type } = value; - - switch (value.type) { - case ReceiptType.Call: { - parts.push(new ReceiptCallCoder().encode(value)); - break; - } - case ReceiptType.Return: { - parts.push(new ReceiptReturnCoder().encode(value)); - break; - } - case ReceiptType.ReturnData: { - parts.push(new ReceiptReturnDataCoder().encode(value)); - break; - } - case ReceiptType.Panic: { - parts.push(new ReceiptPanicCoder().encode(value)); - break; - } - case ReceiptType.Revert: { - parts.push(new ReceiptRevertCoder().encode(value)); - break; - } - case ReceiptType.Log: { - parts.push(new ReceiptLogCoder().encode(value)); - break; - } - case ReceiptType.LogData: { - parts.push(new ReceiptLogDataCoder().encode(value)); - break; - } - case ReceiptType.Transfer: { - parts.push(new ReceiptTransferCoder().encode(value)); - break; - } - case ReceiptType.TransferOut: { - parts.push(new ReceiptTransferOutCoder().encode(value)); - break; - } - case ReceiptType.ScriptResult: { - parts.push(new ReceiptScriptResultCoder().encode(value)); - break; - } - case ReceiptType.MessageOut: { - parts.push(new ReceiptMessageOutCoder().encode(value)); - break; - } - case ReceiptType.Mint: { - parts.push(new ReceiptMintCoder().encode(value)); - break; - } - case ReceiptType.Burn: { - parts.push(new ReceiptBurnCoder().encode(value)); - break; - } - default: { - throw new FuelError(ErrorCode.INVALID_RECEIPT_TYPE, `Invalid receipt type: ${type}`); - } - } - - return concat(parts); - } +export const getMintedAssetId = (contractId: string, subId: string): string => { + const contractIdBytes = arrayify(contractId); + const subIdBytes = arrayify(subId); - decode(data: Uint8Array, offset: number): [Receipt, number] { - let decoded; - let o = offset; + return sha256(concat([contractIdBytes, subIdBytes])); +}; - [decoded, o] = new NumberCoder('u8', { padToWordSize: true }).decode(data, o); - const type = decoded as ReceiptType; - switch (type) { - case ReceiptType.Call: { - [decoded, o] = new ReceiptCallCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.Return: { - [decoded, o] = new ReceiptReturnCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.ReturnData: { - [decoded, o] = new ReceiptReturnDataCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.Panic: { - [decoded, o] = new ReceiptPanicCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.Revert: { - [decoded, o] = new ReceiptRevertCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.Log: { - [decoded, o] = new ReceiptLogCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.LogData: { - [decoded, o] = new ReceiptLogDataCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.Transfer: { - [decoded, o] = new ReceiptTransferCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.TransferOut: { - [decoded, o] = new ReceiptTransferOutCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.ScriptResult: { - [decoded, o] = new ReceiptScriptResultCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.MessageOut: { - [decoded, o] = new ReceiptMessageOutCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.Mint: { - [decoded, o] = new ReceiptMintCoder().decode(data, o); - return [decoded, o]; - } - case ReceiptType.Burn: { - [decoded, o] = new ReceiptBurnCoder().decode(data, o); - return [decoded, o]; - } - default: { - throw new FuelError(ErrorCode.INVALID_RECEIPT_TYPE, `Invalid receipt type: ${type}`); - } - } - } -} +export const createAssetId = (contractId: string, subId: string): AssetId => ({ + bits: getMintedAssetId(contractId, subId), +}); From 9080b19440889cf52e3c92fa0ff3b357d070e654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Wed, 4 Sep 2024 14:28:33 -0300 Subject: [PATCH 03/14] remove receipt coders test suite --- .../transactions/src/coders/receipt.test.ts | 297 ------------------ 1 file changed, 297 deletions(-) delete mode 100644 packages/transactions/src/coders/receipt.test.ts diff --git a/packages/transactions/src/coders/receipt.test.ts b/packages/transactions/src/coders/receipt.test.ts deleted file mode 100644 index 1e2db2b91be..00000000000 --- a/packages/transactions/src/coders/receipt.test.ts +++ /dev/null @@ -1,297 +0,0 @@ -import { sha256 } from '@fuel-ts/hasher'; -import { bn } from '@fuel-ts/math'; -import { arrayify, hexlify, concat } from '@fuel-ts/utils'; - -import type { Receipt } from './receipt'; -import { ReceiptCoder, ReceiptMessageOutCoder, ReceiptType } from './receipt'; - -const B256 = '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'; -const B256_ALT1 = '0x750f560d912ec02d826af8ba3be90a9481fb6d3bc6b4e7f01a89f245cf0a7059'; -const B256_ALT2 = '0x68b401b682ba0c9018150cca596358a6b98576337ea10b9cfb0d02441b3bc61a'; -const B256_ALT3 = '0xeb03488970d05ea240c788a0ea2e07176cc5317b7c7c89f26ac5282bbcd445bd'; -const B256_ALT4 = '0x2f6d40e3ac1a172fb9445f9843440a0fc383bea238a7a35a77a3c73d36902992'; - -/** - * @group node - * @group browser - */ -describe('ReceiptCoder', () => { - it('Can encode Call', () => { - const receipt: Receipt = { - type: ReceiptType.Call, - from: B256, - to: B256, - amount: bn(0), - assetId: B256, - gas: bn(0), - param1: bn(0), - param2: bn(0), - pc: bn(0), - is: bn(0), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930bd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000000000000000000000000000000000000000000000000000' - ); - - const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); - - expect(offset).toEqual((encoded.length - 2) / 2); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode Return', () => { - const receipt: Receipt = { - type: ReceiptType.Return, - id: B256, - val: bn(0), - pc: bn(0), - is: bn(0), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x0000000000000001d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b000000000000000000000000000000000000000000000000' - ); - - const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); - - expect(offset).toEqual((encoded.length - 2) / 2); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode ReturnData', () => { - const receipt: Receipt = { - type: ReceiptType.ReturnData, - id: B256, - ptr: bn(0), - len: bn(0), - digest: B256, - pc: bn(0), - is: bn(0), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x0000000000000002d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000' - ); - - const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); - - expect(offset).toEqual((encoded.length - 2) / 2); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode Panic', () => { - const receipt: Receipt = { - type: ReceiptType.Panic, - id: B256, - reason: bn(0), - pc: bn(0), - is: bn(0), - contractId: B256, - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x0000000000000003d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b000000000000000000000000000000000000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b' - ); - - const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); - - expect(offset).toEqual((encoded.length - 2) / 2); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode Revert', () => { - const receipt: Receipt = { - type: ReceiptType.Revert, - id: B256, - val: bn(0), - pc: bn(0), - is: bn(0), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x0000000000000004d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b000000000000000000000000000000000000000000000000' - ); - - const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); - - expect(offset).toEqual((encoded.length - 2) / 2); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode Log', () => { - const receipt: Receipt = { - type: ReceiptType.Log, - id: B256, - val0: bn(0), - val1: bn(0), - val2: bn(0), - val3: bn(0), - pc: bn(0), - is: bn(0), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x0000000000000005d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' - ); - - const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); - - expect(offset).toEqual((encoded.length - 2) / 2); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode LogData', () => { - const receipt: Receipt = { - type: ReceiptType.LogData, - id: B256, - val0: bn(0), - val1: bn(0), - ptr: bn(0), - len: bn(0), - digest: B256, - pc: bn(0), - is: bn(0), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x0000000000000006d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000000000000000000000000000000000000000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000' - ); - - const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); - - expect(offset).toEqual((encoded.length - 2) / 2); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode Transfer', () => { - const receipt: Receipt = { - type: ReceiptType.Transfer, - from: B256, - to: B256, - amount: bn(0), - assetId: B256, - pc: bn(0), - is: bn(0), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x0000000000000007d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930bd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000' - ); - - const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); - - expect(offset).toEqual((encoded.length - 2) / 2); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode TransferOut', () => { - const receipt: Receipt = { - type: ReceiptType.TransferOut, - from: B256, - to: B256, - amount: bn(0), - assetId: B256, - pc: bn(0), - is: bn(0), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x0000000000000008d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930bd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000' - ); - - const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); - - expect(offset).toEqual((encoded.length - 2) / 2); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode MessageOut', () => { - const receipt: Receipt = { - type: ReceiptType.MessageOut, - messageId: '', - sender: B256_ALT1, - recipient: B256_ALT2, - amount: bn(4000), - nonce: B256_ALT3, - digest: B256_ALT4, - data: Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), - }; - receipt.messageId = ReceiptMessageOutCoder.getMessageId(receipt); - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x000000000000000a750f560d912ec02d826af8ba3be90a9481fb6d3bc6b4e7f01a89f245cf0a705968b401b682ba0c9018150cca596358a6b98576337ea10b9cfb0d02441b3bc61a0000000000000fa0eb03488970d05ea240c788a0ea2e07176cc5317b7c7c89f26ac5282bbcd445bd000000000000000c2f6d40e3ac1a172fb9445f9843440a0fc383bea238a7a35a77a3c73d369029920102030405060708090a0b0c00000000' - ); - - expect(arrayify(encoded).length).toEqual((encoded.length - 2) / 2); - - const [decoded] = new ReceiptCoder().decode(arrayify(encoded), 0); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode Mint', () => { - const receipt: Receipt = { - type: ReceiptType.Mint, - subId: B256_ALT3, - contractId: B256_ALT1, - val: bn(4000), - pc: bn(30), - is: bn(20), - assetId: sha256(concat([B256_ALT1, B256_ALT3])), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x000000000000000beb03488970d05ea240c788a0ea2e07176cc5317b7c7c89f26ac5282bbcd445bd750f560d912ec02d826af8ba3be90a9481fb6d3bc6b4e7f01a89f245cf0a70590000000000000fa0000000000000001e0000000000000014' - ); - - expect(arrayify(encoded).length).toEqual((encoded.length - 2) / 2); - - const [decoded] = new ReceiptCoder().decode(arrayify(encoded), 0); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); - - it('Can encode Burn', () => { - const receipt: Receipt = { - type: ReceiptType.Burn, - subId: B256_ALT1, - contractId: B256_ALT3, - val: bn(300), - pc: bn(90), - is: bn(10), - assetId: sha256(concat([B256_ALT3, B256_ALT1])), - }; - - const encoded = hexlify(new ReceiptCoder().encode(receipt)); - - expect(encoded).toEqual( - '0x000000000000000c750f560d912ec02d826af8ba3be90a9481fb6d3bc6b4e7f01a89f245cf0a7059eb03488970d05ea240c788a0ea2e07176cc5317b7c7c89f26ac5282bbcd445bd000000000000012c000000000000005a000000000000000a' - ); - - expect(arrayify(encoded).length).toEqual((encoded.length - 2) / 2); - - const [decoded] = new ReceiptCoder().decode(arrayify(encoded), 0); - expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); - }); -}); From 2213a2273513cab5bd5a32cadd84919cf228777d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Wed, 4 Sep 2024 14:28:41 -0300 Subject: [PATCH 04/14] adjusting tests --- .../account/src/providers/utils/receipts.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/account/src/providers/utils/receipts.test.ts b/packages/account/src/providers/utils/receipts.test.ts index cc9b6b86c0f..a4e3c252a49 100644 --- a/packages/account/src/providers/utils/receipts.test.ts +++ b/packages/account/src/providers/utils/receipts.test.ts @@ -14,8 +14,8 @@ import type { ReceiptTransfer, ReceiptTransferOut, } from '@fuel-ts/transactions'; -import { ReceiptBurnCoder, ReceiptMessageOutCoder, ReceiptType } from '@fuel-ts/transactions'; -import { arrayify } from '@fuel-ts/utils'; +import { getMintedAssetId, InputMessageCoder, ReceiptType } from '@fuel-ts/transactions'; +import { arrayify, hexlify } from '@fuel-ts/utils'; import { MOCK_GQL_RECEIPT_FRAGMENT, @@ -219,12 +219,12 @@ describe('assembleReceiptByType', () => { const data = arrayify(MOCK_GQL_RECEIPT_FRAGMENT.data || ''); const digest = MOCK_GQL_RECEIPT_FRAGMENT.digest; - const messageId = ReceiptMessageOutCoder.getMessageId({ + const messageId = InputMessageCoder.getMessageId({ sender, recipient, nonce, amount, - data, + data: hexlify(data), }); expect(receipt.type).toBe(ReceiptType.MessageOut); @@ -240,7 +240,7 @@ describe('assembleReceiptByType', () => { it('should return a ReceiptMint when GqlReceiptType.Mint is provided', () => { const contractId = MOCK_GQL_RECEIPT_FRAGMENT.id || ''; const subId = MOCK_GQL_RECEIPT_FRAGMENT.subId || ''; - const assetId = ReceiptBurnCoder.getAssetId(contractId, subId); + const assetId = getMintedAssetId(contractId, subId); const receipt = assembleReceiptByType({ ...MOCK_GQL_RECEIPT_FRAGMENT, @@ -259,7 +259,7 @@ describe('assembleReceiptByType', () => { it('should return a ReceiptBurn when GqlReceiptType.Burn is provided', () => { const contractId = MOCK_GQL_RECEIPT_FRAGMENT.id || ''; const subId = MOCK_GQL_RECEIPT_FRAGMENT.subId || ''; - const assetId = ReceiptBurnCoder.getAssetId(contractId, subId); + const assetId = getMintedAssetId(contractId, subId); const receipt = assembleReceiptByType({ ...MOCK_GQL_RECEIPT_FRAGMENT, From 1e783117c36e4fb231ccaaa4ef6b18465ae979af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:02:12 -0300 Subject: [PATCH 05/14] move receipts types to another file --- packages/transactions/src/coders/receipt.ts | 236 -------------------- packages/transactions/src/index.ts | 1 + packages/transactions/src/receipts.ts | 236 ++++++++++++++++++++ 3 files changed, 237 insertions(+), 236 deletions(-) create mode 100644 packages/transactions/src/receipts.ts diff --git a/packages/transactions/src/coders/receipt.ts b/packages/transactions/src/coders/receipt.ts index 92e0cce4b50..38c65b1daa0 100644 --- a/packages/transactions/src/coders/receipt.ts +++ b/packages/transactions/src/coders/receipt.ts @@ -1,243 +1,7 @@ import { sha256 } from '@fuel-ts/hasher'; import type { AssetId } from '@fuel-ts/interfaces'; -import type { BN } from '@fuel-ts/math'; import { arrayify, concat } from '@fuel-ts/utils'; -export enum ReceiptType /* u8 */ { - Call = 0, - Return = 1, - ReturnData = 2, - Panic = 3, - Revert = 4, - Log = 5, - LogData = 6, - Transfer = 7, - TransferOut = 8, - ScriptResult = 9, - MessageOut = 10, - Mint = 11, - Burn = 12, -} - -export type ReceiptCall = { - type: ReceiptType.Call; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - from: string; - /** Contract ID of called contract (b256) */ - to: string; - /** Amount of coins to forward, i.e. $rB (u64) */ - amount: BN; - /** Asset ID of coins to forward, i.e. MEM[$rC, 32] (b256) */ - assetId: string; - /** Gas to forward, i.e. $rD (u64) */ - gas: BN; - /** First parameter (u64) */ - param1: BN; - /** Second parameter (u64) */ - param2: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptReturn = { - type: ReceiptType.Return; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - val: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptReturnData = { - type: ReceiptType.ReturnData; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - ptr: BN; - /** Value of register $rB (u64) */ - len: BN; - /** Hash of MEM[$rA, $rB] (b256) */ - digest: string; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptPanic = { - type: ReceiptType.Panic; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Panic reason (u64) */ - reason: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; - /** Value of optional contract ID */ - contractId: string; -}; - -export type ReceiptRevert = { - type: ReceiptType.Revert; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - val: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptLog = { - type: ReceiptType.Log; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - val0: BN; - /** Value of register $rB (u64) */ - val1: BN; - /** Value of register $rC (u64) */ - val2: BN; - /** Value of register $rD (u64) */ - val3: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptLogData = { - type: ReceiptType.LogData; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - val0: BN; - /** Value of register $rB (u64) */ - val1: BN; - /** Value of register $rC (u64) */ - ptr: BN; - /** Value of register $rD (u64) */ - len: BN; - /** Hash of MEM[$rC, $rD] (b256) */ - digest: string; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptTransfer = { - type: ReceiptType.Transfer; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - from: string; - /** Contract ID of contract to transfer coins to (b256) */ - to: string; - /** Amount of coins transferred (u64) */ - amount: BN; - /** Asset ID of coins transferred (b256) */ - assetId: string; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptTransferOut = { - type: ReceiptType.TransferOut; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - from: string; - /** Address to transfer coins to (b256) */ - to: string; - /** Amount of coins transferred (u64) */ - amount: BN; - /** Asset ID of coins transferred (b256) */ - assetId: string; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptScriptResult = { - type: ReceiptType.ScriptResult; - /** Result variant with embedded `PanicReason` in first 8 bits and `instr` (u64) */ - result: BN; - /** Gas consumed by the script (u64) */ - gasUsed: BN; -}; - -export type ReceiptMessageOut = { - type: ReceiptType.MessageOut; - /** Hexadecimal string representation of the 256-bit (32-byte) message ID */ - messageId: string; - /** Hexadecimal string representation of the 256-bit (32-byte) address of the message sender: MEM[$fp, 32] */ - sender: string; - /** Hexadecimal string representation of the 256-bit (32-byte) address of the message recipient: MEM[$rA, 32] */ - recipient: string; - /** Hexadecimal string representation of a 64-bit unsigned integer; value of register $rD */ - amount: BN; - /** Hexadecimal string representation of the 256-bit (32-byte) message nonce */ - nonce: string; - /** Hexadecimal string representation of 256-bit (32-byte), hash of MEM[$rA + 32, $rB] */ - digest: string; - /** Hexadecimal string representation of the value of the memory range MEM[$rA + 32, $rB] */ - data: Uint8Array; -}; - -export type ReceiptMint = { - type: ReceiptType.Mint; - - subId: string; - - contractId: string; - - assetId: string; - - val: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptBurn = { - type: ReceiptType.Burn; - - subId: string; - - contractId: string; - - assetId: string; - - val: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type Receipt = - | ReceiptCall - | ReceiptReturn - | ReceiptReturnData - | ReceiptPanic - | ReceiptRevert - | ReceiptLog - | ReceiptLogData - | ReceiptTransfer - | ReceiptTransferOut - | ReceiptScriptResult - | ReceiptMessageOut - | ReceiptMint - | ReceiptBurn; - export const getMintedAssetId = (contractId: string, subId: string): string => { const contractIdBytes = arrayify(contractId); const subIdBytes = arrayify(subId); diff --git a/packages/transactions/src/index.ts b/packages/transactions/src/index.ts index 5b3456c0d86..4bcedc5307a 100644 --- a/packages/transactions/src/index.ts +++ b/packages/transactions/src/index.ts @@ -8,3 +8,4 @@ export * from './coders/witness'; export * from './coders/tx-pointer'; export * from './coders/utxo-id'; export * from './coders/byte-array'; +export * from './receipts'; diff --git a/packages/transactions/src/receipts.ts b/packages/transactions/src/receipts.ts new file mode 100644 index 00000000000..64700248f86 --- /dev/null +++ b/packages/transactions/src/receipts.ts @@ -0,0 +1,236 @@ +import type { BN } from '@fuel-ts/math'; + +export enum ReceiptType /* u8 */ { + Call = 0, + Return = 1, + ReturnData = 2, + Panic = 3, + Revert = 4, + Log = 5, + LogData = 6, + Transfer = 7, + TransferOut = 8, + ScriptResult = 9, + MessageOut = 10, + Mint = 11, + Burn = 12, +} + +export type ReceiptCall = { + type: ReceiptType.Call; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + from: string; + /** Contract ID of called contract (b256) */ + to: string; + /** Amount of coins to forward, i.e. $rB (u64) */ + amount: BN; + /** Asset ID of coins to forward, i.e. MEM[$rC, 32] (b256) */ + assetId: string; + /** Gas to forward, i.e. $rD (u64) */ + gas: BN; + /** First parameter (u64) */ + param1: BN; + /** Second parameter (u64) */ + param2: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptReturn = { + type: ReceiptType.Return; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + val: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptReturnData = { + type: ReceiptType.ReturnData; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + ptr: BN; + /** Value of register $rB (u64) */ + len: BN; + /** Hash of MEM[$rA, $rB] (b256) */ + digest: string; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptPanic = { + type: ReceiptType.Panic; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Panic reason (u64) */ + reason: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; + /** Value of optional contract ID */ + contractId: string; +}; + +export type ReceiptRevert = { + type: ReceiptType.Revert; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + val: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptLog = { + type: ReceiptType.Log; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + val0: BN; + /** Value of register $rB (u64) */ + val1: BN; + /** Value of register $rC (u64) */ + val2: BN; + /** Value of register $rD (u64) */ + val3: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptLogData = { + type: ReceiptType.LogData; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + val0: BN; + /** Value of register $rB (u64) */ + val1: BN; + /** Value of register $rC (u64) */ + ptr: BN; + /** Value of register $rD (u64) */ + len: BN; + /** Hash of MEM[$rC, $rD] (b256) */ + digest: string; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptTransfer = { + type: ReceiptType.Transfer; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + from: string; + /** Contract ID of contract to transfer coins to (b256) */ + to: string; + /** Amount of coins transferred (u64) */ + amount: BN; + /** Asset ID of coins transferred (b256) */ + assetId: string; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptTransferOut = { + type: ReceiptType.TransferOut; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + from: string; + /** Address to transfer coins to (b256) */ + to: string; + /** Amount of coins transferred (u64) */ + amount: BN; + /** Asset ID of coins transferred (b256) */ + assetId: string; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptScriptResult = { + type: ReceiptType.ScriptResult; + /** Result variant with embedded `PanicReason` in first 8 bits and `instr` (u64) */ + result: BN; + /** Gas consumed by the script (u64) */ + gasUsed: BN; +}; + +export type ReceiptMessageOut = { + type: ReceiptType.MessageOut; + /** Hexadecimal string representation of the 256-bit (32-byte) message ID */ + messageId: string; + /** Hexadecimal string representation of the 256-bit (32-byte) address of the message sender: MEM[$fp, 32] */ + sender: string; + /** Hexadecimal string representation of the 256-bit (32-byte) address of the message recipient: MEM[$rA, 32] */ + recipient: string; + /** Hexadecimal string representation of a 64-bit unsigned integer; value of register $rD */ + amount: BN; + /** Hexadecimal string representation of the 256-bit (32-byte) message nonce */ + nonce: string; + /** Hexadecimal string representation of 256-bit (32-byte), hash of MEM[$rA + 32, $rB] */ + digest: string; + /** Hexadecimal string representation of the value of the memory range MEM[$rA + 32, $rB] */ + data: Uint8Array; +}; + +export type ReceiptMint = { + type: ReceiptType.Mint; + + subId: string; + + contractId: string; + + assetId: string; + + val: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptBurn = { + type: ReceiptType.Burn; + + subId: string; + + contractId: string; + + assetId: string; + + val: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type Receipt = + | ReceiptCall + | ReceiptReturn + | ReceiptReturnData + | ReceiptPanic + | ReceiptRevert + | ReceiptLog + | ReceiptLogData + | ReceiptTransfer + | ReceiptTransferOut + | ReceiptScriptResult + | ReceiptMessageOut + | ReceiptMint + | ReceiptBurn; From c26f537cbb535d4a3f698d1a6a606555aad85cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:07:06 -0300 Subject: [PATCH 06/14] move receipt related helpers to another file --- packages/transactions/src/index.ts | 2 +- packages/transactions/src/{coders/receipt.ts => utils.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/transactions/src/{coders/receipt.ts => utils.ts} (100%) diff --git a/packages/transactions/src/index.ts b/packages/transactions/src/index.ts index 4bcedc5307a..8de89ccc210 100644 --- a/packages/transactions/src/index.ts +++ b/packages/transactions/src/index.ts @@ -1,7 +1,6 @@ export * from './coders/input'; export * from './coders/output'; export * from './coders/policy'; -export * from './coders/receipt'; export * from './coders/storage-slot'; export * from './coders/transaction'; export * from './coders/witness'; @@ -9,3 +8,4 @@ export * from './coders/tx-pointer'; export * from './coders/utxo-id'; export * from './coders/byte-array'; export * from './receipts'; +export * from './utils'; diff --git a/packages/transactions/src/coders/receipt.ts b/packages/transactions/src/utils.ts similarity index 100% rename from packages/transactions/src/coders/receipt.ts rename to packages/transactions/src/utils.ts From 6ef43debb941406bf4395b547bf934c359cbe4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Wed, 4 Sep 2024 20:27:32 -0300 Subject: [PATCH 07/14] add changeset --- .changeset/olive-apples-brush.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/olive-apples-brush.md diff --git a/.changeset/olive-apples-brush.md b/.changeset/olive-apples-brush.md new file mode 100644 index 00000000000..e705890f6cc --- /dev/null +++ b/.changeset/olive-apples-brush.md @@ -0,0 +1,6 @@ +--- +"@fuel-ts/transactions": patch +"@fuel-ts/account": patch +--- + +chore: remove all receipt coders From 6f7a603eac210e0a1ec3dc295e82497f93d61a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Fri, 6 Sep 2024 08:30:50 -0300 Subject: [PATCH 08/14] Revert "move receipt related helpers to another file" This reverts commit c26f537cbb535d4a3f698d1a6a606555aad85cef. --- packages/transactions/src/{utils.ts => coders/receipt.ts} | 0 packages/transactions/src/index.ts | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/transactions/src/{utils.ts => coders/receipt.ts} (100%) diff --git a/packages/transactions/src/utils.ts b/packages/transactions/src/coders/receipt.ts similarity index 100% rename from packages/transactions/src/utils.ts rename to packages/transactions/src/coders/receipt.ts diff --git a/packages/transactions/src/index.ts b/packages/transactions/src/index.ts index 8de89ccc210..4bcedc5307a 100644 --- a/packages/transactions/src/index.ts +++ b/packages/transactions/src/index.ts @@ -1,6 +1,7 @@ export * from './coders/input'; export * from './coders/output'; export * from './coders/policy'; +export * from './coders/receipt'; export * from './coders/storage-slot'; export * from './coders/transaction'; export * from './coders/witness'; @@ -8,4 +9,3 @@ export * from './coders/tx-pointer'; export * from './coders/utxo-id'; export * from './coders/byte-array'; export * from './receipts'; -export * from './utils'; From 0c608308a4964a990e477366447a3174e949e3e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Fri, 6 Sep 2024 08:31:07 -0300 Subject: [PATCH 09/14] Revert "move receipts types to another file" This reverts commit 1e783117c36e4fb231ccaaa4ef6b18465ae979af. --- packages/transactions/src/coders/receipt.ts | 236 ++++++++++++++++++++ packages/transactions/src/index.ts | 1 - packages/transactions/src/receipts.ts | 236 -------------------- 3 files changed, 236 insertions(+), 237 deletions(-) delete mode 100644 packages/transactions/src/receipts.ts diff --git a/packages/transactions/src/coders/receipt.ts b/packages/transactions/src/coders/receipt.ts index 38c65b1daa0..92e0cce4b50 100644 --- a/packages/transactions/src/coders/receipt.ts +++ b/packages/transactions/src/coders/receipt.ts @@ -1,7 +1,243 @@ import { sha256 } from '@fuel-ts/hasher'; import type { AssetId } from '@fuel-ts/interfaces'; +import type { BN } from '@fuel-ts/math'; import { arrayify, concat } from '@fuel-ts/utils'; +export enum ReceiptType /* u8 */ { + Call = 0, + Return = 1, + ReturnData = 2, + Panic = 3, + Revert = 4, + Log = 5, + LogData = 6, + Transfer = 7, + TransferOut = 8, + ScriptResult = 9, + MessageOut = 10, + Mint = 11, + Burn = 12, +} + +export type ReceiptCall = { + type: ReceiptType.Call; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + from: string; + /** Contract ID of called contract (b256) */ + to: string; + /** Amount of coins to forward, i.e. $rB (u64) */ + amount: BN; + /** Asset ID of coins to forward, i.e. MEM[$rC, 32] (b256) */ + assetId: string; + /** Gas to forward, i.e. $rD (u64) */ + gas: BN; + /** First parameter (u64) */ + param1: BN; + /** Second parameter (u64) */ + param2: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptReturn = { + type: ReceiptType.Return; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + val: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptReturnData = { + type: ReceiptType.ReturnData; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + ptr: BN; + /** Value of register $rB (u64) */ + len: BN; + /** Hash of MEM[$rA, $rB] (b256) */ + digest: string; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptPanic = { + type: ReceiptType.Panic; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Panic reason (u64) */ + reason: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; + /** Value of optional contract ID */ + contractId: string; +}; + +export type ReceiptRevert = { + type: ReceiptType.Revert; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + val: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptLog = { + type: ReceiptType.Log; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + val0: BN; + /** Value of register $rB (u64) */ + val1: BN; + /** Value of register $rC (u64) */ + val2: BN; + /** Value of register $rD (u64) */ + val3: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptLogData = { + type: ReceiptType.LogData; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + id: string; + /** Value of register $rA (u64) */ + val0: BN; + /** Value of register $rB (u64) */ + val1: BN; + /** Value of register $rC (u64) */ + ptr: BN; + /** Value of register $rD (u64) */ + len: BN; + /** Hash of MEM[$rC, $rD] (b256) */ + digest: string; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptTransfer = { + type: ReceiptType.Transfer; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + from: string; + /** Contract ID of contract to transfer coins to (b256) */ + to: string; + /** Amount of coins transferred (u64) */ + amount: BN; + /** Asset ID of coins transferred (b256) */ + assetId: string; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptTransferOut = { + type: ReceiptType.TransferOut; + /** Contract ID of current context if in an internal context, zero otherwise (b256) */ + from: string; + /** Address to transfer coins to (b256) */ + to: string; + /** Amount of coins transferred (u64) */ + amount: BN; + /** Asset ID of coins transferred (b256) */ + assetId: string; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptScriptResult = { + type: ReceiptType.ScriptResult; + /** Result variant with embedded `PanicReason` in first 8 bits and `instr` (u64) */ + result: BN; + /** Gas consumed by the script (u64) */ + gasUsed: BN; +}; + +export type ReceiptMessageOut = { + type: ReceiptType.MessageOut; + /** Hexadecimal string representation of the 256-bit (32-byte) message ID */ + messageId: string; + /** Hexadecimal string representation of the 256-bit (32-byte) address of the message sender: MEM[$fp, 32] */ + sender: string; + /** Hexadecimal string representation of the 256-bit (32-byte) address of the message recipient: MEM[$rA, 32] */ + recipient: string; + /** Hexadecimal string representation of a 64-bit unsigned integer; value of register $rD */ + amount: BN; + /** Hexadecimal string representation of the 256-bit (32-byte) message nonce */ + nonce: string; + /** Hexadecimal string representation of 256-bit (32-byte), hash of MEM[$rA + 32, $rB] */ + digest: string; + /** Hexadecimal string representation of the value of the memory range MEM[$rA + 32, $rB] */ + data: Uint8Array; +}; + +export type ReceiptMint = { + type: ReceiptType.Mint; + + subId: string; + + contractId: string; + + assetId: string; + + val: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type ReceiptBurn = { + type: ReceiptType.Burn; + + subId: string; + + contractId: string; + + assetId: string; + + val: BN; + /** Value of register $pc (u64) */ + pc: BN; + /** Value of register $is (u64) */ + is: BN; +}; + +export type Receipt = + | ReceiptCall + | ReceiptReturn + | ReceiptReturnData + | ReceiptPanic + | ReceiptRevert + | ReceiptLog + | ReceiptLogData + | ReceiptTransfer + | ReceiptTransferOut + | ReceiptScriptResult + | ReceiptMessageOut + | ReceiptMint + | ReceiptBurn; + export const getMintedAssetId = (contractId: string, subId: string): string => { const contractIdBytes = arrayify(contractId); const subIdBytes = arrayify(subId); diff --git a/packages/transactions/src/index.ts b/packages/transactions/src/index.ts index 4bcedc5307a..5b3456c0d86 100644 --- a/packages/transactions/src/index.ts +++ b/packages/transactions/src/index.ts @@ -8,4 +8,3 @@ export * from './coders/witness'; export * from './coders/tx-pointer'; export * from './coders/utxo-id'; export * from './coders/byte-array'; -export * from './receipts'; diff --git a/packages/transactions/src/receipts.ts b/packages/transactions/src/receipts.ts deleted file mode 100644 index 64700248f86..00000000000 --- a/packages/transactions/src/receipts.ts +++ /dev/null @@ -1,236 +0,0 @@ -import type { BN } from '@fuel-ts/math'; - -export enum ReceiptType /* u8 */ { - Call = 0, - Return = 1, - ReturnData = 2, - Panic = 3, - Revert = 4, - Log = 5, - LogData = 6, - Transfer = 7, - TransferOut = 8, - ScriptResult = 9, - MessageOut = 10, - Mint = 11, - Burn = 12, -} - -export type ReceiptCall = { - type: ReceiptType.Call; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - from: string; - /** Contract ID of called contract (b256) */ - to: string; - /** Amount of coins to forward, i.e. $rB (u64) */ - amount: BN; - /** Asset ID of coins to forward, i.e. MEM[$rC, 32] (b256) */ - assetId: string; - /** Gas to forward, i.e. $rD (u64) */ - gas: BN; - /** First parameter (u64) */ - param1: BN; - /** Second parameter (u64) */ - param2: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptReturn = { - type: ReceiptType.Return; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - val: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptReturnData = { - type: ReceiptType.ReturnData; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - ptr: BN; - /** Value of register $rB (u64) */ - len: BN; - /** Hash of MEM[$rA, $rB] (b256) */ - digest: string; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptPanic = { - type: ReceiptType.Panic; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Panic reason (u64) */ - reason: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; - /** Value of optional contract ID */ - contractId: string; -}; - -export type ReceiptRevert = { - type: ReceiptType.Revert; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - val: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptLog = { - type: ReceiptType.Log; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - val0: BN; - /** Value of register $rB (u64) */ - val1: BN; - /** Value of register $rC (u64) */ - val2: BN; - /** Value of register $rD (u64) */ - val3: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptLogData = { - type: ReceiptType.LogData; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - id: string; - /** Value of register $rA (u64) */ - val0: BN; - /** Value of register $rB (u64) */ - val1: BN; - /** Value of register $rC (u64) */ - ptr: BN; - /** Value of register $rD (u64) */ - len: BN; - /** Hash of MEM[$rC, $rD] (b256) */ - digest: string; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptTransfer = { - type: ReceiptType.Transfer; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - from: string; - /** Contract ID of contract to transfer coins to (b256) */ - to: string; - /** Amount of coins transferred (u64) */ - amount: BN; - /** Asset ID of coins transferred (b256) */ - assetId: string; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptTransferOut = { - type: ReceiptType.TransferOut; - /** Contract ID of current context if in an internal context, zero otherwise (b256) */ - from: string; - /** Address to transfer coins to (b256) */ - to: string; - /** Amount of coins transferred (u64) */ - amount: BN; - /** Asset ID of coins transferred (b256) */ - assetId: string; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptScriptResult = { - type: ReceiptType.ScriptResult; - /** Result variant with embedded `PanicReason` in first 8 bits and `instr` (u64) */ - result: BN; - /** Gas consumed by the script (u64) */ - gasUsed: BN; -}; - -export type ReceiptMessageOut = { - type: ReceiptType.MessageOut; - /** Hexadecimal string representation of the 256-bit (32-byte) message ID */ - messageId: string; - /** Hexadecimal string representation of the 256-bit (32-byte) address of the message sender: MEM[$fp, 32] */ - sender: string; - /** Hexadecimal string representation of the 256-bit (32-byte) address of the message recipient: MEM[$rA, 32] */ - recipient: string; - /** Hexadecimal string representation of a 64-bit unsigned integer; value of register $rD */ - amount: BN; - /** Hexadecimal string representation of the 256-bit (32-byte) message nonce */ - nonce: string; - /** Hexadecimal string representation of 256-bit (32-byte), hash of MEM[$rA + 32, $rB] */ - digest: string; - /** Hexadecimal string representation of the value of the memory range MEM[$rA + 32, $rB] */ - data: Uint8Array; -}; - -export type ReceiptMint = { - type: ReceiptType.Mint; - - subId: string; - - contractId: string; - - assetId: string; - - val: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type ReceiptBurn = { - type: ReceiptType.Burn; - - subId: string; - - contractId: string; - - assetId: string; - - val: BN; - /** Value of register $pc (u64) */ - pc: BN; - /** Value of register $is (u64) */ - is: BN; -}; - -export type Receipt = - | ReceiptCall - | ReceiptReturn - | ReceiptReturnData - | ReceiptPanic - | ReceiptRevert - | ReceiptLog - | ReceiptLogData - | ReceiptTransfer - | ReceiptTransferOut - | ReceiptScriptResult - | ReceiptMessageOut - | ReceiptMint - | ReceiptBurn; From e4e3216710d8175945f8a92e75dce01cb667c933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Fri, 6 Sep 2024 08:31:19 -0300 Subject: [PATCH 10/14] Revert "adjusting tests" This reverts commit 2213a2273513cab5bd5a32cadd84919cf228777d. --- .../account/src/providers/utils/receipts.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/account/src/providers/utils/receipts.test.ts b/packages/account/src/providers/utils/receipts.test.ts index a4e3c252a49..cc9b6b86c0f 100644 --- a/packages/account/src/providers/utils/receipts.test.ts +++ b/packages/account/src/providers/utils/receipts.test.ts @@ -14,8 +14,8 @@ import type { ReceiptTransfer, ReceiptTransferOut, } from '@fuel-ts/transactions'; -import { getMintedAssetId, InputMessageCoder, ReceiptType } from '@fuel-ts/transactions'; -import { arrayify, hexlify } from '@fuel-ts/utils'; +import { ReceiptBurnCoder, ReceiptMessageOutCoder, ReceiptType } from '@fuel-ts/transactions'; +import { arrayify } from '@fuel-ts/utils'; import { MOCK_GQL_RECEIPT_FRAGMENT, @@ -219,12 +219,12 @@ describe('assembleReceiptByType', () => { const data = arrayify(MOCK_GQL_RECEIPT_FRAGMENT.data || ''); const digest = MOCK_GQL_RECEIPT_FRAGMENT.digest; - const messageId = InputMessageCoder.getMessageId({ + const messageId = ReceiptMessageOutCoder.getMessageId({ sender, recipient, nonce, amount, - data: hexlify(data), + data, }); expect(receipt.type).toBe(ReceiptType.MessageOut); @@ -240,7 +240,7 @@ describe('assembleReceiptByType', () => { it('should return a ReceiptMint when GqlReceiptType.Mint is provided', () => { const contractId = MOCK_GQL_RECEIPT_FRAGMENT.id || ''; const subId = MOCK_GQL_RECEIPT_FRAGMENT.subId || ''; - const assetId = getMintedAssetId(contractId, subId); + const assetId = ReceiptBurnCoder.getAssetId(contractId, subId); const receipt = assembleReceiptByType({ ...MOCK_GQL_RECEIPT_FRAGMENT, @@ -259,7 +259,7 @@ describe('assembleReceiptByType', () => { it('should return a ReceiptBurn when GqlReceiptType.Burn is provided', () => { const contractId = MOCK_GQL_RECEIPT_FRAGMENT.id || ''; const subId = MOCK_GQL_RECEIPT_FRAGMENT.subId || ''; - const assetId = getMintedAssetId(contractId, subId); + const assetId = ReceiptBurnCoder.getAssetId(contractId, subId); const receipt = assembleReceiptByType({ ...MOCK_GQL_RECEIPT_FRAGMENT, From c848e52b75e49aff2318a8c52b04a5e7ba9fe5f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Fri, 6 Sep 2024 08:31:41 -0300 Subject: [PATCH 11/14] Revert "remove receipt coders test suite" This reverts commit 9080b19440889cf52e3c92fa0ff3b357d070e654. --- .../transactions/src/coders/receipt.test.ts | 297 ++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 packages/transactions/src/coders/receipt.test.ts diff --git a/packages/transactions/src/coders/receipt.test.ts b/packages/transactions/src/coders/receipt.test.ts new file mode 100644 index 00000000000..1e2db2b91be --- /dev/null +++ b/packages/transactions/src/coders/receipt.test.ts @@ -0,0 +1,297 @@ +import { sha256 } from '@fuel-ts/hasher'; +import { bn } from '@fuel-ts/math'; +import { arrayify, hexlify, concat } from '@fuel-ts/utils'; + +import type { Receipt } from './receipt'; +import { ReceiptCoder, ReceiptMessageOutCoder, ReceiptType } from './receipt'; + +const B256 = '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'; +const B256_ALT1 = '0x750f560d912ec02d826af8ba3be90a9481fb6d3bc6b4e7f01a89f245cf0a7059'; +const B256_ALT2 = '0x68b401b682ba0c9018150cca596358a6b98576337ea10b9cfb0d02441b3bc61a'; +const B256_ALT3 = '0xeb03488970d05ea240c788a0ea2e07176cc5317b7c7c89f26ac5282bbcd445bd'; +const B256_ALT4 = '0x2f6d40e3ac1a172fb9445f9843440a0fc383bea238a7a35a77a3c73d36902992'; + +/** + * @group node + * @group browser + */ +describe('ReceiptCoder', () => { + it('Can encode Call', () => { + const receipt: Receipt = { + type: ReceiptType.Call, + from: B256, + to: B256, + amount: bn(0), + assetId: B256, + gas: bn(0), + param1: bn(0), + param2: bn(0), + pc: bn(0), + is: bn(0), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930bd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000000000000000000000000000000000000000000000000000' + ); + + const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); + + expect(offset).toEqual((encoded.length - 2) / 2); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode Return', () => { + const receipt: Receipt = { + type: ReceiptType.Return, + id: B256, + val: bn(0), + pc: bn(0), + is: bn(0), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x0000000000000001d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b000000000000000000000000000000000000000000000000' + ); + + const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); + + expect(offset).toEqual((encoded.length - 2) / 2); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode ReturnData', () => { + const receipt: Receipt = { + type: ReceiptType.ReturnData, + id: B256, + ptr: bn(0), + len: bn(0), + digest: B256, + pc: bn(0), + is: bn(0), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x0000000000000002d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000' + ); + + const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); + + expect(offset).toEqual((encoded.length - 2) / 2); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode Panic', () => { + const receipt: Receipt = { + type: ReceiptType.Panic, + id: B256, + reason: bn(0), + pc: bn(0), + is: bn(0), + contractId: B256, + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x0000000000000003d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b000000000000000000000000000000000000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b' + ); + + const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); + + expect(offset).toEqual((encoded.length - 2) / 2); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode Revert', () => { + const receipt: Receipt = { + type: ReceiptType.Revert, + id: B256, + val: bn(0), + pc: bn(0), + is: bn(0), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x0000000000000004d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b000000000000000000000000000000000000000000000000' + ); + + const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); + + expect(offset).toEqual((encoded.length - 2) / 2); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode Log', () => { + const receipt: Receipt = { + type: ReceiptType.Log, + id: B256, + val0: bn(0), + val1: bn(0), + val2: bn(0), + val3: bn(0), + pc: bn(0), + is: bn(0), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x0000000000000005d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' + ); + + const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); + + expect(offset).toEqual((encoded.length - 2) / 2); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode LogData', () => { + const receipt: Receipt = { + type: ReceiptType.LogData, + id: B256, + val0: bn(0), + val1: bn(0), + ptr: bn(0), + len: bn(0), + digest: B256, + pc: bn(0), + is: bn(0), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x0000000000000006d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000000000000000000000000000000000000000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000' + ); + + const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); + + expect(offset).toEqual((encoded.length - 2) / 2); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode Transfer', () => { + const receipt: Receipt = { + type: ReceiptType.Transfer, + from: B256, + to: B256, + amount: bn(0), + assetId: B256, + pc: bn(0), + is: bn(0), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x0000000000000007d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930bd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000' + ); + + const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); + + expect(offset).toEqual((encoded.length - 2) / 2); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode TransferOut', () => { + const receipt: Receipt = { + type: ReceiptType.TransferOut, + from: B256, + to: B256, + amount: bn(0), + assetId: B256, + pc: bn(0), + is: bn(0), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x0000000000000008d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930bd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000' + ); + + const [decoded, offset] = new ReceiptCoder().decode(arrayify(encoded), 0); + + expect(offset).toEqual((encoded.length - 2) / 2); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode MessageOut', () => { + const receipt: Receipt = { + type: ReceiptType.MessageOut, + messageId: '', + sender: B256_ALT1, + recipient: B256_ALT2, + amount: bn(4000), + nonce: B256_ALT3, + digest: B256_ALT4, + data: Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), + }; + receipt.messageId = ReceiptMessageOutCoder.getMessageId(receipt); + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x000000000000000a750f560d912ec02d826af8ba3be90a9481fb6d3bc6b4e7f01a89f245cf0a705968b401b682ba0c9018150cca596358a6b98576337ea10b9cfb0d02441b3bc61a0000000000000fa0eb03488970d05ea240c788a0ea2e07176cc5317b7c7c89f26ac5282bbcd445bd000000000000000c2f6d40e3ac1a172fb9445f9843440a0fc383bea238a7a35a77a3c73d369029920102030405060708090a0b0c00000000' + ); + + expect(arrayify(encoded).length).toEqual((encoded.length - 2) / 2); + + const [decoded] = new ReceiptCoder().decode(arrayify(encoded), 0); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode Mint', () => { + const receipt: Receipt = { + type: ReceiptType.Mint, + subId: B256_ALT3, + contractId: B256_ALT1, + val: bn(4000), + pc: bn(30), + is: bn(20), + assetId: sha256(concat([B256_ALT1, B256_ALT3])), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x000000000000000beb03488970d05ea240c788a0ea2e07176cc5317b7c7c89f26ac5282bbcd445bd750f560d912ec02d826af8ba3be90a9481fb6d3bc6b4e7f01a89f245cf0a70590000000000000fa0000000000000001e0000000000000014' + ); + + expect(arrayify(encoded).length).toEqual((encoded.length - 2) / 2); + + const [decoded] = new ReceiptCoder().decode(arrayify(encoded), 0); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); + + it('Can encode Burn', () => { + const receipt: Receipt = { + type: ReceiptType.Burn, + subId: B256_ALT1, + contractId: B256_ALT3, + val: bn(300), + pc: bn(90), + is: bn(10), + assetId: sha256(concat([B256_ALT3, B256_ALT1])), + }; + + const encoded = hexlify(new ReceiptCoder().encode(receipt)); + + expect(encoded).toEqual( + '0x000000000000000c750f560d912ec02d826af8ba3be90a9481fb6d3bc6b4e7f01a89f245cf0a7059eb03488970d05ea240c788a0ea2e07176cc5317b7c7c89f26ac5282bbcd445bd000000000000012c000000000000005a000000000000000a' + ); + + expect(arrayify(encoded).length).toEqual((encoded.length - 2) / 2); + + const [decoded] = new ReceiptCoder().decode(arrayify(encoded), 0); + expect(JSON.stringify(decoded)).toEqual(JSON.stringify(receipt)); + }); +}); From 2a239586e2412c599e843a4c3ba6911c0d06de99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Fri, 6 Sep 2024 08:31:53 -0300 Subject: [PATCH 12/14] Revert "remove all receipt coders" This reverts commit 380c5e79468d0451c8586b5409c7fd5a89f0fddc. --- packages/transactions/src/coders/receipt.ts | 820 +++++++++++++++++++- 1 file changed, 812 insertions(+), 8 deletions(-) diff --git a/packages/transactions/src/coders/receipt.ts b/packages/transactions/src/coders/receipt.ts index 92e0cce4b50..9e8f54881e0 100644 --- a/packages/transactions/src/coders/receipt.ts +++ b/packages/transactions/src/coders/receipt.ts @@ -1,8 +1,13 @@ +/* eslint-disable max-classes-per-file */ +import { Coder, BigNumberCoder, B256Coder, NumberCoder } from '@fuel-ts/abi-coder'; +import { ErrorCode, FuelError } from '@fuel-ts/errors'; import { sha256 } from '@fuel-ts/hasher'; import type { AssetId } from '@fuel-ts/interfaces'; import type { BN } from '@fuel-ts/math'; import { arrayify, concat } from '@fuel-ts/utils'; +import { ByteArrayCoder } from './byte-array'; + export enum ReceiptType /* u8 */ { Call = 0, Return = 1, @@ -41,6 +46,68 @@ export type ReceiptCall = { is: BN; }; +export class ReceiptCallCoder extends Coder { + constructor() { + super('ReceiptCall', 'struct ReceiptCall', 0); + } + + encode(value: ReceiptCall): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.from)); + parts.push(new B256Coder().encode(value.to)); + parts.push(new BigNumberCoder('u64').encode(value.amount)); + parts.push(new B256Coder().encode(value.assetId)); + parts.push(new BigNumberCoder('u64').encode(value.gas)); + parts.push(new BigNumberCoder('u64').encode(value.param1)); + parts.push(new BigNumberCoder('u64').encode(value.param2)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptCall, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const from = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const to = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const amount = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const assetId = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const gas = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const param1 = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const param2 = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + return [ + { + type: ReceiptType.Call, + from, + to, + amount, + assetId, + gas, + param1, + param2, + pc, + is, + }, + o, + ]; + } +} + export type ReceiptReturn = { type: ReceiptType.Return; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -53,6 +120,48 @@ export type ReceiptReturn = { is: BN; }; +export class ReceiptReturnCoder extends Coder { + constructor() { + super('ReceiptReturn', 'struct ReceiptReturn', 0); + } + + encode(value: ReceiptReturn): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.id)); + parts.push(new BigNumberCoder('u64').encode(value.val)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptReturn, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const id = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + return [ + { + type: ReceiptType.Return, + id, + val, + pc, + is, + }, + o, + ]; + } +} + export type ReceiptReturnData = { type: ReceiptType.ReturnData; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -69,6 +178,56 @@ export type ReceiptReturnData = { is: BN; }; +export class ReceiptReturnDataCoder extends Coder { + constructor() { + super('ReceiptReturnData', 'struct ReceiptReturnData', 0); + } + + encode(value: ReceiptReturnData): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.id)); + parts.push(new BigNumberCoder('u64').encode(value.ptr)); + parts.push(new BigNumberCoder('u64').encode(value.len)); + parts.push(new B256Coder().encode(value.digest)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptReturnData, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const id = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const ptr = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const len = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const digest = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + return [ + { + type: ReceiptType.ReturnData, + id, + ptr, + len, + digest, + pc, + is, + }, + o, + ]; + } +} + export type ReceiptPanic = { type: ReceiptType.Panic; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -83,6 +242,52 @@ export type ReceiptPanic = { contractId: string; }; +export class ReceiptPanicCoder extends Coder { + constructor() { + super('ReceiptPanic', 'struct ReceiptPanic', 0); + } + + encode(value: ReceiptPanic): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.id)); + parts.push(new BigNumberCoder('u64').encode(value.reason)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + parts.push(new B256Coder().encode(value.contractId)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptPanic, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const id = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const reason = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const contractId = decoded; + + return [ + { + type: ReceiptType.Panic, + id, + reason, + pc, + is, + contractId, + }, + o, + ]; + } +} + export type ReceiptRevert = { type: ReceiptType.Revert; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -95,6 +300,48 @@ export type ReceiptRevert = { is: BN; }; +export class ReceiptRevertCoder extends Coder { + constructor() { + super('ReceiptRevert', 'struct ReceiptRevert', 0); + } + + encode(value: ReceiptRevert): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.id)); + parts.push(new BigNumberCoder('u64').encode(value.val)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptRevert, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const id = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + return [ + { + type: ReceiptType.Revert, + id, + val, + pc, + is, + }, + o, + ]; + } +} + export type ReceiptLog = { type: ReceiptType.Log; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -113,6 +360,60 @@ export type ReceiptLog = { is: BN; }; +export class ReceiptLogCoder extends Coder { + constructor() { + super('ReceiptLog', 'struct ReceiptLog', 0); + } + + encode(value: ReceiptLog): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.id)); + parts.push(new BigNumberCoder('u64').encode(value.val0)); + parts.push(new BigNumberCoder('u64').encode(value.val1)); + parts.push(new BigNumberCoder('u64').encode(value.val2)); + parts.push(new BigNumberCoder('u64').encode(value.val3)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptLog, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const id = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val0 = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val1 = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val2 = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val3 = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + return [ + { + type: ReceiptType.Log, + id, + val0, + val1, + val2, + val3, + pc, + is, + }, + o, + ]; + } +} + export type ReceiptLogData = { type: ReceiptType.LogData; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -133,6 +434,64 @@ export type ReceiptLogData = { is: BN; }; +export class ReceiptLogDataCoder extends Coder { + constructor() { + super('ReceiptLogData', 'struct ReceiptLogData', 0); + } + + encode(value: ReceiptLogData): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.id)); + parts.push(new BigNumberCoder('u64').encode(value.val0)); + parts.push(new BigNumberCoder('u64').encode(value.val1)); + parts.push(new BigNumberCoder('u64').encode(value.ptr)); + parts.push(new BigNumberCoder('u64').encode(value.len)); + parts.push(new B256Coder().encode(value.digest)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptLogData, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const id = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val0 = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val1 = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const ptr = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const len = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const digest = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + return [ + { + type: ReceiptType.LogData, + id, + val0, + val1, + ptr, + len, + digest, + pc, + is, + }, + o, + ]; + } +} + export type ReceiptTransfer = { type: ReceiptType.Transfer; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -149,6 +508,56 @@ export type ReceiptTransfer = { is: BN; }; +export class ReceiptTransferCoder extends Coder { + constructor() { + super('ReceiptTransfer', 'struct ReceiptTransfer', 0); + } + + encode(value: ReceiptTransfer): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.from)); + parts.push(new B256Coder().encode(value.to)); + parts.push(new BigNumberCoder('u64').encode(value.amount)); + parts.push(new B256Coder().encode(value.assetId)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptTransfer, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const from = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const to = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const amount = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const assetId = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + return [ + { + type: ReceiptType.Transfer, + from, + to, + amount, + assetId, + pc, + is, + }, + o, + ]; + } +} + export type ReceiptTransferOut = { type: ReceiptType.TransferOut; /** Contract ID of current context if in an internal context, zero otherwise (b256) */ @@ -165,6 +574,56 @@ export type ReceiptTransferOut = { is: BN; }; +export class ReceiptTransferOutCoder extends Coder { + constructor() { + super('ReceiptTransferOut', 'struct ReceiptTransferOut', 0); + } + + encode(value: ReceiptTransferOut): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.from)); + parts.push(new B256Coder().encode(value.to)); + parts.push(new BigNumberCoder('u64').encode(value.amount)); + parts.push(new B256Coder().encode(value.assetId)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptTransferOut, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const from = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const to = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const amount = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const assetId = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + return [ + { + type: ReceiptType.TransferOut, + from, + to, + amount, + assetId, + pc, + is, + }, + o, + ]; + } +} + export type ReceiptScriptResult = { type: ReceiptType.ScriptResult; /** Result variant with embedded `PanicReason` in first 8 bits and `instr` (u64) */ @@ -173,6 +632,40 @@ export type ReceiptScriptResult = { gasUsed: BN; }; +export class ReceiptScriptResultCoder extends Coder { + constructor() { + super('ReceiptScriptResult', 'struct ReceiptScriptResult', 0); + } + + encode(value: ReceiptScriptResult): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new BigNumberCoder('u64').encode(value.result)); + parts.push(new BigNumberCoder('u64').encode(value.gasUsed)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptScriptResult, number] { + let decoded; + let o = offset; + + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const result = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const gasUsed = decoded; + + return [ + { + type: ReceiptType.ScriptResult, + result, + gasUsed, + }, + o, + ]; + } +} + export type ReceiptMessageOut = { type: ReceiptType.MessageOut; /** Hexadecimal string representation of the 256-bit (32-byte) message ID */ @@ -191,6 +684,74 @@ export type ReceiptMessageOut = { data: Uint8Array; }; +export class ReceiptMessageOutCoder extends Coder { + constructor() { + super('ReceiptMessageOut', 'struct ReceiptMessageOut', 0); + } + + static getMessageId( + value: Pick + ): string { + const parts: Uint8Array[] = []; + + parts.push(new ByteArrayCoder(32).encode(value.sender)); + parts.push(new ByteArrayCoder(32).encode(value.recipient)); + parts.push(new ByteArrayCoder(32).encode(value.nonce)); + parts.push(new BigNumberCoder('u64').encode(value.amount)); + parts.push(arrayify(value.data || '0x')); + + return sha256(concat(parts)); + } + + encode(value: Omit): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.sender)); + parts.push(new B256Coder().encode(value.recipient)); + parts.push(new BigNumberCoder('u64').encode(value.amount)); + parts.push(new B256Coder().encode(value.nonce)); + parts.push(new NumberCoder('u16', { padToWordSize: true }).encode(value.data.length)); + parts.push(new B256Coder().encode(value.digest)); + parts.push(new ByteArrayCoder(value.data.length).encode(value.data)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptMessageOut, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const sender = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const recipient = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const amount = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const nonce = decoded; + [decoded, o] = new NumberCoder('u16', { padToWordSize: true }).decode(data, o); + const len = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const digest = decoded; + [decoded, o] = new ByteArrayCoder(len).decode(data, o); + const messageData = arrayify(decoded); + + const receiptMessageOut: ReceiptMessageOut = { + type: ReceiptType.MessageOut, + messageId: '', + sender, + recipient, + amount, + nonce, + digest, + data: messageData, + }; + receiptMessageOut.messageId = ReceiptMessageOutCoder.getMessageId(receiptMessageOut); + + return [receiptMessageOut, o]; + } +} + export type ReceiptMint = { type: ReceiptType.Mint; @@ -207,6 +768,69 @@ export type ReceiptMint = { is: BN; }; +export const getMintedAssetId = (contractId: string, subId: string): string => { + const contractIdBytes = arrayify(contractId); + const subIdBytes = arrayify(subId); + + return sha256(concat([contractIdBytes, subIdBytes])); +}; + +export const createAssetId = (contractId: string, subId: string): AssetId => ({ + bits: getMintedAssetId(contractId, subId), +}); + +export class ReceiptMintCoder extends Coder { + constructor() { + super('ReceiptMint', 'struct ReceiptMint', 0); + } + + static getAssetId(contractId: string, subId: string): string { + return getMintedAssetId(contractId, subId); + } + + encode(value: ReceiptMint): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.subId)); + parts.push(new B256Coder().encode(value.contractId)); + parts.push(new BigNumberCoder('u64').encode(value.val)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptMint, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const subId = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const contractId = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + const assetId = ReceiptMintCoder.getAssetId(contractId, subId); + + const receiptMint: ReceiptMint = { + type: ReceiptType.Mint, + subId, + contractId, + val, + pc, + is, + assetId, + }; + + return [receiptMint, o]; + } +} + export type ReceiptBurn = { type: ReceiptType.Burn; @@ -223,6 +847,58 @@ export type ReceiptBurn = { is: BN; }; +export class ReceiptBurnCoder extends Coder { + constructor() { + super('ReceiptBurn', 'struct ReceiptBurn', 0); + } + + static getAssetId(contractId: string, subId: string): string { + return getMintedAssetId(contractId, subId); + } + + encode(value: ReceiptBurn): Uint8Array { + const parts: Uint8Array[] = []; + + parts.push(new B256Coder().encode(value.subId)); + parts.push(new B256Coder().encode(value.contractId)); + parts.push(new BigNumberCoder('u64').encode(value.val)); + parts.push(new BigNumberCoder('u64').encode(value.pc)); + parts.push(new BigNumberCoder('u64').encode(value.is)); + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [ReceiptBurn, number] { + let decoded; + let o = offset; + + [decoded, o] = new B256Coder().decode(data, o); + const subId = decoded; + [decoded, o] = new B256Coder().decode(data, o); + const contractId = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const val = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const pc = decoded; + [decoded, o] = new BigNumberCoder('u64').decode(data, o); + const is = decoded; + + const assetId = ReceiptMintCoder.getAssetId(contractId, subId); + + const receiptBurn: ReceiptBurn = { + type: ReceiptType.Burn, + subId, + contractId, + val, + pc, + is, + assetId, + }; + + return [receiptBurn, o]; + } +} + export type Receipt = | ReceiptCall | ReceiptReturn @@ -238,13 +914,141 @@ export type Receipt = | ReceiptMint | ReceiptBurn; -export const getMintedAssetId = (contractId: string, subId: string): string => { - const contractIdBytes = arrayify(contractId); - const subIdBytes = arrayify(subId); +export class ReceiptCoder extends Coder { + constructor() { + super('Receipt', 'struct Receipt', 0); + } - return sha256(concat([contractIdBytes, subIdBytes])); -}; + encode(value: Receipt): Uint8Array { + const parts: Uint8Array[] = []; -export const createAssetId = (contractId: string, subId: string): AssetId => ({ - bits: getMintedAssetId(contractId, subId), -}); + parts.push(new NumberCoder('u8', { padToWordSize: true }).encode(value.type)); + + const { type } = value; + + switch (value.type) { + case ReceiptType.Call: { + parts.push(new ReceiptCallCoder().encode(value)); + break; + } + case ReceiptType.Return: { + parts.push(new ReceiptReturnCoder().encode(value)); + break; + } + case ReceiptType.ReturnData: { + parts.push(new ReceiptReturnDataCoder().encode(value)); + break; + } + case ReceiptType.Panic: { + parts.push(new ReceiptPanicCoder().encode(value)); + break; + } + case ReceiptType.Revert: { + parts.push(new ReceiptRevertCoder().encode(value)); + break; + } + case ReceiptType.Log: { + parts.push(new ReceiptLogCoder().encode(value)); + break; + } + case ReceiptType.LogData: { + parts.push(new ReceiptLogDataCoder().encode(value)); + break; + } + case ReceiptType.Transfer: { + parts.push(new ReceiptTransferCoder().encode(value)); + break; + } + case ReceiptType.TransferOut: { + parts.push(new ReceiptTransferOutCoder().encode(value)); + break; + } + case ReceiptType.ScriptResult: { + parts.push(new ReceiptScriptResultCoder().encode(value)); + break; + } + case ReceiptType.MessageOut: { + parts.push(new ReceiptMessageOutCoder().encode(value)); + break; + } + case ReceiptType.Mint: { + parts.push(new ReceiptMintCoder().encode(value)); + break; + } + case ReceiptType.Burn: { + parts.push(new ReceiptBurnCoder().encode(value)); + break; + } + default: { + throw new FuelError(ErrorCode.INVALID_RECEIPT_TYPE, `Invalid receipt type: ${type}`); + } + } + + return concat(parts); + } + + decode(data: Uint8Array, offset: number): [Receipt, number] { + let decoded; + let o = offset; + + [decoded, o] = new NumberCoder('u8', { padToWordSize: true }).decode(data, o); + const type = decoded as ReceiptType; + switch (type) { + case ReceiptType.Call: { + [decoded, o] = new ReceiptCallCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.Return: { + [decoded, o] = new ReceiptReturnCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.ReturnData: { + [decoded, o] = new ReceiptReturnDataCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.Panic: { + [decoded, o] = new ReceiptPanicCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.Revert: { + [decoded, o] = new ReceiptRevertCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.Log: { + [decoded, o] = new ReceiptLogCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.LogData: { + [decoded, o] = new ReceiptLogDataCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.Transfer: { + [decoded, o] = new ReceiptTransferCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.TransferOut: { + [decoded, o] = new ReceiptTransferOutCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.ScriptResult: { + [decoded, o] = new ReceiptScriptResultCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.MessageOut: { + [decoded, o] = new ReceiptMessageOutCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.Mint: { + [decoded, o] = new ReceiptMintCoder().decode(data, o); + return [decoded, o]; + } + case ReceiptType.Burn: { + [decoded, o] = new ReceiptBurnCoder().decode(data, o); + return [decoded, o]; + } + default: { + throw new FuelError(ErrorCode.INVALID_RECEIPT_TYPE, `Invalid receipt type: ${type}`); + } + } + } +} From 7ce1887d6d319d37609e6bfe92b187fd6617d307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Fri, 6 Sep 2024 09:01:15 -0300 Subject: [PATCH 13/14] add deprecate notice to receipts coders --- packages/transactions/src/coders/receipt.ts | 82 +++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/packages/transactions/src/coders/receipt.ts b/packages/transactions/src/coders/receipt.ts index 9e8f54881e0..cb25b5bed29 100644 --- a/packages/transactions/src/coders/receipt.ts +++ b/packages/transactions/src/coders/receipt.ts @@ -46,6 +46,11 @@ export type ReceiptCall = { is: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptCallCoder extends Coder { constructor() { super('ReceiptCall', 'struct ReceiptCall', 0); @@ -120,6 +125,11 @@ export type ReceiptReturn = { is: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptReturnCoder extends Coder { constructor() { super('ReceiptReturn', 'struct ReceiptReturn', 0); @@ -178,6 +188,11 @@ export type ReceiptReturnData = { is: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptReturnDataCoder extends Coder { constructor() { super('ReceiptReturnData', 'struct ReceiptReturnData', 0); @@ -242,6 +257,11 @@ export type ReceiptPanic = { contractId: string; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptPanicCoder extends Coder { constructor() { super('ReceiptPanic', 'struct ReceiptPanic', 0); @@ -300,6 +320,11 @@ export type ReceiptRevert = { is: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptRevertCoder extends Coder { constructor() { super('ReceiptRevert', 'struct ReceiptRevert', 0); @@ -360,6 +385,11 @@ export type ReceiptLog = { is: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptLogCoder extends Coder { constructor() { super('ReceiptLog', 'struct ReceiptLog', 0); @@ -434,6 +464,11 @@ export type ReceiptLogData = { is: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptLogDataCoder extends Coder { constructor() { super('ReceiptLogData', 'struct ReceiptLogData', 0); @@ -508,6 +543,11 @@ export type ReceiptTransfer = { is: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptTransferCoder extends Coder { constructor() { super('ReceiptTransfer', 'struct ReceiptTransfer', 0); @@ -574,6 +614,11 @@ export type ReceiptTransferOut = { is: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptTransferOutCoder extends Coder { constructor() { super('ReceiptTransferOut', 'struct ReceiptTransferOut', 0); @@ -632,6 +677,11 @@ export type ReceiptScriptResult = { gasUsed: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptScriptResultCoder extends Coder { constructor() { super('ReceiptScriptResult', 'struct ReceiptScriptResult', 0); @@ -684,11 +734,20 @@ export type ReceiptMessageOut = { data: Uint8Array; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptMessageOutCoder extends Coder { constructor() { super('ReceiptMessageOut', 'struct ReceiptMessageOut', 0); } + /** + * @deprecated `ReceiptMessageOutCoder.getMessageId` is deprecated and will be removed in future versions. + * Use the static method `InputMessageCoder.getMessageId` instead. + */ static getMessageId( value: Pick ): string { @@ -779,11 +838,20 @@ export const createAssetId = (contractId: string, subId: string): AssetId => ({ bits: getMintedAssetId(contractId, subId), }); +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptMintCoder extends Coder { constructor() { super('ReceiptMint', 'struct ReceiptMint', 0); } + /** + * @deprecated `ReceiptMintCoder.getAssetId` is deprecated and will be removed in future versions. + * Use the helper function `getMintedAssetId` instead. + */ static getAssetId(contractId: string, subId: string): string { return getMintedAssetId(contractId, subId); } @@ -847,11 +915,20 @@ export type ReceiptBurn = { is: BN; }; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptBurnCoder extends Coder { constructor() { super('ReceiptBurn', 'struct ReceiptBurn', 0); } + /** + * @deprecated `ReceiptBurnCoder.getAssetId` is deprecated and will be removed in future versions. + * Use the helper function `getMintedAssetId` instead. + */ static getAssetId(contractId: string, subId: string): string { return getMintedAssetId(contractId, subId); } @@ -914,6 +991,11 @@ export type Receipt = | ReceiptMint | ReceiptBurn; +/** + * @deprecated Receipt Coders are deprecated and will be removed in future versions + * because decoding receipts is no longer necessary. No replacement is required as + * this functionality is obsolete. + */ export class ReceiptCoder extends Coder { constructor() { super('Receipt', 'struct Receipt', 0); From b4c267853a5d82582d5a04004779788efa4c14a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Fri, 6 Sep 2024 09:09:51 -0300 Subject: [PATCH 14/14] modify changeset --- .changeset/olive-apples-brush.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/olive-apples-brush.md b/.changeset/olive-apples-brush.md index e705890f6cc..0429b5efa7c 100644 --- a/.changeset/olive-apples-brush.md +++ b/.changeset/olive-apples-brush.md @@ -3,4 +3,4 @@ "@fuel-ts/account": patch --- -chore: remove all receipt coders +chore: deprecate all receipt coders