github.com/algorand/go-algorand-sdk@v1.24.0/types/transaction.go (about)

     1  package types
     2  
     3  // Transaction describes a transaction that can appear in a block.
     4  type Transaction struct {
     5  	_struct struct{} `codec:",omitempty,omitemptyarray"`
     6  
     7  	// Type of transaction
     8  	Type TxType `codec:"type"`
     9  
    10  	// Common fields for all types of transactions
    11  	Header
    12  
    13  	// Fields for different types of transactions
    14  	KeyregTxnFields
    15  	PaymentTxnFields
    16  	AssetConfigTxnFields
    17  	AssetTransferTxnFields
    18  	AssetFreezeTxnFields
    19  	ApplicationFields
    20  	StateProofTxnFields
    21  }
    22  
    23  // SignedTxn wraps a transaction and a signature. The encoding of this struct
    24  // is suitable to broadcast on the network
    25  type SignedTxn struct {
    26  	_struct struct{} `codec:",omitempty,omitemptyarray"`
    27  
    28  	Sig      Signature   `codec:"sig"`
    29  	Msig     MultisigSig `codec:"msig"`
    30  	Lsig     LogicSig    `codec:"lsig"`
    31  	Txn      Transaction `codec:"txn"`
    32  	AuthAddr Address     `codec:"sgnr"`
    33  }
    34  
    35  // KeyregTxnFields captures the fields used for key registration transactions.
    36  type KeyregTxnFields struct {
    37  	_struct struct{} `codec:",omitempty,omitemptyarray"`
    38  
    39  	VotePK           VotePK         `codec:"votekey"`
    40  	SelectionPK      VRFPK          `codec:"selkey"`
    41  	VoteFirst        Round          `codec:"votefst"`
    42  	VoteLast         Round          `codec:"votelst"`
    43  	VoteKeyDilution  uint64         `codec:"votekd"`
    44  	Nonparticipation bool           `codec:"nonpart"`
    45  	StateProofPK     MerkleVerifier `codec:"sprfkey"`
    46  }
    47  
    48  // PaymentTxnFields captures the fields used by payment transactions.
    49  type PaymentTxnFields struct {
    50  	_struct struct{} `codec:",omitempty,omitemptyarray"`
    51  
    52  	Receiver Address    `codec:"rcv"`
    53  	Amount   MicroAlgos `codec:"amt"`
    54  
    55  	// When CloseRemainderTo is set, it indicates that the
    56  	// transaction is requesting that the account should be
    57  	// closed, and all remaining funds be transferred to this
    58  	// address.
    59  	CloseRemainderTo Address `codec:"close"`
    60  }
    61  
    62  // AssetConfigTxnFields captures the fields used for asset
    63  // allocation, re-configuration, and destruction.
    64  type AssetConfigTxnFields struct {
    65  	_struct struct{} `codec:",omitempty,omitemptyarray"`
    66  
    67  	// ConfigAsset is the asset being configured or destroyed.
    68  	// A zero value means allocation.
    69  	ConfigAsset AssetIndex `codec:"caid"`
    70  
    71  	// AssetParams are the parameters for the asset being
    72  	// created or re-configured.  A zero value means destruction.
    73  	AssetParams AssetParams `codec:"apar"`
    74  }
    75  
    76  // AssetTransferTxnFields captures the fields used for asset transfers.
    77  type AssetTransferTxnFields struct {
    78  	_struct struct{} `codec:",omitempty,omitemptyarray"`
    79  
    80  	XferAsset AssetIndex `codec:"xaid"`
    81  
    82  	// AssetAmount is the amount of asset to transfer.
    83  	// A zero amount transferred to self allocates that asset
    84  	// in the account's Assets map.
    85  	AssetAmount uint64 `codec:"aamt"`
    86  
    87  	// AssetSender is the sender of the transfer.  If this is not
    88  	// a zero value, the real transaction sender must be the Clawback
    89  	// address from the AssetParams.  If this is the zero value,
    90  	// the asset is sent from the transaction's Sender.
    91  	AssetSender Address `codec:"asnd"`
    92  
    93  	// AssetReceiver is the recipient of the transfer.
    94  	AssetReceiver Address `codec:"arcv"`
    95  
    96  	// AssetCloseTo indicates that the asset should be removed
    97  	// from the account's Assets map, and specifies where the remaining
    98  	// asset holdings should be transferred.  It's always valid to transfer
    99  	// remaining asset holdings to the creator account.
   100  	AssetCloseTo Address `codec:"aclose"`
   101  }
   102  
   103  // AssetFreezeTxnFields captures the fields used for freezing asset slots.
   104  type AssetFreezeTxnFields struct {
   105  	_struct struct{} `codec:",omitempty,omitemptyarray"`
   106  
   107  	// FreezeAccount is the address of the account whose asset
   108  	// slot is being frozen or un-frozen.
   109  	FreezeAccount Address `codec:"fadd"`
   110  
   111  	// FreezeAsset is the asset ID being frozen or un-frozen.
   112  	FreezeAsset AssetIndex `codec:"faid"`
   113  
   114  	// AssetFrozen is the new frozen value.
   115  	AssetFrozen bool `codec:"afrz"`
   116  }
   117  
   118  // Header captures the fields common to every transaction type.
   119  type Header struct {
   120  	_struct struct{} `codec:",omitempty,omitemptyarray"`
   121  
   122  	Sender      Address    `codec:"snd"`
   123  	Fee         MicroAlgos `codec:"fee"`
   124  	FirstValid  Round      `codec:"fv"`
   125  	LastValid   Round      `codec:"lv"`
   126  	Note        []byte     `codec:"note"`
   127  	GenesisID   string     `codec:"gen"`
   128  	GenesisHash Digest     `codec:"gh"`
   129  
   130  	// Group specifies that this transaction is part of a
   131  	// transaction group (and, if so, specifies the hash
   132  	// of a TxGroup).
   133  	Group Digest `codec:"grp"`
   134  
   135  	// Lease enforces mutual exclusion of transactions.  If this field is
   136  	// nonzero, then once the transaction is confirmed, it acquires the
   137  	// lease identified by the (Sender, Lease) pair of the transaction until
   138  	// the LastValid round passes.  While this transaction possesses the
   139  	// lease, no other transaction specifying this lease can be confirmed.
   140  	Lease [32]byte `codec:"lx"`
   141  
   142  	// RekeyTo, if nonzero, sets the sender's SpendingKey to the given address
   143  	// If the RekeyTo address is the sender's actual address, the SpendingKey is set to zero
   144  	// This allows "re-keying" a long-lived account -- rotating the signing key, changing
   145  	// membership of a multisig account, etc.
   146  	RekeyTo Address `codec:"rekey"`
   147  }
   148  
   149  // TxGroup describes a group of transactions that must appear
   150  // together in a specific order in a block.
   151  type TxGroup struct {
   152  	_struct struct{} `codec:",omitempty,omitemptyarray"`
   153  
   154  	// TxGroupHashes specifies a list of hashes of transactions that must appear
   155  	// together, sequentially, in a block in order for the group to be
   156  	// valid.  Each hash in the list is a hash of a transaction with
   157  	// the `Group` field omitted.
   158  	TxGroupHashes []Digest `codec:"txlist"`
   159  }
   160  
   161  // SuggestedParams wraps the transaction parameters common to all transactions,
   162  // typically received from the SuggestedParams endpoint of algod.
   163  // This struct itself is not sent over the wire to or from algod: see models.TransactionParams.
   164  type SuggestedParams struct {
   165  	// Fee is the suggested transaction fee
   166  	// Fee is in units of micro-Algos per byte.
   167  	// Fee may fall to zero but a group of N atomic transactions must
   168  	// still have a fee of at least N*MinTxnFee for the current network protocol.
   169  	Fee MicroAlgos `codec:"fee"`
   170  
   171  	// Genesis ID
   172  	GenesisID string `codec:"genesis-id"`
   173  
   174  	// Genesis hash
   175  	GenesisHash []byte `codec:"genesis-hash"`
   176  
   177  	// FirstRoundValid is the first protocol round on which the txn is valid
   178  	FirstRoundValid Round `codec:"first-round"`
   179  
   180  	// LastRoundValid is the final protocol round on which the txn may be committed
   181  	LastRoundValid Round `codec:"last-round"`
   182  
   183  	// ConsensusVersion indicates the consensus protocol version
   184  	// as of LastRound.
   185  	ConsensusVersion string `codec:"consensus-version"`
   186  
   187  	// FlatFee indicates whether the passed fee is per-byte or per-transaction
   188  	// If true, txn fee may fall below the MinTxnFee for the current network protocol.
   189  	FlatFee bool `codec:"flat-fee"`
   190  
   191  	// The minimum transaction fee (not per byte) required for the
   192  	// txn to validate for the current network protocol.
   193  	MinFee uint64 `codec:"min-fee"`
   194  }
   195  
   196  // AddLease adds the passed lease (see types/transaction.go) to the header of the passed transaction
   197  // and updates fee accordingly
   198  // - lease: the [32]byte lease to add to the header
   199  // - feePerByte: the new feePerByte
   200  func (tx *Transaction) AddLease(lease [32]byte, feePerByte uint64) {
   201  	copy(tx.Header.Lease[:], lease[:])
   202  	// normally we would use estimateSize,
   203  	// and set fee = feePerByte * estimateSize,
   204  	// but this would cause a circular import.
   205  	// we know we are adding 32 bytes (+ a few bytes to hold the 32), so increase fee accordingly.
   206  	tx.Header.Fee = tx.Header.Fee + MicroAlgos(37*feePerByte)
   207  }
   208  
   209  // AddLeaseWithFlatFee adds the passed lease (see types/transaction.go) to the header of the passed transaction
   210  // and updates fee accordingly
   211  // - lease: the [32]byte lease to add to the header
   212  // - flatFee: the new flatFee
   213  func (tx *Transaction) AddLeaseWithFlatFee(lease [32]byte, flatFee uint64) {
   214  	tx.Header.Lease = lease
   215  	tx.Header.Fee = MicroAlgos(flatFee)
   216  }
   217  
   218  // Rekey sets the rekeyTo field to the passed address. Any future transacrtion will need to be signed by the
   219  // rekeyTo address' corresponding private key.
   220  func (tx *Transaction) Rekey(rekeyToAddress string) error {
   221  	addr, err := DecodeAddress(rekeyToAddress)
   222  	if err != nil {
   223  		return err
   224  	}
   225  
   226  	tx.RekeyTo = addr
   227  	return nil
   228  }