Skip to content

Instantly share code, notes, and snippets.

@cakoyo
Created October 16, 2020 18:52
Show Gist options
  • Save cakoyo/e89e92c8c015c270193b2241588ff4b9 to your computer and use it in GitHub Desktop.
Save cakoyo/e89e92c8c015c270193b2241588ff4b9 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson_v0.5.10+commit.a1d534e.js&optimize=true&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.10;
import "./SafeMath.sol";
import "./Library.sol";
import "./OracleJustswap.sol";
contract Lending is Ownable {
using SafeMath for uint256;
mapping(address => uint256) internal totalDeposits;
mapping(address => uint256) internal totalLendings;
mapping(address => mapping (address => uint256)) internal userDeposits;
mapping(address => mapping (address => uint256)) internal userLendings;
function hasBalanceToLend(address _token, uint256 _amount) public view returns (bool) {
require(totalDeposits[_token].sub(totalLendings[_token]) > _amount, "No enough deposit to lend");
return true;
}
// Whitelist
address[] public whitelistTokens;
function isLendableToken(address _token) public view returns (bool) {
for (uint i = 0; i < whitelistTokens.length; i++) {
if (whitelistTokens[i] == _token)
return true;
}
return false;
}
event AddToken(address _token);
function addToken(address _token) external onlyOwner returns (bool) {
whitelistTokens.push(_token);
emit AddToken(_token);
return true;
}
event RemoveToken(address _token);
function removeToken(address _token) external onlyOwner returns (bool) {
bool exists; uint index;
for (uint i = 0; i < whitelistTokens.length; i++) {
if (whitelistTokens[i] == _token) {
exists = true;
index = i;
break;
}
}
if (exists) {
delete whitelistTokens[index];
emit RemoveToken(_token);
return true;
} else {
return false;
}
}
// Deposit
event Deposit(address _user, address _token, uint256 _amount, uint256 _timestamp);
function deposit(address _token, uint256 _amount) external payable returns (bool) {
require(_amount > 0);
require(isLendableToken(_token));
// Lendable token with positive amount, we could accept that.
totalDeposits[_token] = totalDeposits[_token].add(_amount);
userDeposits[msg.sender][_token] = userDeposits[msg.sender][_token].add(_amount);
transferDepositToReserve(address(this), msg.sender, _amount);
emit Deposit(msg.sender, _token, _amount, block.timestamp);
return true;
}
function getUserDeposit(address _token) public view returns (uint256) {
return getUserDeposit(_token, msg.sender);
}
function getUserDeposit(address _token, address _user) public view returns (uint256) {
require(_user != address(0));
require(isLendableToken(_token));
return userDeposits[_user][_token];
}
function getTotalDeposit(address _token) public view returns (uint256) {
require(isLendableToken(_token));
return totalDeposits[_token];
}
// Pledge
mapping (address => mapping (address => uint256)) public tokenToUserPledges;
mapping (address => uint256) public tokenToTotalPledges;
function getUserAvailable(address _token) public view returns (uint256) {
return getUserAvailable(msg.sender, _token);
}
function getUserAvailable(address _user, address _token) public view returns (uint256) {
require(_user != address(0));
require(isLendableToken(_token));
uint256 available = userDeposits[_user][_token].sub(tokenToUserPledges[_token][_user]);
return available <= 0 ? 0 : available;
}
event Pledge(address _user, address _token, uint256 _amount, uint256 _timestamp);
function pledge(address _user, address _token, uint256 _amount) public payable returns (bool) {
require(_amount > 0);
require(_amount <= getUserAvailable(_user, _token), "No enough available balance of that token to pledge.");
tokenToTotalPledges[_token] = tokenToTotalPledges[_token].add(_amount);
tokenToUserPledges[_token][_user] = tokenToUserPledges[_token][_user].add(_amount);
emit Pledge(_user, _token, _amount, block.timestamp);
return true;
}
function getUserPledge(address _token) public view returns (uint256) {
return getUserPledge(msg.sender, _token);
}
function getUserPledge(address _user, address _token) public view returns (uint256) {
require(_user != address(0));
require(isLendableToken(_token));
return tokenToUserPledges[_token][_user];
}
function getTotalPledge(address _token) public view returns (uint256) {
require(isLendableToken(_token));
return tokenToTotalPledges[_token];
}
OracleJustswap private justswap = new OracleJustswap();
function getTotalPledgeValueInTRX(address _token) public view returns (uint256) {
require(isLendableToken(_token));
return justswap.getAssetValueTRX(_token, tokenToTotalPledges[_token]);
}
function getTotalPledgeValueInUSD(address _token) public view returns (uint256) {
require(isLendableToken(_token));
return justswap.getAssetValueUSD(_token, tokenToTotalPledges[_token]);
}
function getUserPledgeValueInTRX(address _token) public view returns (uint256) {
return getUserPledgeValueInTRX(msg.sender, _token);
}
function getUserPledgeValueInTRX(address _user, address _token) public view returns (uint256) {
require(_user != address(0));
require(isLendableToken(_token));
return justswap.getAssetValueTRX(_token, tokenToUserPledges[_token][_user]);
}
function getUserPledgeValueInUSD(address _token) public view returns (uint256) {
return getUserPledgeValueInUSD(msg.sender, _token);
}
function getUserPledgeValueInUSD(address _user, address _token) public view returns (uint256) {
require(_user != address(0));
require(isLendableToken(_token));
return justswap.getAssetValueUSD(_token, tokenToUserPledges[_token][_user]);
}
// Lend
event Lend(address _user, address _token, uint256 _amount, uint256 _timestamp);
function lend(address _token, uint256 _amount) public payable returns (bool) {
require(_amount > 0);
require(isLendableToken(_token));
// Lendable token with positive amount.
require(hasBalanceToLend(_token, _amount));
// Has sufficient deposit to lend.
totalLendings[_token] = totalLendings[_token].add(_amount);
userLendings[msg.sender][_token] = userLendings[msg.sender][_token].add(_amount);
transferLendFromReserve(address(this), msg.sender, _amount);
emit Lend(msg.sender, _token, _amount, block.timestamp);
return true;
}
function transferDepositToReserve(address _reserve, address payable _user, uint256 _amount) internal {
require(_reserve != address(0));
require(_user != address(0));
require(_amount > 0);
TRC20(_reserve).transferFrom(_user, address(this), _amount);
}
function transferLendFromReserve(address _reserve, address payable _user, uint256 _amount) internal {
require(_reserve != address(0));
require(_user != address(0));
require(_amount > 0);
TRC20(_reserve).transferFrom(address(this), _user, _amount);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.10;
import "./SafeMath.sol";
/**
* @title TRC20 interface
* @dev see https://www.justswap.io/docs/justswap-interfaces_en.pdf
*/
interface ITRC20 {
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);
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
/**
* @title Standard TRC20 token
*
* @dev Implementation of the basic standard token.
* https://eips.ethereum.org/EIPS/eip-20
* Originally based on code by FirstBlood:
* https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
*
* This implementation emits additional Approval events, allowing applications to reconstruct the allowance status for
* all accounts just by listening to said events. Note that this isn't required by the specification, and other
* compliant implementations may not do it.
*/
contract TRC20 is ITRC20 {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowed;
uint256 private _totalSupply;
/**
* @dev Total number of tokens in existence.
*/
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
/**
* @dev Gets the balance of the specified address.
* @param owner The address to query the balance of.
* @return A uint256 representing the amount owned by the passed address.
*/
function balanceOf(address owner) public view returns (uint256) {
return _balances[owner];
}
/**
* @dev Function to check the amount of tokens that an owner allowed to a spender.
* @param owner address The address which owns the funds.
* @param spender address The address which will spend the funds.
* @return A uint256 specifying the amount of tokens still available for the spender.
*/
function allowance(address owner, address spender) public view returns (uint256) {
return _allowed[owner][spender];
}
/**
* @dev Transfer token to a specified address.
* @param to The address to transfer to.
* @param value The amount to be transferred.
*/
function transfer(address to, uint256 value) public returns (bool) {
_transfer(msg.sender, to, value);
return true;
}
/**
* @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
* 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:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
* @param spender The address which will spend the funds.
* @param value The amount of tokens to be spent.
*/
function approve(address spender, uint256 value) public returns (bool) {
_approve(msg.sender, spender, value);
return true;
}
/**
* @dev Transfer tokens from one address to another.
* Note that while this function emits an Approval event, this is not required as per the specification,
* and other compliant implementations may not emit the event.
* @param from address The address which you want to send tokens from
* @param to address The address which you want to transfer to
* @param value uint256 the amount of tokens to be transferred
*/
function transferFrom(address from, address to, uint256 value) public returns (bool) {
_transfer(from, to, value);
_approve(from, msg.sender, _allowed[from][msg.sender].sub(value));
return true;
}
/**
* @dev Increase the amount of tokens that an owner allowed to a spender.
* approve should be called when _allowed[msg.sender][spender] == 0. To increment
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* Emits an Approval event.
* @param spender The address which will spend the funds.
* @param addedValue The amount of tokens to increase the allowance by.
*/
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
_approve(msg.sender, spender, _allowed[msg.sender][spender].add(addedValue));
return true;
}
/**
* @dev Decrease the amount of tokens that an owner allowed to a spender.
* approve should be called when _allowed[msg.sender][spender] == 0. To decrement
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* Emits an Approval event.
* @param spender The address which will spend the funds.
* @param subtractedValue The amount of tokens to decrease the allowance by.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
_approve(msg.sender, spender, _allowed[msg.sender][spender].sub(subtractedValue));
return true;
}
/**
* @dev Transfer token for a specified addresses.
* @param from The address to transfer from.
* @param to The address to transfer to.
* @param value The amount to be transferred.
*/
function _transfer(address from, address to, uint256 value) internal {
require(to != address(0));
_balances[from] = _balances[from].sub(value);
_balances[to] = _balances[to].add(value);
emit Transfer(from, to, value);
}
/**
* @dev Internal function that mints an amount of the token and assigns it to
* an account. This encapsulates the modification of balances such that the
* proper events are emitted.
* @param account The account that will receive the created tokens.
* @param value The amount that will be created.
*/
function _mint(address account, uint256 value) internal {
require(account != address(0));
_totalSupply = _totalSupply.add(value);
_balances[account] = _balances[account].add(value);
emit Transfer(address(0), account, value);
}
/**
* @dev Internal function that burns an amount of the token of a given
* account.
* @param account The account whose tokens will be burnt.
* @param value The amount that will be burnt.
*/
function _burn(address account, uint256 value) internal {
require(account != address(0));
_totalSupply = _totalSupply.sub(value);
_balances[account] = _balances[account].sub(value);
emit Transfer(account, address(0), value);
}
/**
* @dev Approve an address to spend another addresses' tokens.
* @param owner The address that owns the tokens.
* @param spender The address that will spend the tokens.
* @param value The number of tokens that can be spent.
*/
function _approve(address owner, address spender, uint256 value) internal {
require(spender != address(0));
require(owner != address(0));
_allowed[owner][spender] = value;
emit Approval(owner, spender, value);
}
/**
* @dev Internal function that burns an amount of the token of a given
* account, deducting from the sender's allowance for said account. Uses the
* internal burn function.
* Emits an Approval event (reflecting the reduced allowance).
* @param account The account whose tokens will be burnt.
* @param value The amount that will be burnt.
*/
function _burnFrom(address account, uint256 value) internal {
_burn(account, value);
_approve(account, msg.sender, _allowed[account][msg.sender].sub(value));
}
}
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
constructor() public {
owner = msg.sender;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
/**
* @title Pausable
* @dev Base contract which allows children to implement an emergency stop mechanism.
*/
contract Pausable is Ownable {
event Pause();
event Unpause();
bool public paused = false;
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*/
modifier whenNotPaused() {
require(!paused);
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*/
modifier whenPaused() {
require(paused);
_;
}
/**
* @dev called by the owner to pause, triggers stopped state
*/
function pause() onlyOwner whenNotPaused public {
paused = true;
emit Pause();
}
/**
* @dev called by the owner to unpause, returns to normal state
*/
function unpause() onlyOwner whenPaused public {
paused = false;
emit Unpause();
}
}
library Address {
function toAddress(string memory _stringAddress) public pure returns (address _parsedAddress) {
bytes memory tmp = bytes(_stringAddress);
uint160 iaddr = 0;
uint160 b1;
uint160 b2;
for (uint i = 2; i < 2 + 2 * 20; i += 2) {
iaddr *= 256;
b1 = uint160(uint8(tmp[i]));
b2 = uint160(uint8(tmp[i + 1]));
if ((b1 >= 97) && (b1 <= 102)) {
b1 -= 87;
} else if ((b1 >= 65) && (b1 <= 70)) {
b1 -= 55;
} else if ((b1 >= 48) && (b1 <= 57)) {
b1 -= 48;
}
if ((b2 >= 97) && (b2 <= 102)) {
b2 -= 87;
} else if ((b2 >= 65) && (b2 <= 70)) {
b2 -= 55;
} else if ((b2 >= 48) && (b2 <= 57)) {
b2 -= 48;
}
iaddr += (b1 * 16 + b2);
}
return address(iaddr);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.10;
import "./SafeMath.sol";
import "./Library.sol";
/**
* @title JustswapExchange interface
* @dev see https://www.justswap.io/docs/justswap-interfaces_en.pdf
*/
interface IJustswapExchange {
event TokenPurchase(address indexed buyer, uint256 indexed trx_sold, uint256 indexed tokens_bought);
event TrxPurchase(address indexed buyer, uint256 indexed tokens_sold, uint256 indexed trx_bought);
function getInputPrice(uint256 input_amount, uint256 input_reserve, uint256 output_reserve) external view returns (uint256);
function getOutputPrice(uint256 output_amount, uint256 input_reserve, uint256 output_reserve) external view returns (uint256);
function trxToTokenSwapInput(uint256 min_tokens, uint256 deadline) external payable returns (uint256);
function trxToTokenTransferInput(uint256 min_tokens, uint256 deadline, address recipient) external payable returns(uint256);
function trxToTokenSwapOutput(uint256 tokens_bought, uint256 deadline) external payable returns(uint256);
function trxToTokenTransferOutput(uint256 tokens_bought, uint256 deadline, address recipient) external payable returns (uint256);
function getTrxToTokenInputPrice(uint256 trx_sold) external view returns (uint256);
function getTrxToTokenOutputPrice(uint256 tokens_bought) external view returns (uint256);
function getTokenToTrxInputPrice(uint256 tokens_sold) external view returns (uint256);
function getTokenToTrxOutputPrice(uint256 trx_bought) external view returns (uint256);
}
/**
* @title JustswapFactory interface
* @dev see https://www.justswap.io/docs/justswap-interfaces_en.pdf
*/
interface IJustswapFactory {
event NewExchange(address indexed token, address indexed exchange);
function initializeFactory(address template) external;
function createExchange(address token) external returns (address payable);
function getExchange(address token) external view returns (address payable);
function getToken(address token) external view returns (address);
function getTokenWihId(uint256 token_id) external view returns (address);
}
interface Oracle {
function getAssetValueUSD(address _asset, uint256 _amount) external view returns (uint256);
}
contract OracleJustswap is Oracle {
using SafeMath for uint256;
// Justswap factory, see https://www.justswap.io/docs/justswap-interfaces_en.pdf
IJustswapFactory public factory = IJustswapFactory(Address.toAddress("TXk8rQSAvPvBBNtqSoY6nCfsXWCSSpTVQF"));
// USDJ Exchange, see https://tronscan.org/#/token20/TMwFHYXLJaRUPeW6421aqXL4ZEzPRFGkGT
IJustswapExchange public USDT_Exchange = IJustswapExchange(factory.getExchange(
Address.toAddress("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t")));
function getAssetValueTRX(address _asset, uint256 _amount) public view returns (uint256) {
IJustswapExchange exchange = IJustswapExchange(factory.getExchange(_asset));
return exchange.getTokenToTrxInputPrice(_amount);
}
function getAssetValueUSD(address _asset, uint256 _amount) public view returns (uint256) {
return USDT_Exchange.getTrxToTokenInputPrice(getAssetValueTRX(_asset, _amount));
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.10;
/**
* @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) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
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-contracts/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) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message 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, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
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) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message 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, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment