Open mikemaccana opened 10 months ago
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 */
}
}
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:
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!
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
Proposing
Example use case
A JS user, familiar with common JS practices of specifying config as a plain JS
Object
or TSRecord<string, unknown>
makes a transaction withcreateTransaction()
. They know transactions have a series of instructions, so look for theinstructions
key find it. They add an array instructions as the value.