From 1f5c2780070f739e6edbce4125bffe1e5845e50b Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Wed, 25 Mar 2020 13:25:05 -0500 Subject: [PATCH 01/13] Use TBTCDepositToken instead of address Only TBTCDepositToken should be changed since it is the only contract used. The rest are just address placeholders for init funcitons. --- solidity/contracts/proxy/DepositFactory.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/solidity/contracts/proxy/DepositFactory.sol b/solidity/contracts/proxy/DepositFactory.sol index 478fae231..b2d9131e4 100644 --- a/solidity/contracts/proxy/DepositFactory.sol +++ b/solidity/contracts/proxy/DepositFactory.sol @@ -18,9 +18,9 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ // Holds the address of the deposit contract // which will be used as a master contract for cloning. address payable public masterDepositAddress; + TBTCDepositToken tbtcDepositToken; address public tbtcSystem; address public tbtcToken; - address public tbtcDepositToken; address public feeRebateToken; address public vendingMachine; uint256 public keepThreshold; @@ -34,7 +34,7 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ /// @param _masterDepositAddress The address of the master deposit contract. /// @param _tbtcSystem Address of system contract. /// @param _tbtcToken Address of TBTC token contract. - /// @param _depositOwnerToken Address of the Deposit Owner Token contract. + /// @param _tbtcDepositToken Address of the TBTC Deposit Token contract. /// @param _feeRebateToken Address of the Fee Rebate Token contract. /// @param _vendingMachine Address of the Vending Machine contract. /// @param _keepThreshold Minimum number of honest keep members. @@ -43,16 +43,16 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ address payable _masterDepositAddress, address _tbtcSystem, address _tbtcToken, - address _depositOwnerToken, + address _tbtcDepositToken, address _feeRebateToken, address _vendingMachine, uint256 _keepThreshold, uint256 _keepSize ) public onlyTbtcSystem { masterDepositAddress = _masterDepositAddress; + tbtcDepositToken = TBTCDepositToken(_tbtcDepositToken); tbtcSystem = _tbtcSystem; tbtcToken = _tbtcToken; - tbtcDepositToken = _depositOwnerToken; feeRebateToken = _feeRebateToken; vendingMachine = _vendingMachine; keepThreshold = _keepThreshold; @@ -77,7 +77,7 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ deposit.createNewDeposit.value(msg.value)( tbtcSystem, tbtcToken, - tbtcDepositToken, + address(tbtcDepositToken), feeRebateToken, vendingMachine, keepThreshold, From 3e6b63acd820e39fb4292ca3db074df760c377fb Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Wed, 25 Mar 2020 13:27:44 -0500 Subject: [PATCH 02/13] Use contract over addresses types where reasonable FeeRebateToken not using IERC721 since the interface does not expose an exists() function --- solidity/contracts/deposit/Deposit.sol | 13 +++++--- solidity/contracts/deposit/DepositFunding.sol | 2 +- .../contracts/deposit/DepositLiquidation.sol | 8 ++--- .../contracts/deposit/DepositRedemption.sol | 24 +++++--------- solidity/contracts/deposit/DepositUtils.sol | 33 ++++++++----------- .../deposit/OutsourceDepositLogging.sol | 24 +++++++------- .../contracts/test/deposit/TestDeposit.sol | 20 ++++++----- 7 files changed, 59 insertions(+), 65 deletions(-) diff --git a/solidity/contracts/deposit/Deposit.sol b/solidity/contracts/deposit/Deposit.sol index f6c2a3f52..d7792ea51 100644 --- a/solidity/contracts/deposit/Deposit.sol +++ b/solidity/contracts/deposit/Deposit.sol @@ -5,6 +5,11 @@ import {DepositUtils} from "./DepositUtils.sol"; import {DepositFunding} from "./DepositFunding.sol"; import {DepositRedemption} from "./DepositRedemption.sol"; import {DepositStates} from "./DepositStates.sol"; +import {ITBTCSystem} from "../interfaces/ITBTCSystem.sol"; +import {IERC721} from "openzeppelin-solidity/contracts/token/ERC721/IERC721.sol"; +import {TBTCToken} from "../system/TBTCToken.sol"; +import {FeeRebateToken} from "../system/FeeRebateToken.sol"; + import "../system/DepositFactoryAuthority.sol"; /// @title Deposit. @@ -113,10 +118,10 @@ contract Deposit is DepositFactoryAuthority { uint256 _n, uint256 _lotSizeSatoshis ) public onlyFactory payable returns (bool) { - self.TBTCSystem = _TBTCSystem; - self.TBTCToken = _TBTCToken; - self.TBTCDepositToken = _TBTCDepositToken; - self.FeeRebateToken = _FeeRebateToken; + self.tbtcSystem = ITBTCSystem(_TBTCSystem); + self.tbtcToken = TBTCToken(_TBTCToken); + self.tbtcDepositToken = IERC721(_TBTCDepositToken); + self.feeRebateToken = FeeRebateToken(_FeeRebateToken); self.VendingMachine = _VendingMachine; self.createNewDeposit(_m, _n, _lotSizeSatoshis); return true; diff --git a/solidity/contracts/deposit/DepositFunding.sol b/solidity/contracts/deposit/DepositFunding.sol index fc55dae48..d326bcc5b 100644 --- a/solidity/contracts/deposit/DepositFunding.sol +++ b/solidity/contracts/deposit/DepositFunding.sol @@ -55,7 +55,7 @@ library DepositFunding { uint256 _n, uint256 _lotSizeSatoshis ) public returns (bool) { - TBTCSystem _system = TBTCSystem(_d.TBTCSystem); + TBTCSystem _system = TBTCSystem(address(_d.tbtcSystem)); require(_system.getAllowNewDeposits(), "Opening new deposits is currently disabled."); require(_d.inStart(), "Deposit setup already requested"); diff --git a/solidity/contracts/deposit/DepositLiquidation.sol b/solidity/contracts/deposit/DepositLiquidation.sol index 04e9b7db5..80a7743a7 100644 --- a/solidity/contracts/deposit/DepositLiquidation.sol +++ b/solidity/contracts/deposit/DepositLiquidation.sol @@ -167,16 +167,14 @@ library DepositLiquidation { // send the TBTC to the TDT holder. If the TDT holder is the Vending Machine, burn it to maintain the peg. address tdtHolder = _d.depositOwner(); - TBTCToken _tbtcToken = TBTCToken(_d.TBTCToken); - uint256 lotSizeTbtc = _d.lotSizeTbtc(); - require(_tbtcToken.balanceOf(msg.sender) >= lotSizeTbtc, "Not enough TBTC to cover outstanding debt"); + require(_d.tbtcToken.balanceOf(msg.sender) >= lotSizeTbtc, "Not enough TBTC to cover outstanding debt"); if(tdtHolder == _d.VendingMachine){ - _tbtcToken.burnFrom(msg.sender, lotSizeTbtc); // burn minimal amount to cover size + _d.tbtcToken.burnFrom(msg.sender, lotSizeTbtc); // burn minimal amount to cover size } else{ - _tbtcToken.transferFrom(msg.sender, tdtHolder, lotSizeTbtc); + _d.tbtcToken.transferFrom(msg.sender, tdtHolder, lotSizeTbtc); } // Distribute funds to auction buyer diff --git a/solidity/contracts/deposit/DepositRedemption.sol b/solidity/contracts/deposit/DepositRedemption.sol index 26dd0f561..c6f0d1b0f 100644 --- a/solidity/contracts/deposit/DepositRedemption.sol +++ b/solidity/contracts/deposit/DepositRedemption.sol @@ -31,13 +31,10 @@ library DepositRedemption { /// @notice Pushes signer fee to the Keep group by transferring it to the Keep address. /// @dev Approves the keep contract, then expects it to call transferFrom. function distributeSignerFee(DepositUtils.Deposit storage _d) internal { - address _tbtcTokenAddress = _d.TBTCToken; - TBTCToken _tbtcToken = TBTCToken(_tbtcTokenAddress); - IBondedECDSAKeep _keep = IBondedECDSAKeep(_d.keepAddress); - _tbtcToken.approve(_d.keepAddress, _d.signerFee()); - _keep.distributeERC20Reward(_tbtcTokenAddress, _d.signerFee()); + _d.tbtcToken.approve(_d.keepAddress, _d.signerFee()); + _keep.distributeERC20Reward(address(_d.tbtcToken), _d.signerFee()); } /// @notice Closes keep associated with the deposit. @@ -61,7 +58,6 @@ library DepositRedemption { /// @notice Handles TBTC requirements for redemption. /// @dev Burns or transfers depending on term and supply-peg impact. function performRedemptionTBTCTransfers(DepositUtils.Deposit storage _d) internal { - TBTCToken _tbtc = TBTCToken(_d.TBTCToken); address tdtHolder = _d.depositOwner(); address vendingMachine = _d.VendingMachine; @@ -75,7 +71,7 @@ library DepositRedemption { } // if we owe > 0 & < signerfee, msg.sender is TDT owner but not FRT holder. if(tbtcOwed <= signerFee){ - _tbtc.transferFrom(msg.sender, address(this), tbtcOwed); + _d.tbtcToken.transferFrom(msg.sender, address(this), tbtcOwed); return; } // Redemmer always owes a full TBTC for at-term redemption. @@ -86,16 +82,16 @@ library DepositRedemption { // Vending Machine-owned TDTs have been used to mint TBTC, // and we should always burn a full TBTC to redeem the deposit. if(tdtHolder == vendingMachine){ - _tbtc.burnFrom(msg.sender, tbtcLot); + _d.tbtcToken.burnFrom(msg.sender, tbtcLot); } // if signer fee is not escrowed, escrow and it here and send the rest to TDT owner - else if(_tbtc.balanceOf(address(this)) < signerFee){ - _tbtc.transferFrom(msg.sender, address(this), signerFee); - _tbtc.transferFrom(msg.sender, tdtHolder, tbtcLot.sub(signerFee)); + else if(_d.tbtcToken.balanceOf(address(this)) < signerFee){ + _d.tbtcToken.transferFrom(msg.sender, address(this), signerFee); + _d.tbtcToken.transferFrom(msg.sender, tdtHolder, tbtcLot.sub(signerFee)); } // tansfer a full TBTC to TDT owner if signerFee is escrowed else{ - _tbtc.transferFrom(msg.sender, tdtHolder, tbtcLot); + _d.tbtcToken.transferFrom(msg.sender, tdtHolder, tbtcLot); } return; } @@ -162,9 +158,7 @@ library DepositRedemption { bytes memory _redeemerOutputScript, address payable _finalRecipient ) public { - IERC721 _tbtcDepositToken = IERC721(_d.TBTCDepositToken); - - _tbtcDepositToken.transferFrom(msg.sender, _finalRecipient, uint256(address(this))); + _d.tbtcDepositToken.transferFrom(msg.sender, _finalRecipient, uint256(address(this))); _requestRedemption(_d, _outputValueBytes, _redeemerOutputScript, _finalRecipient); } diff --git a/solidity/contracts/deposit/DepositUtils.sol b/solidity/contracts/deposit/DepositUtils.sol index 04c8790c4..9851fca78 100644 --- a/solidity/contracts/deposit/DepositUtils.sol +++ b/solidity/contracts/deposit/DepositUtils.sol @@ -25,10 +25,10 @@ library DepositUtils { struct Deposit { // SET DURING CONSTRUCTION - address TBTCSystem; - address TBTCToken; - address TBTCDepositToken; - address FeeRebateToken; + ITBTCSystem tbtcSystem; + TBTCToken tbtcToken; + IERC721 tbtcDepositToken; + FeeRebateToken feeRebateToken; address VendingMachine; uint256 lotSizeSatoshis; uint8 currentState; @@ -74,16 +74,14 @@ library DepositUtils { /// @dev Calls the light relay and gets the current block difficulty. /// @return The difficulty. function currentBlockDifficulty(Deposit storage _d) public view returns (uint256) { - ITBTCSystem _sys = ITBTCSystem(_d.TBTCSystem); - return _sys.fetchRelayCurrentDifficulty(); + return _d.tbtcSystem.fetchRelayCurrentDifficulty(); } /// @notice Gets the previous block difficulty. /// @dev Calls the light relay and gets the previous block difficulty. /// @return The difficulty. function previousBlockDifficulty(Deposit storage _d) public view returns (uint256) { - ITBTCSystem _sys = ITBTCSystem(_d.TBTCSystem); - return _sys.fetchRelayPreviousDifficulty(); + return _d.tbtcSystem.fetchRelayPreviousDifficulty(); } /// @notice Evaluates the header difficulties in a proof. @@ -310,8 +308,7 @@ library DepositUtils { /// @dev Polls the price feed via the system contract. /// @return The current price of 1 sat in wei. function fetchBitcoinPrice(Deposit storage _d) public view returns (uint256) { - ITBTCSystem _sys = ITBTCSystem(_d.TBTCSystem); - return _sys.fetchBitcoinPrice(); + return _d.tbtcSystem.fetchBitcoinPrice(); } /// @notice Fetches the Keep's bond amount in wei. @@ -342,10 +339,9 @@ library DepositUtils { /// @return The current token holder if the Token exists. /// address(0) if the token does not exist. function feeRebateTokenHolder(Deposit storage _d) public view returns (address payable) { - FeeRebateToken _feeRebateToken = FeeRebateToken(_d.FeeRebateToken); address tokenHolder; - if(_feeRebateToken.exists(uint256(address(this)))){ - tokenHolder = address(uint160(_feeRebateToken.ownerOf(uint256(address(this))))); + if(_d.feeRebateToken.exists(uint256(address(this)))){ + tokenHolder = address(uint160(_d.feeRebateToken.ownerOf(uint256(address(this))))); } return address(uint160(tokenHolder)); } @@ -354,8 +350,7 @@ library DepositUtils { /// @dev We cast the address to a uint256 to match the 721 standard. /// @return The current deposit beneficiary. function depositOwner(Deposit storage _d) public view returns (address payable) { - IERC721 _tbtcDepositToken = IERC721(_d.TBTCDepositToken); - return address(uint160(_tbtcDepositToken.ownerOf(uint256(address(this))))); + return address(uint160(_d.tbtcDepositToken.ownerOf(uint256(address(this))))); } /// @notice Deletes state after termination of redemption process. @@ -394,16 +389,14 @@ library DepositUtils { /// @notice Distributes the fee rebate to the Fee Rebate Token owner. /// @dev Whenever this is called we are shutting down. function distributeFeeRebate(Deposit storage _d) internal { - TBTCToken _tbtc = TBTCToken(_d.TBTCToken); - address rebateTokenHolder = feeRebateTokenHolder(_d); // We didn't escrow a rebate if the redeemer is also the Fee Rebate Token holder if(_d.redeemerAddress == rebateTokenHolder) return; // pay out the rebate if it is available - if(_tbtc.balanceOf(address(this)) >= signerFee(_d)) { - _tbtc.transfer(rebateTokenHolder, signerFee(_d)); + if(_d.tbtcToken.balanceOf(address(this)) >= signerFee(_d)) { + _d.tbtcToken.transfer(rebateTokenHolder, signerFee(_d)); } } @@ -430,7 +423,7 @@ library DepositUtils { return fee; } } - uint256 contractTbtcBalance = TBTCToken(_d.TBTCToken).balanceOf(address(this)); + uint256 contractTbtcBalance = _d.tbtcToken.balanceOf(address(this)); if(contractTbtcBalance < fee) { return fee.sub(contractTbtcBalance); } diff --git a/solidity/contracts/deposit/OutsourceDepositLogging.sol b/solidity/contracts/deposit/OutsourceDepositLogging.sol index 784d021ff..204e6f41e 100644 --- a/solidity/contracts/deposit/OutsourceDepositLogging.sol +++ b/solidity/contracts/deposit/OutsourceDepositLogging.sol @@ -12,7 +12,7 @@ library OutsourceDepositLogging { /// msg.sender will be the calling Deposit's address. /// @param _keepAddress The address of the associated keep. function logCreated(DepositUtils.Deposit storage _d, address _keepAddress) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logCreated(_keepAddress); } @@ -34,7 +34,7 @@ library OutsourceDepositLogging { uint256 _requestedFee, bytes memory _outpoint ) public { // not external to allow bytes memory output scripts - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logRedemptionRequested( _redeemer, _digest, @@ -57,7 +57,7 @@ library OutsourceDepositLogging { bytes32 _r, bytes32 _s ) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logGotRedemptionSignature( _digest, _r, @@ -72,7 +72,7 @@ library OutsourceDepositLogging { bytes32 _signingGroupPubkeyX, bytes32 _signingGroupPubkeyY ) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logRegisteredPubkey( _signingGroupPubkeyX, _signingGroupPubkeyY); @@ -81,28 +81,28 @@ library OutsourceDepositLogging { /// @notice Fires a SetupFailed event. /// @dev The logger is on a system contract, so all logs from all deposits are from the same address. function logSetupFailed(DepositUtils.Deposit storage _d) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logSetupFailed(); } /// @notice Fires a FraudDuringSetup event. /// @dev The logger is on a system contract, so all logs from all deposits are from the same address. function logFraudDuringSetup(DepositUtils.Deposit storage _d) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logFraudDuringSetup(); } /// @notice Fires a Funded event. /// @dev The logger is on a system contract, so all logs from all deposits are from the same address. function logFunded(DepositUtils.Deposit storage _d) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logFunded(); } /// @notice Fires a CourtesyCalled event. /// @dev The logger is on a system contract, so all logs from all deposits are from the same address. function logCourtesyCalled(DepositUtils.Deposit storage _d) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logCourtesyCalled(); } @@ -110,28 +110,28 @@ library OutsourceDepositLogging { /// @dev We append the sender, which is the deposit contract that called. /// @param _wasFraud True if liquidating for fraud. function logStartedLiquidation(DepositUtils.Deposit storage _d, bool _wasFraud) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logStartedLiquidation(_wasFraud); } /// @notice Fires a Redeemed event. /// @dev The logger is on a system contract, so all logs from all deposits are from the same address. function logRedeemed(DepositUtils.Deposit storage _d, bytes32 _txid) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logRedeemed(_txid); } /// @notice Fires a Liquidated event. /// @dev The logger is on a system contract, so all logs from all deposits are from the same address. function logLiquidated(DepositUtils.Deposit storage _d) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logLiquidated(); } /// @notice Fires a ExitedCourtesyCall event. /// @dev The logger is on a system contract, so all logs from all deposits are from the same address. function logExitedCourtesyCall(DepositUtils.Deposit storage _d) external { - DepositLog _logger = DepositLog(_d.TBTCSystem); + DepositLog _logger = DepositLog(address(_d.tbtcSystem)); _logger.logExitedCourtesyCall(); } } diff --git a/solidity/contracts/test/deposit/TestDeposit.sol b/solidity/contracts/test/deposit/TestDeposit.sol index 2e4d2c1bf..7cfad79d4 100644 --- a/solidity/contracts/test/deposit/TestDeposit.sol +++ b/solidity/contracts/test/deposit/TestDeposit.sol @@ -1,6 +1,10 @@ pragma solidity ^0.5.10; import {Deposit} from "../../../contracts/deposit/Deposit.sol"; +import {ITBTCSystem} from "../../interfaces/ITBTCSystem.sol"; +import {IERC721} from "openzeppelin-solidity/contracts/token/ERC721/IERC721.sol"; +import {TBTCToken} from "../../system/TBTCToken.sol"; +import {FeeRebateToken} from "../../system/FeeRebateToken.sol"; contract TestDeposit is Deposit { @@ -18,10 +22,10 @@ contract TestDeposit is Deposit { uint256 _n, uint256 _lotSizeSatoshis ) public payable returns (bool) { - self.TBTCSystem = _TBTCSystem; - self.TBTCToken = _TBTCToken; - self.TBTCDepositToken = _TBTCDepositToken; - self.FeeRebateToken = _FeeRebateToken; + self.tbtcSystem = ITBTCSystem(_TBTCSystem); + self.tbtcToken = TBTCToken(_TBTCToken); + self.tbtcDepositToken = IERC721(_TBTCDepositToken); + self.feeRebateToken = FeeRebateToken(_FeeRebateToken); self.VendingMachine = _VendingMachine; self.createNewDeposit(_m, _n, _lotSizeSatoshis); return true; @@ -34,10 +38,10 @@ contract TestDeposit is Deposit { address _feeRebateToken, address _vendingMachine ) public { - self.TBTCSystem = _sys; - self.TBTCToken = _token; - self.TBTCDepositToken = _tbtcDepositToken; - self.FeeRebateToken = _feeRebateToken; + self.tbtcSystem = ITBTCSystem(_sys); + self.tbtcToken = TBTCToken(_token); + self.tbtcDepositToken = IERC721(_tbtcDepositToken); + self.feeRebateToken = FeeRebateToken(_feeRebateToken); self.VendingMachine = _vendingMachine; } From 3303dc57821b5e73aecff9f6596da200631c05e0 Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Wed, 25 Mar 2020 13:40:00 -0500 Subject: [PATCH 03/13] TBTCSystem address-> contract type where reasonable --- solidity/contracts/system/TBTCSystem.sol | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/solidity/contracts/system/TBTCSystem.sol b/solidity/contracts/system/TBTCSystem.sol index 44f5da039..140663c02 100644 --- a/solidity/contracts/system/TBTCSystem.sol +++ b/solidity/contracts/system/TBTCSystem.sol @@ -48,9 +48,9 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { uint256 constant pausedDuration = 10 days; TBTCDepositToken tbtcDepositToken; - address public keepVendor; - address public priceFeed; - address public relay; + IBTCETHPriceFeed priceFeed; + IBondedECDSAKeepVendor keepVendor; + IRelay relay; // Parameters governed by the TBTCSystem owner bool private allowNewDeposits = false; @@ -73,8 +73,8 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { uint128 private newSeverelyUndercollateralizedThresholdPercent; constructor(address _priceFeed, address _relay) public { - priceFeed = _priceFeed; - relay = _relay; + priceFeed = IBTCETHPriceFeed(_priceFeed); + relay = IRelay(_relay); } /// @notice Initialize contracts @@ -101,7 +101,7 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { ) external onlyOwner { require(!_initialized, "already initialized"); tbtcDepositToken = TBTCDepositToken(_tbtcDepositToken); - keepVendor = _keepVendor; + keepVendor = IBondedECDSAKeepVendor(_keepVendor); VendingMachine(_vendingMachine).setExternalAddresses( _tbtcToken, _tbtcDepositToken, @@ -354,7 +354,7 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { /// if the price of one satoshi is 1 ether. /// @return The price of one satoshi in wei. function fetchBitcoinPrice() external view returns (uint256) { - uint256 price = IBTCETHPriceFeed(priceFeed).getPrice(); + uint256 price = priceFeed.getPrice(); if (price == 0 || price > 10 ** 18) { /* This is if a sat is worth 0 wei, or is worth 1 ether @@ -368,11 +368,11 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { // Difficulty Oracle // TODO: This is a workaround. It will be replaced by tbtc-difficulty-oracle. function fetchRelayCurrentDifficulty() external view returns (uint256) { - return IRelay(relay).getCurrentEpochDifficulty(); + return relay.getCurrentEpochDifficulty(); } function fetchRelayPreviousDifficulty() external view returns (uint256) { - return IRelay(relay).getPrevEpochDifficulty(); + return relay.getPrevEpochDifficulty(); } /// @notice Gets a fee estimate for creating a new Deposit. @@ -382,8 +382,7 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { view returns (uint256) { - IBondedECDSAKeepVendor _keepVendor = IBondedECDSAKeepVendor(keepVendor); - IBondedECDSAKeepFactory _keepFactory = IBondedECDSAKeepFactory(_keepVendor.selectFactory()); + IBondedECDSAKeepFactory _keepFactory = IBondedECDSAKeepFactory(keepVendor.selectFactory()); return _keepFactory.openKeepFeeEstimate(); } @@ -397,8 +396,7 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { returns (address) { require(tbtcDepositToken.exists(uint256(msg.sender)), "Caller must be a Deposit contract"); - IBondedECDSAKeepVendor _keepVendor = IBondedECDSAKeepVendor(keepVendor); - IBondedECDSAKeepFactory _keepFactory = IBondedECDSAKeepFactory(_keepVendor.selectFactory()); + IBondedECDSAKeepFactory _keepFactory = IBondedECDSAKeepFactory(keepVendor.selectFactory()); return _keepFactory.openKeep.value(msg.value)(_n, _m, msg.sender, _bond); } } From 2577752452f31f323d2c007baf416a21c6ce500f Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Wed, 25 Mar 2020 16:02:11 -0500 Subject: [PATCH 04/13] Use contract types direclty in parameters Instead of casting the parameter adress, accept Contract type directly --- solidity/contracts/deposit/Deposit.sol | 16 ++++++++-------- solidity/contracts/proxy/DepositFactory.sol | 16 +++++++++------- solidity/contracts/test/deposit/TestDeposit.sol | 16 ++++++++-------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/solidity/contracts/deposit/Deposit.sol b/solidity/contracts/deposit/Deposit.sol index d7792ea51..55ac9e7f6 100644 --- a/solidity/contracts/deposit/Deposit.sol +++ b/solidity/contracts/deposit/Deposit.sol @@ -109,19 +109,19 @@ contract Deposit is DepositFactoryAuthority { /// (10**7 satoshi == 0.1 BTC == 0.1 TBTC). /// @return True if successful, otherwise revert. function createNewDeposit( - address _TBTCSystem, - address _TBTCToken, - address _TBTCDepositToken, - address _FeeRebateToken, + ITBTCSystem _TBTCSystem, + TBTCToken _TBTCToken, + IERC721 _TBTCDepositToken, + FeeRebateToken _FeeRebateToken, address _VendingMachine, uint256 _m, uint256 _n, uint256 _lotSizeSatoshis ) public onlyFactory payable returns (bool) { - self.tbtcSystem = ITBTCSystem(_TBTCSystem); - self.tbtcToken = TBTCToken(_TBTCToken); - self.tbtcDepositToken = IERC721(_TBTCDepositToken); - self.feeRebateToken = FeeRebateToken(_FeeRebateToken); + self.tbtcSystem = (_TBTCSystem); + self.tbtcToken = (_TBTCToken); + self.tbtcDepositToken = (_TBTCDepositToken); + self.feeRebateToken = (_FeeRebateToken); self.VendingMachine = _VendingMachine; self.createNewDeposit(_m, _n, _lotSizeSatoshis); return true; diff --git a/solidity/contracts/proxy/DepositFactory.sol b/solidity/contracts/proxy/DepositFactory.sol index b2d9131e4..82a13b492 100644 --- a/solidity/contracts/proxy/DepositFactory.sol +++ b/solidity/contracts/proxy/DepositFactory.sol @@ -3,6 +3,8 @@ pragma solidity ^0.5.10; import "./CloneFactory.sol"; import "../deposit/Deposit.sol"; import "../system/TBTCSystem.sol"; +import "../system/TBTCToken.sol"; +import "../system/FeeRebateToken.sol"; import "../system/TBTCSystemAuthority.sol"; import {TBTCDepositToken} from "../system/TBTCDepositToken.sol"; @@ -19,9 +21,9 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ // which will be used as a master contract for cloning. address payable public masterDepositAddress; TBTCDepositToken tbtcDepositToken; - address public tbtcSystem; - address public tbtcToken; - address public feeRebateToken; + TBTCSystem public tbtcSystem; + TBTCToken public tbtcToken; + FeeRebateToken public feeRebateToken; address public vendingMachine; uint256 public keepThreshold; uint256 public keepSize; @@ -51,9 +53,9 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ ) public onlyTbtcSystem { masterDepositAddress = _masterDepositAddress; tbtcDepositToken = TBTCDepositToken(_tbtcDepositToken); - tbtcSystem = _tbtcSystem; - tbtcToken = _tbtcToken; - feeRebateToken = _feeRebateToken; + tbtcSystem = TBTCSystem(_tbtcSystem); + tbtcToken = TBTCToken(_tbtcToken); + feeRebateToken = FeeRebateToken(_feeRebateToken); vendingMachine = _vendingMachine; keepThreshold = _keepThreshold; keepSize = _keepSize; @@ -77,7 +79,7 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ deposit.createNewDeposit.value(msg.value)( tbtcSystem, tbtcToken, - address(tbtcDepositToken), + tbtcDepositToken, feeRebateToken, vendingMachine, keepThreshold, diff --git a/solidity/contracts/test/deposit/TestDeposit.sol b/solidity/contracts/test/deposit/TestDeposit.sol index 7cfad79d4..cf6e0f330 100644 --- a/solidity/contracts/test/deposit/TestDeposit.sol +++ b/solidity/contracts/test/deposit/TestDeposit.sol @@ -13,19 +13,19 @@ contract TestDeposit is Deposit { } function createNewDeposit( - address _TBTCSystem, - address _TBTCToken, - address _TBTCDepositToken, - address _FeeRebateToken, + ITBTCSystem _TBTCSystem, + TBTCToken _TBTCToken, + IERC721 _TBTCDepositToken, + FeeRebateToken _FeeRebateToken, address _VendingMachine, uint256 _m, uint256 _n, uint256 _lotSizeSatoshis ) public payable returns (bool) { - self.tbtcSystem = ITBTCSystem(_TBTCSystem); - self.tbtcToken = TBTCToken(_TBTCToken); - self.tbtcDepositToken = IERC721(_TBTCDepositToken); - self.feeRebateToken = FeeRebateToken(_FeeRebateToken); + self.tbtcSystem = (_TBTCSystem); + self.tbtcToken = (_TBTCToken); + self.tbtcDepositToken = (_TBTCDepositToken); + self.feeRebateToken = (_FeeRebateToken); self.VendingMachine = _VendingMachine; self.createNewDeposit(_m, _n, _lotSizeSatoshis); return true; From 54b15670c78645223679a36733ba0648d7efc69d Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Wed, 25 Mar 2020 16:18:25 -0500 Subject: [PATCH 05/13] Update ITBTCSystem interface Add functions needed to create a new Deposit. --- solidity/contracts/interfaces/ITBTCSystem.sol | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/solidity/contracts/interfaces/ITBTCSystem.sol b/solidity/contracts/interfaces/ITBTCSystem.sol index f8acb9f4e..050b06498 100644 --- a/solidity/contracts/interfaces/ITBTCSystem.sol +++ b/solidity/contracts/interfaces/ITBTCSystem.sol @@ -16,4 +16,10 @@ interface ITBTCSystem { function fetchRelayPreviousDifficulty() external view returns (uint256); function getInitialCollateralizedPercent() external view returns (uint128); + function getAllowNewDeposits() external view returns (bool); + function isAllowedLotSize(uint256 _lotSizeSatoshis) external view returns (bool); + function requestNewKeep(uint256 _m, uint256 _n, uint256 _bond) external payable returns (address); + function getSignerFeeDivisor() external view returns (uint256); + function getUndercollateralizedThresholdPercent() external view returns (uint128); + function getSeverelyUndercollateralizedThresholdPercent() external view returns (uint128); } From 9878ca5c97abafc5092ae3baca86788bc82e4205 Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Wed, 25 Mar 2020 16:24:37 -0500 Subject: [PATCH 06/13] Use ITBTCSystem directly from DepositUtils The interface should contain all function sneeded to create the Deposit. --- solidity/contracts/deposit/DepositFunding.sol | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/solidity/contracts/deposit/DepositFunding.sol b/solidity/contracts/deposit/DepositFunding.sol index d326bcc5b..cd7a15438 100644 --- a/solidity/contracts/deposit/DepositFunding.sol +++ b/solidity/contracts/deposit/DepositFunding.sol @@ -5,7 +5,6 @@ import {BytesLib} from "@summa-tx/bitcoin-spv-sol/contracts/BytesLib.sol"; import {BTCUtils} from "@summa-tx/bitcoin-spv-sol/contracts/BTCUtils.sol"; import {IBondedECDSAKeep} from "@keep-network/keep-ecdsa/contracts/api/IBondedECDSAKeep.sol"; import {TBTCToken} from "../system/TBTCToken.sol"; -import {TBTCSystem} from "../system/TBTCSystem.sol"; import {DepositUtils} from "./DepositUtils.sol"; import {DepositLiquidation} from "./DepositLiquidation.sol"; import {DepositStates} from "./DepositStates.sol"; @@ -55,22 +54,20 @@ library DepositFunding { uint256 _n, uint256 _lotSizeSatoshis ) public returns (bool) { - TBTCSystem _system = TBTCSystem(address(_d.tbtcSystem)); - - require(_system.getAllowNewDeposits(), "Opening new deposits is currently disabled."); + require(_d.tbtcSystem.getAllowNewDeposits(), "Opening new deposits is currently disabled."); require(_d.inStart(), "Deposit setup already requested"); - require(_system.isAllowedLotSize(_lotSizeSatoshis), "provided lot size not supported"); + require(_d.tbtcSystem.isAllowedLotSize(_lotSizeSatoshis), "provided lot size not supported"); _d.lotSizeSatoshis = _lotSizeSatoshis; - uint256 _bondRequirementSatoshi = _lotSizeSatoshis.mul(_system.getInitialCollateralizedPercent()).div(100); + uint256 _bondRequirementSatoshi = _lotSizeSatoshis.mul(_d.tbtcSystem.getInitialCollateralizedPercent()).div(100); uint256 _bondRequirementWei = _d.fetchBitcoinPrice().mul(_bondRequirementSatoshi); /* solium-disable-next-line value-in-payable */ - _d.keepAddress = _system.requestNewKeep.value(msg.value)(_m, _n, _bondRequirementWei); - _d.signerFeeDivisor = _system.getSignerFeeDivisor(); - _d.undercollateralizedThresholdPercent = _system.getUndercollateralizedThresholdPercent(); - _d.severelyUndercollateralizedThresholdPercent = _system.getSeverelyUndercollateralizedThresholdPercent(); - _d.initialCollateralizedPercent = _system.getInitialCollateralizedPercent(); + _d.keepAddress = _d.tbtcSystem.requestNewKeep.value(msg.value)(_m, _n, _bondRequirementWei); + _d.signerFeeDivisor = _d.tbtcSystem.getSignerFeeDivisor(); + _d.undercollateralizedThresholdPercent = _d.tbtcSystem.getUndercollateralizedThresholdPercent(); + _d.severelyUndercollateralizedThresholdPercent = _d.tbtcSystem.getSeverelyUndercollateralizedThresholdPercent(); + _d.initialCollateralizedPercent = _d.tbtcSystem.getInitialCollateralizedPercent(); _d.signingGroupRequestedAt = block.timestamp; _d.setAwaitingSignerSetup(); From ef8c467d516cdcdd04fac897fba372576f70c2e9 Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Wed, 25 Mar 2020 18:04:31 -0500 Subject: [PATCH 07/13] Make contract types public Explicit visibility for consistency --- solidity/contracts/system/TBTCSystem.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solidity/contracts/system/TBTCSystem.sol b/solidity/contracts/system/TBTCSystem.sol index 140663c02..54e79d98f 100644 --- a/solidity/contracts/system/TBTCSystem.sol +++ b/solidity/contracts/system/TBTCSystem.sol @@ -47,10 +47,10 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { uint256 pausedTimestamp; uint256 constant pausedDuration = 10 days; - TBTCDepositToken tbtcDepositToken; - IBTCETHPriceFeed priceFeed; - IBondedECDSAKeepVendor keepVendor; - IRelay relay; + TBTCDepositToken public tbtcDepositToken; + IBTCETHPriceFeed public priceFeed; + IBondedECDSAKeepVendor public keepVendor; + IRelay public relay; // Parameters governed by the TBTCSystem owner bool private allowNewDeposits = false; From 41104d05b0aa0398a63d86fa21ef03e448c2cf6d Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Wed, 25 Mar 2020 18:05:17 -0500 Subject: [PATCH 08/13] Contract type for Function parameters When a contract has use beyon it's address, Pass the contract type isntead of the address. --- solidity/contracts/DepositLog.sol | 6 ++-- solidity/contracts/deposit/Deposit.sol | 16 ++++----- solidity/contracts/proxy/DepositFactory.sol | 24 ++++++------- solidity/contracts/system/TBTCSystem.sol | 38 ++++++++++---------- solidity/contracts/system/VendingMachine.sol | 18 +++++----- 5 files changed, 52 insertions(+), 50 deletions(-) diff --git a/solidity/contracts/DepositLog.sol b/solidity/contracts/DepositLog.sol index 15367a29a..56e3a5c13 100644 --- a/solidity/contracts/DepositLog.sol +++ b/solidity/contracts/DepositLog.sol @@ -118,13 +118,13 @@ contract DepositLog { /// @notice Sets the tbtcDepositToken contract. /// @dev The contract is used by `approvedToLog` to check if the /// caller is a Deposit contract. This should only be called once. - /// @param _tbtcDepositTokenAddress The address of the tbtcDepositToken. - function setTbtcDepositToken(address _tbtcDepositTokenAddress) public { + /// @param _tbtcDepositToken tbtcDepositToken contract. + function setTbtcDepositToken(TBTCDepositToken _tbtcDepositToken) public { require( address(tbtcDepositToken) == address(0), "tbtcDepositToken is already set" ); - tbtcDepositToken = TBTCDepositToken(_tbtcDepositTokenAddress); + tbtcDepositToken = _tbtcDepositToken; } // diff --git a/solidity/contracts/deposit/Deposit.sol b/solidity/contracts/deposit/Deposit.sol index 55ac9e7f6..cfb6a42e5 100644 --- a/solidity/contracts/deposit/Deposit.sol +++ b/solidity/contracts/deposit/Deposit.sol @@ -97,10 +97,10 @@ contract Deposit is DepositFactoryAuthority { // THIS IS THE INIT FUNCTION /// @notice The Deposit Factory can spin up a new deposit. /// @dev Only the Deposit factory can call this. - /// @param _TBTCSystem `TBTCSystem` address. More info in `VendingMachine`. - /// @param _TBTCToken `TBTCToken` address. More info in TBTCToken`. - /// @param _TBTCDepositToken `TBTCDepositToken` (TDT) address. More info in `TBTCDepositToken`. - /// @param _FeeRebateToken `FeeRebateToken` (FRT) address. More info in `FeeRebateToken`. + /// @param _TBTCSystem `TBTCSystem` contract. More info in `VendingMachine`. + /// @param _TBTCToken `TBTCToken` contract. More info in TBTCToken`. + /// @param _TBTCDepositToken `TBTCDepositToken` (TDT) contract. More info in `TBTCDepositToken`. + /// @param _FeeRebateToken `FeeRebateToken` (FRT) contract. More info in `FeeRebateToken`. /// @param _VendingMachine `VendingMachine` address. More info in `VendingMachine`. /// @param _m Signing group honesty threshold. /// @param _n Signing group size. @@ -118,10 +118,10 @@ contract Deposit is DepositFactoryAuthority { uint256 _n, uint256 _lotSizeSatoshis ) public onlyFactory payable returns (bool) { - self.tbtcSystem = (_TBTCSystem); - self.tbtcToken = (_TBTCToken); - self.tbtcDepositToken = (_TBTCDepositToken); - self.feeRebateToken = (_FeeRebateToken); + self.tbtcSystem = _TBTCSystem; + self.tbtcToken = _TBTCToken; + self.tbtcDepositToken = _TBTCDepositToken; + self.feeRebateToken = _FeeRebateToken; self.VendingMachine = _VendingMachine; self.createNewDeposit(_m, _n, _lotSizeSatoshis); return true; diff --git a/solidity/contracts/proxy/DepositFactory.sol b/solidity/contracts/proxy/DepositFactory.sol index 82a13b492..70de4c05e 100644 --- a/solidity/contracts/proxy/DepositFactory.sol +++ b/solidity/contracts/proxy/DepositFactory.sol @@ -34,28 +34,28 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ /// @dev Set the required external variables. /// @param _masterDepositAddress The address of the master deposit contract. - /// @param _tbtcSystem Address of system contract. - /// @param _tbtcToken Address of TBTC token contract. - /// @param _tbtcDepositToken Address of the TBTC Deposit Token contract. - /// @param _feeRebateToken Address of the Fee Rebate Token contract. + /// @param _tbtcSystem Tbtc system contract. + /// @param _tbtcToken TBTC token contract. + /// @param _tbtcDepositToken TBTC Deposit Token contract. + /// @param _feeRebateToken AFee Rebate Token contract. /// @param _vendingMachine Address of the Vending Machine contract. /// @param _keepThreshold Minimum number of honest keep members. /// @param _keepSize Number of all members in a keep. function setExternalDependencies( address payable _masterDepositAddress, - address _tbtcSystem, - address _tbtcToken, - address _tbtcDepositToken, - address _feeRebateToken, + TBTCSystem _tbtcSystem, + TBTCToken _tbtcToken, + TBTCDepositToken _tbtcDepositToken, + FeeRebateToken _feeRebateToken, address _vendingMachine, uint256 _keepThreshold, uint256 _keepSize ) public onlyTbtcSystem { masterDepositAddress = _masterDepositAddress; - tbtcDepositToken = TBTCDepositToken(_tbtcDepositToken); - tbtcSystem = TBTCSystem(_tbtcSystem); - tbtcToken = TBTCToken(_tbtcToken); - feeRebateToken = FeeRebateToken(_feeRebateToken); + tbtcDepositToken = _tbtcDepositToken; + tbtcSystem = _tbtcSystem; + tbtcToken = _tbtcToken; + feeRebateToken = _feeRebateToken; vendingMachine = _vendingMachine; keepThreshold = _keepThreshold; keepSize = _keepSize; diff --git a/solidity/contracts/system/TBTCSystem.sol b/solidity/contracts/system/TBTCSystem.sol index 54e79d98f..f5cbd43ba 100644 --- a/solidity/contracts/system/TBTCSystem.sol +++ b/solidity/contracts/system/TBTCSystem.sol @@ -14,6 +14,8 @@ import {IBTCETHPriceFeed} from "../interfaces/IBTCETHPriceFeed.sol"; import {DepositLog} from "../DepositLog.sol"; import {TBTCDepositToken} from "./TBTCDepositToken.sol"; +import "./TBTCToken.sol"; +import "./FeeRebateToken.sol"; import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; @@ -79,41 +81,41 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { /// @notice Initialize contracts /// @dev Only the Deposit factory should call this, and only once. - /// @param _keepVendor ECDSA keep vendor address. - /// @param _depositFactory Deposit Factory address. More info in `DepositFactory`. + /// @param _keepVendor ECDSA keep vendor. + /// @param _depositFactory Deposit Factory. More info in `DepositFactory`. /// @param _masterDepositAddress Master Deposit address. More info in `Deposit`. - /// @param _tbtcToken TBTCToken address. More info in `TBTCToken`. - /// @param _tbtcDepositToken TBTCDepositToken (TDT) address. More info in `TBTCDepositToken`. - /// @param _feeRebateToken FeeRebateToken (FRT) address. More info in `FeeRebateToken`. - /// @param _vendingMachine Vending Machine address. More info in `VendingMachine`. + /// @param _tbtcToken TBTCToken. More info in `TBTCToken`. + /// @param _tbtcDepositToken TBTCDepositToken (TDT). More info in `TBTCDepositToken`. + /// @param _feeRebateToken FeeRebateToken (FRT). More info in `FeeRebateToken`. + /// @param _vendingMachine Vending Machine. More info in `VendingMachine`. /// @param _keepThreshold Signing group honesty threshold. /// @param _keepSize Signing group size. function initialize( - address _keepVendor, - address _depositFactory, + IBondedECDSAKeepVendor _keepVendor, + DepositFactory _depositFactory, address payable _masterDepositAddress, - address _tbtcToken, - address _tbtcDepositToken, - address _feeRebateToken, - address _vendingMachine, + TBTCToken _tbtcToken, + TBTCDepositToken _tbtcDepositToken, + FeeRebateToken _feeRebateToken, + VendingMachine _vendingMachine, uint256 _keepThreshold, uint256 _keepSize ) external onlyOwner { require(!_initialized, "already initialized"); - tbtcDepositToken = TBTCDepositToken(_tbtcDepositToken); - keepVendor = IBondedECDSAKeepVendor(_keepVendor); - VendingMachine(_vendingMachine).setExternalAddresses( + tbtcDepositToken = _tbtcDepositToken; + keepVendor = _keepVendor; + _vendingMachine.setExternalAddresses( _tbtcToken, _tbtcDepositToken, _feeRebateToken ); - DepositFactory(_depositFactory).setExternalDependencies( + _depositFactory.setExternalDependencies( _masterDepositAddress, - address(this), + TBTCSystem(address(this)), _tbtcToken, _tbtcDepositToken, _feeRebateToken, - _vendingMachine, + address(_vendingMachine), _keepThreshold, _keepSize ); diff --git a/solidity/contracts/system/VendingMachine.sol b/solidity/contracts/system/VendingMachine.sol index 31528989d..f78b8c3fb 100644 --- a/solidity/contracts/system/VendingMachine.sol +++ b/solidity/contracts/system/VendingMachine.sol @@ -27,17 +27,17 @@ contract VendingMachine is TBTCSystemAuthority{ /// @notice Set external contracts needed by the Vending Machine. /// @dev Addresses are used to update the local contract instance. - /// @param _tbtcToken TBTCToken address. More info in `TBTCToken`. - /// @param _tbtcDepositToken TBTCDepositToken (TDT) address. More info in `TBTCDepositToken`. - /// @param _feeRebateToken FeeRebateToken (FRT) address. More info in `FeeRebateToken`. + /// @param _tbtcToken TBTCToken contract. More info in `TBTCToken`. + /// @param _tbtcDepositToken TBTCDepositToken (TDT) contract. More info in `TBTCDepositToken`. + /// @param _feeRebateToken FeeRebateToken (FRT) contract. More info in `FeeRebateToken`. function setExternalAddresses( - address _tbtcToken, - address _tbtcDepositToken, - address _feeRebateToken + TBTCToken _tbtcToken, + TBTCDepositToken _tbtcDepositToken, + FeeRebateToken _feeRebateToken ) public onlyTbtcSystem { - tbtcToken = TBTCToken(_tbtcToken); - tbtcDepositToken = TBTCDepositToken(_tbtcDepositToken); - feeRebateToken = FeeRebateToken(_feeRebateToken); + tbtcToken = _tbtcToken; + tbtcDepositToken = _tbtcDepositToken; + feeRebateToken = _feeRebateToken; } /// @notice Determines whether a deposit is qualified for minting TBTC. From fc03c4fc234f5107a04486d958d26171fbf2721a Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Thu, 26 Mar 2020 15:30:09 -0500 Subject: [PATCH 09/13] Avoid unnecessary double-case Use `this` instead of casting twice to get to the same result. --- solidity/contracts/system/TBTCSystem.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solidity/contracts/system/TBTCSystem.sol b/solidity/contracts/system/TBTCSystem.sol index f5cbd43ba..652947db7 100644 --- a/solidity/contracts/system/TBTCSystem.sol +++ b/solidity/contracts/system/TBTCSystem.sol @@ -111,7 +111,7 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog { ); _depositFactory.setExternalDependencies( _masterDepositAddress, - TBTCSystem(address(this)), + this, _tbtcToken, _tbtcDepositToken, _feeRebateToken, From 9f31086cd101ba6986ef3951669fd807f520de2a Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Thu, 26 Mar 2020 15:43:02 -0500 Subject: [PATCH 10/13] distributeERC20Reward takes IERC20 Take an explicit contract type over an address --- solidity/contracts/deposit/DepositRedemption.sol | 2 +- solidity/contracts/test/keep/ECDSAKeepStub.sol | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/solidity/contracts/deposit/DepositRedemption.sol b/solidity/contracts/deposit/DepositRedemption.sol index c6f0d1b0f..1df689aa7 100644 --- a/solidity/contracts/deposit/DepositRedemption.sol +++ b/solidity/contracts/deposit/DepositRedemption.sol @@ -34,7 +34,7 @@ library DepositRedemption { IBondedECDSAKeep _keep = IBondedECDSAKeep(_d.keepAddress); _d.tbtcToken.approve(_d.keepAddress, _d.signerFee()); - _keep.distributeERC20Reward(address(_d.tbtcToken), _d.signerFee()); + _keep.distributeERC20Reward(_d.tbtcToken, _d.signerFee()); } /// @notice Closes keep associated with the deposit. diff --git a/solidity/contracts/test/keep/ECDSAKeepStub.sol b/solidity/contracts/test/keep/ECDSAKeepStub.sol index 0f0e79317..2727ffa98 100644 --- a/solidity/contracts/test/keep/ECDSAKeepStub.sol +++ b/solidity/contracts/test/keep/ECDSAKeepStub.sol @@ -3,6 +3,7 @@ pragma solidity ^0.5.10; import { IBondedECDSAKeep } from "@keep-network/keep-ecdsa/contracts/api/IBondedECDSAKeep.sol"; +import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; /// @notice Implementation of ECDSAKeep interface used in tests only /// @dev This is a stub used in tests, so we don't have to call actual ECDSAKeep @@ -35,7 +36,7 @@ contract ECDSAKeepStub is IBondedECDSAKeep { // solium-disable-previous-line no-empty-blocks } - function distributeERC20Reward(address _asset, uint256 _value) external { + function distributeERC20Reward(IERC20 _asset, uint256 _value) external { // solium-disable-previous-line no-empty-blocks } From 08bba81f93fb446e0bbd10f6064394c74c869ff5 Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Thu, 26 Mar 2020 15:55:05 -0500 Subject: [PATCH 11/13] vendingMachine -> vendingMachineAddress We're not using the contract instance, clarify that we are only using the address --- solidity/contracts/deposit/Deposit.sol | 6 +++--- solidity/contracts/deposit/DepositLiquidation.sol | 2 +- solidity/contracts/deposit/DepositRedemption.sol | 4 ++-- solidity/contracts/deposit/DepositUtils.sol | 2 +- solidity/contracts/proxy/DepositFactory.sol | 10 +++++----- solidity/contracts/test/deposit/TestDeposit.sol | 8 ++++---- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/solidity/contracts/deposit/Deposit.sol b/solidity/contracts/deposit/Deposit.sol index cfb6a42e5..19e729bba 100644 --- a/solidity/contracts/deposit/Deposit.sol +++ b/solidity/contracts/deposit/Deposit.sol @@ -101,7 +101,7 @@ contract Deposit is DepositFactoryAuthority { /// @param _TBTCToken `TBTCToken` contract. More info in TBTCToken`. /// @param _TBTCDepositToken `TBTCDepositToken` (TDT) contract. More info in `TBTCDepositToken`. /// @param _FeeRebateToken `FeeRebateToken` (FRT) contract. More info in `FeeRebateToken`. - /// @param _VendingMachine `VendingMachine` address. More info in `VendingMachine`. + /// @param _VendingMachineAddress `VendingMachine` address. More info in `VendingMachine`. /// @param _m Signing group honesty threshold. /// @param _n Signing group size. /// @param _lotSizeSatoshis The minimum amount of satoshi the funder is required to send. @@ -113,7 +113,7 @@ contract Deposit is DepositFactoryAuthority { TBTCToken _TBTCToken, IERC721 _TBTCDepositToken, FeeRebateToken _FeeRebateToken, - address _VendingMachine, + address _VendingMachineAddress, uint256 _m, uint256 _n, uint256 _lotSizeSatoshis @@ -122,7 +122,7 @@ contract Deposit is DepositFactoryAuthority { self.tbtcToken = _TBTCToken; self.tbtcDepositToken = _TBTCDepositToken; self.feeRebateToken = _FeeRebateToken; - self.VendingMachine = _VendingMachine; + self.VendingMachineAddress = _VendingMachineAddress; self.createNewDeposit(_m, _n, _lotSizeSatoshis); return true; } diff --git a/solidity/contracts/deposit/DepositLiquidation.sol b/solidity/contracts/deposit/DepositLiquidation.sol index 80a7743a7..2a6ab37a3 100644 --- a/solidity/contracts/deposit/DepositLiquidation.sol +++ b/solidity/contracts/deposit/DepositLiquidation.sol @@ -170,7 +170,7 @@ library DepositLiquidation { uint256 lotSizeTbtc = _d.lotSizeTbtc(); require(_d.tbtcToken.balanceOf(msg.sender) >= lotSizeTbtc, "Not enough TBTC to cover outstanding debt"); - if(tdtHolder == _d.VendingMachine){ + if(tdtHolder == _d.VendingMachineAddress){ _d.tbtcToken.burnFrom(msg.sender, lotSizeTbtc); // burn minimal amount to cover size } else{ diff --git a/solidity/contracts/deposit/DepositRedemption.sol b/solidity/contracts/deposit/DepositRedemption.sol index 1df689aa7..eda1a03eb 100644 --- a/solidity/contracts/deposit/DepositRedemption.sol +++ b/solidity/contracts/deposit/DepositRedemption.sol @@ -59,7 +59,7 @@ library DepositRedemption { /// @dev Burns or transfers depending on term and supply-peg impact. function performRedemptionTBTCTransfers(DepositUtils.Deposit storage _d) internal { address tdtHolder = _d.depositOwner(); - address vendingMachine = _d.VendingMachine; + address vendingMachineAddress = _d.VendingMachineAddress; uint256 tbtcLot = _d.lotSizeTbtc(); uint256 signerFee = _d.signerFee(); @@ -81,7 +81,7 @@ library DepositRedemption { // As compensation, the TDT owner is reimbursed in TBTC // Vending Machine-owned TDTs have been used to mint TBTC, // and we should always burn a full TBTC to redeem the deposit. - if(tdtHolder == vendingMachine){ + if(tdtHolder == vendingMachineAddress){ _d.tbtcToken.burnFrom(msg.sender, tbtcLot); } // if signer fee is not escrowed, escrow and it here and send the rest to TDT owner diff --git a/solidity/contracts/deposit/DepositUtils.sol b/solidity/contracts/deposit/DepositUtils.sol index 9851fca78..bd57ee4dd 100644 --- a/solidity/contracts/deposit/DepositUtils.sol +++ b/solidity/contracts/deposit/DepositUtils.sol @@ -29,7 +29,7 @@ library DepositUtils { TBTCToken tbtcToken; IERC721 tbtcDepositToken; FeeRebateToken feeRebateToken; - address VendingMachine; + address VendingMachineAddress; uint256 lotSizeSatoshis; uint8 currentState; uint256 signerFeeDivisor; diff --git a/solidity/contracts/proxy/DepositFactory.sol b/solidity/contracts/proxy/DepositFactory.sol index 70de4c05e..7306c0bdc 100644 --- a/solidity/contracts/proxy/DepositFactory.sol +++ b/solidity/contracts/proxy/DepositFactory.sol @@ -24,7 +24,7 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ TBTCSystem public tbtcSystem; TBTCToken public tbtcToken; FeeRebateToken public feeRebateToken; - address public vendingMachine; + address public vendingMachineAddress; uint256 public keepThreshold; uint256 public keepSize; @@ -38,7 +38,7 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ /// @param _tbtcToken TBTC token contract. /// @param _tbtcDepositToken TBTC Deposit Token contract. /// @param _feeRebateToken AFee Rebate Token contract. - /// @param _vendingMachine Address of the Vending Machine contract. + /// @param _vendingMachineAddress Address of the Vending Machine contract. /// @param _keepThreshold Minimum number of honest keep members. /// @param _keepSize Number of all members in a keep. function setExternalDependencies( @@ -47,7 +47,7 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ TBTCToken _tbtcToken, TBTCDepositToken _tbtcDepositToken, FeeRebateToken _feeRebateToken, - address _vendingMachine, + address _vendingMachineAddress, uint256 _keepThreshold, uint256 _keepSize ) public onlyTbtcSystem { @@ -56,7 +56,7 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ tbtcSystem = _tbtcSystem; tbtcToken = _tbtcToken; feeRebateToken = _feeRebateToken; - vendingMachine = _vendingMachine; + vendingMachineAddress = _vendingMachineAddress; keepThreshold = _keepThreshold; keepSize = _keepSize; } @@ -81,7 +81,7 @@ contract DepositFactory is CloneFactory, TBTCSystemAuthority{ tbtcToken, tbtcDepositToken, feeRebateToken, - vendingMachine, + vendingMachineAddress, keepThreshold, keepSize, _lotSizeSatoshis diff --git a/solidity/contracts/test/deposit/TestDeposit.sol b/solidity/contracts/test/deposit/TestDeposit.sol index cf6e0f330..7306d2e57 100644 --- a/solidity/contracts/test/deposit/TestDeposit.sol +++ b/solidity/contracts/test/deposit/TestDeposit.sol @@ -17,7 +17,7 @@ contract TestDeposit is Deposit { TBTCToken _TBTCToken, IERC721 _TBTCDepositToken, FeeRebateToken _FeeRebateToken, - address _VendingMachine, + address _vendingMachineAddress, uint256 _m, uint256 _n, uint256 _lotSizeSatoshis @@ -26,7 +26,7 @@ contract TestDeposit is Deposit { self.tbtcToken = (_TBTCToken); self.tbtcDepositToken = (_TBTCDepositToken); self.feeRebateToken = (_FeeRebateToken); - self.VendingMachine = _VendingMachine; + self.VendingMachineAddress = _vendingMachineAddress; self.createNewDeposit(_m, _n, _lotSizeSatoshis); return true; } @@ -36,13 +36,13 @@ contract TestDeposit is Deposit { address _token, address _tbtcDepositToken, address _feeRebateToken, - address _vendingMachine + address _vendingMachineAddress ) public { self.tbtcSystem = ITBTCSystem(_sys); self.tbtcToken = TBTCToken(_token); self.tbtcDepositToken = IERC721(_tbtcDepositToken); self.feeRebateToken = FeeRebateToken(_feeRebateToken); - self.VendingMachine = _vendingMachine; + self.VendingMachineAddress = _vendingMachineAddress; } function reset() public { From 38e0b59968c156a459f53097203921b33371540f Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Thu, 26 Mar 2020 16:25:56 -0500 Subject: [PATCH 12/13] Update capitalization for consistency --- solidity/contracts/deposit/Deposit.sol | 30 +++++++++---------- .../contracts/deposit/DepositLiquidation.sol | 2 +- .../contracts/deposit/DepositRedemption.sol | 2 +- solidity/contracts/deposit/DepositUtils.sol | 2 +- .../contracts/test/deposit/TestDeposit.sol | 20 ++++++------- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/solidity/contracts/deposit/Deposit.sol b/solidity/contracts/deposit/Deposit.sol index 19e729bba..3b5b6f451 100644 --- a/solidity/contracts/deposit/Deposit.sol +++ b/solidity/contracts/deposit/Deposit.sol @@ -97,11 +97,11 @@ contract Deposit is DepositFactoryAuthority { // THIS IS THE INIT FUNCTION /// @notice The Deposit Factory can spin up a new deposit. /// @dev Only the Deposit factory can call this. - /// @param _TBTCSystem `TBTCSystem` contract. More info in `VendingMachine`. - /// @param _TBTCToken `TBTCToken` contract. More info in TBTCToken`. - /// @param _TBTCDepositToken `TBTCDepositToken` (TDT) contract. More info in `TBTCDepositToken`. - /// @param _FeeRebateToken `FeeRebateToken` (FRT) contract. More info in `FeeRebateToken`. - /// @param _VendingMachineAddress `VendingMachine` address. More info in `VendingMachine`. + /// @param _tbtcSystem `TBTCSystem` contract. More info in `VendingMachine`. + /// @param _tbtcToken `TBTCToken` contract. More info in TBTCToken`. + /// @param _tbtcDepositToken `TBTCDepositToken` (TDT) contract. More info in `TBTCDepositToken`. + /// @param _feeRebateToken `FeeRebateToken` (FRT) contract. More info in `FeeRebateToken`. + /// @param _vendingMachineAddress `VendingMachine` address. More info in `VendingMachine`. /// @param _m Signing group honesty threshold. /// @param _n Signing group size. /// @param _lotSizeSatoshis The minimum amount of satoshi the funder is required to send. @@ -109,20 +109,20 @@ contract Deposit is DepositFactoryAuthority { /// (10**7 satoshi == 0.1 BTC == 0.1 TBTC). /// @return True if successful, otherwise revert. function createNewDeposit( - ITBTCSystem _TBTCSystem, - TBTCToken _TBTCToken, - IERC721 _TBTCDepositToken, - FeeRebateToken _FeeRebateToken, - address _VendingMachineAddress, + ITBTCSystem _tbtcSystem, + TBTCToken _tbtcToken, + IERC721 _tbtcDepositToken, + FeeRebateToken _feeRebateToken, + address _vendingMachineAddress, uint256 _m, uint256 _n, uint256 _lotSizeSatoshis ) public onlyFactory payable returns (bool) { - self.tbtcSystem = _TBTCSystem; - self.tbtcToken = _TBTCToken; - self.tbtcDepositToken = _TBTCDepositToken; - self.feeRebateToken = _FeeRebateToken; - self.VendingMachineAddress = _VendingMachineAddress; + self.tbtcSystem = _tbtcSystem; + self.tbtcToken = _tbtcToken; + self.tbtcDepositToken = _tbtcDepositToken; + self.feeRebateToken = _feeRebateToken; + self.vendingMachineAddress = _vendingMachineAddress; self.createNewDeposit(_m, _n, _lotSizeSatoshis); return true; } diff --git a/solidity/contracts/deposit/DepositLiquidation.sol b/solidity/contracts/deposit/DepositLiquidation.sol index 2a6ab37a3..e8331d6c7 100644 --- a/solidity/contracts/deposit/DepositLiquidation.sol +++ b/solidity/contracts/deposit/DepositLiquidation.sol @@ -170,7 +170,7 @@ library DepositLiquidation { uint256 lotSizeTbtc = _d.lotSizeTbtc(); require(_d.tbtcToken.balanceOf(msg.sender) >= lotSizeTbtc, "Not enough TBTC to cover outstanding debt"); - if(tdtHolder == _d.VendingMachineAddress){ + if(tdtHolder == _d.vendingMachineAddress){ _d.tbtcToken.burnFrom(msg.sender, lotSizeTbtc); // burn minimal amount to cover size } else{ diff --git a/solidity/contracts/deposit/DepositRedemption.sol b/solidity/contracts/deposit/DepositRedemption.sol index eda1a03eb..c090f0ad7 100644 --- a/solidity/contracts/deposit/DepositRedemption.sol +++ b/solidity/contracts/deposit/DepositRedemption.sol @@ -59,7 +59,7 @@ library DepositRedemption { /// @dev Burns or transfers depending on term and supply-peg impact. function performRedemptionTBTCTransfers(DepositUtils.Deposit storage _d) internal { address tdtHolder = _d.depositOwner(); - address vendingMachineAddress = _d.VendingMachineAddress; + address vendingMachineAddress = _d.vendingMachineAddress; uint256 tbtcLot = _d.lotSizeTbtc(); uint256 signerFee = _d.signerFee(); diff --git a/solidity/contracts/deposit/DepositUtils.sol b/solidity/contracts/deposit/DepositUtils.sol index bd57ee4dd..e4cc46307 100644 --- a/solidity/contracts/deposit/DepositUtils.sol +++ b/solidity/contracts/deposit/DepositUtils.sol @@ -29,7 +29,7 @@ library DepositUtils { TBTCToken tbtcToken; IERC721 tbtcDepositToken; FeeRebateToken feeRebateToken; - address VendingMachineAddress; + address vendingMachineAddress; uint256 lotSizeSatoshis; uint8 currentState; uint256 signerFeeDivisor; diff --git a/solidity/contracts/test/deposit/TestDeposit.sol b/solidity/contracts/test/deposit/TestDeposit.sol index 7306d2e57..9f334e1d7 100644 --- a/solidity/contracts/test/deposit/TestDeposit.sol +++ b/solidity/contracts/test/deposit/TestDeposit.sol @@ -13,20 +13,20 @@ contract TestDeposit is Deposit { } function createNewDeposit( - ITBTCSystem _TBTCSystem, - TBTCToken _TBTCToken, - IERC721 _TBTCDepositToken, - FeeRebateToken _FeeRebateToken, + ITBTCSystem _tbtcSystem, + TBTCToken _tbtcToken, + IERC721 _tbtcDepositToken, + FeeRebateToken _feeRebateToken, address _vendingMachineAddress, uint256 _m, uint256 _n, uint256 _lotSizeSatoshis ) public payable returns (bool) { - self.tbtcSystem = (_TBTCSystem); - self.tbtcToken = (_TBTCToken); - self.tbtcDepositToken = (_TBTCDepositToken); - self.feeRebateToken = (_FeeRebateToken); - self.VendingMachineAddress = _vendingMachineAddress; + self.tbtcSystem = _tbtcSystem; + self.tbtcToken = _tbtcToken; + self.tbtcDepositToken = _tbtcDepositToken; + self.feeRebateToken = _feeRebateToken; + self.vendingMachineAddress = _vendingMachineAddress; self.createNewDeposit(_m, _n, _lotSizeSatoshis); return true; } @@ -42,7 +42,7 @@ contract TestDeposit is Deposit { self.tbtcToken = TBTCToken(_token); self.tbtcDepositToken = IERC721(_tbtcDepositToken); self.feeRebateToken = FeeRebateToken(_feeRebateToken); - self.VendingMachineAddress = _vendingMachineAddress; + self.vendingMachineAddress = _vendingMachineAddress; } function reset() public { From 6756361bd1b4af20dc23772c35e6aae956793190 Mon Sep 17 00:00:00 2001 From: NicholasDotSol Date: Thu, 26 Mar 2020 16:38:02 -0500 Subject: [PATCH 13/13] Under distributeERC20Reward Interface change Changing distributeERC20Reward to accept IERC20 param Requires some dependency syncing Revert the change for now --- solidity/contracts/deposit/DepositRedemption.sol | 2 +- solidity/contracts/test/keep/ECDSAKeepStub.sol | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/solidity/contracts/deposit/DepositRedemption.sol b/solidity/contracts/deposit/DepositRedemption.sol index c090f0ad7..dec2b101a 100644 --- a/solidity/contracts/deposit/DepositRedemption.sol +++ b/solidity/contracts/deposit/DepositRedemption.sol @@ -34,7 +34,7 @@ library DepositRedemption { IBondedECDSAKeep _keep = IBondedECDSAKeep(_d.keepAddress); _d.tbtcToken.approve(_d.keepAddress, _d.signerFee()); - _keep.distributeERC20Reward(_d.tbtcToken, _d.signerFee()); + _keep.distributeERC20Reward(address(_d.tbtcToken), _d.signerFee()); } /// @notice Closes keep associated with the deposit. diff --git a/solidity/contracts/test/keep/ECDSAKeepStub.sol b/solidity/contracts/test/keep/ECDSAKeepStub.sol index 2727ffa98..0f0e79317 100644 --- a/solidity/contracts/test/keep/ECDSAKeepStub.sol +++ b/solidity/contracts/test/keep/ECDSAKeepStub.sol @@ -3,7 +3,6 @@ pragma solidity ^0.5.10; import { IBondedECDSAKeep } from "@keep-network/keep-ecdsa/contracts/api/IBondedECDSAKeep.sol"; -import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; /// @notice Implementation of ECDSAKeep interface used in tests only /// @dev This is a stub used in tests, so we don't have to call actual ECDSAKeep @@ -36,7 +35,7 @@ contract ECDSAKeepStub is IBondedECDSAKeep { // solium-disable-previous-line no-empty-blocks } - function distributeERC20Reward(IERC20 _asset, uint256 _value) external { + function distributeERC20Reward(address _asset, uint256 _value) external { // solium-disable-previous-line no-empty-blocks }