- (LoanSettings.sol)
View Source: contracts/modules/LoanSettings.sol
↗ Extends: State, LoanSettingsEvents, ModuleCommonFunctionalities
This contract code comes from bZx. bZx is a protocol for tokenized margin trading and lending https://bzx.network similar to the dYdX protocol.
- This contract contains functions to get and set loan parameters.
- constructor()
- constructor()
- initialize(address target)
- setupLoanParams(struct LoanParamsStruct.LoanParams[] loanParamsList)
- disableLoanParams(bytes32[] loanParamsIdList)
- getLoanParams(bytes32[] loanParamsIdList)
- getLoanParamsList(address owner, uint256 start, uint256 count)
- getTotalPrincipal(address lender, address loanToken)
- _setupLoanParams(struct LoanParamsStruct.LoanParams loanParamsLocal)
- minInitialMargin(bytes32 loanParamsId)
Empty public constructor.
function () public nonpayable
Source Code
constructor() public {}
Fallback function is to react to receiving value (rBTC).
function () external nonpayable
Source Code
function() external {
revert("LoanSettings - fallback not allowed");
}
Set function selectors on target contract. *
function initialize(address target) external nonpayable onlyOwner
Arguments
Name | Type | Description |
---|---|---|
target | address | The address of the target contract. |
Source Code
function initialize(address target) external onlyOwner {
address prevModuleContractAddress = logicTargets[this.setupLoanParams.selector];
_setTarget(this.setupLoanParams.selector, target);
_setTarget(this.disableLoanParams.selector, target);
_setTarget(this.getLoanParams.selector, target);
_setTarget(this.getLoanParamsList.selector, target);
_setTarget(this.getTotalPrincipal.selector, target);
_setTarget(this.minInitialMargin.selector, target);
emit ProtocolModuleContractReplaced(prevModuleContractAddress, target, "LoanSettings");
}
Setup loan parameters, by looping every loan and populating its parameters. *
function setupLoanParams(struct LoanParamsStruct.LoanParams[] loanParamsList) external nonpayable whenNotPaused
returns(loanParamsIdList bytes32[])
Arguments
Name | Type | Description |
---|---|---|
loanParamsList | struct LoanParamsStruct.LoanParams[] | The array of loan parameters. * |
Returns
loanParamsIdList The array of loan parameters IDs.
Source Code
function setupLoanParams(LoanParams[] calldata loanParamsList)
external
whenNotPaused
returns (bytes32[] memory loanParamsIdList)
{
loanParamsIdList = new bytes32[](loanParamsList.length);
for (uint256 i = 0; i < loanParamsList.length; i++) {
loanParamsIdList[i] = _setupLoanParams(loanParamsList[i]);
}
}
Deactivate LoanParams for future loans. Active loans using it are unaffected. *
function disableLoanParams(bytes32[] loanParamsIdList) external nonpayable whenNotPaused
Arguments
Name | Type | Description |
---|---|---|
loanParamsIdList | bytes32[] | The array of loan parameters IDs to deactivate. |
Source Code
function disableLoanParams(bytes32[] calldata loanParamsIdList) external whenNotPaused {
for (uint256 i = 0; i < loanParamsIdList.length; i++) {
require(msg.sender == loanParams[loanParamsIdList[i]].owner, "unauthorized owner");
loanParams[loanParamsIdList[i]].active = false;
LoanParams memory loanParamsLocal = loanParams[loanParamsIdList[i]];
emit LoanParamsDisabled(
loanParamsLocal.id,
loanParamsLocal.owner,
loanParamsLocal.loanToken,
loanParamsLocal.collateralToken,
loanParamsLocal.minInitialMargin,
loanParamsLocal.maintenanceMargin,
loanParamsLocal.maxLoanTerm
);
emit LoanParamsIdDisabled(loanParamsLocal.id, loanParamsLocal.owner);
}
}
Get loan parameters for every matching IDs. *
function getLoanParams(bytes32[] loanParamsIdList) public view
returns(loanParamsList struct LoanParamsStruct.LoanParams[])
Arguments
Name | Type | Description |
---|---|---|
loanParamsIdList | bytes32[] | The array of loan parameters IDs to match. * |
Returns
loanParamsList The result array of loan parameters.
Source Code
function getLoanParams(bytes32[] memory loanParamsIdList)
public
view
returns (LoanParams[] memory loanParamsList)
{
loanParamsList = new LoanParams[](loanParamsIdList.length);
uint256 itemCount;
for (uint256 i = 0; i < loanParamsIdList.length; i++) {
LoanParams memory loanParamsLocal = loanParams[loanParamsIdList[i]];
if (loanParamsLocal.id == 0) {
continue;
}
loanParamsList[itemCount] = loanParamsLocal;
itemCount++;
}
if (itemCount < loanParamsList.length) {
assembly {
mstore(loanParamsList, itemCount)
}
}
}
Get loan parameters for an owner and a given page defined by an offset and a limit. *
function getLoanParamsList(address owner, uint256 start, uint256 count) external view
returns(loanParamsList bytes32[])
Arguments
Name | Type | Description |
---|---|---|
owner | address | The address of the loan owner. |
start | uint256 | The page offset. |
count | uint256 | The page limit. * |
Returns
loanParamsList The result array of loan parameters.
Source Code
function getLoanParamsList(
address owner,
uint256 start,
uint256 count
) external view returns (bytes32[] memory loanParamsList) {
EnumerableBytes32Set.Bytes32Set storage set = userLoanParamSets[owner];
uint256 end = start.add(count).min256(set.length());
if (start >= end) {
return loanParamsList;
}
loanParamsList = new bytes32[](count);
uint256 itemCount;
for (uint256 i = end - start; i > 0; i--) {
if (itemCount == count) {
break;
}
loanParamsList[itemCount] = set.get(i + start - 1);
itemCount++;
}
if (itemCount < count) {
assembly {
mstore(loanParamsList, itemCount)
}
}
}
Get the total principal of the loans by a lender. *
function getTotalPrincipal(address lender, address loanToken) external view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
lender | address | The address of the lender. |
loanToken | address | The address of the token instance. * |
Returns
The total principal of the loans.
Source Code
function getTotalPrincipal(address lender, address loanToken) external view returns (uint256) {
return lenderInterest[lender][loanToken].principalTotal;
}
Setup a loan parameters. *
function _setupLoanParams(struct LoanParamsStruct.LoanParams loanParamsLocal) internal nonpayable
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
loanParamsLocal | struct LoanParamsStruct.LoanParams | The loan parameters. * |
Returns
loanParamsId The loan parameters ID.
Source Code
function _setupLoanParams(LoanParams memory loanParamsLocal) internal returns (bytes32) {
bytes32 loanParamsId =
keccak256(
abi.encodePacked(
loanParamsLocal.loanToken,
loanParamsLocal.collateralToken,
loanParamsLocal.minInitialMargin,
loanParamsLocal.maintenanceMargin,
loanParamsLocal.maxLoanTerm,
block.timestamp
)
);
require(loanParams[loanParamsId].id == 0, "loanParams exists");
require(
loanParamsLocal.loanToken != address(0) &&
loanParamsLocal.collateralToken != address(0) &&
loanParamsLocal.minInitialMargin > loanParamsLocal.maintenanceMargin &&
(loanParamsLocal.maxLoanTerm == 0 || loanParamsLocal.maxLoanTerm > 3600), /// A defined maxLoanTerm has to be greater than one hour.
"invalid params"
);
loanParamsLocal.id = loanParamsId;
loanParamsLocal.active = true;
loanParamsLocal.owner = msg.sender;
loanParams[loanParamsId] = loanParamsLocal;
userLoanParamSets[msg.sender].addBytes32(loanParamsId);
emit LoanParamsSetup(
loanParamsId,
loanParamsLocal.owner,
loanParamsLocal.loanToken,
loanParamsLocal.collateralToken,
loanParamsLocal.minInitialMargin,
loanParamsLocal.maintenanceMargin,
loanParamsLocal.maxLoanTerm
);
emit LoanParamsIdSetup(loanParamsId, loanParamsLocal.owner);
return loanParamsId;
}
function minInitialMargin(bytes32 loanParamsId) external view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
loanParamsId | bytes32 |
Source Code
function minInitialMargin(bytes32 loanParamsId) external view returns (uint256) {
return loanParams[loanParamsId].minInitialMargin;
}
- Address
- Administered
- AdminRole
- AdvancedToken
- AdvancedTokenStorage
- Affiliates
- AffiliatesEvents
- ApprovalReceiver
- BProPriceFeed
- CheckpointsShared
- Constants
- Context
- DevelopmentFund
- DummyContract
- EnumerableAddressSet
- EnumerableBytes32Set
- EnumerableBytes4Set
- ERC20
- ERC20Detailed
- ErrorDecoder
- Escrow
- EscrowReward
- FeedsLike
- FeesEvents
- FeeSharingCollector
- FeeSharingCollectorProxy
- FeeSharingCollectorStorage
- FeesHelper
- FourYearVesting
- FourYearVestingFactory
- FourYearVestingLogic
- FourYearVestingStorage
- GenericTokenSender
- GovernorAlpha
- GovernorVault
- IApproveAndCall
- IChai
- IContractRegistry
- IConverterAMM
- IERC1820Registry
- IERC20_
- IERC20
- IERC777
- IERC777Recipient
- IERC777Sender
- IFeeSharingCollector
- IFourYearVesting
- IFourYearVestingFactory
- IFunctionsList
- ILiquidityMining
- ILiquidityPoolV1Converter
- ILoanPool
- ILoanToken
- ILoanTokenLogicBeacon
- ILoanTokenLogicModules
- ILoanTokenLogicProxy
- ILoanTokenModules
- ILoanTokenWRBTC
- ILockedSOV
- IMoCState
- IModulesProxyRegistry
- Initializable
- InterestUser
- IPot
- IPriceFeeds
- IPriceFeedsExt
- IProtocol
- IRSKOracle
- ISovryn
- ISovrynSwapNetwork
- IStaking
- ISwapsImpl
- ITeamVesting
- ITimelock
- IV1PoolOracle
- IVesting
- IVestingFactory
- IVestingRegistry
- IWrbtc
- IWrbtcERC20
- LenderInterestStruct
- LiquidationHelper
- LiquidityMining
- LiquidityMiningConfigToken
- LiquidityMiningProxy
- LiquidityMiningStorage
- LoanClosingsEvents
- LoanClosingsLiquidation
- LoanClosingsRollover
- LoanClosingsShared
- LoanClosingsWith
- LoanClosingsWithoutInvariantCheck
- LoanInterestStruct
- LoanMaintenance
- LoanMaintenanceEvents
- LoanOpenings
- LoanOpeningsEvents
- LoanParamsStruct
- LoanSettings
- LoanSettingsEvents
- LoanStruct
- LoanToken
- LoanTokenBase
- LoanTokenLogicBeacon
- LoanTokenLogicLM
- LoanTokenLogicProxy
- LoanTokenLogicStandard
- LoanTokenLogicStorage
- LoanTokenLogicWrbtc
- LoanTokenSettingsLowerAdmin
- LockedSOV
- MarginTradeStructHelpers
- Medianizer
- ModuleCommonFunctionalities
- ModulesCommonEvents
- ModulesProxy
- ModulesProxyRegistry
- MultiSigKeyHolders
- MultiSigWallet
- Mutex
- Objects
- OrderStruct
- OrigingVestingCreator
- OriginInvestorsClaim
- Ownable
- Pausable
- PausableOz
- PreviousLoanToken
- PreviousLoanTokenSettingsLowerAdmin
- PriceFeedRSKOracle
- PriceFeeds
- PriceFeedsLocal
- PriceFeedsMoC
- PriceFeedV1PoolOracle
- ProtocolAffiliatesInterface
- ProtocolLike
- ProtocolSettings
- ProtocolSettingsEvents
- ProtocolSettingsLike
- ProtocolSwapExternalInterface
- ProtocolTokenUser
- Proxy
- ProxyOwnable
- ReentrancyGuard
- RewardHelper
- RSKAddrValidator
- SafeERC20
- SafeMath
- SafeMath96
- setGet
- SharedReentrancyGuard
- SignedSafeMath
- SOV
- sovrynProtocol
- StakingAdminModule
- StakingGovernanceModule
- StakingInterface
- StakingProxy
- StakingRewards
- StakingRewardsProxy
- StakingRewardsStorage
- StakingShared
- StakingStakeModule
- StakingStorageModule
- StakingStorageShared
- StakingVestingModule
- StakingWithdrawModule
- State
- SwapsEvents
- SwapsExternal
- SwapsImplLocal
- SwapsImplSovrynSwap
- SwapsUser
- TeamVesting
- Timelock
- TimelockHarness
- TimelockInterface
- TokenSender
- UpgradableProxy
- USDTPriceFeed
- Utils
- VaultController
- Vesting
- VestingCreator
- VestingFactory
- VestingLogic
- VestingRegistry
- VestingRegistry2
- VestingRegistry3
- VestingRegistryLogic
- VestingRegistryProxy
- VestingRegistryStorage
- VestingStorage
- WeightedStakingModule
- WRBTC