Skip to content

Commit

Permalink
Add transfer() fortransfering minted cToken to mintCToken()
Browse files Browse the repository at this point in the history
  • Loading branch information
masaun committed Feb 11, 2020
1 parent e93fcf2 commit 772b6bd
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 1 deletion.
4 changes: 3 additions & 1 deletion contracts/NewBancorPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ contract NewBancorPool is BnStorage, BnConstants, Managed {
* @notice - Mint cToken (Compound Token)
* @dev - Reference from this link => https://compound.finance/developers/ctokens
***/
function mintCToken(uint256 mintAmount) public returns (bool) {
function mintCToken(uint256 mintAmount, address recipient) public returns (bool) {
iErc20.approve(address(cDAItokenAddr), mintAmount); // approve the transfer
iCErc20.mint(mintAmount);

iCErc20.transfer(recipient, mintAmount);

// [Ref]:
// Erc20 underlying = Erc20(_ERC20tokenAddr); // get a handle for the underlying asset contract
// CErc20 cToken = CErc20(cDAItokenAddr); // get a handle for the corresponding cToken contract
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pragma solidity 0.4.26;

/**
* @title ERC 20 Token Standard Interface
* https://eips.ethereum.org/EIPS/eip-20
*/
interface EIP20Interface {

/**
* @notice Get the total number of tokens in circulation
* @return The supply of tokens
*/
function totalSupply() external view returns (uint256);

/**
* @notice Gets the balance of the specified address
* @param owner The address from which the balance will be retrieved
* @return The balance
*/
function balanceOf(address owner) external view returns (uint256 balance);

/**
* @notice Transfer `amount` tokens from `msg.sender` to `dst`
* @param dst The address of the destination account
* @param amount The number of tokens to transfer
* @return Whether or not the transfer succeeded
*/
function transfer(address dst, uint256 amount) external returns (bool success);

/**
* @notice Transfer `amount` tokens from `src` to `dst`
* @param src The address of the source account
* @param dst The address of the destination account
* @param amount The number of tokens to transfer
* @return Whether or not the transfer succeeded
*/
function transferFrom(address src, address dst, uint256 amount) external returns (bool success);

/**
* @notice Approve `spender` to transfer up to `amount` from `src`
* @dev This will overwrite the approval amount for `spender`
* and is subject to issues noted [here](https://eips.ethereum.org/EIPS/eip-20#approve)
* @param spender The address of the account which may transfer tokens
* @param amount The number of tokens that are approved (-1 means infinite)
* @return Whether or not the approval succeeded
*/
function approve(address spender, uint256 amount) external returns (bool success);

/**
* @notice Get the current allowance from `owner` for `spender`
* @param owner The address of the account which owns the tokens to be spent
* @param spender The address of the account which may transfer tokens
* @return The number of tokens allowed to be spent (-1 means infinite)
*/
function allowance(address owner, address spender) external view returns (uint256 remaining);

event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
}
115 changes: 115 additions & 0 deletions contracts_old/contracts_ref_compound-protocol/mockToken/CERC20Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
pragma solidity 0.4.26;

import '../../bancor-protocol/utility/SafeMath.sol';
import "../../bancor-protocol/token/interfaces/IERC20Token.sol";

import "../interfaces/EIP20Interface.sol";

/**
* @title CERC20 Mock
* @dev See https://compound.finance/developers
*/
contract CERC20Mock {
using SafeMath for uint256;

/**
* @notice Indicator that this is a CToken contract (for inspection)
*/
bool public constant isCToken = true;

/**
* @notice Block number that interest started to increase from
*/
uint256 public initialBlockNumber;

/**
* @notice Initial exchange rate used when minting the first CTokens (used when totalSupply = 0)
*/
uint256 public initialExchangeRate;

/**
* @notice Underlying asset for this CToken
*/
address public underlying;

/**
* @notice EIP-20 token decimals for this token
*/
uint256 public decimals;

/**
* @notice Construct a new money market
* @param underlying_ The address of the underlying asset
* @param initialExchangeRate_ The initial exchange rate, scaled by 1e18
* @param decimals_ ERC-20 decimal precision of this token
*/
constructor(address underlying_, uint256 initialExchangeRate_, uint256 decimals_) public {
initialBlockNumber = block.number;
underlying = underlying_;
initialExchangeRate = initialExchangeRate_;
decimals = decimals_;
EIP20Interface(underlying).totalSupply(); // Sanity check the underlying
}

/*** User Interface ***/

/**
* @notice Get the underlying balance of the `owner`
* @dev This also accrues interest in a transaction
* @param owner The address of the account to query
* @return The amount of underlying owned by `owner`
*/
function balanceOfUnderlying(address owner) public view returns (uint256) {
uint256 underlyingBalance = EIP20Interface(underlying).balanceOf(address(this));
if (balanceOf(owner) == 0) return 0;
return (totalSupply().mul(1e18).div(balanceOf(owner))).mul(underlyingBalance).div(1e18);
}

/**
* @notice Accrue interest then return the up-to-date exchange rate
* @return Calculated exchange rate scaled by 1e18
*/
function exchangeRateCurrent() public view returns (uint256) {
uint256 totalUnderlying = EIP20Interface(underlying).balanceOf(address(this));
uint256 tokenValueExp = initialExchangeRate;
if (totalSupply() > 0 && totalUnderlying > 0) {
totalUnderlying = totalUnderlying.mul(1e18);
tokenValueExp = totalUnderlying.div(totalSupply());
}
return tokenValueExp;
}

/**
* @notice Sender supplies assets into the market and receives cTokens in exchange
* @dev Accrues interest whether or not the operation succeeds, unless reverted
* @param mintAmount The amount of the underlying asset to supply
* @return true=success, otherwise a failure
*/
function mint(uint256 mintAmount) external returns (bool) {
uint256 mintedTokens = (mintAmount.mul(1e18)).div(exchangeRateCurrent());
_mint(msg.sender, mintedTokens);
return EIP20Interface(underlying).transferFrom(msg.sender, address(this), mintAmount);
}

/**
* @notice Sender supplies underlying to the money market
* @dev This is just a mock
* @param supplyAmount The amount of underlying to supply
* @return true=success, otherwise a failure
*/
function supplyUnderlying(uint256 supplyAmount) external returns (bool) {
return EIP20Interface(underlying).transferFrom(msg.sender, address(this), supplyAmount);
}

/**
* @notice Sender redeems cTokens in exchange for a specified amount of underlying asset
* @dev This is just a mock
* @param redeemAmount The amount of underlying to redeem
* @return true=success, otherwise a failure
*/
function redeemUnderlying(uint256 redeemAmount) external returns (bool) {
uint256 redeemTokens = redeemAmount.mul(1e18).div(exchangeRateCurrent());
_burn(msg.sender, redeemTokens);
return EIP20Interface(underlying).transfer(msg.sender, redeemAmount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity 0.4.26;

import "../../bancor-protocol/token/interfaces/IERC20Token.sol";

/**
* @title ERC20 Mock
* @dev Mock class using ERC20
*/
contract ERC20Mock {
/**
* @dev Allows anyone to mint tokens to any address
* @param to The address that will receive the minted tokens.
* @param amount The amount of tokens to mint.
* @return A boolean that indicates if the operation was successful.
*/
function mint(address to, uint256 amount) public {
_mint(to, amount);
}
}

0 comments on commit 772b6bd

Please sign in to comment.