@@ -4,15 +4,19 @@ import "../interfaces/IChai.sol";
44import "../interfaces/ERC677Receiver.sol " ;
55import "./Ownable.sol " ;
66import "./ERC20Bridge.sol " ;
7+ import "./TokenSwapper.sol " ;
78import "openzeppelin-solidity/contracts/math/SafeMath.sol " ;
89
910/**
1011* @title ChaiConnector
1112* @dev This logic allows to use Chai token (https://github.com/dapphub/chai)
1213*/
13- contract ChaiConnector is Ownable , ERC20Bridge {
14+ contract ChaiConnector is Ownable , ERC20Bridge , TokenSwapper {
1415 using SafeMath for uint256 ;
1516
17+ // emitted when specified value of Chai tokens is transfered to interest receiver
18+ event PaidInterest (address to , uint256 value );
19+
1620 bytes32 internal constant CHAI_TOKEN_ENABLED = 0x2ae87563606f93f71ad2adf4d62661ccdfb63f3f508f94700934d5877fb92278 ; // keccak256(abi.encodePacked("chaiTokenEnabled"))
1721 bytes32 internal constant INTEREST_RECEIVER = 0xd88509eb1a8da5d5a2fc7b9bad1c72874c9818c788e81d0bc46b29bfaa83adf6 ; // keccak256(abi.encodePacked("interestReceiver"))
1822 bytes32 internal constant INTEREST_COLLECTION_PERIOD = 0x68a6a652d193e5d6439c4309583048050a11a4cfb263a220f4cd798c61c3ad6e ; // keccak256(abi.encodePacked("interestCollectionPeriod"))
@@ -112,6 +116,14 @@ contract ChaiConnector is Ownable, ERC20Bridge {
112116 * @param receiver New receiver address
113117 */
114118 function setInterestReceiver (address receiver ) external onlyOwner {
119+ // the bridge account is not allowed to receive an interest by the following reason:
120+ // during the Chai to Dai convertion, the Dai is minted to the receiver account,
121+ // the Transfer(address(0), bridgeAddress, value) is emitted during this process,
122+ // something can go wrong in the oracle logic, so that it will process this event as a request to the bridge
123+ // Instead, the interest can be transfered to any other account, and then converted to Dai,
124+ // which won't be related to the oracle logic anymore
125+ require (receiver != address (this ));
126+
115127 addressStorage[INTEREST_RECEIVER] = receiver;
116128 }
117129
@@ -156,23 +168,26 @@ contract ChaiConnector is Ownable, ERC20Bridge {
156168 * @dev Internal function for paying all available interest, in Dai tokens
157169 */
158170 function _payInterest () internal {
159- require (address (interestReceiver ()) != address (0 ));
171+ address receiver = address (interestReceiver ());
172+ require (receiver != address (0 ));
160173
161174 // since investedAmountInChai() returns a ceiled value,
162175 // the value of chaiBalance() - investedAmountInChai() will be floored,
163176 // leading to excess remaining chai balance
164- uint256 balanceBefore = daiBalance ();
165- chaiToken ().exit (address (this ), chaiBalance ().sub (investedAmountInChai ()));
166- uint256 interestInDai = daiBalance ().sub (balanceBefore);
167177
168178 // solhint-disable-next-line not-rely-on-time
169179 uintStorage[LAST_TIME_INTEREST_PAID] = now ;
170180
171- erc20token ().transfer (interestReceiver (), interestInDai);
181+ uint256 interest = chaiBalance ().sub (investedAmountInChai ());
182+ // interest is paid in Chai, paying interest directly in Dai can cause an unwanter Transfer event
183+ // see a comment in setInterestReceiver describing why we cannot pay interest to the bridge directly
184+ chaiToken ().transfer (receiver, interest);
172185
173- interestReceiver () .call (abi.encodeWithSelector (ON_TOKEN_TRANSFER, address (this ), interestInDai , "" ));
186+ receiver .call (abi.encodeWithSelector (ON_TOKEN_TRANSFER, address (this ), interest , "" ));
174187
175188 require (dsrBalance () >= investedAmountInDai ());
189+
190+ emit PaidInterest (receiver, interest);
176191 }
177192
178193 /**
@@ -260,6 +275,8 @@ contract ChaiConnector is Ownable, ERC20Bridge {
260275 // The 10000 constant is considered to be small enough when decimals = 18, however,
261276 // it is not recommended to use it for smaller values of decimals, since it won't be negligible anymore
262277 require (dsrBalance () + 10000 >= newInvestedAmountInDai);
278+
279+ emit TokensSwapped (erc20token (), chaiToken (), amount);
263280 }
264281
265282 /**
@@ -286,5 +303,7 @@ contract ChaiConnector is Ownable, ERC20Bridge {
286303 setInvestedAmountInDai (newInvested);
287304
288305 require (dsrBalance () >= newInvested);
306+
307+ emit TokensSwapped (chaiToken (), erc20token (), redeemed);
289308 }
290309}
0 commit comments