github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/pingpong/hermes_channel.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package pingpong
    19  
    20  import (
    21  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/jinzhu/copier"
    25  
    26  	"github.com/mysteriumnetwork/node/identity"
    27  	"github.com/mysteriumnetwork/payments/client"
    28  )
    29  
    30  // NewHermesChannel creates HermesChannel model.
    31  func NewHermesChannel(channelID string, id identity.Identity, hermesID common.Address, channel client.ProviderChannel, promise HermesPromise, beneficiary common.Address) HermesChannel {
    32  	hc := HermesChannel{
    33  		ChannelID:   channelID,
    34  		Identity:    id,
    35  		HermesID:    hermesID,
    36  		Channel:     channel,
    37  		lastPromise: promise,
    38  		Beneficiary: beneficiary,
    39  	}
    40  	return hc
    41  }
    42  
    43  // Copy returns a deep copy of channel
    44  func (hc HermesChannel) Copy() HermesChannel {
    45  	var hcCopy HermesChannel
    46  	if err := copier.CopyWithOption(&hcCopy, hc, copier.Option{DeepCopy: true}); err != nil {
    47  		panic(err)
    48  	}
    49  	return hcCopy
    50  }
    51  
    52  // HermesChannel represents opened payment channel between identity and hermes.
    53  type HermesChannel struct {
    54  	ChannelID   string
    55  	Identity    identity.Identity
    56  	HermesID    common.Address
    57  	Channel     client.ProviderChannel
    58  	lastPromise HermesPromise
    59  	Beneficiary common.Address
    60  }
    61  
    62  // LifetimeBalance returns earnings of all history.
    63  func (hc HermesChannel) LifetimeBalance() *big.Int {
    64  	if hc.lastPromise.Promise.Amount == nil {
    65  		return new(big.Int)
    66  	}
    67  	return hc.lastPromise.Promise.Amount
    68  }
    69  
    70  // UnsettledBalance returns current unsettled earnings.
    71  func (hc HermesChannel) UnsettledBalance() *big.Int {
    72  	settled := new(big.Int)
    73  	if hc.Channel.Settled != nil {
    74  		settled = hc.Channel.Settled
    75  	}
    76  
    77  	lastPromise := new(big.Int)
    78  	if hc.lastPromise.Promise.Amount != nil {
    79  		lastPromise = hc.lastPromise.Promise.Amount
    80  	}
    81  
    82  	return safeSub(lastPromise, settled)
    83  }
    84  
    85  func (hc HermesChannel) availableBalance() *big.Int {
    86  	balance := new(big.Int)
    87  	if hc.Channel.Stake != nil {
    88  		balance = hc.Channel.Stake
    89  	}
    90  
    91  	settled := new(big.Int)
    92  	if hc.Channel.Settled != nil {
    93  		settled = hc.Channel.Settled
    94  	}
    95  
    96  	return new(big.Int).Add(balance, settled)
    97  }
    98  
    99  func (hc HermesChannel) balance() *big.Int {
   100  	promised := new(big.Int)
   101  	if hc.lastPromise.Promise.Amount != nil {
   102  		promised = hc.lastPromise.Promise.Amount
   103  	}
   104  	return safeSub(hc.availableBalance(), promised)
   105  }