github.com/XinFinOrg/xdcchain@v1.1.0/contracts/blocksigner/blocksigner.go (about)

     1  // Copyright (c) 2018 XDCchain
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Lesser General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11  // GNU Lesser General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Lesser General Public License
    14  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package blocksigner
    17  
    18  import (
    19  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/contracts/blocksigner/contract"
    22  	"math/big"
    23  )
    24  
    25  type BlockSigner struct {
    26  	*contract.BlockSignerSession
    27  	contractBackend bind.ContractBackend
    28  }
    29  
    30  func NewBlockSigner(transactOpts *bind.TransactOpts, contractAddr common.Address, contractBackend bind.ContractBackend) (*BlockSigner, error) {
    31  	blockSigner, err := contract.NewBlockSigner(contractAddr, contractBackend)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	return &BlockSigner{
    37  		&contract.BlockSignerSession{
    38  			Contract:     blockSigner,
    39  			TransactOpts: *transactOpts,
    40  		},
    41  		contractBackend,
    42  	}, nil
    43  }
    44  
    45  func DeployBlockSigner(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend, epochNumber *big.Int) (common.Address, *BlockSigner, error) {
    46  	blockSignerAddr, _, _, err := contract.DeployBlockSigner(transactOpts, contractBackend, epochNumber)
    47  	if err != nil {
    48  		return blockSignerAddr, nil, err
    49  	}
    50  
    51  	blockSigner, err := NewBlockSigner(transactOpts, blockSignerAddr, contractBackend)
    52  	if err != nil {
    53  		return blockSignerAddr, nil, err
    54  	}
    55  
    56  	return blockSignerAddr, blockSigner, nil
    57  }