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);
}
}
}
}
--------------------------------
//test/TokenFarm.test.js
const { assert } = require('chai')
const DappToken = artifacts.require('DappToken')
const DaiToken = artifacts.require('DaiToken')
const TokenFarm = artifacts.require('TokenFarm')
require('chai')
.use(require('chai-as-promised'))
.should()
function tokens(n) {
return web3.utils.toWei(n, 'Ether')
}
contract('TokenFarm', ([owner, investor]) => {
let daiToken, dappToken, tokenFarm
before(async () => {
daiToken = await DaiToken.new()
dappToken = await DappToken.new()
tokenFarm = await TokenFarm.new(dappToken.address, daiToken.address)
await dappToken.transfer(tokenFarm.address, tokens('1000000'))
await daiToken.transfer(investor, tokens('100'), { from: owner })
})
describe('Mock Dai deployment', async () => {
it('has a name', async () => {
const name = await daiToken.name()
assert.equal(name, 'Mock DAI Token')
})
})
describe('Dapp Token deployment', async () => {
it('has a name', async () => {
const name = await dappToken.name()
assert.equal(name, 'DApp Token')
})
})
describe('Token Farm deployment', async () => {
it('has a name', async () => {
const name = await tokenFarm.name()
assert.equal(name, 'Dapp Token Farm')
})
it('contract has tokens', async () => {
let balance = await dappToken.balanceOf(tokenFarm.address)
assert.equal(balance.toString(), tokens('1000000'))
})
})
describe('Farming tokens', async () => {
it('rewards investors for staking mDai tokens', async () => {
let result
result = await daiToken.balanceOf(investor)
assert.equal(result.toString(), tokens('100', 'investor Mock Dai wallet balance correct before staking'))
await daiToken.approve(tokenFarm.address, tokens('100'), { from: investor })
await tokenFarm.stakeTokens(tokens('100'), { from: investor })
result = await daiToken.balanceOf(investor)
assert.equal(result.toString(), tokens('0'), 'investor Mock DAI wallet balance correct after staking')
result = await tokenFarm.stakingBalance(investor)
assert.equal(result.toString(), tokens('100'), 'investor staking balance correct after staking')
result = await tokenFarm.isStaking(investor)
assert.equal(result.toString(), 'true', 'investor staking status correct after staking')
await tokenFarm.issueTokens({ from: owner })
result = await dappToken.balanceOf(investor)
assert.equal(result.toString(), tokens('100'), 'investor DApp Token wallet ballance correct after staking')
await tokenFarm.issueTokens({ from: investor }).should.be.rejected;
await tokenFarm.unstakeTokens({ from: investor })
result = await daiToken.balanceOf(investor)
assert.equal(result.toString(), tokens('100'), 'investor Mock DAI wallet balance correct after unstaking')
result = await daiToken.balanceOf(tokenFarm.address)
assert.equal(result.toString(), tokens('0'), 'Token Farm Mock DAI wallet balance correct after unstaking')
result = await tokenFarm.stakingBalance(investor)
assert.equal(result.toString(), tokens('0'), 'investor staking balance correct after unstaking')
result = await tokenFarm.isStaking(investor)
assert.equal(result.toString(), 'false', 'investor staking status correct after unstaking')
})
})
})
--------------------------------------
//logs
Contract: TokenFarm
Mock Dai deployment
√ has a name (243ms)
Dapp Token deployment
√ has a name (68ms)
Token Farm deployment
√ has a name (91ms)
√ contract has tokens (101ms)
Farming tokens
√ rewards investors for staking mDai tokens (3323ms)
5 passing (6s)
reference:
No comments:
Post a Comment