Module: Solana::SplToken

Defined in:
lib/solana/spl_token.rb

Class Method Summary collapse

Class Method Details

.create_associated_token_account_instruction(payer:, wallet:, mint:) ⇒ Object

Build a CreateAssociatedTokenAccount instruction Returns hash compatible with Transaction#add_instruction



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/solana/spl_token.rb', line 19

def (payer:, wallet:, mint:)
  payer_bytes = normalize(payer)
  wallet_bytes = normalize(wallet)
  mint_bytes = normalize(mint)
  ata_bytes, _ = find_associated_token_address(wallet_bytes, mint_bytes)

  {
    program_id: Transaction::ASSOCIATED_TOKEN_PROGRAM_ID,
    accounts: [
      { pubkey: payer_bytes, is_signer: true, is_writable: true },
      { pubkey: ata_bytes, is_signer: false, is_writable: true },
      { pubkey: wallet_bytes, is_signer: false, is_writable: false },
      { pubkey: mint_bytes, is_signer: false, is_writable: false },
      { pubkey: Transaction::SYSTEM_PROGRAM_ID, is_signer: false, is_writable: false },
      { pubkey: Transaction::TOKEN_PROGRAM_ID, is_signer: false, is_writable: false }
    ],
    data: "".b
  }
end

.find_associated_token_address(wallet, mint) ⇒ Object

Derive the Associated Token Address for a wallet + mint Returns [ata_bytes, bump]



7
8
9
10
11
12
13
14
15
# File 'lib/solana/spl_token.rb', line 7

def find_associated_token_address(wallet, mint)
  wallet_bytes = normalize(wallet)
  mint_bytes = normalize(mint)

  Transaction.find_pda(
    [wallet_bytes, Transaction::TOKEN_PROGRAM_ID, mint_bytes],
    Transaction::ASSOCIATED_TOKEN_PROGRAM_ID
  )
end

.mint_to_instruction(mint:, destination:, authority:, amount:) ⇒ Object

Build a SPL Token MintTo instruction (discriminator byte 7) Returns hash compatible with Transaction#add_instruction



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/solana/spl_token.rb', line 41

def mint_to_instruction(mint:, destination:, authority:, amount:)
  mint_bytes = normalize(mint)
  dest_bytes = normalize(destination)
  auth_bytes = normalize(authority)

  data = [7].pack("C") + [amount].pack("Q<")

  {
    program_id: Transaction::TOKEN_PROGRAM_ID,
    accounts: [
      { pubkey: mint_bytes, is_signer: false, is_writable: true },
      { pubkey: dest_bytes, is_signer: false, is_writable: true },
      { pubkey: auth_bytes, is_signer: true, is_writable: false }
    ],
    data: data
  }
end

.transfer_instruction(from:, to:, authority:, amount:) ⇒ Object

Build a SPL Token Transfer instruction (discriminator byte 3) Returns hash compatible with Transaction#add_instruction



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/solana/spl_token.rb', line 61

def transfer_instruction(from:, to:, authority:, amount:)
  from_bytes = normalize(from)
  to_bytes = normalize(to)
  auth_bytes = normalize(authority)

  data = [3].pack("C") + [amount].pack("Q<")

  {
    program_id: Transaction::TOKEN_PROGRAM_ID,
    accounts: [
      { pubkey: from_bytes, is_signer: false, is_writable: true },
      { pubkey: to_bytes, is_signer: false, is_writable: true },
      { pubkey: auth_bytes, is_signer: true, is_writable: false }
    ],
    data: data
  }
end