Skip to content

Commit

Permalink
feat: initial Token2022 support (#1512)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinenerio committed Jul 7, 2024
1 parent e0c53a9 commit 3e299f4
Show file tree
Hide file tree
Showing 12 changed files with 596 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/solana/lib/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ bool isPointOnEd25519Curve(Iterable<int> data) {
Future<Ed25519HDPublicKey> findAssociatedTokenAddress({
required Ed25519HDPublicKey owner,
required Ed25519HDPublicKey mint,
TokenProgramType tokenProgramType = TokenProgramType.tokenProgram,
}) =>
Ed25519HDPublicKey.findProgramAddress(
seeds: [owner.bytes, TokenProgram.id.toByteArray(), mint.bytes],
seeds: [owner.bytes, tokenProgramType.id.toByteArray(), mint.bytes],
programId: AssociatedTokenAccountProgram.id,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class AssociatedTokenAccountInstruction extends Instruction {
required Ed25519HDPublicKey address,
required Ed25519HDPublicKey owner,
required Ed25519HDPublicKey mint,
Ed25519HDPublicKey? tokenProgramId,
}) =>
AssociatedTokenAccountInstruction._(
accounts: [
Expand All @@ -40,7 +41,9 @@ class AssociatedTokenAccountInstruction extends Instruction {
isSigner: false,
),
AccountMeta.readonly(
pubKey: Ed25519HDPublicKey.fromBase58(TokenProgram.programId),
pubKey: Ed25519HDPublicKey.fromBase58(
tokenProgramId?.toBase58() ?? TokenProgram.id.toBase58(),
),
isSigner: false,
),
AccountMeta.readonly(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extension SolanaClientAssociatedTokenAccontProgram on SolanaClient {
Ed25519HDPublicKey? owner,
required Ed25519HDPublicKey mint,
required Wallet funder,
TokenProgramType tokenProgramType = TokenProgramType.tokenProgram,
SignatureCallback? onSigned,
Commitment commitment = Commitment.finalized,
}) async {
Expand All @@ -58,6 +59,7 @@ extension SolanaClientAssociatedTokenAccontProgram on SolanaClient {
final derivedAddress = await findAssociatedTokenAddress(
owner: effectiveOwner,
mint: mint,
tokenProgramType: tokenProgramType,
);
final instruction = AssociatedTokenAccountInstruction.createAccount(
mint: mint,
Expand Down Expand Up @@ -129,9 +131,14 @@ extension SolanaClientAssociatedTokenAccontProgram on SolanaClient {
Future<TokenAmount> getTokenBalance({
required Ed25519HDPublicKey owner,
required Ed25519HDPublicKey mint,
TokenProgramType tokenProgramType = TokenProgramType.tokenProgram,
Commitment commitment = Commitment.finalized,
}) async {
final ata = await findAssociatedTokenAddress(owner: owner, mint: mint);
final ata = await findAssociatedTokenAddress(
owner: owner,
mint: mint,
tokenProgramType: tokenProgramType,
);

return rpcClient
.getTokenAccountBalance(ata.toBase58(), commitment: commitment)
Expand Down
28 changes: 28 additions & 0 deletions packages/solana/lib/src/programs/token_program/extension_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
enum ExtensionType {
uninitialized(0),
transferFeeConfig(1),
transferFeeAmount(2),
mintCloseAuthority(3),
confidentialTransferMint(4),
confidentialTransferAccount(5),
defaultAccountState(6),
immutableOwner(7),
memoTransfer(8),
nonTransferable(9),
interestBearingConfig(10),
cpiGuard(11),
permanentDelegate(12),
nonTransferableAccount(13),
transferHook(14),
transferHookAccount(15),
metadataPointer(18),
tokenMetadata(19),
groupPointer(20),
tokenGroup(21),
groupMemberPointer(22),
tokenGroupMember(23);

const ExtensionType(this.value);

final int value;
}
Loading

0 comments on commit 3e299f4

Please sign in to comment.