Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MyContract.sol
- contract IErc20 {
- function transferFrom(address, address, uint) public returns (bool);
- }
- contract MyContract {
- IErc20 public token;
- constructor(address _token) {
- token = IErc20(_token);
- }
- function deposit(uint _amount) public {
- require(token.transferFrom(msg.sender, address(this), _amount);
- }
- }
- //MyContract.test.js
- const { BN } = require("openzeppelin-test-helpers");
- const MyContract = artifacts.require("MyContract");
- const ERC20Mock = artifacts.require("ERC20Mock");
- contract("MyContract", function([_, adminRole, user]) {
- const dai = toBytes("DAI");
- let daiAddress;
- beforeEach(async function () {
- daiAddress = (await web3.eth.getAccounts())[5];
- this.MyContract = await MyContract.new(
- dai,
- daiAddress
- );
- });
- this.daiToken = ERC20Mock.deployed();
- });
- describe("deposit()", function() {
- const amount = new BN(1);
- it("should deposit if tokens are approved by user", async function () {
- await this.daiToken.approve(this.MyContract.address, amount, {from: user});
- await this.MyContract.deposit(amount, {from: user}); //REVERTS!
- });
- });
- });
- // migrations/deploy_dai.js
- const ERC20Mock = artifacts.require("ERC20Mock");
- module.exports = async function(deployer, network, accounts) {
- if (network === "development") {
- console.log("Deploying MockDAI on Development");
- await deployer.deploy(ERC20Mock, accounts[5], 100*(10**6), {from: accounts[5]);
- }
- }
- //ERC20Mock.sol
- pragma solidity ^0.5.0;
- import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
- // mock class using ERC20
- contract ERC20Mock is ERC20 {
- constructor (address initialAccount, uint256 initialBalance) public {
- _mint(initialAccount, initialBalance);
- }
- function mint(address account, uint256 amount) public {
- _mint(account, amount);
- }
- function burn(address account, uint256 amount) public {
- _burn(account, amount);
- }
- function burnFrom(address account, uint256 amount) public {
- _burnFrom(account, amount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement