github.com/klaytn/klaytn@v1.12.1/contracts/extbridge/ext_bridge.sol (about) 1 // Copyright 2019 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The klaytn library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 pragma solidity 0.5.6; 18 19 import "../bridge/BridgeTransferERC20.sol"; 20 import "../bridge/BridgeTransferERC721.sol"; 21 import "./callback.sol"; 22 23 24 // ExtBridge is an extended bridge contract example inherited by BridgeTransferERC20 and BridgeTransferERC721. 25 // This contract overrides handleERC20Transfer and handleERC721Transfer to make an internal call to callback contract. 26 contract ExtBridge is BridgeTransferERC20, BridgeTransferERC721 { 27 address public callback = address(0); 28 29 constructor(bool _modeMintBurn) BridgeTransfer(_modeMintBurn) public payable { 30 } 31 32 function setCallback(address _addr) public onlyOwner { 33 callback = _addr; 34 } 35 36 // requestSellERC20 requests transfer ERC20 to _to on relative chain to sell it. 37 function requestSellERC20( 38 address _tokenAddress, 39 address _to, 40 uint256 _value, 41 uint256 _feeLimit, 42 uint256 _price 43 ) 44 external 45 { 46 super.requestERC20Transfer( 47 _tokenAddress, 48 _to, 49 _value, 50 _feeLimit, 51 abi.encode(_price) 52 ); 53 } 54 55 // requestERC20Transfer requests transfer ERC20 to _to on relative chain. 56 function requestERC20Transfer( 57 address _tokenAddress, 58 address _to, 59 uint256 _value, 60 uint256 _feeLimit, 61 bytes memory _extraData 62 ) 63 public 64 { 65 revert("not support"); 66 } 67 68 // requestSellERC721 requests transfer ERC721 to _to on relative chain to sell it. 69 function requestSellERC721( 70 address _tokenAddress, 71 address _to, 72 uint256 _tokenId, 73 uint256 _price 74 ) 75 external 76 { 77 super.requestERC721Transfer( 78 _tokenAddress, 79 _to, 80 _tokenId, 81 abi.encode(_price) 82 ); 83 } 84 85 // requestERC721Transfer requests transfer ERC721 to _to on relative chain. 86 function requestERC721Transfer( 87 address _tokenAddress, 88 address _to, 89 uint256 _tokenId, 90 bytes memory _extraData 91 ) 92 public 93 { 94 revert("not support"); 95 } 96 97 // handleERC20Transfer sends the ERC20 token by the request and processes the extended feature. 98 function handleERC20Transfer( 99 bytes32 _requestTxHash, 100 address _from, 101 address _to, 102 address _tokenAddress, 103 uint256 _value, 104 uint64 _requestNonce, 105 uint64 _requestBlockNumber, 106 bytes memory _extraData 107 ) 108 public 109 { 110 require(_extraData.length == 32, "extraData size error"); 111 112 require(callback != address(0), "callback address error"); 113 114 uint256 offerPrice = abi.decode(_extraData, (uint256)); 115 require(offerPrice > 0, "offerPrice error"); 116 117 super.handleERC20Transfer(_requestTxHash, _from, callback, _tokenAddress, _value, _requestNonce, _requestBlockNumber, _extraData); 118 Callback(callback).registerOffer(_to, _value, _tokenAddress, offerPrice); 119 } 120 121 // handleERC721Transfer sends the ERC721 token by the request and processes the extended feature. 122 function handleERC721Transfer( 123 bytes32 _requestTxHash, 124 address _from, 125 address _to, 126 address _tokenAddress, 127 uint256 _tokenId, 128 uint64 _requestNonce, 129 uint64 _requestBlockNumber, 130 string memory _tokenURI, 131 bytes memory _extraData 132 ) 133 public 134 { 135 require(_extraData.length == 32, "extraData size error"); 136 137 require(callback != address(0), "callback address error"); 138 139 uint256 offerPrice = abi.decode(_extraData, (uint256)); 140 require(offerPrice > 0, "offerPrice error"); 141 142 super.handleERC721Transfer(_requestTxHash, _from, callback, _tokenAddress, _tokenId, _requestNonce, _requestBlockNumber, _tokenURI, _extraData); 143 Callback(callback).registerOffer(_to, _tokenId, _tokenAddress, offerPrice); 144 } 145 }