github.com/gagliardetto/solana-go@v1.11.0/programs/tokenregistry/types.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  // This file has been modified by github.com/gagliardetto
     3  //
     4  // Copyright 2020 dfuse Platform Inc.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //      http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  package tokenregistry
    18  
    19  import (
    20  	"fmt"
    21  
    22  	bin "github.com/gagliardetto/binary"
    23  
    24  	"github.com/gagliardetto/solana-go"
    25  )
    26  
    27  const TOKEN_META_SIZE = 229
    28  
    29  type TokenMeta struct {
    30  	IsInitialized         bool
    31  	Reg                   [3]byte `text:"-"`
    32  	DataType              byte
    33  	MintAddress           *solana.PublicKey
    34  	RegistrationAuthority *solana.PublicKey
    35  	Logo                  Logo
    36  	Name                  Name
    37  	Website               Website
    38  	Symbol                Symbol
    39  }
    40  
    41  func DecodeTokenMeta(in []byte) (*TokenMeta, error) {
    42  	var t *TokenMeta
    43  	decoder := bin.NewBinDecoder(in)
    44  	err := decoder.Decode(&t)
    45  	if err != nil {
    46  		return nil, fmt.Errorf("unpack: %w", err)
    47  	}
    48  	return t, nil
    49  }
    50  
    51  type Logo [64]byte
    52  
    53  func LogoFromString(logo string) (Logo, error) {
    54  	data := []byte(logo)
    55  	if len(data) > 64 {
    56  		return Logo{}, fmt.Errorf("logo data to long expected 64 got %d", len(data))
    57  	}
    58  	l := Logo{}
    59  	copy(l[:], data)
    60  	return l, nil
    61  }
    62  func (l Logo) String() string {
    63  	return AsciiString(l[:])
    64  }
    65  
    66  type Name [32]byte
    67  
    68  func NameFromString(name string) (Name, error) {
    69  	data := []byte(name)
    70  	if len(data) > 32 {
    71  		return Name{}, fmt.Errorf("name data to long expected 32 got %d", len(data))
    72  	}
    73  	n := Name{}
    74  	copy(n[:], data)
    75  	return n, nil
    76  }
    77  
    78  func (n Name) String() string {
    79  	return AsciiString(n[:])
    80  }
    81  
    82  type Symbol [32]byte
    83  
    84  func SymbolFromString(symbol string) (Symbol, error) {
    85  	data := []byte(symbol)
    86  	if len(data) > 32 {
    87  		return Symbol{}, fmt.Errorf("symbol data to long expected 12 got %d", len(data))
    88  	}
    89  	s := Symbol{}
    90  	copy(s[:], data)
    91  	return s, nil
    92  }
    93  
    94  func (s Symbol) String() string {
    95  	return AsciiString(s[:])
    96  }
    97  
    98  type Website [32]byte
    99  
   100  func WebsiteFromString(symbol string) (Website, error) {
   101  	data := []byte(symbol)
   102  	if len(data) > 32 {
   103  		return Website{}, fmt.Errorf("website data to long expected 32 got %d", len(data))
   104  	}
   105  	s := Website{}
   106  	copy(s[:], data)
   107  	return s, nil
   108  }
   109  
   110  func (s Website) String() string {
   111  	return AsciiString(s[:])
   112  }
   113  
   114  func AsciiString(data []byte) string {
   115  	var trimmed []byte
   116  	for _, b := range data {
   117  		if b > 0 {
   118  			trimmed = append(trimmed, b)
   119  		}
   120  	}
   121  	return string(trimmed)
   122  }