github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/beneficiary/saver.go (about)

     1  /*
     2   * Copyright (C) 2021 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 beneficiary
    19  
    20  import (
    21  	"github.com/ethereum/go-ethereum/common"
    22  	"github.com/mysteriumnetwork/node/identity"
    23  )
    24  
    25  // Saver saves a given beneficiary and tracks its
    26  // progress.
    27  type Saver struct {
    28  	set     settler
    29  	ad      addressProvider
    30  	chainID int64
    31  	*beneficiaryChangeKeeper
    32  }
    33  
    34  type storage interface {
    35  	GetValue(bucket string, key interface{}, to interface{}) error
    36  	SetValue(bucket string, key interface{}, to interface{}) error
    37  }
    38  
    39  type multiChainBC interface {
    40  	GetBeneficiary(chainID int64, registryAddress, identity common.Address) (common.Address, error)
    41  }
    42  
    43  type settler interface {
    44  	SettleWithBeneficiary(chainID int64, id identity.Identity, beneficiary common.Address, hermeses []common.Address) error
    45  }
    46  
    47  type addressProvider interface {
    48  	GetActiveHermes(chainID int64) (common.Address, error)
    49  	GetRegistryAddress(chainID int64) (common.Address, error)
    50  	GetActiveChannelAddress(chainID int64, id common.Address) (common.Address, error)
    51  }
    52  
    53  // NewSaver returns a new beneficiary saver according to the given chain.
    54  func NewSaver(currentChain int64, ad addressProvider, st storage, bc multiChainBC, set settler) *Saver {
    55  	return &Saver{
    56  		chainID:                 currentChain,
    57  		set:                     set,
    58  		ad:                      ad,
    59  		beneficiaryChangeKeeper: newBeneficiaryChangeKeeper(currentChain, st),
    60  	}
    61  }
    62  
    63  // SettleAndSaveBeneficiary executes a settlement transaction saving the beneficiary to the blockchain.
    64  func (b *Saver) SettleAndSaveBeneficiary(id identity.Identity, hermeses []common.Address, beneficiary common.Address) error {
    65  	return b.executeWithStatusTracking(id, beneficiary, func() error {
    66  		return b.set.SettleWithBeneficiary(b.chainID, id, beneficiary, hermeses)
    67  	})
    68  }