investor deposit 100mDai and earned 100Dapp
investor earned another 100Dapp
investor withdraw principle 100mDai, interest 200Dapp left in token farm
//cmdtruffle exec scripts/issue-token.js
------------------------
//scripts/issue-token.js
const TokenFarm = artifacts.require('TokenFarm')
module.exports = async function (callback) {
let tokenFarm = await TokenFarm.deployed()
await tokenFarm.issueTokens()
console.log("Tokens issued!")
callback()
}
------------------------
//src/contracts/TokenFarm.sol
pragma solidity ^0.5.0;
import "./DappToken.sol";
import "./DaiToken.sol";
contract TokenFarm{
string public name = "Dapp Token Farm";
DappToken public dappToken;
DaiToken public daiToken;
address public owner;
address[] public stakers;
mapping(address => uint) public stakingBalance;
mapping(address => bool) public hasStaked;
mapping(address => bool) public isStaking;
constructor(DappToken _dappToken, DaiToken _daiToken) public{
dappToken = _dappToken;
daiToken = _daiToken;
owner = msg.sender;
}
function stakeTokens(uint _amount) public{
require(_amount > 0, "amount greater than 0");
daiToken.transferFrom(msg.sender, address(this), _amount);
stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;
if(!hasStaked[msg.sender]){
stakers.push(msg.sender);
}
isStaking[msg.sender] = true;
hasStaked[msg.sender] = true;
}
function unstakeTokens() public {
uint balance = stakingBalance[msg.sender];
require(balance > 0, "staking balance greater than 0");
daiToken.transfer(msg.sender, balance);
stakingBalance[msg.sender] = 0;
isStaking[msg.sender] = false;
}
function issueTokens() public {
require(msg.sender == owner, "caller must be the owner");
for (uint i=0; i<stakers.length; i++){
uint balance = stakingBalance[stakers[i]];
if(balance > 0){
dappToken.transfer(stakers[i], balance);
}
}
}
}
reference:
No comments:
Post a Comment