Contract Name:
MultiCollateralSynthLightChain
Contract Source Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, "SafeMath: division by zero");
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "SafeMath: modulo by zero");
return a % b;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IAddressResolver {
function getAddress(bytes32 name) external view returns (address);
function getSynth(bytes32 key) external view returns (address);
function getAvailableBridge(bytes32 bridgeName) external view returns (address);
function getBridgeList() external view returns (bytes32[] memory);
function requireAndGetAddress(bytes32 name, string calldata reason) external view returns (address);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IERC20 {
// ERC20 Optional Views
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
// Views
function totalSupply() external view returns (uint256);
function balanceOf(address owner) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
// Mutative functions
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
// Events
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IExchanger {
struct ExchangeEntrySettlement {
bytes32 src;
uint256 amount;
bytes32 dest;
uint256 reclaim;
uint256 rebate;
uint256 srcRoundIdAtPeriodEnd;
uint256 destRoundIdAtPeriodEnd;
uint256 timestamp;
}
struct ExchangeEntry {
uint256 sourceRate;
uint256 destinationRate;
uint256 destinationAmount;
uint256 exchangeFeeRate;
uint256 exchangeDynamicFeeRate;
uint256 roundIdForSrc;
uint256 roundIdForDest;
}
struct ExchangeArgs {
address fromAccount;
address destAccount;
bytes32 sourceCurrencyKey;
bytes32 destCurrencyKey;
uint256 sourceAmount;
uint256 destAmount;
uint256 fee;
uint256 reclaimed;
uint256 refunded;
uint16 destChainId;
bool erc20Payment;
}
// Views
function calculateAmountAfterSettlement(
address from,
bytes32 currencyKey,
uint256 amount,
uint256 refunded
) external view returns (uint256 amountAfterSettlement);
function isSynthRateInvalid(bytes32 currencyKey) external view returns (bool);
function maxSecsLeftInWaitingPeriod(address account, bytes32 currencyKey) external view returns (uint256);
function settlementOwing(
address account,
bytes32 currencyKey
) external view returns (uint256 reclaimAmount, uint256 rebateAmount, uint256 numEntries);
// function hasWaitingPeriodOrSettlementOwing(address account, bytes32 currencyKey) external view returns (bool);
function feeRateForExchange(bytes32 sourceCurrencyKey, bytes32 destinationCurrencyKey) external view returns (uint256);
function dynamicFeeRateForExchange(
bytes32 sourceCurrencyKey,
bytes32 destinationCurrencyKey
) external view returns (uint256 feeRate, bool tooVolatile);
function getAmountsForExchange(
uint256 sourceAmount,
bytes32 sourceCurrencyKey,
bytes32 destinationCurrencyKey
) external view returns (uint256 amountReceived, uint256 fee, uint256 exchangeFeeRate);
// function priceDeviationThresholdFactor() external view returns (uint256);
// function waitingPeriodSecs() external view returns (uint256);
// function lastExchangeRate(bytes32 currencyKey) external view returns (uint256);
// Mutative functions
function exchange(ExchangeArgs calldata args, bytes32 bridgeName) external payable returns (uint256 amountReceived);
function exchangeAtomically(
uint256 minAmount,
ExchangeArgs calldata args,
bytes32 bridgeName
) external payable returns (uint256 amountReceived);
function settle(address from, bytes32 currencyKey) external returns (uint256 reclaimed, uint256 refunded, uint256 numEntries);
function suspendSynthWithInvalidRate(bytes32 currencyKey) external;
function updateDestinationForExchange(address recipient, bytes32 destinationKey, uint256 destinationAmount) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../interfaces/ISynth.sol";
interface IIssuer {
// Views
function allNetworksDebtInfo() external view returns (uint256 debt, uint256 sharesSupply);
function availableCurrencyKeys() external view returns (bytes32[] memory);
function availableSynthCount() external view returns (uint256);
function availableSynths(uint256 index) external view returns (ISynth);
function canBurnSynths(address account) external view returns (bool);
function collateral(address account) external view returns (uint256);
function collateralisationRatio(address issuer) external view returns (uint256);
function collateralisationRatioAndAnyRatesInvalid(
address _issuer
) external view returns (uint256 cratio, bool anyRateIsInvalid);
function debtBalanceOf(address issuer) external view returns (uint256 debtBalance);
function issuanceRatio() external view returns (uint256);
function lastIssueEvent(address account) external view returns (uint256);
function maxIssuableSynths(address issuer) external view returns (uint256 maxIssuable);
function minimumStakeTime() external view returns (uint256);
function remainingIssuableSynths(
address issuer
) external view returns (uint256 maxIssuable, uint256 alreadyIssued, uint256 totalSystemDebt);
function synths(bytes32 currencyKey) external view returns (ISynth);
function getSynths(bytes32[] calldata currencyKeys) external view returns (ISynth[] memory);
function synthsByAddress(address synthAddress) external view returns (bytes32);
function totalIssuedSynths(bytes32 currencyKey) external view returns (uint256);
function checkFreeCollateral(
address _issuer,
bytes32 _collateralKey,
uint16 _chainId
) external view returns (uint256 withdrawableSynthr);
function issueSynths(
address from,
uint256 amount,
uint256 destChainId
) external returns (uint256 synthAmount, uint256 debtShare);
function issueMaxSynths(address from, uint256 destChainId) external returns (uint256 synthAmount, uint256 debtShare);
function burnSynths(
address from,
bytes32 synthKey,
uint256 amount
) external returns (uint256 synthAmount, uint256 debtShare, uint256 reclaimed, uint256 refunded);
function burnSynthsToTarget(
address from,
bytes32 synthKey
) external returns (uint256 synthAmount, uint256 debtShare, uint256 reclaimed, uint256 refunded);
function burnForRedemption(address deprecatedSynthProxy, address account, uint256 balance) external;
function burnSynthsWithoutDebt(bytes32 currencyKey, address from, uint amount) external returns (uint256 burnAmount);
function synthIssueFromSynthrSwap(address _account, bytes32 _synthKey, uint256 _synthAmount) external;
function liquidateAccount(
address account,
bytes32 collateralKey,
uint16 chainId,
bool isSelfLiquidation
) external returns (uint256 totalRedeemed, uint256 amountToLiquidate, uint256 sharesToRemove);
function destIssue(address _account, bytes32 _synthKey, uint256 _synthAmount) external;
function destBurn(address _account, bytes32 _synthKey, uint256 _synthAmount) external returns (uint256);
function transferMargin(address account, uint256 marginDelta) external returns (uint256);
function destTransferMargin(address _account, uint256 _marginDelta, bytes32 _marketKey) external returns (bool);
function setCurrentPeriodId(uint128 periodId) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./IExchanger.sol";
interface IOffChainExchanger {
// Views
function offchainPriceMaxAge(bytes32 _currencyKey) external view returns (uint256);
function offchainPriceMinAge(bytes32 _currencyKey) external view returns (uint256);
function offchainPriceDivergence(bytes32 _currencyKey) external view returns (uint256);
function feeRateForOffChainExchange(
bytes32 sourceCurrencyKey,
bytes32 destinationCurrencyKey
) external view returns (uint256);
function getAmountsForOffChainExchangeMinusFees(
bytes32 _sourceKey,
bytes32 _destKey,
uint256 _destAmount
) external view returns (uint256 amountReceived, uint256 fee);
// Mutative functions
function exchange(
IExchanger.ExchangeArgs calldata args,
bytes32 bridgeName,
bytes[] calldata priceUpdateData
) external payable returns (uint256 amountReceived);
function updateDestinationForExchange(address recipient, bytes32 destinationKey, uint256 destinationAmount) external;
function exchangeForDexAggregation(
address _account,
bytes32 _sourceKey,
bytes32 _destKey,
uint256 _sourceAmount,
bytes[] memory _priceUpdateData,
uint16 _destChainId
) external payable returns (uint256 destAmount, uint256 fee);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface ISynth {
// Views
function balanceOf(address _account) external view returns (uint256);
function currencyKey() external view returns (bytes32);
function transferableSynths(address account) external view returns (uint256);
// Mutative functions
function transferAndSettle(address to, uint256 value) external payable returns (bool);
function transferFromAndSettle(address from, address to, uint256 value) external payable returns (bool);
function burn(address account, uint256 amount) external;
function issue(address account, uint256 amount) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface ISystemStatus {
struct Status {
bool canSuspend;
bool canResume;
}
struct Suspension {
bool suspended;
// reason is an integer code,
// 0 => no reason, 1 => upgrading, 2+ => defined by system usage
uint248 reason;
}
// Views
function accessControl(bytes32 section, address account) external view returns (bool canSuspend, bool canResume);
function requireSystemActive() external view;
function systemSuspended() external view returns (bool);
function requireIssuanceActive() external view;
function requireExchangeActive() external view;
function requireFuturesActive() external view;
function requireFuturesMarketActive(bytes32 marketKey) external view;
function requireExchangeBetweenSynthsAllowed(bytes32 sourceCurrencyKey, bytes32 destinationCurrencyKey) external view;
function requireSynthActive(bytes32 currencyKey) external view;
function synthSuspended(bytes32 currencyKey) external view returns (bool);
function requireSynthsActive(bytes32 sourceCurrencyKey, bytes32 destinationCurrencyKey) external view;
function systemSuspension() external view returns (bool suspended, uint248 reason);
function issuanceSuspension() external view returns (bool suspended, uint248 reason);
function exchangeSuspension() external view returns (bool suspended, uint248 reason);
function futuresSuspension() external view returns (bool suspended, uint248 reason);
function synthExchangeSuspension(bytes32 currencyKey) external view returns (bool suspended, uint248 reason);
function synthSuspension(bytes32 currencyKey) external view returns (bool suspended, uint248 reason);
function futuresMarketSuspension(bytes32 marketKey) external view returns (bool suspended, uint248 reason);
function getSynthExchangeSuspensions(
bytes32[] calldata synths
) external view returns (bool[] memory exchangeSuspensions, uint256[] memory reasons);
function getSynthSuspensions(
bytes32[] calldata synths
) external view returns (bool[] memory suspensions, uint256[] memory reasons);
function getFuturesMarketSuspensions(
bytes32[] calldata marketKeys
) external view returns (bool[] memory suspensions, uint256[] memory reasons);
// Restricted functions
function suspendIssuance(uint256 reason) external;
function suspendSynth(bytes32 currencyKey, uint256 reason) external;
function suspendFuturesMarket(bytes32 marketKey, uint256 reason) external;
function updateAccessControl(bytes32 section, address account, bool canSuspend, bool canResume) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Inheritance
import "./Owned.sol";
import "../interfaces/IAddressResolver.sol";
// Internal references
import "../interfaces/IIssuer.sol";
import "./MixinResolver.sol";
contract AddressResolverLightChain is Owned, IAddressResolver {
mapping(bytes32 => address) public repository;
mapping(bytes32 => address) public availableBridge;
mapping(address => bool) public isBridge;
bytes32[] public bridgeList;
constructor(address _owner) Owned(_owner) {}
/* ========== RESTRICTED FUNCTIONS ========== */
function importAddresses(bytes32[] calldata names, address[] calldata destinations) external onlyOwner {
require(names.length == destinations.length, "Input lengths must match");
for (uint256 i = 0; i < names.length; i++) {
bytes32 name = names[i];
address destination = destinations[i];
repository[name] = destination;
emit AddressImported(name, destination);
}
}
function addAvailableBridge(bytes32 bridgeName, address bridgeAddress) external onlyOwner {
_addAvailableBridge(bridgeName, bridgeAddress);
}
function removeAvailableBridge(bytes32 bridgeName) external onlyOwner {
_removeAvailableBridge(bridgeName);
}
/* ========= PUBLIC FUNCTIONS ========== */
function rebuildCaches(MixinResolver[] calldata destinations) external {
for (uint256 i = 0; i < destinations.length; i++) {
destinations[i].rebuildCache();
}
}
/* ========== PRIVATE FUNCTIONS ========== */
function _addAvailableBridge(bytes32 bridgeName, address bridgeAddress) private {
if (availableBridge[bridgeName] != address(0)) {
_removeAvailableBridge(bridgeName);
}
availableBridge[bridgeName] = bridgeAddress;
isBridge[bridgeAddress] = true;
bridgeList.push(bridgeName);
emit AddBridge(bridgeName, bridgeAddress);
}
function _removeAvailableBridge(bytes32 bridgeName) private {
require(availableBridge[bridgeName] != address(0), "The bridge no exist.");
uint lastBridgeNumber = bridgeList.length - 1;
for (uint ii = 0; ii <= lastBridgeNumber; ii++) {
if (bridgeList[ii] == bridgeName) {
bridgeList[ii] = bridgeList[lastBridgeNumber];
bridgeList.pop();
break;
}
}
address bridgeToRemove = availableBridge[bridgeName];
delete availableBridge[bridgeName];
delete isBridge[bridgeToRemove];
emit RemoveBridge(bridgeName, bridgeToRemove);
}
/* ========== VIEWS ========== */
function areAddressesImported(bytes32[] calldata names, address[] calldata destinations) external view returns (bool) {
for (uint256 i = 0; i < names.length; i++) {
if (repository[names[i]] != destinations[i]) {
return false;
}
}
return true;
}
function getAddress(bytes32 name) external view returns (address) {
return repository[name];
}
function requireAndGetAddress(bytes32 name, string calldata reason) external view returns (address) {
address _foundAddress = repository[name];
require(_foundAddress != address(0), reason);
return _foundAddress;
}
function getSynth(bytes32 key) external view returns (address) {
IIssuer issuer = IIssuer(repository["Issuer"]);
require(address(issuer) != address(0), "Cannot find Issuer address");
return address(issuer.synths(key));
}
function getAvailableBridge(bytes32 bridgeName) external view returns (address) {
return availableBridge[bridgeName];
}
function getBridgeList() external view returns (bytes32[] memory) {
return bridgeList;
}
/* ========== EVENTS ========== */
event AddressImported(bytes32 name, address destination);
event AddBridge(bytes32 indexed bridgeName, address bridgeAddress);
event RemoveBridge(bytes32 indexed bridgeName, address bridgeAddress);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Inheritance
import "./Owned.sol";
// Libraries
import "./SafeDecimalMath.sol";
// Internal references
import "./TokenStateLightChain.sol";
contract ExternStateToken is Owned {
using SafeMath for uint256;
using SafeDecimalMath for uint256;
/* ========== STATE VARIABLES ========== */
/* Stores balances and allowances. */
TokenStateLightChain public tokenState;
/* Other ERC20 fields. */
string public name;
string public symbol;
uint8 public decimals;
constructor(
TokenStateLightChain _tokenState,
string memory _name,
string memory _symbol,
uint8 _decimals,
address _owner
) Owned(_owner) {
tokenState = _tokenState;
name = _name;
symbol = _symbol;
decimals = _decimals;
}
/* ========== VIEWS ========== */
/**
* @notice Returns the ERC20 allowance of one party to spend on behalf of another.
* @param owner The party authorising spending of their funds.
* @param spender The party spending tokenOwner's funds.
*/
function allowance(address owner, address spender) public view returns (uint256) {
return tokenState.allowance(owner, spender);
}
/**
* @notice Returns the ERC20 token balance of a given account.
*/
function balanceOf(address account) external view returns (uint256) {
return tokenState.balanceOf(account);
}
function totalSupply() external view returns (uint256) {
return tokenState.totalSupply();
}
/* ========== MUTATIVE FUNCTIONS ========== */
/**
* @notice Set the address of the TokenState contract.
* @dev This can be used to "pause" transfer functionality, by pointing the tokenState at 0x000..
* as balances would be unreachable.
*/
function setTokenState(TokenStateLightChain _tokenState) external onlyOwner {
tokenState = _tokenState;
emit TokenStateUpdated(address(_tokenState));
}
function _internalTransfer(address from, address to, uint256 value) internal virtual returns (bool) {
/* Disallow transfers to irretrievable-addresses. */
require(to != address(0) && to != address(this), "Cannot transfer to this address");
// Insufficient balance will be handled by the safe subtraction.
tokenState.setBalanceOf(from, tokenState.balanceOf(from).sub(value));
tokenState.setBalanceOf(to, tokenState.balanceOf(to).add(value));
// Emit a standard ERC20 transfer event
emit Transfer(from, to, value);
return true;
}
/**
* @dev Perform an ERC20 token transfer. Designed to be called by transfer functions possessing
* the onlyProxy or optionalProxy modifiers.
*/
function _transferByProxy(address from, address to, uint256 value) internal returns (bool) {
return _internalTransfer(from, to, value);
}
/*
* @dev Perform an ERC20 token transferFrom. Designed to be called by transferFrom functions
* possessing the optionalProxy or optionalProxy modifiers.
*/
function _transferFromByProxy(address sender, address from, address to, uint256 value) internal returns (bool) {
/* Insufficient allowance will be handled by the safe subtraction. */
tokenState.setAllowance(from, sender, tokenState.allowance(from, sender).sub(value));
return _internalTransfer(from, to, value);
}
/**
* @notice Approves spender to transfer on the message sender's behalf.
*/
function approve(address spender, uint256 value) public returns (bool) {
address sender = msg.sender;
tokenState.setAllowance(sender, spender, value);
emit Approval(sender, spender, value);
return true;
}
/* ========== EVENTS ========== */
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event TokenStateUpdated(address newTokenState);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Internal references
import "./AddressResolverLightChain.sol";
contract MixinResolver {
AddressResolverLightChain public resolver;
mapping(bytes32 => address) private addressCache;
constructor(address _resolver) {
resolver = AddressResolverLightChain(_resolver);
}
/* ========== INTERNAL FUNCTIONS ========== */
function combineArrays(bytes32[] memory first, bytes32[] memory second) internal pure returns (bytes32[] memory combination) {
combination = new bytes32[](first.length + second.length);
for (uint256 i = 0; i < first.length; i++) {
combination[i] = first[i];
}
for (uint256 j = 0; j < second.length; j++) {
combination[first.length + j] = second[j];
}
}
/* ========== PUBLIC FUNCTIONS ========== */
// Note: this function is public not external in order for it to be overridden and invoked via super in subclasses
function resolverAddressesRequired() public view virtual returns (bytes32[] memory addresses) {}
function rebuildCache() public {
bytes32[] memory requiredAddresses = resolverAddressesRequired();
// The resolver must call this function whenver it updates its state
for (uint256 i = 0; i < requiredAddresses.length; i++) {
bytes32 name = requiredAddresses[i];
// Note: can only be invoked once the resolver has all the targets needed added
address destination = resolver.requireAndGetAddress(
name,
string(abi.encodePacked("Resolver missing target: ", name))
);
addressCache[name] = destination;
emit CacheUpdated(name, destination);
}
}
/* ========== VIEWS ========== */
function isResolverCached() external view returns (bool) {
bytes32[] memory requiredAddresses = resolverAddressesRequired();
for (uint256 i = 0; i < requiredAddresses.length; i++) {
bytes32 name = requiredAddresses[i];
// false if our cache is invalid or if the resolver doesn't have the required address
if (resolver.getAddress(name) != addressCache[name] || addressCache[name] == address(0)) {
return false;
}
}
return true;
}
/* ========== INTERNAL FUNCTIONS ========== */
function requireAndGetAddress(bytes32 name) internal view returns (address) {
address _foundAddress = addressCache[name];
require(_foundAddress != address(0), string(abi.encodePacked("Missing address: ", name)));
return _foundAddress;
}
/* ========== EVENTS ========== */
event CacheUpdated(bytes32 name, address destination);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Inheritance
import "./Synth.sol";
contract MultiCollateralSynthLightChain is Synth {
// bytes32 public constant CONTRACT_NAME = "MultiCollateralSynth";
/* ========== CONSTRUCTOR ========== */
constructor(
TokenStateLightChain _tokenState,
string memory _tokenName,
string memory _tokenSymbol,
address _owner,
bytes32 _currencyKey,
address _resolver
) Synth(_tokenState, _tokenName, _tokenSymbol, _owner, _currencyKey, _resolver) {}
/* ========== VIEWS ======================= */
/* ========== MUTATIVE FUNCTIONS ========== */
/**
* @notice Function that allows multi Collateral to issue a certain number of synths from an account.
* @param account Account to issue synths to
* @param amount Number of synths
*/
function issue(address account, uint256 amount) external override onlyInternalContracts {
super._internalIssue(account, amount);
}
/**
* @notice Function that allows multi Collateral to burn a certain number of synths from an account.
* @param account Account to burn synths from
* @param amount Number of synths
*/
function burn(address account, uint256 amount) external override onlyInternalContracts {
super._internalBurn(account, amount);
}
/* ========== MODIFIERS ========== */
// overriding modifier from super to add more internal contracts and checks
function _isInternalContract(address account) internal view override returns (bool) {
return super._isInternalContract(account);
// || wrapperFactory().isWrapper(account);
// || (account == address(etherWrapper()))
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Owned {
address public owner;
address public nominatedOwner;
constructor(address _owner) {
require(_owner != address(0), "Owner address cannot be 0");
owner = _owner;
emit OwnerChanged(address(0), _owner);
}
function nominateNewOwner(address _owner) external onlyOwner {
nominatedOwner = _owner;
emit OwnerNominated(_owner);
}
function acceptOwnership() external {
require(msg.sender == nominatedOwner, "You must be nominated before you can accept ownership");
emit OwnerChanged(owner, nominatedOwner);
owner = nominatedOwner;
nominatedOwner = address(0);
}
modifier onlyOwner() {
_onlyOwner();
_;
}
function _onlyOwner() private view {
require(msg.sender == owner, "Only the contract owner may perform this action");
}
event OwnerNominated(address newOwner);
event OwnerChanged(address oldOwner, address newOwner);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Libraries
// import "openzeppelin-solidity-2.3.0/contracts/math/SafeMath.sol";
import "../externals/openzeppelin/SafeMath.sol";
library SafeDecimalMath {
using SafeMath for uint256;
/* Number of decimal places in the representations. */
uint8 public constant decimals = 18;
uint8 public constant highPrecisionDecimals = 27;
/* The number representing 1.0. */
uint256 public constant UNIT = 10 ** uint256(decimals);
/* The number representing 1.0 for higher fidelity numbers. */
uint256 public constant PRECISE_UNIT = 10 ** uint256(highPrecisionDecimals);
uint256 private constant UNIT_TO_HIGH_PRECISION_CONVERSION_FACTOR = 10 ** uint256(highPrecisionDecimals - decimals);
/**
* @return Provides an interface to UNIT.
*/
function unit() external pure returns (uint256) {
return UNIT;
}
/**
* @return Provides an interface to PRECISE_UNIT.
*/
function preciseUnit() external pure returns (uint256) {
return PRECISE_UNIT;
}
/**
* @return The result of multiplying x and y, interpreting the operands as fixed-point
* decimals.
*
* @dev A unit factor is divided out after the product of x and y is evaluated,
* so that product must be less than 2**256. As this is an integer division,
* the internal division always rounds down. This helps save on gas. Rounding
* is more expensive on gas.
*/
function multiplyDecimal(uint256 x, uint256 y) internal pure returns (uint256) {
/* Divide by UNIT to remove the extra factor introduced by the product. */
return x.mul(y) / UNIT;
}
/**
* @return The result of safely multiplying x and y, interpreting the operands
* as fixed-point decimals of the specified precision unit.
*
* @dev The operands should be in the form of a the specified unit factor which will be
* divided out after the product of x and y is evaluated, so that product must be
* less than 2**256.
*
* Unlike multiplyDecimal, this function rounds the result to the nearest increment.
* Rounding is useful when you need to retain fidelity for small decimal numbers
* (eg. small fractions or percentages).
*/
function _multiplyDecimalRound(uint256 x, uint256 y, uint256 precisionUnit) private pure returns (uint256) {
/* Divide by UNIT to remove the extra factor introduced by the product. */
uint256 quotientTimesTen = x.mul(y) / (precisionUnit / 10);
if (quotientTimesTen % 10 >= 5) {
quotientTimesTen += 10;
}
return quotientTimesTen / 10;
}
/**
* @return The result of safely multiplying x and y, interpreting the operands
* as fixed-point decimals of a precise unit.
*
* @dev The operands should be in the precise unit factor which will be
* divided out after the product of x and y is evaluated, so that product must be
* less than 2**256.
*
* Unlike multiplyDecimal, this function rounds the result to the nearest increment.
* Rounding is useful when you need to retain fidelity for small decimal numbers
* (eg. small fractions or percentages).
*/
function multiplyDecimalRoundPrecise(uint256 x, uint256 y) internal pure returns (uint256) {
return _multiplyDecimalRound(x, y, PRECISE_UNIT);
}
/**
* @return The result of safely multiplying x and y, interpreting the operands
* as fixed-point decimals of a standard unit.
*
* @dev The operands should be in the standard unit factor which will be
* divided out after the product of x and y is evaluated, so that product must be
* less than 2**256.
*
* Unlike multiplyDecimal, this function rounds the result to the nearest increment.
* Rounding is useful when you need to retain fidelity for small decimal numbers
* (eg. small fractions or percentages).
*/
function multiplyDecimalRound(uint256 x, uint256 y) internal pure returns (uint256) {
return _multiplyDecimalRound(x, y, UNIT);
}
/**
* @return The result of safely dividing x and y. The return value is a high
* precision decimal.
*
* @dev y is divided after the product of x and the standard precision unit
* is evaluated, so the product of x and UNIT must be less than 2**256. As
* this is an integer division, the result is always rounded down.
* This helps save on gas. Rounding is more expensive on gas.
*/
function divideDecimal(uint256 x, uint256 y) internal pure returns (uint256) {
/* Reintroduce the UNIT factor that will be divided out by y. */
return x.mul(UNIT).div(y);
}
/**
* @return The result of safely dividing x and y. The return value is as a rounded
* decimal in the precision unit specified in the parameter.
*
* @dev y is divided after the product of x and the specified precision unit
* is evaluated, so the product of x and the specified precision unit must
* be less than 2**256. The result is rounded to the nearest increment.
*/
function _divideDecimalRound(uint256 x, uint256 y, uint256 precisionUnit) private pure returns (uint256) {
uint256 resultTimesTen = x.mul(precisionUnit * 10).div(y);
if (resultTimesTen % 10 >= 5) {
resultTimesTen += 10;
}
return resultTimesTen / 10;
}
/**
* @return The result of safely dividing x and y. The return value is as a rounded
* standard precision decimal.
*
* @dev y is divided after the product of x and the standard precision unit
* is evaluated, so the product of x and the standard precision unit must
* be less than 2**256. The result is rounded to the nearest increment.
*/
function divideDecimalRound(uint256 x, uint256 y) internal pure returns (uint256) {
return _divideDecimalRound(x, y, UNIT);
}
/**
* @return The result of safely dividing x and y. The return value is as a rounded
* high precision decimal.
*
* @dev y is divided after the product of x and the high precision unit
* is evaluated, so the product of x and the high precision unit must
* be less than 2**256. The result is rounded to the nearest increment.
*/
function divideDecimalRoundPrecise(uint256 x, uint256 y) internal pure returns (uint256) {
return _divideDecimalRound(x, y, PRECISE_UNIT);
}
/**
* @dev Convert a standard decimal representation to a high precision one.
*/
function decimalToPreciseDecimal(uint256 i) internal pure returns (uint256) {
return i.mul(UNIT_TO_HIGH_PRECISION_CONVERSION_FACTOR);
}
/**
* @dev Convert a high precision decimal to a standard decimal representation.
*/
function preciseDecimalToDecimal(uint256 i) internal pure returns (uint256) {
uint256 quotientTimesTen = i / (UNIT_TO_HIGH_PRECISION_CONVERSION_FACTOR / 10);
if (quotientTimesTen % 10 >= 5) {
quotientTimesTen += 10;
}
return quotientTimesTen / 10;
}
// Computes `a - b`, setting the value to 0 if b > a.
function floorsub(uint256 a, uint256 b) internal pure returns (uint256) {
return b >= a ? 0 : a - b;
}
/* ---------- Utilities ---------- */
/*
* Absolute value of the input, returned as a signed number.
*/
function signedAbs(int256 x) internal pure returns (int256) {
return x < 0 ? -x : x;
}
/*
* Absolute value of the input, returned as an unsigned number.
*/
function abs(int256 x) internal pure returns (uint256) {
return uint256(signedAbs(x));
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Inheritance
import "./Owned.sol";
contract State is Owned {
// the address of the contract that can modify variables
// this can only be changed by the owner of this contract
address public associatedContract;
constructor(address _owner, address _associatedContract) Owned(_owner) {
// This contract is abstract, and thus cannot be instantiated directly
require(owner != address(0), "Owner must be set");
associatedContract = _associatedContract;
emit AssociatedContractUpdated(_associatedContract);
}
/* ========== SETTERS ========== */
// Change the associated contract to a new address
function setAssociatedContract(address _associatedContract) external onlyOwner {
associatedContract = _associatedContract;
emit AssociatedContractUpdated(_associatedContract);
}
/* ========== MODIFIERS ========== */
modifier onlyAssociatedContract() {
require(msg.sender == associatedContract, "Only the associated contract can perform this action");
_;
}
/* ========== EVENTS ========== */
event AssociatedContractUpdated(address associatedContract);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Inheritance
import "./Owned.sol";
import "./ExternStateToken.sol";
import "./MixinResolver.sol";
import "../interfaces/ISynth.sol";
import "../interfaces/IERC20.sol";
// Internal references
import "../interfaces/ISystemStatus.sol";
import "../interfaces/IExchanger.sol";
import "../interfaces/IOffChainExchanger.sol";
import "../interfaces/IIssuer.sol";
import "./SafeDecimalMath.sol";
contract Synth is Owned, ExternStateToken, MixinResolver {
using SafeMath for uint256;
using SafeDecimalMath for uint256;
bytes32 public constant CONTRACT_NAME = "Synth";
/* ========== STATE VARIABLES ========== */
// Currency key which identifies this Synth to the Synthr system
bytes32 public currencyKey;
address private syAggregator;
uint8 public constant DECIMALS = 18;
// Where fees are pooled in sUSD
address public constant FEE_ADDRESS = 0xfeEFEEfeefEeFeefEEFEEfEeFeefEEFeeFEEFEeF;
/* ========== ADDRESS RESOLVER CONFIGURATION ========== */
bytes32 private constant CONTRACT_SYSTEMSTATUS = "SystemStatus";
bytes32 private constant CONTRACT_EXCHANGER = "Exchanger";
bytes32 private constant CONTRACT_OFF_CHAIN_EXCHANGER = "OffChainExchanger";
bytes32 private constant CONTRACT_ISSUER = "Issuer";
/* ========== CONSTRUCTOR ========== */
constructor(
TokenStateLightChain _tokenState,
string memory _tokenName,
string memory _tokenSymbol,
address _owner,
bytes32 _currencyKey,
address _resolver
) ExternStateToken(_tokenState, _tokenName, _tokenSymbol, DECIMALS, _owner) MixinResolver(_resolver) {
require(_owner != address(0), "_owner cannot be 0");
currencyKey = _currencyKey;
}
/* ========== VIEWS ========== */
// Note: use public visibility so that it can be invoked in a subclass
function resolverAddressesRequired() public view virtual override returns (bytes32[] memory addresses) {
addresses = new bytes32[](4);
addresses[0] = CONTRACT_SYSTEMSTATUS;
addresses[1] = CONTRACT_EXCHANGER;
addresses[2] = CONTRACT_ISSUER;
addresses[3] = CONTRACT_OFF_CHAIN_EXCHANGER;
}
function systemStatus() internal view returns (ISystemStatus) {
return ISystemStatus(requireAndGetAddress(CONTRACT_SYSTEMSTATUS));
}
function exchanger() internal view returns (IExchanger) {
return IExchanger(requireAndGetAddress(CONTRACT_EXCHANGER));
}
function offChainExchanger() internal view returns (IOffChainExchanger) {
return IOffChainExchanger(requireAndGetAddress(CONTRACT_OFF_CHAIN_EXCHANGER));
}
function issuer() internal view returns (IIssuer) {
return IIssuer(requireAndGetAddress(CONTRACT_ISSUER));
}
function _ensureCanTransfer(address from, uint256 value) internal view {
require(exchanger().maxSecsLeftInWaitingPeriod(from, currencyKey) == 0, "Cannot transfer during waiting period");
require(transferableSynths(from) >= value, "Insufficient balance after any settlement owing");
systemStatus().requireSynthActive(currencyKey);
}
function transferableSynths(address account) public view returns (uint256) {
(uint256 reclaimAmount, , ) = exchanger().settlementOwing(account, currencyKey);
// Note: ignoring rebate amount here because a settle() is required in order to
// allow the transfer to actually work
uint256 balance = tokenState.balanceOf(account);
if (reclaimAmount > balance) {
return 0;
} else {
return balance.sub(reclaimAmount);
}
}
/* ========== MUTATIVE FUNCTIONS ========== */
function transfer(address to, uint256 value) public payable returns (bool) {
_ensureCanTransfer(msg.sender, value);
// transfers to 0x address will be burned
if (to == address(0)) {
return _internalBurn(msg.sender, value);
}
return _internalTransfer(msg.sender, to, value);
}
function transferAndSettle(address to, uint256 value) public payable returns (bool) {
// Exchanger.settle ensures synth is active
(, , uint256 numEntriesSettled) = exchanger().settle(msg.sender, currencyKey);
// Save gas instead of calling transferableSynths
uint256 balanceAfter = value;
if (numEntriesSettled > 0) {
balanceAfter = tokenState.balanceOf(msg.sender);
}
// Reduce the value to transfer if balance is insufficient after reclaimed
value = value > balanceAfter ? balanceAfter : value;
return _internalTransfer(msg.sender, to, value);
}
function transferFrom(address from, address to, uint256 value) public payable returns (bool) {
_ensureCanTransfer(from, value);
return _internalTransferFrom(from, to, value);
}
function transferFromAndSettle(address from, address to, uint256 value) public payable returns (bool) {
// Exchanger.settle() ensures synth is active
(, , uint256 numEntriesSettled) = exchanger().settle(from, currencyKey);
// Save gas instead of calling transferableSynths
uint256 balanceAfter = value;
if (numEntriesSettled > 0) {
balanceAfter = tokenState.balanceOf(from);
}
// Reduce the value to transfer if balance is insufficient after reclaimed
value = value >= balanceAfter ? balanceAfter : value;
return _internalTransferFrom(from, to, value);
}
function issue(address account, uint256 amount) external virtual onlyInternalContracts {
_internalIssue(account, amount);
}
function burn(address account, uint256 amount) external virtual onlyInternalContracts {
_internalBurn(account, amount);
}
function _internalIssue(address account, uint256 amount) internal {
tokenState.setBalanceOf(account, tokenState.balanceOf(account).add(amount));
tokenState.setTotalSupply(tokenState.totalSupply().add(amount));
emit Transfer(address(0), account, amount);
// emit Issued(account, amount);
}
function _internalBurn(address account, uint256 amount) internal returns (bool) {
tokenState.setBalanceOf(account, tokenState.balanceOf(account).sub(amount));
tokenState.setTotalSupply(tokenState.totalSupply().sub(amount));
emit Transfer(account, address(0), amount);
// emit Burned(account, amount);
return true;
}
// Allow owner to set the total supply on import.
function setTotalSupply(uint256 amount) external onlyOwner {
tokenState.setTotalSupply(amount);
}
/* ========== INTERNAL FUNCTIONS ========== */
function _internalTransferFrom(address from, address to, uint256 value) internal returns (bool) {
// Skip allowance update in case of infinite allowance
if (tokenState.allowance(from, msg.sender) != type(uint256).max) {
// Reduce the allowance by the amount we're transferring.
// The safeSub call will handle an insufficient allowance.
tokenState.setAllowance(from, msg.sender, tokenState.allowance(from, msg.sender).sub(value));
}
return super._internalTransfer(from, to, value);
}
function _internalTransfer(address from, address to, uint256 value) internal override returns (bool) {
return super._internalTransfer(from, to, value);
}
/* ========== MODIFIERS ========== */
function _isInternalContract(address account) internal view virtual returns (bool) {
return account == address(exchanger()) || account == address(issuer()) || account == address(offChainExchanger());
}
modifier onlyInternalContracts() {
require(_isInternalContract(msg.sender), "Only internal contracts allowed");
_;
}
/* ========== EVENTS ========== */
// event Issued(address indexed account, uint256 value);
// event Burned(address indexed account, uint256 value);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Inheritance
import "./State.sol";
contract TokenStateLightChain is State {
/* ERC20 fields. */
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
uint256 public totalSupply;
constructor(address _owner, address _associatedContract, uint256 _totalSupply) State(_owner, _associatedContract) {}
/* ========== SETTERS ========== */
/**
* @notice Set ERC20 allowance.
* @dev Only the associated contract may call this.
* @param tokenOwner The authorising party.
* @param spender The authorised party.
* @param value The total value the authorised party may spend on the
* authorising party's behalf.
*/
function setAllowance(address tokenOwner, address spender, uint256 value) external onlyAssociatedContract {
allowance[tokenOwner][spender] = value;
}
/**
* @notice Set the balance in a given account
* @dev Only the associated contract may call this.
* @param account The account whose value to set.
* @param value The new balance of the given account.
*/
function setBalanceOf(address account, uint256 value) external onlyAssociatedContract {
balanceOf[account] = value;
}
function setTotalSupply(uint256 _amount) external onlyAssociatedContract {
totalSupply = _amount;
}
}