-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from avianlabs/token2022
token2022 support
- Loading branch information
Showing
6 changed files
with
198 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/program/Token2022Program.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package net.avianlabs.solana.domain.program | ||
|
||
import net.avianlabs.solana.domain.core.PublicKey | ||
import net.avianlabs.solana.domain.core.TransactionInstruction | ||
|
||
private val TOKEN_2022_PROGRAM_ID = | ||
PublicKey.fromBase58("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb") | ||
|
||
public object Token2022Program : TokenProgramBase(TOKEN_2022_PROGRAM_ID) { | ||
|
||
public enum class Extensions( | ||
public val index: UByte, | ||
) { | ||
TransferFeeExtension(26u), | ||
ConfidentialTransferExtension(27u), | ||
DefaultAccountStateExtension(28u), | ||
Reallocate(29u), | ||
MemoTransferExtension(30u), | ||
CreateNativeMint(31u), | ||
InitializeNonTransferableMint(32u), | ||
InterestBearingMintExtension(33u), | ||
CpiGuardExtension(34u), | ||
InitializePermanentDelegate(35u), | ||
TransferHookExtension(36u), | ||
ConfidentialTransferFeeExtension(37u), | ||
WithdrawalExcessLamports(38u), | ||
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, | ||
) | ||
} |
70 changes: 10 additions & 60 deletions
70
solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/program/TokenProgram.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,22 @@ | ||
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 okio.Buffer | ||
|
||
private val TOKEN_PROGRAM_ID = PublicKey.fromBase58("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA") | ||
|
||
public object TokenProgram : Program( | ||
programId = TOKEN_PROGRAM_ID | ||
) { | ||
public object TokenProgram : TokenProgramBase(TOKEN_PROGRAM_ID) { | ||
|
||
public enum class Instruction( | ||
public val index: UByte, | ||
) { | ||
InitializeMint(0u), | ||
InitializeAccount(1u), | ||
InitializeMultisig(2u), | ||
Transfer(3u), | ||
Approve(4u), | ||
Revoke(5u), | ||
SetAuthority(6u), | ||
MintTo(7u), | ||
Burn(8u), | ||
CloseAccount(9u), | ||
FreezeAccount(10u), | ||
ThawAccount(11u), | ||
TransferChecked(12u), | ||
ApproveChecked(13u), | ||
MintToChecked(14u), | ||
BurnChecked(15u), | ||
InitializeAccount2(16u), | ||
SyncNative(17u), | ||
InitializeAccount3(18u), | ||
; | ||
} | ||
|
||
public fun transfer( | ||
source: PublicKey, | ||
destination: PublicKey, | ||
owner: PublicKey, | ||
amount: ULong, | ||
): TransactionInstruction = createTransactionInstruction( | ||
programId = programId, | ||
keys = listOf( | ||
AccountMeta(source, isSigner = false, isWritable = true), | ||
AccountMeta(destination, isSigner = false, isWritable = true), | ||
AccountMeta(owner, isSigner = true, isWritable = false), | ||
), | ||
data = Buffer() | ||
.writeByte(Instruction.Transfer.index.toInt()) | ||
.writeLongLe(amount.toLong()) | ||
.readByteArray(), | ||
) | ||
|
||
public fun closeAccount( | ||
account: PublicKey, | ||
destination: PublicKey, | ||
public fun createAssociatedTokenAccountInstruction( | ||
mint: PublicKey, | ||
associatedAccount: PublicKey, | ||
owner: PublicKey, | ||
): TransactionInstruction = createTransactionInstruction( | ||
payer: PublicKey, | ||
): TransactionInstruction = AssociatedTokenProgram.createAssociatedTokenAccountInstruction( | ||
programId = programId, | ||
keys = listOf( | ||
AccountMeta(account, isSigner = false, isWritable = true), | ||
AccountMeta(destination, isSigner = false, isWritable = true), | ||
AccountMeta(owner, isSigner = false, isWritable = false), | ||
), | ||
data = Buffer() | ||
.writeByte(Instruction.CloseAccount.index.toInt()) | ||
.readByteArray(), | ||
mint = mint, | ||
associatedAccount = associatedAccount, | ||
owner = owner, | ||
payer = payer, | ||
) | ||
} |
100 changes: 100 additions & 0 deletions
100
solana-kotlin/src/commonMain/kotlin/net/avianlabs/solana/domain/program/TokenProgramBase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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 okio.Buffer | ||
|
||
public abstract class TokenProgramBase internal constructor( | ||
programId: PublicKey, | ||
) : Program( | ||
programId = programId, | ||
) { | ||
|
||
public enum class Instruction( | ||
public val index: UByte, | ||
) { | ||
InitializeAccount(1u), | ||
InitializeMultisig(2u), | ||
Transfer(3u), | ||
Approve(4u), | ||
Revoke(5u), | ||
SetAuthority(6u), | ||
MintTo(7u), | ||
Burn(8u), | ||
CloseAccount(9u), | ||
FreezeAccount(10u), | ||
ThawAccount(11u), | ||
TransferChecked(12u), | ||
ApproveChecked(13u), | ||
MintToChecked(14u), | ||
BurnChecked(15u), | ||
InitializeAccount2(16u), | ||
SyncNative(17u), | ||
InitializeAccount3(18u), | ||
InitializeMultisig2(19u), | ||
InitializeMint2(20u), | ||
GetAccountDataSize(21u), | ||
InitializeImmutableOwner(22u), | ||
AmountToUiAmount(23u), | ||
UiAmountToAmount(24u), | ||
InitializeMintCloseAuthority(25u), | ||
; | ||
} | ||
|
||
public fun transfer( | ||
source: PublicKey, | ||
destination: PublicKey, | ||
owner: PublicKey, | ||
amount: ULong, | ||
): TransactionInstruction = createTransactionInstruction( | ||
programId = programId, | ||
keys = listOf( | ||
AccountMeta(source, isSigner = false, isWritable = true), | ||
AccountMeta(destination, isSigner = false, isWritable = true), | ||
AccountMeta(owner, isSigner = true, isWritable = false), | ||
), | ||
data = Buffer() | ||
.writeByte(Instruction.Transfer.index.toInt()) | ||
.writeLongLe(amount.toLong()) | ||
.readByteArray(), | ||
) | ||
|
||
public fun closeAccount( | ||
account: PublicKey, | ||
destination: PublicKey, | ||
owner: PublicKey, | ||
): TransactionInstruction = createTransactionInstruction( | ||
programId = programId, | ||
keys = listOf( | ||
AccountMeta(account, isSigner = false, isWritable = true), | ||
AccountMeta(destination, isSigner = false, isWritable = true), | ||
AccountMeta(owner, isSigner = false, isWritable = false), | ||
), | ||
data = Buffer() | ||
.writeByte(Instruction.CloseAccount.index.toInt()) | ||
.readByteArray(), | ||
) | ||
|
||
public fun transferChecked( | ||
source: PublicKey, | ||
mint: PublicKey, | ||
destination: PublicKey, | ||
owner: PublicKey, | ||
amount: ULong, | ||
decimals: UByte, | ||
): TransactionInstruction = createTransactionInstruction( | ||
programId = programId, | ||
keys = listOf( | ||
AccountMeta(source, isSigner = false, isWritable = true), | ||
AccountMeta(mint, isSigner = false, isWritable = false), | ||
AccountMeta(destination, isSigner = false, isWritable = true), | ||
AccountMeta(owner, isSigner = true, isWritable = false), | ||
), | ||
data = Buffer() | ||
.writeByte(Instruction.TransferChecked.index.toInt()) | ||
.writeLongLe(amount.toLong()) | ||
.writeByte(decimals.toInt()) | ||
.readByteArray(), | ||
) | ||
} |