Skip to content

Commit

Permalink
Merge pull request #10 from LMAX-Exchange/6-standalone-instruction-bu…
Browse files Browse the repository at this point in the history
…ilding

[6] build individual instructions
  • Loading branch information
ml-james authored Sep 27, 2024
2 parents 767204d + 0f3255d commit dedc18f
Show file tree
Hide file tree
Showing 46 changed files with 1,832 additions and 1,295 deletions.
13 changes: 8 additions & 5 deletions src/main/java/com/lmax/solana4j/Solana.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import com.lmax.solana4j.api.AddressLookupTable;
import com.lmax.solana4j.api.Blockhash;
import com.lmax.solana4j.api.InnerTransactionBuilder;
import com.lmax.solana4j.api.InstructionBuilderBase;
import com.lmax.solana4j.api.Message;
import com.lmax.solana4j.api.MessageBuilder;
import com.lmax.solana4j.api.ProgramDerivedAddress;
import com.lmax.solana4j.api.PublicKey;
import com.lmax.solana4j.api.SignedMessageBuilder;
import com.lmax.solana4j.api.Slot;
import com.lmax.solana4j.api.TransactionInstruction;
import com.lmax.solana4j.encoding.SolanaEncoding;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.function.Consumer;

/**
* Utility class for Solana blockchain operations.
Expand Down Expand Up @@ -54,13 +56,14 @@ public static MessageBuilder builder(final ByteBuffer buffer)
}

/**
* Creates a new inner transaction builder.
* Creates a new transaction instructions using the builder provided.
*
* @return a new instance of {@link InnerTransactionBuilder}
* @param instructionBuilder the builder of the required instruction
* @return a new instance of {@link TransactionInstruction}
*/
public static InnerTransactionBuilder innerTxBuilder()
public static TransactionInstruction instruction(final Consumer<InstructionBuilderBase> instructionBuilder)
{
return SolanaEncoding.innerTxBuilder();
return SolanaEncoding.instruction(instructionBuilder);
}

/**
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/com/lmax/solana4j/api/InnerInstructionBuilder.java

This file was deleted.

28 changes: 0 additions & 28 deletions src/main/java/com/lmax/solana4j/api/InnerInstructions.java

This file was deleted.

37 changes: 0 additions & 37 deletions src/main/java/com/lmax/solana4j/api/InnerTransactionBuilder.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/com/lmax/solana4j/api/InstructionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public interface InstructionBuilder extends InstructionBuilderBase
{

/**
* Builds and returns a message builder with the instructions.
* Builds and returns a transaction instruction.
*
* @return a {@link MessageBuilder} object representing the built instructions
* @return a {@link TransactionInstruction} object representing the built instruction
*/
MessageBuilder build();
TransactionInstruction build();
}
9 changes: 9 additions & 0 deletions src/main/java/com/lmax/solana4j/api/MessageBuilderLegacy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lmax.solana4j.api;

import java.nio.BufferOverflowException;
import java.util.List;
import java.util.function.Consumer;

/**
Expand All @@ -21,6 +22,14 @@ public interface MessageBuilderLegacy
*/
MessageBuilderLegacy instructions(Consumer<TransactionBuilder> builder);

/**
* Sets the instructions for the legacy message.
*
* @param instructions a list of {@link TransactionInstruction} of prebuilt instructions
* @return this {@code MessageBuilderLegacy} instance for method chaining
*/
MessageBuilderLegacy instructions(List<TransactionInstruction> instructions);

/**
* Sets the payer for the legacy message.
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/lmax/solana4j/api/MessageBuilderV0.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public interface MessageBuilderV0
*/
MessageBuilderV0 instructions(Consumer<TransactionBuilder> builder);

/**
* Sets the instructions for the legacy message.
*
* @param instructions a list of {@link TransactionInstruction} of prebuilt instructions
* @return this {@code MessageBuilderV0} instance for method chaining
*/
MessageBuilderV0 instructions(List<TransactionInstruction> instructions);

/**
* Sets the payer for the version 0 message.
*
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/lmax/solana4j/api/MessageInstructionBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.lmax.solana4j.api;

/**
* Interface for building instructions in a Solana blockchain transaction.
* <p>
* This interface extends {@link InstructionBuilderBase} and provides an additional method
* for building a {@link MessageBuilder}.
* </p>
*/
public interface MessageInstructionBuilder extends InstructionBuilderBase
{

/**
* Builds and returns a message builder with the instructions.
*
* @return a {@link MessageBuilder} object representing the built instructions
*/
MessageBuilder build();
}
13 changes: 7 additions & 6 deletions src/main/java/com/lmax/solana4j/api/MessageVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ interface Version0MessageView extends MessageView
* @param addressLookupTables the list of address lookup tables to consult when determining if the account is a writer
* @return {@code true} if the account is a writer, {@code false} otherwise
* @throws IllegalArgumentException if the address lookup tables provided do not map to
* any of the lookup accounts in the message
* any of the lookup accounts in the message
*/
boolean isWriter(PublicKey account, List<AddressLookupTable> addressLookupTables) throws IllegalArgumentException;

Expand All @@ -195,7 +195,7 @@ interface Version0MessageView extends MessageView
* @param addressLookupTables the list of address lookup tables to include
* @return a list of {@link PublicKey} objects representing all accounts
* @throws IllegalArgumentException if the address lookup tables provided do not map to
* any of the lookup accounts in the message
* any of the lookup accounts in the message
*/
List<PublicKey> allAccounts(List<AddressLookupTable> addressLookupTables) throws IllegalArgumentException;
}
Expand Down Expand Up @@ -266,17 +266,18 @@ interface V0InstructionView extends InstructionView
* @param addressLookupTables the list of address lookup tables
* @return a list of {@link PublicKey} objects representing the accounts
* @throws IllegalArgumentException if the address lookup tables provided do not map to
* any of the lookup accounts in the message
* any of the lookup accounts in the message
*/
List<PublicKey> accounts(List<AddressLookupTable> addressLookupTables) throws IllegalArgumentException;

/**
* Returns the public key of the program for the instruction, using the address lookup tables to map
* the indexes stored in the message to actual addresses
*
* @param addressLookupTables the list of address lookup tables
* @return the {@link PublicKey} of the program
* @throws IllegalArgumentException if the address lookup tables provided do not map to
* any of the lookup accounts in the message
* any of the lookup accounts in the message
*/
PublicKey program(List<AddressLookupTable> addressLookupTables) throws IllegalArgumentException;
}
Expand Down Expand Up @@ -353,12 +354,12 @@ interface V0AccountsView extends AccountsView

/**
* Returns the list of all accounts, using the address lookup tables to map
* the indexes stored in the message to actual addresses
* the indexes stored in the message to actual addresses
*
* @param addressLookupTables the list of address lookup tables to include
* @return a list of {@link PublicKey} objects representing all accounts
* @throws IllegalArgumentException if the address lookup tables provided do not map to
* any of the lookup accounts in the message
* any of the lookup accounts in the message
*/
List<PublicKey> accounts(List<AddressLookupTable> addressLookupTables) throws IllegalArgumentException;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/lmax/solana4j/api/TransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ public interface TransactionBuilder
* @return this {@code TransactionBuilder} instance for method chaining
*/
TransactionBuilder append(Consumer<InstructionBuilderBase> builder);

/**
* Appends an instruction to the transaction.
*
* @param instruction a {@link TransactionInstruction} that is a pre-built instruction to be appended to the transaction
* @return this {@code TransactionBuilder} instance for method chaining
*/
TransactionBuilder append(TransactionInstruction instruction);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.lmax.solana4j.encoding;

import com.lmax.solana4j.api.AddressLookupTable;
import com.lmax.solana4j.api.AccountLookupEntry;
import com.lmax.solana4j.api.AddressLookupTable;
import com.lmax.solana4j.api.PublicKey;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public void addReadWriteEntry(final PublicKey address, final int readWriteIndex)
@Override
public List<PublicKey> getAddresses()
{
return Stream.concat(readWriteEntrys.stream().map(LookupEntry::getAddress), readOnlyEntrys.stream().map(LookupEntry::getAddress)).collect(Collectors.toList());
return Stream.concat(
readWriteEntrys.stream().map(LookupEntry::getAddress),
readOnlyEntrys.stream().map(LookupEntry::getAddress)
).collect(Collectors.toList());
}

@Override
Expand Down
38 changes: 22 additions & 16 deletions src/main/java/com/lmax/solana4j/encoding/SolanaAccounts.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.lmax.solana4j.encoding;

import com.lmax.solana4j.api.AccountLookupEntry;
import com.lmax.solana4j.api.Accounts;
import com.lmax.solana4j.api.AddressLookupTable;
import com.lmax.solana4j.api.AccountLookupEntry;
import com.lmax.solana4j.api.PublicKey;
import com.lmax.solana4j.api.TransactionInstruction;

Expand Down Expand Up @@ -38,7 +38,10 @@ private SolanaAccounts(
@Override
public List<PublicKey> getFlattenedAccountList()
{
return Stream.concat(staticAccounts.stream(), accountLookups.stream().flatMap(lookupTable -> lookupTable.getAddresses().stream())).collect(Collectors.toList());
return Stream.concat(staticAccounts.stream(), accountLookups.stream()
.flatMap(lookupTable -> lookupTable.getAddresses()
.stream()))
.collect(Collectors.toList());
}

@Override
Expand Down Expand Up @@ -111,7 +114,10 @@ else if (!accountReference.isWriter())
}

return new SolanaAccounts(
staticAccountReferences.stream().map(TransactionInstruction.AccountReference::account).collect(Collectors.toList()),
staticAccountReferences
.stream()
.map(TransactionInstruction.AccountReference::account)
.collect(Collectors.toList()),
accountLookups.getAccountLookupEntrys(),
countSigned,
countSignedReadOnly,
Expand All @@ -128,14 +134,14 @@ private static List<TransactionInstruction.AccountReference> mergeAccountReferen
final LinkedHashMap<PublicKey, TransactionInstruction.AccountReference> staticAccountReferences = new LinkedHashMap<>();
for (final var accountReference : allAccountReferences)
{
if (staticAccountReferences.containsKey(accountReference.account()))
{
staticAccountReferences.merge(accountReference.account(), accountReference, SolanaAccounts::merge);
}
else
{
staticAccountReferences.put(accountReference.account(), accountReference);
}
if (staticAccountReferences.containsKey(accountReference.account()))
{
staticAccountReferences.merge(accountReference.account(), accountReference, SolanaAccounts::merge);
}
else
{
staticAccountReferences.put(accountReference.account(), accountReference);
}
}

return new ArrayList<>(staticAccountReferences.values());
Expand All @@ -152,11 +158,11 @@ private static List<TransactionInstruction.AccountReference> concatAccountRefere
return Stream.concat(
Stream.of(payerReference),
instructions.stream()
.flatMap(instruction -> Stream.concat(
Stream.of(new SolanaAccountReference(instruction.program(), false, false, true)),
instruction.accountReferences().stream())
)
.sorted(cmp)).collect(Collectors.toList());
.flatMap(instruction -> Stream.concat(
Stream.of(new SolanaAccountReference(instruction.program(), false, false, true)),
instruction.accountReferences().stream())
)
.sorted(cmp)).collect(Collectors.toList());
}

private static TransactionInstruction.AccountReference merge(
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/lmax/solana4j/encoding/SolanaEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
import com.lmax.solana4j.api.AddressLookupTable;
import com.lmax.solana4j.api.Blockhash;
import com.lmax.solana4j.api.Destination;
import com.lmax.solana4j.api.InnerTransactionBuilder;
import com.lmax.solana4j.api.InstructionBuilderBase;
import com.lmax.solana4j.api.Message;
import com.lmax.solana4j.api.MessageBuilder;
import com.lmax.solana4j.api.ProgramDerivedAddress;
import com.lmax.solana4j.api.PublicKey;
import com.lmax.solana4j.api.SignedMessageBuilder;
import com.lmax.solana4j.api.Slot;
import com.lmax.solana4j.api.TransactionInstruction;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import java.util.function.Consumer;

/**
* Utility class for Solana encoding and decoding operations.
Expand Down Expand Up @@ -43,13 +45,16 @@ public static MessageBuilder builder(final ByteBuffer buffer)
}

/**
* Creates a new inner transaction builder.
* Creates a new transaction instructions using the builder provided.
*
* @return a new instance of {@link InnerTransactionBuilder}
* @param instructionBuilder the builder of the required instruction
* @return a new instance of {@link TransactionInstruction}
*/
public static InnerTransactionBuilder innerTxBuilder()
public static TransactionInstruction instruction(final Consumer<InstructionBuilderBase> instructionBuilder)
{
return new SolanaInnerTransactionBuilder();
final SolanaTransactionBuilder.SolanaInstructionBuilderBase builder = new SolanaTransactionBuilder.SolanaInstructionBuilderBase();
instructionBuilder.accept(builder);
return builder.build();
}

/**
Expand Down
Loading

0 comments on commit dedc18f

Please sign in to comment.