github.com/klaytn/klaytn@v1.12.1/client/bridge_client.go (about)

     1  // Modifications Copyright 2019 The klaytn Authors
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of go-ethereum.
     4  //
     5  // go-ethereum is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // go-ethereum is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from ethclient/ethclient.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package client
    22  
    23  import (
    24  	"context"
    25  	"errors"
    26  	"math/big"
    27  
    28  	"github.com/klaytn/klaytn"
    29  	"github.com/klaytn/klaytn/blockchain/types"
    30  	"github.com/klaytn/klaytn/common"
    31  	"github.com/klaytn/klaytn/common/hexutil"
    32  	"github.com/klaytn/klaytn/networks/p2p"
    33  )
    34  
    35  // BridgeAddPeerOnParentChain can add a static peer on bridge node for service chain.
    36  func (ec *Client) BridgeAddPeerOnBridge(ctx context.Context, url string) (bool, error) {
    37  	var result bool
    38  	err := ec.c.CallContext(ctx, &result, "subbridge_addPeer", url)
    39  	return result, err
    40  }
    41  
    42  // BridgeRemovePeerOnParentChain can remove a static peer on bridge node.
    43  func (ec *Client) BridgeRemovePeerOnBridge(ctx context.Context, url string) (bool, error) {
    44  	var result bool
    45  	err := ec.c.CallContext(ctx, &result, "subbridge_removePeer", url)
    46  	return result, err
    47  }
    48  
    49  // BridgePeersOnBridge returns the peer list of bridge node for service chain.
    50  func (ec *Client) BridgePeersOnBridge(ctx context.Context) ([]*p2p.PeerInfo, error) {
    51  	var result []*p2p.PeerInfo
    52  	err := ec.c.CallContext(ctx, &result, "subbridge_peers")
    53  	return result, err
    54  }
    55  
    56  // BridgeNodeInfo returns the node information
    57  func (ec *Client) BridgeNodeInfo(ctx context.Context) (*p2p.NodeInfo, error) {
    58  	var result p2p.NodeInfo
    59  	err := ec.c.CallContext(ctx, &result, "subbridge_nodeInfo")
    60  	return &result, err
    61  }
    62  
    63  // BridgePeersOnBridge returns the peer list of bridge node for service chain.
    64  func (ec *Client) MainBridgePeersOnBridge(ctx context.Context) ([]*p2p.PeerInfo, error) {
    65  	var result []*p2p.PeerInfo
    66  	err := ec.c.CallContext(ctx, &result, "mainbridge_peers")
    67  	return result, err
    68  }
    69  
    70  // BridgeNodeInfo returns the node information
    71  func (ec *Client) MainBridgeNodeInfo(ctx context.Context) (*p2p.NodeInfo, error) {
    72  	var result p2p.NodeInfo
    73  	err := ec.c.CallContext(ctx, &result, "mainbridge_nodeInfo")
    74  	return &result, err
    75  }
    76  
    77  // BridgeGetChildChainIndexingEnabled can get if child chain indexing is enabled or not.
    78  func (ec *Client) BridgeGetChildChainIndexingEnabled(ctx context.Context) (bool, error) {
    79  	var result bool
    80  	err := ec.c.CallContext(ctx, &result, "mainbridge_getChildChainIndexingEnabled")
    81  	return result, err
    82  }
    83  
    84  // BridgeConvertChildChainBlockHashToParentChainTxHash can convert child chain block hash to
    85  // anchoring tx hash which contains anchored data.
    86  func (ec *Client) BridgeConvertChildChainBlockHashToParentChainTxHash(ctx context.Context, scBlockHash common.Hash) (common.Hash, error) {
    87  	var txHash common.Hash
    88  	err := ec.c.CallContext(ctx, &txHash, "mainbridge_convertChildChainBlockHashToParentChainTxHash", scBlockHash)
    89  	return txHash, err
    90  }
    91  
    92  // BridgeConvertRequestTxHashToHandleTxHash can convert a request value transfer tx hash to
    93  // the corresponded handle value transfer tx hash.
    94  func (ec *Client) BridgeConvertRequestTxHashToHandleTxHash(ctx context.Context, requestTxHash common.Hash) (common.Hash, error) {
    95  	var handleTxHash common.Hash
    96  	err := ec.c.CallContext(ctx, &handleTxHash, "subbridge_convertRequestTxHashToHandleTxHash", requestTxHash)
    97  	return handleTxHash, err
    98  }
    99  
   100  // BridgeGetReceiptFromParentChain can get the receipt of child chain tx from parent node.
   101  func (ec *Client) BridgeGetReceiptFromParentChain(ctx context.Context, hash common.Hash) (*types.Receipt, error) {
   102  	var result *types.Receipt
   103  	err := ec.c.CallContext(ctx, &result, "subbridge_getReceiptFromParentChain", hash)
   104  	if err == nil && result == nil {
   105  		return nil, klaytn.NotFound
   106  	}
   107  	return result, err
   108  }
   109  
   110  // BridgeGetParentOperatorAddr can get a parent chain operator address.
   111  func (ec *Client) BridgeGetParentOperatorAddr(ctx context.Context) (common.Address, error) {
   112  	var result common.Address
   113  	err := ec.c.CallContext(ctx, &result, "subbridge_getParentOperatorAddr")
   114  	return result, err
   115  }
   116  
   117  // BridgeGetChildOperatorAddr can get a child chain operator address.
   118  func (ec *Client) BridgeGetChildOperatorAddr(ctx context.Context) (common.Address, error) {
   119  	var result common.Address
   120  	err := ec.c.CallContext(ctx, &result, "subbridge_getChildOperatorAddr")
   121  	return result, err
   122  }
   123  
   124  // BridgeGetParentOperatorNonce can get a parent chain operator nonce.
   125  func (ec *Client) BridgeGetParentOperatorNonce(ctx context.Context) (uint64, error) {
   126  	var result uint64
   127  	err := ec.c.CallContext(ctx, &result, "subbridge_getParentOperatorNonce")
   128  	return result, err
   129  }
   130  
   131  // BridgeGetChildOperatorAddr can get a child chain operator nonce.
   132  func (ec *Client) BridgeGetChildOperatorNonce(ctx context.Context) (uint64, error) {
   133  	var result uint64
   134  	err := ec.c.CallContext(ctx, &result, "subbridge_getChildOperatorNonce")
   135  	return result, err
   136  }
   137  
   138  // BridgeGetLatestAnchoredBlockNumber can return the latest anchored block number.
   139  func (ec *Client) BridgeGetLatestAnchoredBlockNumber(ctx context.Context) (uint64, error) {
   140  	var result uint64
   141  	err := ec.c.CallContext(ctx, &result, "subbridge_getLatestAnchoredBlockNumber")
   142  	return result, err
   143  }
   144  
   145  // BridgeEnableAnchoring can enable anchoring function and return the set value.
   146  func (ec *Client) BridgeEnableAnchoring(ctx context.Context) (bool, error) {
   147  	return ec.setAnchoring(ctx, true)
   148  }
   149  
   150  // BridgeDisableAnchoring can disable anchoring function and return the set value.
   151  func (ec *Client) BridgeDisableAnchoring(ctx context.Context) (bool, error) {
   152  	return ec.setAnchoring(ctx, false)
   153  }
   154  
   155  // setAnchoring can set if anchoring is enabled or not.
   156  func (ec *Client) setAnchoring(ctx context.Context, enable bool) (bool, error) {
   157  	var result bool
   158  	err := ec.c.CallContext(ctx, &result, "subbridge_anchoring", enable)
   159  	return result, err
   160  }
   161  
   162  // BridgeGetAnchoringPeriod can get the block period to anchor chain data.
   163  func (ec *Client) BridgeGetAnchoringPeriod(ctx context.Context) (uint64, error) {
   164  	var result uint64
   165  	err := ec.c.CallContext(ctx, &result, "subbridge_getAnchoringPeriod")
   166  	return result, err
   167  }
   168  
   169  // BridgeGetSentChainTxsLimit can get the maximum number of transaction which child peer can send to parent peer once.
   170  func (ec *Client) BridgeGetSentChainTxsLimit(ctx context.Context) (uint64, error) {
   171  	var result uint64
   172  	err := ec.c.CallContext(ctx, &result, "subbridge_getSentChainTxsLimit")
   173  	return result, err
   174  }
   175  
   176  // BridgeRegisteredOperators registers an operator.
   177  func (ec *Client) BridgeRegisterOperator(ctx context.Context, bridge, operator common.Address) (common.Hash, error) {
   178  	var result common.Hash
   179  	err := ec.c.CallContext(ctx, &result, "subbridge_registerOperator", bridge, operator)
   180  	return result, err
   181  }
   182  
   183  // BridgeGetRegisteredOperators returns the list of operators.
   184  func (ec *Client) BridgeGetRegisteredOperators(ctx context.Context, bridge common.Address) ([]common.Address, error) {
   185  	var result []common.Address
   186  	err := ec.c.CallContext(ctx, &result, "subbridge_getRegisteredOperators", bridge)
   187  	return result, err
   188  }
   189  
   190  // BridgeSetValueTransferOperatorThreshold can set the operator threshold of the bridge contract.
   191  func (ec *Client) BridgeSetValueTransferOperatorThreshold(ctx context.Context, bridge common.Address, threshold uint8) (common.Hash, error) {
   192  	var result common.Hash
   193  	err := ec.c.CallContext(ctx, &result, "subbridge_setValueTransferOperatorThreshold", bridge, threshold)
   194  	return result, err
   195  }
   196  
   197  // BridgeGetValueTransferOperatorThreshold can get the operator threshold of the bridge contract.
   198  func (ec *Client) BridgeGetValueTransferOperatorThreshold(ctx context.Context, bridge common.Address) (uint8, error) {
   199  	var result uint8
   200  	err := ec.c.CallContext(ctx, &result, "subbridge_getValueTransferOperatorThreshold", bridge)
   201  	return result, err
   202  }
   203  
   204  // BridgeDeployBridge can deploy the pair of bridge for parent/child chain.
   205  func (ec *Client) BridgeDeployBridge(ctx context.Context) (common.Address, common.Address, error) {
   206  	var result []common.Address
   207  
   208  	err := ec.c.CallContext(ctx, &result, "subbridge_deployBridge")
   209  	if err != nil {
   210  		return common.Address{}, common.Address{}, err
   211  	}
   212  
   213  	if len(result) != 2 {
   214  		return common.Address{}, common.Address{}, errors.New("output arguments length err")
   215  	}
   216  
   217  	return result[0], result[1], nil
   218  }
   219  
   220  // BridgeRegisterBridge can register the given pair of deployed child/parent bridges.
   221  func (ec *Client) BridgeRegisterBridge(ctx context.Context, cBridge common.Address, pBridge common.Address) error {
   222  	return ec.c.CallContext(ctx, nil, "subbridge_registerBridge", cBridge, pBridge)
   223  }
   224  
   225  // BridgeDeregisterBridge can deregister the given pair of deployed child/parent bridges.
   226  func (ec *Client) BridgeDeregisterBridge(ctx context.Context, cBridge common.Address, pBridge common.Address) error {
   227  	return ec.c.CallContext(ctx, nil, "subbridge_deregisterBridge", cBridge, pBridge)
   228  }
   229  
   230  // TODO-Klaytn if client pkg is removed in sc pkg, this will be replaced origin struct.
   231  type BridgeJournal struct {
   232  	ChildAddress  common.Address `json:"childAddress"`
   233  	ParentAddress common.Address `json:"parentAddress"`
   234  	Subscribed    bool           `json:"subscribed"`
   235  }
   236  
   237  // BridgeListBridge can return the list of the bridge.
   238  func (ec *Client) BridgeListBridge(ctx context.Context) ([]*BridgeJournal, error) {
   239  	var result []*BridgeJournal
   240  	err := ec.c.CallContext(ctx, &result, "subbridge_listBridge")
   241  	return result, err
   242  }
   243  
   244  // BridgeSubscribeBridge can enable for subbridge to subscribe the event of given child/parent chain bridges.
   245  // If the subscribing is failed, it returns an error.
   246  func (ec *Client) BridgeSubscribeBridge(ctx context.Context, cBridge common.Address, pBridge common.Address) error {
   247  	return ec.c.CallContext(ctx, nil, "subbridge_subscribeBridge", cBridge, pBridge)
   248  }
   249  
   250  // BridgeUnsubscribeBridge disables the event subscription of the given child/parent chain bridges.
   251  // If the unsubscribing is failed, it returns an error.
   252  func (ec *Client) BridgeUnsubscribeBridge(ctx context.Context, cBridge common.Address, pBridge common.Address) error {
   253  	return ec.c.CallContext(ctx, nil, "subbridge_unsubscribeBridge", cBridge, pBridge)
   254  }
   255  
   256  // BridgeRegisterTokenContract can register the given pair of deployed child/parent chain token contracts.
   257  // If the registering is failed, it returns an error.
   258  func (ec *Client) BridgeRegisterTokenContract(ctx context.Context, cBridge, pBridge, cToken, pToken common.Address) error {
   259  	return ec.c.CallContext(ctx, nil, "subbridge_registerToken", cBridge, pBridge, cToken, pToken)
   260  }
   261  
   262  // BridgeDeregisterTokenContract can deregister the given pair of deployed child/parent chain token contracts.
   263  // If the registering is failed, it returns an error.
   264  func (ec *Client) BridgeDeregisterTokenContract(ctx context.Context, cBridge, pBridge, cToken, pToken common.Address) error {
   265  	return ec.c.CallContext(ctx, nil, "subbridge_deregisterToken", cBridge, pBridge, cToken, pToken)
   266  }
   267  
   268  // BridgeTxPendingCount can return the count of the pend tx in bridge txpool.
   269  func (ec *Client) BridgeTxPendingCount(ctx context.Context) (int, error) {
   270  	var result int
   271  	err := ec.c.CallContext(ctx, &result, "subbridge_txPendingCount")
   272  	return result, err
   273  }
   274  
   275  // BridgeGetTxPending can return the pend tx list mapped by address.
   276  func (ec *Client) BridgeGetTxPending(ctx context.Context) (map[common.Address]types.Transactions, error) {
   277  	var result map[common.Address]types.Transactions
   278  	err := ec.c.CallContext(ctx, &result, "subbridge_txPending")
   279  	return result, err
   280  }
   281  
   282  // BridgeSetERC20Fee can set the ERC20 transfer fee.
   283  func (ec *Client) BridgeSetERC20Fee(ctx context.Context, bridgeAddr, tokenAddr common.Address, fee *big.Int) (common.Hash, error) {
   284  	var result common.Hash
   285  	err := ec.c.CallContext(ctx, &result, "subbridge_setERC20Fee", bridgeAddr, tokenAddr, fee)
   286  	return result, err
   287  }
   288  
   289  // BridgeSetKLAYFee can set the KLAY transfer fee.
   290  func (ec *Client) BridgeSetKLAYFee(ctx context.Context, bridgeAddr common.Address, fee *big.Int) (common.Hash, error) {
   291  	var result common.Hash
   292  	err := ec.c.CallContext(ctx, &result, "subbridge_setKLAYFee", bridgeAddr, fee)
   293  	return result, err
   294  }
   295  
   296  // BridgeGetERC20Fee returns the ERC20 transfer fee.
   297  func (ec *Client) BridgeGetERC20Fee(ctx context.Context, bridgeAddr, tokenAddr common.Address) (*big.Int, error) {
   298  	var result hexutil.Big
   299  	err := ec.c.CallContext(ctx, &result, "subbridge_getERC20Fee", bridgeAddr, tokenAddr)
   300  	return (*big.Int)(&result), err
   301  }
   302  
   303  // BridgeGetKLAYFee returns the KLAY transfer fee.
   304  func (ec *Client) BridgeGetKLAYFee(ctx context.Context, bridgeAddr common.Address) (*big.Int, error) {
   305  	var result hexutil.Big
   306  	err := ec.c.CallContext(ctx, &result, "subbridge_getKLAYFee", bridgeAddr)
   307  	return (*big.Int)(&result), err
   308  }
   309  
   310  // BridgeSetFeeReceiver can set the fee receiver.
   311  func (ec *Client) BridgeSetFeeReceiver(ctx context.Context, bridgeAddr, receiver common.Address) (common.Hash, error) {
   312  	var result common.Hash
   313  	err := ec.c.CallContext(ctx, &result, "subbridge_setFeeReceiver", bridgeAddr, receiver)
   314  	return result, err
   315  }
   316  
   317  // BridgeGetFeeReceiver returns the fee receiver.
   318  func (ec *Client) BridgeGetFeeReceiver(ctx context.Context, bridgeAddr common.Address) (common.Address, error) {
   319  	var result common.Address
   320  	err := ec.c.CallContext(ctx, &result, "subbridge_getFeeReceiver", bridgeAddr)
   321  	return result, err
   322  }