Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use polymorphism in token programs #10

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package net.avianlabs.solana.domain.core
import net.avianlabs.solana.domain.program.AssociatedTokenProgram
import net.avianlabs.solana.domain.program.SystemProgram
import net.avianlabs.solana.domain.program.TokenProgram
import net.avianlabs.solana.domain.program.TokenProgramBase
import net.avianlabs.solana.methods.TransactionResponse
import net.avianlabs.solana.vendor.decodeBase58
import net.avianlabs.solana.vendor.encodeToBase58String
Expand Down Expand Up @@ -68,7 +67,7 @@ public sealed class DecodedInstruction(
val owner: PublicKey,
val amount: Long,
) :
TokenProgram(net.avianlabs.solana.domain.program.TokenProgramBase.Instruction.Transfer.index)
TokenProgram(net.avianlabs.solana.domain.program.TokenProgram.Instruction.Transfer.index)

public data class TransferChecked(
val source: PublicKey,
Expand All @@ -78,7 +77,7 @@ public sealed class DecodedInstruction(
val amount: Long,
val decimals: UByte,
) :
TokenProgram(net.avianlabs.solana.domain.program.TokenProgramBase.Instruction.TransferChecked.index)
TokenProgram(net.avianlabs.solana.domain.program.TokenProgram.Instruction.TransferChecked.index)
}

public sealed class AssociatedTokenProgram :
Expand Down Expand Up @@ -175,7 +174,7 @@ public fun TransactionResponse.decode(): DecodedTransaction? {
TokenProgram.programId -> {
val programIndex = buffer.readByte().toUByte()
when (programIndex) {
TokenProgramBase.Instruction.Transfer.index -> {
TokenProgram.Instruction.Transfer.index -> {
val (source, destination, owner) = accountsMeta!!
DecodedInstruction.TokenProgram.Transfer(
source = source.publicKey,
Expand All @@ -185,7 +184,7 @@ public fun TransactionResponse.decode(): DecodedTransaction? {
)
}

TokenProgramBase.Instruction.TransferChecked.index -> {
TokenProgram.Instruction.TransferChecked.index -> {
val (source, mint, destination, owner) = accountsMeta!!
DecodedInstruction.TokenProgram.TransferChecked(
source = source.publicKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import net.avianlabs.solana.domain.core.AccountMeta
import net.avianlabs.solana.domain.core.PublicKey
import net.avianlabs.solana.domain.core.TransactionInstruction

private val ASSOCIATED_TOKEN_PROGRAM_PROGRAM_ID =
PublicKey.fromBase58("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL")
public object AssociatedTokenProgram : Program {

public object AssociatedTokenProgram : Program(
programId = ASSOCIATED_TOKEN_PROGRAM_PROGRAM_ID
) {
public override val programId: PublicKey =
PublicKey.fromBase58("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL")

public fun createAssociatedTokenAccountInstruction(
associatedProgramId: PublicKey = ASSOCIATED_TOKEN_PROGRAM_PROGRAM_ID,
associatedProgramId: PublicKey = this.programId,
programId: PublicKey,
mint: PublicKey,
associatedAccount: PublicKey,
Expand All @@ -29,7 +27,7 @@ public object AssociatedTokenProgram : Program(
)

public fun createAssociatedTokenAccountInstructionIdempotent(
associatedProgramId: PublicKey = ASSOCIATED_TOKEN_PROGRAM_PROGRAM_ID,
associatedProgramId: PublicKey = this.programId,
programId: PublicKey,
mint: PublicKey,
associatedAccount: PublicKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package net.avianlabs.solana.domain.program

import net.avianlabs.solana.domain.core.PublicKey
import net.avianlabs.solana.domain.core.TransactionInstruction
import net.avianlabs.solana.domain.program.Program.Companion.createTransactionInstruction
import okio.Buffer

private val COMPUTE_BUDGET_PROGRAM_ID =
PublicKey.fromBase58("ComputeBudget111111111111111111111111111111")

public object ComputeBudgetProgram : Program(
programId = COMPUTE_BUDGET_PROGRAM_ID,
) {
public object ComputeBudgetProgram : Program {

public override val programId: PublicKey =
PublicKey.fromBase58("ComputeBudget111111111111111111111111111111")

public enum class Instruction(
public val index: UByte,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import net.avianlabs.solana.domain.core.TransactionInstruction
import net.avianlabs.solana.vendor.Sha256
import okio.Buffer

public abstract class Program(
public val programId: PublicKey,
) {
public interface Program {

public val programId: PublicKey

public companion object {

public fun createTransactionInstruction(
internal fun createTransactionInstruction(
programId: PublicKey,
keys: List<AccountMeta>,
data: ByteArray,
): TransactionInstruction = TransactionInstruction(programId, keys, data)

public fun findProgramAddress(
internal fun findProgramAddress(
seeds: List<ByteArray>,
programId: PublicKey,
): ProgramDerivedAddress {
Expand All @@ -37,7 +37,7 @@ public abstract class Program(
throw Exception("Unable to find a viable program address nonce")
}

public fun createProgramAddress(seeds: List<ByteArray>, programId: PublicKey): PublicKey {
internal fun createProgramAddress(seeds: List<ByteArray>, programId: PublicKey): PublicKey {
if (seeds.size > 16) {
throw RuntimeException("max seed length exceeded: ${seeds.size}")
}
Expand Down Expand Up @@ -65,5 +65,4 @@ public abstract class Program(
return PublicKey(hash)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package net.avianlabs.solana.domain.program
import net.avianlabs.solana.domain.core.AccountMeta
import net.avianlabs.solana.domain.core.PublicKey
import net.avianlabs.solana.domain.core.TransactionInstruction
import net.avianlabs.solana.domain.program.Program.Companion.createTransactionInstruction
import okio.Buffer

private val SYSTEM_PROGRAM_ID = PublicKey.fromBase58("11111111111111111111111111111111")
public object SystemProgram : Program {

public object SystemProgram : Program(
programId = SYSTEM_PROGRAM_ID
) {
public override val programId: PublicKey =
PublicKey.fromBase58("11111111111111111111111111111111")

public val SYSVAR_RENT_ACCOUNT: PublicKey =
PublicKey.fromBase58("SysvarRent111111111111111111111111111111111")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package net.avianlabs.solana.domain.program

import net.avianlabs.solana.domain.core.PublicKey
import net.avianlabs.solana.domain.core.TransactionInstruction
import net.avianlabs.solana.domain.program.TokenProgram.Companion.createCloseAccountInstruction
import net.avianlabs.solana.domain.program.TokenProgram.Companion.createTransferCheckedInstruction

private val TOKEN_2022_PROGRAM_ID =
PublicKey.fromBase58("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb")

public object Token2022Program : TokenProgramBase(TOKEN_2022_PROGRAM_ID) {
public interface Token2022Program : TokenProgram {

public enum class Extensions(
public val index: UByte,
Expand All @@ -27,16 +27,50 @@ public object Token2022Program : TokenProgramBase(TOKEN_2022_PROGRAM_ID) {
MetadataPointerExtension(39u),
}

public fun createAssociatedTokenAccountInstruction(
mint: PublicKey,
associatedAccount: PublicKey,
owner: PublicKey,
payer: PublicKey,
): TransactionInstruction = AssociatedTokenProgram.createAssociatedTokenAccountInstruction(
programId = programId,
mint = mint,
associatedAccount = associatedAccount,
owner = owner,
payer = payer,
)
public companion object : Token2022Program, TokenProgram by TokenProgram.Companion {

public override val programId: PublicKey =
PublicKey.fromBase58("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb")

@Deprecated(
message = "Token2022Program does not support transfer, use transferChecked instead",
replaceWith = ReplaceWith("transferChecked(source, destination, owner, amount, decimals, mint)"),
level = DeprecationLevel.ERROR,
)
public override fun transfer(
source: PublicKey,
destination: PublicKey,
owner: PublicKey,
amount: ULong,
): TransactionInstruction =
error("Token2022Program does not support transfer, use transferChecked instead")

public override fun closeAccount(
account: PublicKey,
destination: PublicKey,
owner: PublicKey,
): TransactionInstruction = createCloseAccountInstruction(
account = account,
destination = destination,
owner = owner,
programId = programId,
)

public override fun transferChecked(
source: PublicKey,
mint: PublicKey,
destination: PublicKey,
owner: PublicKey,
amount: ULong,
decimals: UByte,
): TransactionInstruction = createTransferCheckedInstruction(
source = source,
mint = mint,
destination = destination,
owner = owner,
amount = amount,
decimals = decimals,
programId = programId,
)
}
}
Loading
Loading