Use large amounts of accounts by using lookup tables.
legacy
- the original transaction format0
- the newest transaction format that includes support for Address Lookup
Tables@nexis-network/web3.js
library.
TransactionMessage
with
the following parameters:
payerKey
- the public key of the account that will pay for the transactionrecentBlockhash
- a recent blockhash from the networkinstructions
- the instructions to include in the transaction0
transaction using the
compileToV0Message()
method.
VersionedTransaction
constructor to create a new versioned transaction. Your code can then sign and
send the transaction to the network, similar to a legacy transaction.
@nexis-network/web3.js
library
includes an AddressLookupTableProgram
class which provides a set of methods to
create instructions for managing LUTs. These methods include:
createLookupTable
- creates a new LUT accountfreezeLookupTable
- makes an existing LUT immutableextendLookupTable
- adds addresses to an existing LUTdeactivateLookupTable
- puts an LUT in a “deactivation” period before it can
be closedcloseLookupTable
- permanently closes an LUT accountcreateLookupTable
method to construct the instruction that creates
a lookup table. The function requires the following parameters:
authority
- the account that will have permission to modify the lookup tablepayer
- the account that will pay for the account creationrecentSlot
- a recent slot to derive the lookup table’s addressauthority
and recentSlot
as seeds.
recentSlot: slot - 1
). However, if you still encounter
an error when sending the transaction, you can try resending the transaction.
extendLookupTable
method to create an instruction that adds
addresses to an existing lookup table. It takes the following parameters:
payer
- the account that will pay for the transaction fees and any increased
rentauthority
- the account that has permission to change the lookup tablelookupTable
- the address of the lookup table to extendaddresses
- the addresses to add to the lookup tabledeactivateLookupTable
method and pass in the
following parameters:
lookupTable
- the address of the LUT to be deactivatedauthority
- the account with permission to deactivate the LUTcloseLookupTable
method. This method creates an instruction to close a
deactivated lookup table and reclaim its rent balance. It takes the following
parameters:
lookupTable
- the address of the LUT to be closedauthority
- the account with permission to close the LUTrecipient
- the account that will receive the reclaimed rent balancefreezeLookupTable
method. It takes the
following parameters:
lookupTable
- the address of the LUT to be frozenauthority
- the account with permission to freeze the LUTTransactionMessage
, you can include any lookup table
accounts by passing them as an array to the compileToV0Message()
method. You
can also provide multiple lookup table accounts.
npm install
in the terminal to install the
required dependencies.
The starter code includes an example of creating a legacy transaction that
intends to atomically transfer NZT to 22 recipients. The transaction contains 22
instructions where each instruction transfers NZT from the signer to a different
recipient.
The purpose of the starter code is to illustrate the limitation on the number of
addresses that can be included in a legacy transaction. The transaction built in
the starter code is expected to fail when sent.
The following starter code can be found in the index.ts
file.
npm start
. This will create a new keypair, write it
to the .env
file, airdrop devnet NZT to the keypair, and send the transaction
built in the starter code. The transaction is expected to fail with the error
message Transaction too large
.
main
function to leave
only the following:
sendV0Transaction
helper functionwaitForNewBlock
helper functionwaitForNewBlock
helper function that we’ll use to wait for
lookup tables to activate between sending transactions.
This function will have parameters for a connection and a target block height.
It then starts an interval that checks the current block height of the network
every 1000ms. Once the new block height exceeds the target height, the interval
is cleared and the promise is resolved.
initializeLookupTable
functioninitializeLookupTable
. This function has parameters user
, connection
, and
addresses
. The function will:
main
to use lookup tablesmain
to use versioned transactions and lookup tables. We’ll need
to:
initializeLookupTable
waitForNewBlock
connection.getAddressLookupTable
web3.js
framework to replace
any recipient addresses that match addresses in the lookup table with pointers
to the lookup table instead. By the time the transaction is sent to the network,
addresses that exist in the lookup table will be referenced by a single byte
rather than the full 32 bytes.
Use npm start
in the command line to execute the main
function. You should
see an output similar to the following:
initializeLookupTable
and do two things:
extendLookupTable
to only add the first 30
addresses (any more than that and the transaction will be too large)main()
function