Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it should be possible to create a transaction using plain objects #1693

Open
mikemaccana opened this issue Oct 6, 2023 · 3 comments
Open
Labels
enhancement New feature or request

Comments

@mikemaccana
Copy link
Contributor

mikemaccana commented Oct 6, 2023

Motivation

Piping and method chaining are not common approach to configuration in JS SDKs.

  • JS doesn't include an inbuilt pipeline operator, and the proposal to add one has been in draft since 2015, and is still in stage 2, which means it is still a draft and subject to change. Since pipelining isn't part of JS/TS, less people are familiar with the concept than with other languages. Many JS/TS front end developers do not have experience with languages (eg Elixir) where pipelinign is more popular.

  • Well regarded APIs like node.js, Svelte, React, Stripe, and Twilio use plain JS Object / TS Records to do configuration, rather than having end users use pipelining, method chaining, or similar.

It should be possible to create a transaction using Plain Old JavaScript Objects:

Before

// Create a transaction.
const transaction = pipe(
  createTransaction({ version: 0 }),
  (transaction) =>
    appendTransactionInstruction(
      getTransferInstruction(
        publicKey,
        destinationAddress,
        lamports(10_000_000n)
      ),
      transaction
    ),
  (transaction) => setTransactionFeePayer(publicKey, transaction),
  (transaction) =>
    setTransactionLifetimeUsingBlockhash(latestBlockhash, transaction)
);

Proposing

// Create a transaction.
const transaction = createTransaction({ 
  version: 0 
  instructions: [
    createInstruction(
      senderAddress,
      destinationAddress,
      lamports(10_000_000n)
    )
  ]
  feePayer: senderAddress,
  latestBlockhash
})

Example use case

A JS user, familiar with common JS practices of specifying config as a plain JS Object or TS Record<string, unknown> makes a transaction with createTransaction(). They know transactions have a series of instructions, so look for the instructions key find it. They add an array instructions as the value.

@mikemaccana mikemaccana added the enhancement New feature or request label Oct 6, 2023
@buffalojoec
Copy link
Collaborator

I guess it would be totally possible to just use pipe under the hood here, but the resulting object would still be frozen like we're doing with each of those modular transaction modifiers.

We could roll something along these lines:

export function createTransaction(config: {
   feePayer?: Base58EncodedAddress,
   lifetime?: BlockhashLifetime | DurableNonceLifetime,
   instructions?: TransactionInstruction[],
   version: 'legacy' | 0,
}): Transaction | Transaction & TransactionWithFeePayer | Transaction & TransactionWithFeePayer & TransactionWithBlockhashLifetime {
   if 'feePayer' in config {
      pipe(
         createTransactionInner({ version: config.version }),
         tx => setTransactionFeePayer(config.feePayer, tx),
      )
   } else if {
      /* all the rest */
   }
}

@mikemaccana mikemaccana changed the title it should be possible to create a transaction using plain objects, without pipelining. it should be possible to create a transaction using plain objects Oct 6, 2023
@mikemaccana
Copy link
Contributor Author

mikemaccana commented Nov 22, 2023

Pardon the wait, it's been a long month with Breakpoint

The intention is that the result not be frozen - rather that the transaction is undateable using standard JS mechanisms (object and array accessors) until the moment the transaction is sent.

Signing, building a JSON-RPC body and sending would be handled as an atomic operation after the developer considers the transaction 'complete', ie, so the signature always matches the transaction body. For example a developer could add a instruction by using array methods they already know, or set values inside the transaction using object.key accessors.

When the developer wants to sign and send the transaction, they run const txID = await signAndSendTransaction(transaction) and:

  • a recent blockhash is added, if needed
  • a transaction version is added, if not specified
  • the transaction is signed with the private keys listed
  • the resulting JSON RPC body is built
  • the JSON RPC body is sent to the RPC server and confirmed

@buffalojoec
Copy link
Collaborator

buffalojoec commented Nov 22, 2023

Thanks for circling back @mikemaccana, I think @lorisleiva has just the thing for you on this.
You can see glimpse of how this is going to look with the new signers API in #1792.

TL/DR: If you want short-hand, you'll do something like createDefaultTransaction(instructions) where instructions have actual signers in them as IAccountMeta, and then you'll signSendAndConfirmTransaction(..).

So it's coming along nicely!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants