decred.org/dcrdex@v1.0.5/client/asset/eth/fundingcoin.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package eth
     5  
     6  import (
     7  	"encoding/binary"
     8  	"fmt"
     9  
    10  	"decred.org/dcrdex/client/asset"
    11  	"decred.org/dcrdex/dex"
    12  	"github.com/ethereum/go-ethereum/common"
    13  )
    14  
    15  const fundingCoinIDSize = 28      // address (20) + amount (8) = 28
    16  const tokenFundingCoinIDSize = 36 // address (20) + amount (8) + amount (8) = 36
    17  
    18  // fundingCoin is an identifier for a coin which has not yet been sent to the
    19  // swap contract.
    20  type fundingCoin struct {
    21  	addr common.Address
    22  	amt  uint64
    23  }
    24  
    25  // String creates a human readable string.
    26  func (c *fundingCoin) String() string {
    27  	return fmt.Sprintf("address: %v, amount: %d", c.addr, c.amt)
    28  }
    29  
    30  // ID utf-8 encodes the account address. This ID will be sent to the server as
    31  // part of the order.
    32  func (c *fundingCoin) ID() dex.Bytes {
    33  	return []byte(c.addr.String())
    34  }
    35  
    36  func (c *fundingCoin) TxID() string {
    37  	return ""
    38  }
    39  
    40  // Value returns the value reserved in the funding coin.
    41  func (c *fundingCoin) Value() uint64 {
    42  	return c.amt
    43  }
    44  
    45  // RecoveryID is a byte-encoded address and value of a funding coin. RecoveryID
    46  // satisfies the asset.RecoveryCoin interface, so this ID will be used as input
    47  // for (asset.Wallet).FundingCoins.
    48  func (c *fundingCoin) RecoveryID() dex.Bytes {
    49  	b := make([]byte, fundingCoinIDSize)
    50  	copy(b[:20], c.addr[:])
    51  	binary.BigEndian.PutUint64(b[20:28], c.amt)
    52  	return b
    53  }
    54  
    55  // decodeFundingCoin decodes a byte slice into an fundingCoinID struct.
    56  func decodeFundingCoin(coinID []byte) (*fundingCoin, error) {
    57  	if len(coinID) != fundingCoinIDSize {
    58  		return nil, fmt.Errorf("decodeFundingCoin: length expected %v, got %v",
    59  			fundingCoinIDSize, len(coinID))
    60  	}
    61  
    62  	var address [20]byte
    63  	copy(address[:], coinID[:20])
    64  	return &fundingCoin{
    65  		addr: address,
    66  		amt:  binary.BigEndian.Uint64(coinID[20:28]),
    67  	}, nil
    68  }
    69  
    70  // createFundingCoin constructs a new fundingCoinID for the provided account
    71  // address and amount in Gwei.
    72  func createFundingCoin(address common.Address, amount uint64) *fundingCoin {
    73  	return &fundingCoin{
    74  		addr: address,
    75  		amt:  amount,
    76  	}
    77  }
    78  
    79  // tokenFundingCoin is a funding coin for a token.
    80  type tokenFundingCoin struct {
    81  	addr common.Address
    82  	amt  uint64
    83  	fees uint64
    84  }
    85  
    86  var _ asset.TokenCoin = (*tokenFundingCoin)(nil)
    87  
    88  // String creates a human readable string.
    89  func (c *tokenFundingCoin) String() string {
    90  	return fmt.Sprintf("address: %s, amount:%v, fees:%v", c.addr, c.amt, c.fees)
    91  }
    92  
    93  // ID utf-8 encodes the account address. This ID will be sent to the server as
    94  // part of the an order.
    95  func (c *tokenFundingCoin) ID() dex.Bytes {
    96  	return []byte(c.addr.String())
    97  }
    98  
    99  func (c *tokenFundingCoin) TxID() string {
   100  	return ""
   101  }
   102  
   103  // ID creates a byte slice that can be decoded with DecodeCoinID.
   104  func (c *tokenFundingCoin) RecoveryID() dex.Bytes {
   105  	b := make([]byte, tokenFundingCoinIDSize)
   106  	copy(b[:20], c.addr[:])
   107  	binary.BigEndian.PutUint64(b[20:28], c.amt)
   108  	binary.BigEndian.PutUint64(b[28:36], c.fees)
   109  	return b
   110  }
   111  
   112  func (c *tokenFundingCoin) Value() uint64 {
   113  	return c.amt
   114  }
   115  
   116  func (c *tokenFundingCoin) Fees() uint64 {
   117  	return c.fees
   118  }
   119  
   120  // decodeTokenFundingCoin decodes a byte slice into an tokenFundingCoinID.
   121  func decodeTokenFundingCoin(coinID []byte) (*tokenFundingCoin, error) {
   122  	if len(coinID) != tokenFundingCoinIDSize {
   123  		return nil, fmt.Errorf("decodeTokenFundingCoin: length expected %v, got %v",
   124  			tokenFundingCoinIDSize, len(coinID))
   125  	}
   126  
   127  	var address [20]byte
   128  	copy(address[:], coinID[:20])
   129  	return &tokenFundingCoin{
   130  		addr: address,
   131  		amt:  binary.BigEndian.Uint64(coinID[20:28]),
   132  		fees: binary.BigEndian.Uint64(coinID[28:36]),
   133  	}, nil
   134  }
   135  
   136  // createTokenFundingCoin constructs a new tokenFundingCoin for the provided
   137  // account address, value, and fees in gwei.
   138  func createTokenFundingCoin(address common.Address, tokenValue, fees uint64) *tokenFundingCoin {
   139  	return &tokenFundingCoin{
   140  		addr: address,
   141  		amt:  tokenValue,
   142  		fees: fees,
   143  	}
   144  }