SAFEEngine

Git Source

Inherits: Authorizable, Disableable, Modifiable, ModifiablePerCollateral, ISAFEEngine

Core contract that manages the state of the SAFE system

State Variables

_params

Getter for the unpacked contract parameters struct

SAFEEngineParams public _params;

_cParams

Getter for the unpacked collateral parameters struct

mapping(bytes32 _cType => SAFEEngineCollateralParams) public _cParams;

_cData

Getter for the unpacked collateral data struct

mapping(bytes32 _cType => SAFEEngineCollateralData) public _cData;

_safes

Unpacked data about each SAFE

mapping(bytes32 _cType => mapping(address _safe => SAFE)) public _safes;

safeRights

Who can transfer collateral & debt in/out of a SAFE

mapping(address _caller => mapping(address _account => bool _isAllowed)) public safeRights;

tokenCollateral

Balance of each collateral type

mapping(bytes32 _cType => mapping(address _safe => uint256 _wad)) public tokenCollateral;

coinBalance

Internal balance of system coins held by an account

mapping(address _safe => uint256 _rad) public coinBalance;

debtBalance

Amount of debt held by an account

mapping(address _safe => uint256 _rad) public debtBalance;

globalDebt

Total amount of debt (coins) currently issued

uint256 public globalDebt;

globalUnbackedDebt

'Bad' debt that's not covered by collateral

uint256 public globalUnbackedDebt;

Functions

params

Getter for the contract parameters struct

function params() external view returns (SAFEEngineParams memory _safeEngineParams);

Returns

NameTypeDescription
_safeEngineParamsSAFEEngineParamsThe active SAFEEngineParams

cParams

Getter for the collateral parameters struct

function cParams(bytes32 _cType) external view returns (SAFEEngineCollateralParams memory _safeEngineCParams);

Parameters

NameTypeDescription
_cTypebytes32Bytes32 representation of the collateral type

Returns

NameTypeDescription
_safeEngineCParamsSAFEEngineCollateralParamsSAFEEngineCollateralParams for the collateral type

cData

Getter for the collateral data struct

function cData(bytes32 _cType) external view returns (SAFEEngineCollateralData memory _safeEngineCData);

Parameters

NameTypeDescription
_cTypebytes32Bytes32 representation of the collateral type

Returns

NameTypeDescription
_safeEngineCDataSAFEEngineCollateralDataSAFEEngineCollateralData for the collateral type

safes

Data about each SAFE

function safes(bytes32 _cType, address _safe) external view returns (SAFE memory _safeData);

Parameters

NameTypeDescription
_cTypebytes32Bytes32 representation of the collateral type
_safeaddress

Returns

NameTypeDescription
_safeDataSAFESAFE data for the specified collateral type of the specified safe.

constructor

constructor(SAFEEngineParams memory _safeEngineParams) Authorizable(msg.sender) validParams;

Parameters

NameTypeDescription
_safeEngineParamsSAFEEngineParamsInitial SAFEEngine valid parameters struct

transferCollateral

Transfer collateral between accounts

function transferCollateral(
  bytes32 _cType,
  address _source,
  address _destination,
  uint256 _wad
) external isSAFEAllowed(_source, msg.sender);

Parameters

NameTypeDescription
_cTypebytes32Collateral type transferred
_sourceaddressCollateral source
_destinationaddressCollateral destination
_waduint256Amount of collateral transferred

transferInternalCoins

Transfer internal coins (does not affect external balances from Coin.sol)

function transferInternalCoins(
  address _source,
  address _destination,
  uint256 _rad
) external isSAFEAllowed(_source, msg.sender);

Parameters

NameTypeDescription
_sourceaddressCoins source
_destinationaddressCoins destination
_raduint256Amount of coins transferred

modifyCollateralBalance

Join/exit collateral into and and out of the system

function modifyCollateralBalance(bytes32 _cType, address _account, int256 _wad) external isAuthorized;

Parameters

NameTypeDescription
_cTypebytes32Collateral type to join/exit
_accountaddressAccount that gets credited/debited
_wadint256Amount of collateral

modifySAFECollateralization

Add/remove collateral or put back/generate more debt in a SAFE

function modifySAFECollateralization(
  bytes32 _cType,
  address _safe,
  address _collateralSource,
  address _debtDestination,
  int256 _deltaCollateral,
  int256 _deltaDebt
) external whenEnabled;

Parameters

NameTypeDescription
_cTypebytes32Type of collateral to withdraw/deposit in and from the SAFE
_safeaddressTarget SAFE
_collateralSourceaddressAccount we take collateral from/put collateral into
_debtDestinationaddressAccount from which we credit/debit coins and debt
_deltaCollateralint256Amount of collateral added/extracted from the SAFE [wad]
_deltaDebtint256Amount of debt to generate/repay [wad]

transferSAFECollateralAndDebt

Transfer collateral and/or debt between SAFEs

function transferSAFECollateralAndDebt(
  bytes32 _cType,
  address _src,
  address _dst,
  int256 _deltaCollateral,
  int256 _deltaDebt
) external isSAFEAllowed(_src, msg.sender) isSAFEAllowed(_dst, msg.sender);

Parameters

NameTypeDescription
_cTypebytes32Collateral type transferred between SAFEs
_srcaddressSource SAFE
_dstaddressDestination SAFE
_deltaCollateralint256Amount of collateral to take/add into src and give/take from dst [wad]
_deltaDebtint256Amount of debt to take/add into src and give/take from dst [wad]

confiscateSAFECollateralAndDebt

Normally used by the LiquidationEngine in order to confiscate collateral and debt from a SAFE and give them to someone else

function confiscateSAFECollateralAndDebt(
  bytes32 _cType,
  address _safe,
  address _collateralSource,
  address _debtDestination,
  int256 _deltaCollateral,
  int256 _deltaDebt
) external isAuthorized;

Parameters

NameTypeDescription
_cTypebytes32Collateral type the SAFE has locked inside
_safeaddressTarget SAFE
_collateralSourceaddressWho we take/give collateral to
_debtDestinationaddressWho we take/give debt to
_deltaCollateralint256Amount of collateral taken/added into the SAFE [wad]
_deltaDebtint256Amount of debt taken/added into the SAFE [wad]

settleDebt

Nullify an amount of coins with an equal amount of debt

Coins & debt are like matter and antimatter, they nullify each other

function settleDebt(uint256 _rad) external;

Parameters

NameTypeDescription
_raduint256Amount of debt & coins to destroy

createUnbackedDebt

Allows an authorized contract to create debt without collateral

Usually called by DebtAuctionHouse in order to terminate auctions prematurely post settlement

function createUnbackedDebt(address _debtDestination, address _coinDestination, uint256 _rad) external isAuthorized;

Parameters

NameTypeDescription
_debtDestinationaddressThe account that will receive the newly created debt
_coinDestinationaddressThe account that will receive the newly created coins
_raduint256Amount of debt to create

updateAccumulatedRate

Allows an authorized contract to accrue interest on a specific collateral type

The rateMultiplier is usually calculated by the TaxCollector contract

function updateAccumulatedRate(
  bytes32 _cType,
  address _surplusDst,
  int256 _rateMultiplier
) external isAuthorized whenEnabled;

Parameters

NameTypeDescription
_cTypebytes32Collateral type we accrue interest for
_surplusDstaddressDestination for the newly created surplus
_rateMultiplierint256Multiplier applied to the debtAmount in order to calculate the surplus [ray]

updateCollateralPrice

Allows an authorized contract to update the safety price and liquidation price of a collateral type

function updateCollateralPrice(
  bytes32 _cType,
  uint256 _safetyPrice,
  uint256 _liquidationPrice
) external isAuthorized whenEnabled;

Parameters

NameTypeDescription
_cTypebytes32Collateral type we update the prices for
_safetyPriceuint256New safety price [ray]
_liquidationPriceuint256New liquidation price [ray]

addAuthorization

Add auth to an account

This overriden method avoids adding new authorizations after the contract has been disabled

function addAuthorization(address _account) external override(Authorizable, IAuthorizable) isAuthorized whenEnabled;

Parameters

NameTypeDescription
_accountaddressAccount to add auth to

removeAuthorization

Remove auth from an account

function removeAuthorization(address _account) external override(Authorizable, IAuthorizable) isAuthorized whenEnabled;

Parameters

NameTypeDescription
_accountaddressAccount to remove auth from

approveSAFEModification

Allow an address to modify your SAFE

function approveSAFEModification(address _account) external;

Parameters

NameTypeDescription
_accountaddressAccount to give SAFE permissions to

denySAFEModification

Deny an address the rights to modify your SAFE

function denySAFEModification(address _account) external;

Parameters

NameTypeDescription
_accountaddressAccount that is denied SAFE permissions

canModifySAFE

Checks whether an account has the right to modify a SAFE

function canModifySAFE(address _safe, address _account) public view returns (bool _canModifySafe);

Parameters

NameTypeDescription
_safeaddressThe safe to check
_accountaddressThe account to check

Returns

NameTypeDescription
_canModifySafebool_allowed Whether the account can modify the safe

_modifyCollateralBalance

function _modifyCollateralBalance(bytes32 _cType, address _account, int256 _wad) internal;

_modifyInternalCoins

function _modifyInternalCoins(address _dst, int256 _rad) internal;

_modifySAFECollateralization

function _modifySAFECollateralization(
  bytes32 _cType,
  address _safe,
  int256 _deltaCollateral,
  int256 _deltaDebt
) internal;

_emitTransferCollateral

function _emitTransferCollateral(bytes32 _cType, address _src, address _dst, int256 _deltaCollateral) internal;

_initializeCollateralType

Register a new collateral type in the SAFEEngine

function _initializeCollateralType(bytes32 _cType, bytes memory _collateralParams) internal override whenEnabled;

Parameters

NameTypeDescription
_cTypebytes32Collateral type to register
_collateralParamsbytesCollateral parameters

_modifyParameters

Internal function to be overriden with custom logic to modify parameters

This function is set to revert if not overriden

function _modifyParameters(bytes32 _param, bytes memory _data) internal override;

_modifyParameters

Set a new value for a collateral specific parameter

function _modifyParameters(bytes32 _cType, bytes32 _param, bytes memory _data) internal override;

Parameters

NameTypeDescription
_cTypebytes32String identifier of the collateral to modify
_parambytes32String identifier of the parameter to modify
_databytesEncoded data to modify the parameter

isSAFEAllowed

modifier isSAFEAllowed(address _safe, address _account);