View Source: contracts/connectors/loantoken/AdvancedToken.sol
↗ Extends: AdvancedTokenStorage ↘ Derived Contracts: LoanTokenLogicStorage
This contract code comes from bZx. bZx is a protocol for tokenized margin trading and lending https://bzx.network similar to the dYdX protocol.
- AdvancedToken implements standard ERC-20 approval, mint and burn token functionality. Logic (AdvancedToken) is kept aside from storage (AdvancedTokenStorage).
- For example, LoanTokenLogicDai contract uses AdvancedToken::_mint() to mint its Loan Dai iTokens.
- approve(address _spender, uint256 _value)
- _mint(address _to, uint256 _tokenAmount, uint256 _assetAmount, uint256 _price)
- _burn(address _who, uint256 _tokenAmount, uint256 _assetAmount, uint256 _price)
Set an amount as the allowance of spender over the caller's tokens.
* Returns a boolean value indicating whether the operation succeeded.
* IMPORTANT: Beware that changing an allowance with this method brings the risk
that someone may use both the old and the new allowance by unfortunate
transaction ordering. One possible solution to mitigate this race
condition is to first reduce the spender's allowance to 0 and set the
desired value afterwards:
ethereum/EIPs#20 (comment)
* Emits an {Approval} event.
*
function approve(address _spender, uint256 _value) public nonpayable
returns(bool)Arguments
| Name | Type | Description |
|---|---|---|
| _spender | address | The account address that will be able to spend the tokens. |
| _value | uint256 | The amount of tokens allowed to spend. |
Source Code
function approve(address _spender, uint256 _value) public returns (bool) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}The iToken minting process. Meant to issue Loan iTokens. Lenders are able to open an iToken position, by minting them. This function is called by LoanTokenLogicStandard::_mintToken
function _mint(address _to, uint256 _tokenAmount, uint256 _assetAmount, uint256 _price) internal nonpayable
returns(uint256)Arguments
| Name | Type | Description |
|---|---|---|
| _to | address | The recipient of the minted tTokens. |
| _tokenAmount | uint256 | The amount of iTokens to be minted. |
| _assetAmount | uint256 | The amount of lended tokens (asset to lend). |
| _price | uint256 | The price of the lended tokens. |
Returns
The updated balance of the recipient.
Source Code
function _mint(
address _to,
uint256 _tokenAmount,
uint256 _assetAmount,
uint256 _price
) internal returns (uint256) {
require(_to != address(0), "15");
uint256 _balance = balances[_to].add(_tokenAmount);
balances[_to] = _balance;
totalSupply_ = totalSupply_.add(_tokenAmount);
emit Mint(_to, _tokenAmount, _assetAmount, _price);
emit Transfer(address(0), _to, _tokenAmount);
return _balance;
}The iToken burning process. Meant to destroy Loan iTokens. Lenders are able to close an iToken position, by burning them. This function is called by LoanTokenLogicStandard::_burnToken
function _burn(address _who, uint256 _tokenAmount, uint256 _assetAmount, uint256 _price) internal nonpayable
returns(uint256)Arguments
| Name | Type | Description |
|---|---|---|
| _who | address | The owner of the iTokens to burn. |
| _tokenAmount | uint256 | The amount of iTokens to burn. |
| _assetAmount | uint256 | The amount of lended tokens. |
| _price | uint256 | The price of the lended tokens. |
Returns
The updated balance of the iTokens owner.
Source Code
function _burn(
address _who,
uint256 _tokenAmount,
uint256 _assetAmount,
uint256 _price
) internal returns (uint256) {
//bzx compare
//TODO: Unit test
uint256 _balance = balances[_who].sub(_tokenAmount, "16");
// a rounding error may leave dust behind, so we clear this out
if (_balance <= 10) {
// We can't leave such small balance quantities.
_tokenAmount = _tokenAmount.add(_balance);
_balance = 0;
}
balances[_who] = _balance;
totalSupply_ = totalSupply_.sub(_tokenAmount);
emit Burn(_who, _tokenAmount, _assetAmount, _price);
emit Transfer(_who, address(0), _tokenAmount);
return _balance;
}- 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