โš™๏ธTransaction Factory

The TransactionFactory component of the SDK is outlined in the interface found here. This component allows the user to craft executable solana transactions.

Instantiating an TransactionFactory

Mainnet

import { clusterApiUrl, Connection } from "@solana/web3.js";
import { TransactionFactoryImpl } from "@seabed-labs/pre-authorized-debit";

// You can use any connection object you'd like, this is just an example
const connection = new Connection(clusterApiUrl("mainnet-beta"));

const txFactory = TransactionFactoryImpl.mainnet(connection);

Devnet

import { clusterApiUrl, Connection } from "@solana/web3.js";
import { TransactionFactoryImpl } from "@seabed-labs/pre-authorized-debit";

// You can use any connection object you'd like, this is just an example
const connection = new Connection(clusterApiUrl("devnet"));

const txFactory = TransactionFactoryImpl.devnet(connection);

Custom

Point the instruction factory to a custom deployment on any cluster:

Supported Methods

The transaction factory supports the following methods. These methods are 1:1 identical TX counterparts to the IX factory methods in the Instruction Factory. The main difference is that they return a different type TransactionWithMetadata which will be explored below.

โš ๏ธ NOTE

Not all these transactions have instructions that map 1:1 to the instructions in the program. For example, pause and unpause are modeled as 2 methods here but utilize the same instruction with different parameters in the program.

Approve smart delegate has an instruction that is sent to the SPL token (or token 2022) program and not the pre-authorized-debit program.

These abstractions will hopefully make the SDK slightly easier to use.

All the methods listed above return the following generic type. This type will contain the instruction in the IX factory counterpart but might also contain additional setup/cleanup instructions such as SOL -> wSOL wrapping (or vice versa).

You can simulate and/or execute the transaction by using the utility method attached to the return type.

You can also extract the TX from it and execute it yourself outside the SDK (for example, in a browser environment where you don't have the keypair directly and only the injected wallet/provider).

Both examples are shown below using buildDebitTx (this applies to all methods).

  1. Direct execution example (same applies to simulate):

  1. Indirect execution example (same applies to simulate):

Build InitSmartDelegate transaction

Builds the init_smart_delegate instruction and wrap it in an executable TX. While this is available to SDK users, this instruction only needs to be run once and it will likely be run by us for any instances of the program we deploy.

This transaction will initialize the singleton smart_delegate account.

Build InitOneTimePreAuthorization transaction

Build the init_pre_authorization instruction with one-time configuration and wrap it in an executable TX.

The TX can also include IXs to wrap SOL into the token account if mint is SOL.

This transaction will initialize a pre_authorization account that represents a one-time pre-authorization.

Build InitRecurringPreAuthorization transaction

Build the init_pre_authorization instruction with recurring configuration and wrap it in an executable TX.

The TX can also include IXs to wrap SOL into the token account if mint is SOL.

This transaction will initialize a pre_authorization account that represents a recurring pre-authorization.

Build PausePreAuthorization transaction

Build the update_pause_pre_authorization instruction with pause = true configuration and wrap it in an executable TX.

This transaction will pause a pre_authorization account.

Build UnpausePreAuthorization transaction

Build the update_pause_pre_authorization instruction with pause = false configuration and wrap it in an executable TX.

This transaction will un-pause a pre_authorization account.

Build ClosePreAuthorizationAsOwner transaction

Build the close_pre_authorization instruction with authority as pre-authorization's token account's owner and wrap it in an executable TX.

The TX can also include IXs to unwrap SOL from the token account if mint is SOL.

This transaction will close a pre_authorization account.

Build ClosePreAuthorizationAsDebitAuthority transaction

Build the close_pre_authorization instruction with authority as pre-authorization's debit authority and wrap it in an executable TX.

This transaction will close a pre_authorization account.

Build Debit transaction

Build the debit instruction and wrap it in an executable TX.

The TX can also include IXs to unwrap SOL from the destination token account if mint is SOL.

This transaction will debit the token account via the pre-authorization if it is valid.

Build ApproveSmartDelegate transaction

Build the approve instruction (on SPL Token Program or on SPL Token2022 Program) for a given token account to set its delegate to the smart_delegate account and delegated amount to u64::MAX and wrap it around an executable TX.

When a pre-authorization account is created, the delegate of the token account is already configured as the smart_delegate and the delegated amount is set to u64::MAX. This instruction can be used to "reset" this delegation so that the token account's associated pre-authorizations are still valid if it was unset by another Dapp. Alternatively, if the delegated amount isn't sufficient anymore, this same method can be used to reset it back to u64::MAX.

โš ๏ธ NOTE

This instruction does not call our pre-authorized-debit-v1 program, it calls the SPL Token/Token2022 program directly.

Last updated