Skip to content

Latest commit

 

History

History
366 lines (323 loc) · 12.7 KB

File metadata and controls

366 lines (323 loc) · 12.7 KB

Advanced Token contract. (AdvancedToken.sol)

View Source: contracts/connectors/loantoken/AdvancedToken.sol

↗ Extends: AdvancedTokenStorage ↘ Derived Contracts: LoanTokenLogicStorage

AdvancedToken contract

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.

Functions


approve

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;
    }

_mint

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;
    }

_burn

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;
    }

Contracts