token.bend source
token.bend on the hub · documented module
import Baseimport ./sol.bend as Simport ./types.bend as Ty# SPL Token account words (165-byte layout, little-endian u32 view):# 0..7 mint# 8..15 owner# 16..17 amount (full SPL u64)# Mint decimals live at byte 44 (low byte of word 11).## TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA# SPL Token program id, Tokenkeg...VQ5DA.def token_id() -> Ty.Pubkey: Ty.Pk{ 3791052038, 2476828119, 1189202905, 2893671374, 3984962588, 2436324191, 2247461946, 2835414910, }# 42 words (168 bytes) covers the 165-byte token account.def TOKEN_WORDS() -> Nat: 42ndef token_mint(data: List<&2, U32>) -> Ty.Pubkey: S.get_pubkey(data, 0n)def token_owner(data: List<&2, U32>) -> Ty.Pubkey: S.get_pubkey(data, 8n)def token_amount(data: List<&2, U32>) -> Ty.u64: S.get_u64(data, 16n)def token_set_amount(data: List<&2, U32>, amt: Ty.u64) -> List<&2, U32>: S.set_u64(data, 16n, amt)# Low byte of mint word 11 (byte 44).def mint_decimals(data: List<&2, U32>) -> U32: U32.and(S.get_u32(data, 11n), 255)# Account owner is the Token program.def acc_is_token(+a: Ty.Account) -> Bool: Ty.Pubkey.eq(S.owner(a), token_id())# Both token accounts name the same mint.def token_same_mint(+a: List<&2, U32>, +b: List<&2, U32>) -> Bool: Ty.Pubkey.eq(token_mint(a), token_mint(b))# Token-account owner word equals owner.def token_owned(+data: List<&2, U32>, +owner: Ty.Pubkey) -> Bool: Ty.Pubkey.eq(token_owner(data), owner)