Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. //MyContract.sol
  2.  
  3. contract IErc20 {
  4. function transferFrom(address, address, uint) public returns (bool);
  5. }
  6.  
  7. contract MyContract {
  8. IErc20 public token;
  9.  
  10. constructor(address _token) {
  11. token = IErc20(_token);
  12. }
  13.  
  14. function deposit(uint _amount) public {
  15. require(token.transferFrom(msg.sender, address(this), _amount);
  16. }
  17. }
  18.  
  19. //MyContract.test.js
  20.  
  21. const { BN } = require("openzeppelin-test-helpers");
  22. const MyContract = artifacts.require("MyContract");
  23. const ERC20Mock = artifacts.require("ERC20Mock");
  24.  
  25. contract("MyContract", function([_, adminRole, user]) {
  26. const dai = toBytes("DAI");
  27. let daiAddress;
  28. beforeEach(async function () {
  29. daiAddress = (await web3.eth.getAccounts())[5];
  30. this.MyContract = await MyContract.new(
  31. dai,
  32. daiAddress
  33. );
  34. });
  35. this.daiToken = ERC20Mock.deployed();
  36. });
  37. describe("deposit()", function() {
  38. const amount = new BN(1);
  39. it("should deposit if tokens are approved by user", async function () {
  40. await this.daiToken.approve(this.MyContract.address, amount, {from: user});
  41. await this.MyContract.deposit(amount, {from: user}); //REVERTS!
  42. });
  43. });
  44. });
  45.  
  46. // migrations/deploy_dai.js
  47.  
  48. const ERC20Mock = artifacts.require("ERC20Mock");
  49.  
  50. module.exports = async function(deployer, network, accounts) {
  51. if (network === "development") {
  52. console.log("Deploying MockDAI on Development");
  53. await deployer.deploy(ERC20Mock, accounts[5], 100*(10**6), {from: accounts[5]);
  54. }
  55. }
  56.  
  57. //ERC20Mock.sol
  58.  
  59. pragma solidity ^0.5.0;
  60.  
  61. import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
  62.  
  63. // mock class using ERC20
  64. contract ERC20Mock is ERC20 {
  65. constructor (address initialAccount, uint256 initialBalance) public {
  66. _mint(initialAccount, initialBalance);
  67. }
  68.  
  69. function mint(address account, uint256 amount) public {
  70. _mint(account, amount);
  71. }
  72.  
  73. function burn(address account, uint256 amount) public {
  74. _burn(account, amount);
  75. }
  76.  
  77. function burnFrom(address account, uint256 amount) public {
  78. _burnFrom(account, amount);
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement