github.com/gagliardetto/solana-go@v1.11.0/programs/token/accounts.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package token
    16  
    17  import (
    18  	"encoding/binary"
    19  
    20  	bin "github.com/gagliardetto/binary"
    21  	"github.com/gagliardetto/solana-go"
    22  )
    23  
    24  type Mint struct {
    25  	// Optional authority used to mint new tokens. The mint authority may only be provided during
    26  	// mint creation. If no mint authority is present then the mint has a fixed supply and no
    27  	// further tokens may be minted.
    28  	MintAuthority *solana.PublicKey `bin:"optional"`
    29  
    30  	// Total supply of tokens.
    31  	Supply uint64
    32  
    33  	// Number of base 10 digits to the right of the decimal place.
    34  	Decimals uint8
    35  
    36  	// Is `true` if this structure has been initialized
    37  	IsInitialized bool
    38  
    39  	// Optional authority to freeze token accounts.
    40  	FreezeAuthority *solana.PublicKey `bin:"optional"`
    41  }
    42  
    43  func (mint *Mint) UnmarshalWithDecoder(dec *bin.Decoder) (err error) {
    44  	{
    45  		v, err := dec.ReadUint32(binary.LittleEndian)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		if v == 1 {
    50  			v, err := dec.ReadNBytes(32)
    51  			if err != nil {
    52  				return err
    53  			}
    54  			mint.MintAuthority = solana.PublicKeyFromBytes(v).ToPointer()
    55  		} else {
    56  			// discard:
    57  			_, err := dec.ReadNBytes(32)
    58  			if err != nil {
    59  				return err
    60  			}
    61  		}
    62  	}
    63  	{
    64  		v, err := dec.ReadUint64(binary.LittleEndian)
    65  		if err != nil {
    66  			return err
    67  		}
    68  		mint.Supply = v
    69  	}
    70  	{
    71  		v, err := dec.ReadUint8()
    72  		if err != nil {
    73  			return err
    74  		}
    75  		mint.Decimals = v
    76  	}
    77  	{
    78  		v, err := dec.ReadBool()
    79  		if err != nil {
    80  			return err
    81  		}
    82  		mint.IsInitialized = v
    83  	}
    84  	{
    85  		v, err := dec.ReadUint32(binary.LittleEndian)
    86  		if err != nil {
    87  			return err
    88  		}
    89  		if v == 1 {
    90  			v, err := dec.ReadNBytes(32)
    91  			if err != nil {
    92  				return err
    93  			}
    94  			mint.FreezeAuthority = solana.PublicKeyFromBytes(v).ToPointer()
    95  		} else {
    96  			// discard:
    97  			_, err := dec.ReadNBytes(32)
    98  			if err != nil {
    99  				return err
   100  			}
   101  		}
   102  	}
   103  	return nil
   104  }
   105  
   106  func (mint Mint) MarshalWithEncoder(encoder *bin.Encoder) (err error) {
   107  	{
   108  		if mint.MintAuthority == nil {
   109  			err = encoder.WriteUint32(0, binary.LittleEndian)
   110  			if err != nil {
   111  				return err
   112  			}
   113  			empty := solana.PublicKey{}
   114  			err = encoder.WriteBytes(empty[:], false)
   115  			if err != nil {
   116  				return err
   117  			}
   118  		} else {
   119  			err = encoder.WriteUint32(1, binary.LittleEndian)
   120  			if err != nil {
   121  				return err
   122  			}
   123  			err = encoder.WriteBytes(mint.MintAuthority[:], false)
   124  			if err != nil {
   125  				return err
   126  			}
   127  		}
   128  	}
   129  	err = encoder.WriteUint64(mint.Supply, binary.LittleEndian)
   130  	if err != nil {
   131  		return err
   132  	}
   133  	err = encoder.WriteUint8(mint.Decimals)
   134  	if err != nil {
   135  		return err
   136  	}
   137  	err = encoder.WriteBool(mint.IsInitialized)
   138  	if err != nil {
   139  		return err
   140  	}
   141  	{
   142  		if mint.FreezeAuthority == nil {
   143  			err = encoder.WriteUint32(0, binary.LittleEndian)
   144  			if err != nil {
   145  				return err
   146  			}
   147  			empty := solana.PublicKey{}
   148  			err = encoder.WriteBytes(empty[:], false)
   149  			if err != nil {
   150  				return err
   151  			}
   152  		} else {
   153  			err = encoder.WriteUint32(1, binary.LittleEndian)
   154  			if err != nil {
   155  				return err
   156  			}
   157  			err = encoder.WriteBytes(mint.FreezeAuthority[:], false)
   158  			if err != nil {
   159  				return err
   160  			}
   161  		}
   162  	}
   163  	return nil
   164  }
   165  
   166  type Account struct {
   167  	// The mint associated with this account
   168  	Mint solana.PublicKey
   169  
   170  	// The owner of this account.
   171  	Owner solana.PublicKey
   172  
   173  	// The amount of tokens this account holds.
   174  	Amount uint64
   175  
   176  	// If `delegate` is `Some` then `delegated_amount` represents
   177  	// the amount authorized by the delegate
   178  	Delegate *solana.PublicKey `bin:"optional"`
   179  
   180  	// The account's state
   181  	State AccountState
   182  
   183  	// If is_some, this is a native token, and the value logs the rent-exempt reserve. An Account
   184  	// is required to be rent-exempt, so the value is used by the Processor to ensure that wrapped
   185  	// SOL accounts do not drop below this threshold.
   186  	IsNative *uint64 `bin:"optional"`
   187  
   188  	// The amount delegated
   189  	DelegatedAmount uint64
   190  
   191  	// Optional authority to close the account.
   192  	CloseAuthority *solana.PublicKey `bin:"optional"`
   193  }
   194  
   195  func (mint *Account) UnmarshalWithDecoder(dec *bin.Decoder) (err error) {
   196  	{
   197  		v, err := dec.ReadNBytes(32)
   198  		if err != nil {
   199  			return err
   200  		}
   201  		mint.Mint = solana.PublicKeyFromBytes(v)
   202  	}
   203  	{
   204  		v, err := dec.ReadNBytes(32)
   205  		if err != nil {
   206  			return err
   207  		}
   208  		mint.Owner = solana.PublicKeyFromBytes(v)
   209  	}
   210  	{
   211  		v, err := dec.ReadUint64(binary.LittleEndian)
   212  		if err != nil {
   213  			return err
   214  		}
   215  		mint.Amount = v
   216  	}
   217  	{
   218  		v, err := dec.ReadUint32(binary.LittleEndian)
   219  		if err != nil {
   220  			return err
   221  		}
   222  		if v == 1 {
   223  			v, err := dec.ReadNBytes(32)
   224  			if err != nil {
   225  				return err
   226  			}
   227  			mint.Delegate = solana.PublicKeyFromBytes(v).ToPointer()
   228  		} else {
   229  			// discard:
   230  			_, err := dec.ReadNBytes(32)
   231  			if err != nil {
   232  				return err
   233  			}
   234  		}
   235  	}
   236  	{
   237  		v, err := dec.ReadUint8()
   238  		if err != nil {
   239  			return err
   240  		}
   241  		mint.State = AccountState(v)
   242  	}
   243  	{
   244  		v, err := dec.ReadUint32(binary.LittleEndian)
   245  		if err != nil {
   246  			return err
   247  		}
   248  		if v == 1 {
   249  			v, err := dec.ReadUint64(bin.LE)
   250  			if err != nil {
   251  				return err
   252  			}
   253  			mint.IsNative = &v
   254  		} else {
   255  			// discard:
   256  			_, err := dec.ReadUint64(bin.LE)
   257  			if err != nil {
   258  				return err
   259  			}
   260  		}
   261  	}
   262  	{
   263  		v, err := dec.ReadUint64(binary.LittleEndian)
   264  		if err != nil {
   265  			return err
   266  		}
   267  		mint.DelegatedAmount = v
   268  	}
   269  	{
   270  		v, err := dec.ReadUint32(binary.LittleEndian)
   271  		if err != nil {
   272  			return err
   273  		}
   274  		if v == 1 {
   275  			v, err := dec.ReadNBytes(32)
   276  			if err != nil {
   277  				return err
   278  			}
   279  			mint.CloseAuthority = solana.PublicKeyFromBytes(v).ToPointer()
   280  		} else {
   281  			// discard:
   282  			_, err := dec.ReadNBytes(32)
   283  			if err != nil {
   284  				return err
   285  			}
   286  		}
   287  	}
   288  	return nil
   289  }
   290  
   291  func (mint Account) MarshalWithEncoder(encoder *bin.Encoder) (err error) {
   292  	{
   293  		err = encoder.WriteBytes(mint.Mint[:], false)
   294  		if err != nil {
   295  			return err
   296  		}
   297  	}
   298  	{
   299  		err = encoder.WriteBytes(mint.Owner[:], false)
   300  		if err != nil {
   301  			return err
   302  		}
   303  	}
   304  	{
   305  		err = encoder.WriteUint64(mint.Amount, bin.LE)
   306  		if err != nil {
   307  			return err
   308  		}
   309  	}
   310  	{
   311  		if mint.Delegate == nil {
   312  			err = encoder.WriteUint32(0, binary.LittleEndian)
   313  			if err != nil {
   314  				return err
   315  			}
   316  			empty := solana.PublicKey{}
   317  			err = encoder.WriteBytes(empty[:], false)
   318  			if err != nil {
   319  				return err
   320  			}
   321  		} else {
   322  			err = encoder.WriteUint32(1, binary.LittleEndian)
   323  			if err != nil {
   324  				return err
   325  			}
   326  			err = encoder.WriteBytes(mint.Delegate[:], false)
   327  			if err != nil {
   328  				return err
   329  			}
   330  		}
   331  	}
   332  	err = encoder.WriteUint8(uint8(mint.State))
   333  	if err != nil {
   334  		return err
   335  	}
   336  	{
   337  		if mint.IsNative == nil {
   338  			err = encoder.WriteUint32(0, binary.LittleEndian)
   339  			if err != nil {
   340  				return err
   341  			}
   342  			err = encoder.WriteUint64(0, bin.LE)
   343  			if err != nil {
   344  				return err
   345  			}
   346  		} else {
   347  			err = encoder.WriteUint32(1, binary.LittleEndian)
   348  			if err != nil {
   349  				return err
   350  			}
   351  			err = encoder.WriteUint64(*mint.IsNative, bin.LE)
   352  			if err != nil {
   353  				return err
   354  			}
   355  		}
   356  	}
   357  	{
   358  		err = encoder.WriteUint64(mint.DelegatedAmount, bin.LE)
   359  		if err != nil {
   360  			return err
   361  		}
   362  	}
   363  	{
   364  		if mint.CloseAuthority == nil {
   365  			err = encoder.WriteUint32(0, binary.LittleEndian)
   366  			if err != nil {
   367  				return err
   368  			}
   369  			empty := solana.PublicKey{}
   370  			err = encoder.WriteBytes(empty[:], false)
   371  			if err != nil {
   372  				return err
   373  			}
   374  		} else {
   375  			err = encoder.WriteUint32(1, binary.LittleEndian)
   376  			if err != nil {
   377  				return err
   378  			}
   379  			err = encoder.WriteBytes(mint.CloseAuthority[:], false)
   380  			if err != nil {
   381  				return err
   382  			}
   383  		}
   384  	}
   385  	return nil
   386  }
   387  
   388  type Multisig struct {
   389  	// Number of signers required
   390  	M uint8
   391  	// Number of valid signers
   392  	N uint8
   393  	// Is `true` if this structure has been initialized
   394  	IsInitialized bool
   395  	// Signer public keys
   396  	Signers [MAX_SIGNERS]solana.PublicKey
   397  }