solana-labs / solana-web3.js

Solana JavaScript SDK
https://solana-labs.github.io/solana-web3.js
MIT License
2.12k stars 847 forks source link

`setTransactionMessageFeePayerSigner` does not accept a `CompilableTransactionMessage` as input #2854

Closed steveluscher closed 3 months ago

steveluscher commented 3 months ago
Argument of type 'CompilableTransactionMessage' is not assignable to parameter of type '(Readonly<{ instructions: readonly IInstruction<string, readonly (IAccountMeta<string> | IAccountLookupMeta<string, string>)[]>[]; version: TransactionVersion; }> & TransactionMessageWithBlockhashLifetime) | (ITransactionMessageWithFeePayer<...> & ... 1 more ... & TransactionMessageWithBlockhashLifetime)'.
  Type 'Readonly<{ instructions: readonly IInstruction<string, readonly (IAccountMeta<string> | IAccountLookupMeta<string, string>)[]>[]; version: TransactionVersion; }> & ITransactionMessageWithFeePayer<...> & TransactionMessageWithDurableNonceLifetime<...>' is not assignable to type '(Readonly<{ instructions: readonly IInstruction<string, readonly (IAccountMeta<string> | IAccountLookupMeta<string, string>)[]>[]; version: TransactionVersion; }> & TransactionMessageWithBlockhashLifetime) | (ITransactionMessageWithFeePayer<...> & ... 1 more ... & TransactionMessageWithBlockhashLifetime)'.
    Type 'Readonly<{ instructions: readonly IInstruction<string, readonly (IAccountMeta<string> | IAccountLookupMeta<string, string>)[]>[]; version: TransactionVersion; }> & ITransactionMessageWithFeePayer<...> & TransactionMessageWithDurableNonceLifetime<...>' is not assignable to type 'ITransactionMessageWithFeePayer<string> & Readonly<{ instructions: readonly IInstruction<string, readonly (IAccountMeta<string> | IAccountLookupMeta<string, string>)[]>[]; version: TransactionVersion; }> & TransactionMessageWithBlockhashLifetime'.
      Type 'Readonly<{ instructions: readonly IInstruction<string, readonly (IAccountMeta<string> | IAccountLookupMeta<string, string>)[]>[]; version: TransactionVersion; }> & ITransactionMessageWithFeePayer<...> & TransactionMessageWithDurableNonceLifetime<...>' is not assignable to type 'TransactionMessageWithBlockhashLifetime'.
        Types of property 'lifetimeConstraint' are incompatible.
          Type 'Readonly<{ nonce: Nonce<string>; }>' is missing the following properties from type 'Readonly<{ blockhash: Blockhash; lastValidBlockHeight: bigint; }>': blockhash, lastValidBlockHeight

Sandbox link

lorisleiva commented 3 months ago

This error seems to also occur with the setTransactionMessageFeePayer function.

import {
  CompilableTransactionMessage,
  setTransactionMessageFeePayer,
} from "@solana/web3.js";

const payer = null as unknown as Address;
const compilableMessage = null as unknown as CompilableTransactionMessage;

setTransactionMessageFeePayer(payer, compilableMessage);

Whilst CompilableTransactionMessage satisfies BaseTransactionMessage, I think TypeScript isn't able to resolve infer the TTransaction type parameter properly using the following signature:

export function setTransactionMessageFeePayer<
    TFeePayerAddress extends string,
    TTransaction extends BaseTransactionMessage,
>(
    feePayer: Address<TFeePayerAddress>,
  transaction: TTransaction | (ITransactionMessageWithFeePayer<string> & TTransaction),
): ITransactionMessageWithFeePayer<TFeePayerAddress> & TTransaction;

The following change seems to make TypeScript happy but I guess by doing this we are no longer telling TS to remove the existing ITransactionMessageWithFeePayer flag if any exists.

  export function setTransactionMessageFeePayer<
      TFeePayerAddress extends string,
      TTransaction extends BaseTransactionMessage,
  >(
      feePayer: Address<TFeePayerAddress>,
-     transaction: TTransaction | (ITransactionMessageWithFeePayer<string> & TTransaction),
+     transaction: TTransaction,
  ): ITransactionMessageWithFeePayer<TFeePayerAddress> & TTransaction;

EDIT: perhaps something like this could do the trick?

export function setTransactionMessageFeePayer<
    TFeePayerAddress extends string,
    TTransaction extends BaseTransactionMessage,
>(
    feePayer: Address<TFeePayerAddress>,
    transaction: TTransaction,
): TTransaction extends ITransactionMessageWithFeePayer<string>
    ? ITransactionMessageWithFeePayer<TFeePayerAddress> & Omit<TTransaction, 'feePayer'>
    : ITransactionMessageWithFeePayer<TFeePayerAddress> & TTransaction;
steveluscher commented 3 months ago

EDIT: perhaps something like this could do the trick?

Yeah, that 100% works. I'll PR that.

github-actions[bot] commented 2 months ago

:tada: This issue has been resolved in version 1.95.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 2 months ago

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.