Skip to content

Commit

Permalink
fix: make MintTransaction to conform with new fuel-core 0.21 up…
Browse files Browse the repository at this point in the history
…grade (#1503)
  • Loading branch information
Torres-ssf authored Dec 11, 2023
1 parent d13a831 commit 280facf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-pens-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/transactions": minor
---

update Mint transaction to conform with fuel-core 0.21
56 changes: 22 additions & 34 deletions packages/transactions/src/coders/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,28 +246,39 @@ describe('TransactionCoder', () => {
);
});

it('Can encode/decode TransactionMint with outputs', () => {
it('Can encode/decode TransactionMint', () => {
const transaction: Transaction<TransactionType.Mint> = {
type: TransactionType.Mint,
outputsCount: 1,
outputs: [
{
type: OutputType.Coin,
to: B256,
amount: bn(1),
assetId: B256,
},
],
txPointer: {
blockHeight: 0,
txIndex: 0,
},
inputContract: {
type: InputType.Contract,
txID: B256,
outputIndex: 0,
balanceRoot: B256,
stateRoot: B256,
contractID: B256,
txPointer: {
blockHeight: 0,
txIndex: 0,
},
},
outputContract: {
type: OutputType.Contract,
inputIndex: 0,
balanceRoot: B256,
stateRoot: B256,
},
mintAmount: bn(0),
mintAssetId: B256,
};

const encoded = hexlify(new TransactionCoder().encode(transaction));

expect(encoded).toEqual(
'0x00000000000000020000000000000000000000000000000000000000000000010000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000001d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'
'0x000000000000000200000000000000000000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930bd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b00000000000000000000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930bd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b0000000000000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'
);

const [decoded, offset] = new TransactionCoder().decode(getBytesCopy(encoded), 0);
Expand All @@ -277,27 +288,4 @@ describe('TransactionCoder', () => {
JSON.parse(JSON.stringify(transaction))
);
});

it('Can encode/decode TransactionMint without outputs', () => {
const transaction: Transaction<TransactionType.Mint> = {
type: TransactionType.Mint,
outputsCount: 0,
outputs: [],
txPointer: {
blockHeight: 0,
txIndex: 0,
},
};

const encoded = hexlify(new TransactionCoder().encode(transaction));

expect(encoded).toEqual('0x0000000000000002000000000000000000000000000000000000000000000000');

const [decoded, offset] = new TransactionCoder().decode(getBytesCopy(encoded), 0);

expect(offset).toEqual((encoded.length - 2) / 2);
expect(JSON.parse(JSON.stringify(decoded))).toMatchObject(
JSON.parse(JSON.stringify(transaction))
);
});
});
50 changes: 32 additions & 18 deletions packages/transactions/src/coders/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { type BN } from '@fuel-ts/math';
import { concat } from '@fuel-ts/utils';

import { ByteArrayCoder } from './byte-array';
import type { Input } from './input';
import { InputCoder } from './input';
import type { Output } from './output';
import { OutputCoder } from './output';
import type { Input, InputContract } from './input';
import { InputCoder, InputContractCoder } from './input';
import type { Output, OutputContract } from './output';
import { OutputCoder, OutputContractCoder } from './output';
import type { Policy } from './policy';
import { PoliciesCoder } from './policy';
import { StorageSlotCoder } from './storage-slot';
Expand Down Expand Up @@ -278,14 +278,20 @@ export class TransactionCreateCoder extends Coder<TransactionCreate, Transaction
export type TransactionMint = {
type: TransactionType.Mint;

/** Number of outputs (u8) */
outputsCount: number;

/** List of outputs (Output[]) */
outputs: Output[];

/** The location of the Mint transaction in the block. */
txPointer: TxPointer;

/** The contract utxo that assets are minted to. */
inputContract: InputContract;

/** The contract utxo that assets are being minted to. */
outputContract: OutputContract;

/** The amount of funds minted. */
mintAmount: BN;

/** The asset ID corresponding to the minted amount. */
mintAssetId: string;
};

export class TransactionMintCoder extends Coder<TransactionMint, TransactionMint> {
Expand All @@ -297,8 +303,10 @@ export class TransactionMintCoder extends Coder<TransactionMint, TransactionMint
const parts: Uint8Array[] = [];

parts.push(new TxPointerCoder().encode(value.txPointer));
parts.push(new NumberCoder('u8').encode(value.outputsCount));
parts.push(new ArrayCoder(new OutputCoder(), value.outputsCount).encode(value.outputs));
parts.push(new InputContractCoder().encode(value.inputContract));
parts.push(new OutputContractCoder().encode(value.outputContract));
parts.push(new U64Coder().encode(value.mintAmount));
parts.push(new B256Coder().encode(value.mintAssetId));

return concat(parts);
}
Expand All @@ -309,17 +317,23 @@ export class TransactionMintCoder extends Coder<TransactionMint, TransactionMint

[decoded, o] = new TxPointerCoder().decode(data, o);
const txPointer = decoded;
[decoded, o] = new NumberCoder('u8').decode(data, o);
const outputsCount = decoded;
[decoded, o] = new ArrayCoder(new OutputCoder(), outputsCount).decode(data, o);
const outputs = decoded;
[decoded, o] = new InputContractCoder().decode(data, o);
const inputContract = decoded;
[decoded, o] = new OutputContractCoder().decode(data, o);
const outputContract = decoded;
[decoded, o] = new U64Coder().decode(data, o);
const mintAmount = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const mintAssetId = decoded;

return [
{
type: TransactionType.Mint,
outputsCount,
outputs,
txPointer,
inputContract,
outputContract,
mintAmount,
mintAssetId,
},
o,
];
Expand Down

0 comments on commit 280facf

Please sign in to comment.