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 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;
}
function stakeTokens(uint _amount) public{
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;
}
}
--------------------------------
//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')
})
})
})
-----------------------
//logs
Contract: TokenFarm
Mock Dai deployment
√ has a name (188ms)
Dapp Token deployment
√ has a name (126ms)
Token Farm deployment
√ has a name (95ms)
√ contract has tokens (155ms)
Farming tokens
√ rewards investors for staking mDai tokens (1107ms)
5 passing (3s)
reference;
No comments:
Post a Comment