How to add a message input to TransactionRequestLike

Hi All,
In typescript and working with the FuelConnector, one of the parameters the FuelConnector sendTransaction() function takes is a TransactionRequestLike.

  sendTransaction(address: string, transaction: TransactionRequestLike): Promise<string>;

If i have set up the following dummy TransactionRequestLike tx, how can i add a Message input to the this type?

// as an example:

// Dummy tx:
  const script = Uint8Array.from([1, 2, 3, 4]);
  const scriptData = Uint8Array.from([5, 6]);
  const txRequestLike: TransactionRequestLike = {
    type: TransactionType.Script,
    script,
    scriptData,
    tip: 1,
    gasLimit: 10000,
    maturity: 1,
    inputs: [],
    outputs: [],
    witnesses: [],
  };

 // ... then sends it off in ...

const result = await currentConnector?.sendTransaction(
  currentAccount,
  txRequestLike,
);

I can only find references to adding a Message to a ScriptTransactionRequest in transaction-request.ts, I realize these data structures are not quite the same and have different purposes. Are there any examples of adding a Message (or request to have a message added) to TransactionRequestLike floating around?

snip from fuels-ts:
packages/account/src/providers/transaction-request/transaction-request.ts

  /**
   * Adds a single message input to the transaction and a change output for the
   * asset against the message
   *
   * @param message - Message resource.
   */
  addMessageInput(message: Message | MessageCoin) {
    const { recipient, sender, amount, predicate, nonce, predicateData } = message;

    let witnessIndex;

    if (message.predicate) {
      witnessIndex = 0;
    } else {
      witnessIndex = this.getCoinInputWitnessIndexByOwner(recipient);

      // Insert a dummy witness if no witness exists
      if (typeof witnessIndex !== 'number') {
        witnessIndex = this.addEmptyWitness();
      }
    }

    const input: MessageTransactionRequestInput = {
      nonce,
      type: InputType.Message,
      sender: sender.toB256(),
      recipient: recipient.toB256(),
      data: isMessageCoin(message) ? '0x' : message.data,
      amount,
      witnessIndex,
      predicate,
      predicateData,
    };

    // Insert the Input
    this.pushInput(input);

    // Insert a ChangeOutput if it does not exist
    if (isMessageCoin(message)) {
      this.addChangeOutput(recipient, message.assetId);
    }
  }
1 Like

Ok i think it could be something like:


import { TransactionRequestLike } from "@fuels";
import { InputType, TransactionType } from '@fuel-ts/transactions';
import { bn } from '@fuel-ts/math';
import { Address } from '@fuel-ts/address';

...

  const senderAddress = Address.fromRandom();
  const recipientAddress = Address.fromRandom();

  const txRequestLike: TransactionRequestLike = {
    type: TransactionType.Script,
    script: Uint8Array.from([1, 2, 3, 4]),
    scriptData: Uint8Array.from([5, 6]),
    tip: 1,
    gasLimit: 10000,
    maturity: 1,
    inputs: [
      {
        type: InputType.Message,
        amount: bn(100), 
        sender: senderAddress.toB256(), 
        recipient: recipientAddress.toB256(), 
        data: '0x', 
        nonce: '0x0000000000000000000000000000000000000000000000000000000000000000', // Message nonce
        witnessIndex: 0, 
        // Optional predicate fields
        predicate: '0x', 
        predicateData: '0x', 
      }
    ],
    outputs: [],
    witnesses: [
      '0x0000000000000000000000000000000000000000000000000000000000000000' +
      '0000000000000000000000000000000000000000000000000000000000000000'
    ],
  };

Hey @Antony , actually the first suggestion you made was correct. You can see an example of this in Adding Resources to a Transaction Request

// Instantiate the transaction request
const transactionRequest = new ScriptTransactionRequest({
  script: SumScript.bytecode,
});
 
const message: MessageCoin = {
  assetId: provider.getBaseAssetId(),
  sender: address,
  recipient: address,
  nonce: '0x',
  amount: bn(0),
  daHeight: bn(0),
};

// Adding message inputs
transactionRequest.addMessageInput(message);

Awesome, thanks for the reply and info @maschad !

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.