Skip to content

Commit

Permalink
cleanup of bits and pieces including adding remaining but as yet unim…
Browse files Browse the repository at this point in the history
…plemented integration tests
  • Loading branch information
ml-james committed Jun 19, 2024
1 parent 1d2d8bd commit 6d50853
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.lmax.solana4j.programs;

import com.lmax.solana4j.base.IntegrationTestBase;
import com.lmax.solana4j.base.ParameterizedMessageEncodingTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;

import static org.junit.jupiter.api.Assertions.fail;

public class AssociatedTokenProgramIntegrationTest extends IntegrationTestBase
{
@BeforeEach
void beforeEachTest()
{
solana.createKeyPair("payer");
solana.airdrop("payer", "100");
}

@Disabled
@ParameterizedMessageEncodingTest
void shouldCreateAssociatedTokenAddress(final String messageEncoding)
{
fail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.lmax.solana4j.programs;

import com.lmax.solana4j.base.IntegrationTestBase;
import com.lmax.solana4j.base.ParameterizedMessageEncodingTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;

import static org.junit.jupiter.api.Assertions.fail;

public class BpfLoaderUpgradableProgramIntegrationTest extends IntegrationTestBase
{
@BeforeEach
void beforeEachTest()
{
solana.createKeyPair("payer");
solana.airdrop("payer", "100");
}

@Disabled
@ParameterizedMessageEncodingTest
void shouldSetUpgradeAuthority(final String messageEncoding)
{
fail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.lmax.solana4j.programs;

import com.lmax.solana4j.base.IntegrationTestBase;
import com.lmax.solana4j.base.ParameterizedMessageEncodingTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;

import static org.junit.jupiter.api.Assertions.fail;

public class ComputeBudgetProgramIntegrationTest extends IntegrationTestBase
{
@BeforeEach
void beforeEachTest()
{
solana.createKeyPair("payer");
solana.airdrop("payer", "100");
}

@Disabled
@ParameterizedMessageEncodingTest
void shouldSetComputeUnitLimit(final String messageEncoding)
{
fail();
}


@Disabled
@ParameterizedMessageEncodingTest
void shouldSetComputeUnitPrice(final String messageEncoding)
{
fail();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.lmax.solana4j.base.IntegrationTestBase;
import com.lmax.solana4j.base.ParameterizedTokenTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;

import static org.junit.jupiter.api.Assertions.fail;

class TokenProgramIntegrationTest extends IntegrationTestBase
{
Expand Down Expand Up @@ -67,4 +70,19 @@ void shouldTransferToken(final String messageEncoding, final String tokenProgram
solana.tokenBalance("tokenAccountSender", "0.00000000000000009");
solana.tokenBalance("tokenAccountReceiver", "0.00000000000000001");
}

@Disabled
@ParameterizedTokenTest
void shouldCreateMultisig(final String messageEncoding, final String tokenProgram)
{
fail();
}

@Disabled
@ParameterizedTokenTest
void shouldSetAuthority(final String messageEncoding, final String tokenProgram)
{
fail();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private AddressLookupTableProgram(final TransactionBuilderBase tb)
this.tb = tb;
}

public void createAddressLookupTableInstruction(final ProgramDerivedAddress programDerivedAddress, final PublicKey authority, final PublicKey payer, final Slot recentSlot)
public AddressLookupTableProgram createLookupTable(final ProgramDerivedAddress programDerivedAddress, final PublicKey authority, final PublicKey payer, final Slot recentSlot)
{
tb.append(ib -> ib
.program(PROGRAM_ACCOUNT)
Expand All @@ -52,9 +52,11 @@ public void createAddressLookupTableInstruction(final ProgramDerivedAddress prog
}
)
);

return this;
}

public void extendAddressLookupTable(final PublicKey lookupTable, final PublicKey authority, final PublicKey payer, final List<PublicKey> addresses)
public AddressLookupTableProgram extendLookupTable(final PublicKey lookupTable, final PublicKey authority, final PublicKey payer, final List<PublicKey> addresses)
{
tb.append(ib -> ib
.program(PROGRAM_ACCOUNT)
Expand All @@ -70,6 +72,8 @@ public void extendAddressLookupTable(final PublicKey lookupTable, final PublicKe
}
)
);

return this;
}

public static ProgramDerivedAddress deriveAddress(final PublicKey authority, final Slot slot)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
package com.lmax.solana4j.programs;

import com.lmax.solana4j.Solana;
import com.lmax.solana4j.api.ProgramDerivedAddress;
import com.lmax.solana4j.api.PublicKey;
import com.lmax.solana4j.api.TransactionBuilderBase;
import org.bitcoinj.core.Base58;

import java.nio.ByteOrder;

import static com.lmax.solana4j.encoding.SysVar.RENT;
import static com.lmax.solana4j.programs.SystemProgram.SYSTEM_PROGRAM_ACCOUNT;

public final class AssociatedTokenProgram
{
private static final byte[] ASSOCIATED_TOKEN_PROGRAM_ID = Base58.decode("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
public static final PublicKey ASSOCIATED_TOKEN_PROGRAM_ACCOUNT = Solana.account(ASSOCIATED_TOKEN_PROGRAM_ID);

public static final int IDEMPOTENT_CREATE_INSTRUCTION = 1;
public static final int CREATE_INSTRUCTION = 0;

private final TransactionBuilderBase tb;

public static AssociatedTokenProgram factory(final TransactionBuilderBase tb)
{
return new AssociatedTokenProgram(tb);
}

private AssociatedTokenProgram(final TransactionBuilderBase tb)
{
this.tb = tb;
}

public AssociatedTokenProgram createAssociatedToken(
final ProgramDerivedAddress programDerivedAddress,
final PublicKey mint,
final PublicKey owner,
final PublicKey payer,
final boolean idempotent)
{
tb.append(ib -> ib
.program(ASSOCIATED_TOKEN_PROGRAM_ACCOUNT)
.account(payer, true, true)
.account(programDerivedAddress.address(), false, true)
.account(owner, false, false)
.account(mint, false, false)
.account(SYSTEM_PROGRAM_ACCOUNT, false, false)
.account(ASSOCIATED_TOKEN_PROGRAM_ACCOUNT, false, false)
.account(RENT, false, false)
.data(1, bb -> bb.order(ByteOrder.LITTLE_ENDIAN)
.put((byte) (idempotent ? IDEMPOTENT_CREATE_INSTRUCTION : CREATE_INSTRUCTION)))
);

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public BpfLoaderUpgradableProgram setUpgradeAuthority(
maybeNewAuthorityAddress.ifPresent(newAuthorityAddress -> ib.account(newAuthorityAddress, false, false));
ib.data(4, bb -> bb.order(ByteOrder.LITTLE_ENDIAN).putInt(SET_AUTHORITY_INSTRUCTION));
});

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public ComputeBudgetProgram setComputeUnitLimit(final int computeUnits)
.put((byte) SET_COMPUTE_UNIT_LIMIT_INSTRUCTION)
.putInt(computeUnits))
);

return this;
}

Expand All @@ -48,6 +49,7 @@ public ComputeBudgetProgram setComputeUnitPrice(final long microLamports)
.put((byte) SET_COMPUTE_UNIT_PRICE_INSTRUCTION)
.putLong(microLamports))
);

return this;
}
}
10 changes: 3 additions & 7 deletions src/main/java/com/lmax/solana4j/programs/SystemProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public final class SystemProgram
private static final byte[] SYSTEM_PROGRAM_ID = Base58.decode("11111111111111111111111111111111");
public static final PublicKey SYSTEM_PROGRAM_ACCOUNT = Solana.account(SYSTEM_PROGRAM_ID);

public static final int NONCE_ACCOUNT_LENGTH = 80; // ref solana-web3.js PATH:programs/system.ts (NonceAccountLayout)
public static final int NONCE_ACCOUNT_LENGTH = 80;
public static final int MINT_ACCOUNT_LENGTH = 82;

private static final int CREATE_ACCOUNT_INSTRUCTION = 0;
private static final int TRANSFER_INSTRUCTION = 2;
Expand All @@ -34,12 +35,6 @@ public static SystemProgram factory(final TransactionBuilderBase tb)
this.tb = tb;
}

public SystemProgram createAccount(final PublicKey payer, final PublicKey newAccount, final long lamports, final long space)
{
createAccount(payer, newAccount, lamports, space, SYSTEM_PROGRAM_ACCOUNT);
return this;
}

public SystemProgram createAccount(final PublicKey payer, final PublicKey newAccount, final long lamports, final long space, final PublicKey program)
{
tb.append(ib -> ib
Expand All @@ -56,6 +51,7 @@ public SystemProgram createAccount(final PublicKey payer, final PublicKey newAcc
program.write(bb);
})
);

return this;
}

Expand Down
Loading

0 comments on commit 6d50853

Please sign in to comment.