forked from jklepatch/eattheblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
5,046 additions
and
71 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
...liquidity-mining/contracts/BonusToken.sol → ...dity-mining/contracts/GovernanceToken.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
...elopment-mastery/1-defi-building-blocks/10-liquidity-mining/contracts/LiquidityMining.sol
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
...evelopment-mastery/1-defi-building-blocks/10-liquidity-mining/contracts/LiquidityPool.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
pragma solidity ^0.7.3; | ||
|
||
import './UnderlyingToken.sol'; | ||
import './LpToken.sol'; | ||
import './GovernanceToken.sol'; | ||
|
||
contract LiquidityPool is LpToken { | ||
mapping(address => uint) public checkpoints; | ||
UnderlyingToken public underlyingToken; | ||
GovernanceToken public governanceToken; | ||
uint constant public REWARD_PER_BLOCK = 1; | ||
|
||
constructor(address _underlyingToken, address _governanceToken) { | ||
underlyingToken = UnderlyingToken(_underlyingToken); | ||
governanceToken = GovernanceToken(_governanceToken); | ||
} | ||
|
||
function deposit(uint amount) external { | ||
if(checkpoints[msg.sender] == 0) { | ||
checkpoints[msg.sender] = block.number; | ||
} | ||
_distributeRewards(msg.sender); | ||
underlyingToken.transferFrom(msg.sender, address(this), amount); | ||
_mint(msg.sender, amount); | ||
} | ||
|
||
function withdraw(uint amount) external { | ||
require(balanceOf(msg.sender) >= amount, 'not enough lp token'); | ||
_distributeRewards(msg.sender); | ||
underlyingToken.transfer(msg.sender, amount); | ||
_burn(msg.sender, amount); | ||
} | ||
|
||
function _distributeRewards(address beneficiary) internal { | ||
uint checkpoint = checkpoints[beneficiary]; | ||
if(block.number - checkpoint > 0) { | ||
uint distributionAmount = (balanceOf(beneficiary) * (block.number - checkpoint) * REWARD_PER_BLOCK); | ||
governanceToken.mint(beneficiary, distributionAmount); | ||
checkpoints[beneficiary] = block.number; | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...velopment-mastery/1-defi-building-blocks/10-liquidity-mining/contracts/LiquidityPool2.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
pragma solidity ^0.7.3; | ||
|
||
import './UnderlyingToken.sol'; | ||
import './LpToken.sol'; | ||
import './GovernanceToken.sol'; | ||
|
||
//More advanced distribution mechanism with a fixed amount of reward per block | ||
//for the whole contract | ||
//WIP | ||
contract LiquidityPool2 is LpToken { | ||
struct Checkpoint { | ||
uint blockNumber; | ||
uint avgTotalBalance; | ||
} | ||
|
||
mapping(address => Checkpoint) public checkpoints; | ||
Checkpoint public globalCheckpoint; | ||
|
||
uint constant public REWARD_PER_BLOCK = 1000 * 10 ** 18; | ||
|
||
UnderlyingToken public underlyingToken; | ||
GovernanceToken public governanceToken; | ||
uint public genesisBlock; | ||
|
||
constructor(address _underlyingToken, address _governanceToken) { | ||
underlyingToken = UnderlyingToken(_underlyingToken); | ||
governanceToken = GovernanceToken(_governanceToken); | ||
globalCheckpoint.blockNumber = block.number; | ||
genesisBlock = block.number; | ||
} | ||
|
||
function deposit(uint amount) external { | ||
globalCheckpoint.avgTotalBalance = _getAvgTotalBalance(); | ||
globalCheckpoint.blockNumber = block.number; | ||
|
||
_distributeRewards(msg.sender); | ||
|
||
underlyingToken.transferFrom(msg.sender, address(this), amount); | ||
_mint(msg.sender, amount); | ||
|
||
globalCheckpoint.avgTotalBalance = _getAvgTotalBalance(); | ||
checkpoints[msg.sender].avgTotalBalance = globalCheckpoint.avgTotalBalance; | ||
checkpoints[msg.sender].blockNumber = block.number; | ||
} | ||
|
||
function withdraw(uint amount) external { | ||
require(balanceOf(msg.sender) >= amount, 'not enough lp token'); | ||
|
||
globalCheckpoint.avgTotalBalance = _getAvgTotalBalance(); | ||
globalCheckpoint.blockNumber = block.number; | ||
|
||
_distributeRewards(msg.sender); | ||
|
||
checkpoints[msg.sender].avgTotalBalance = globalCheckpoint.avgTotalBalance; | ||
checkpoints[msg.sender].blockNumber = block.number; | ||
|
||
underlyingToken.transfer(msg.sender, amount); | ||
_burn(msg.sender, amount); | ||
|
||
globalCheckpoint.avgTotalBalance = _getAvgTotalBalance(); | ||
checkpoints[msg.sender].avgTotalBalance = globalCheckpoint.avgTotalBalance; | ||
checkpoints[msg.sender].blockNumber = block.number; | ||
} | ||
|
||
function _getAvgTotalBalance() public view returns(uint) { | ||
if(block.number - genesisBlock == 0) { | ||
return totalSupply(); | ||
} | ||
return (globalCheckpoint.avgTotalBalance * (globalCheckpoint.blockNumber - genesisBlock) + totalSupply() * (block.number - globalCheckpoint.blockNumber)) / (block.number - genesisBlock); | ||
} | ||
|
||
function _distributeRewards(address beneficiary) internal { | ||
Checkpoint storage userCheckpoint = checkpoints[beneficiary]; | ||
if(block.number - userCheckpoint.blockNumber > 0) { | ||
uint avgTotalBalanceRewardPeriod = (globalCheckpoint.avgTotalBalance * globalCheckpoint.blockNumber - userCheckpoint.avgTotalBalance * userCheckpoint.blockNumber) / (block.number - userCheckpoint.blockNumber); | ||
if(avgTotalBalanceRewardPeriod > 0) { | ||
uint distributionAmount = (balanceOf(beneficiary) * (block.number - userCheckpoint.blockNumber) * REWARD_PER_BLOCK) / avgTotalBalanceRewardPeriod; | ||
governanceToken.mint(beneficiary, distributionAmount); | ||
} | ||
} | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
defi-development-mastery/1-defi-building-blocks/10-liquidity-mining/contracts/LpToken.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pragma solidity ^0.7.2; | ||
pragma solidity ^0.7.3; | ||
|
||
import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; | ||
|
||
|
11 changes: 11 additions & 0 deletions
11
...elopment-mastery/1-defi-building-blocks/10-liquidity-mining/contracts/UnderlyingToken.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pragma solidity ^0.7.3; | ||
|
||
import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; | ||
|
||
contract UnderlyingToken is ERC20 { | ||
constructor() ERC20('Underlying Token', 'UTK') {} | ||
|
||
function faucet(address to, uint amount) external { | ||
_mint(to, amount); | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
...pment-mastery/1-defi-building-blocks/10-liquidity-mining/migrations/2_deploy_contracts.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.