I'm trying to test making some smart-contract that uses DAI ERC20 tokens on Kovan testnet (DAI contract on kovan is 0xc4375b7de8af5a38a93548eb8453a498222c4ff2). I developed this very simple contract that is meant to just:
getContractBalance() - Contract balance of DAI
getSenderBalance () - Sender balance of DAI
- tranferTokenstoContract( uint _amount ) - transfer _amount DAI tokens from Sender to Contract
here is my code :
pragma solidity ^0.5.3;
// Import OpenZeppelin's ERC20 interface defenition
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
contract Example {
IERC20 public associatedToken;
// @param _token is the address of DAI in Kovan -> 0xc4375b7de8af5a38a93548eb8453a498222c4ff2
constructor(IERC20 _token) public {
associatedToken = _token;
}
function tranferTokenstoContract( uint256 _ammount) public returns (bool) {
return associatedToken.transfer(address(this), _ammount);
}
function getContractBalance() public view returns (uint256) {
return associatedToken.balanceOf(address(this));
}
function getSenderBalance() public view returns (uint256) {
return associatedToken.balanceOf(address(msg.sender));
}
}
I'm using injected Web3 with metamask and I have 82 DAI tokens in it (Kovan testnet). Whenever I call getSenderBalance()
, it works fine and returns 82000000000000000000 as expected. Although when I call tranferTokenstoContract()
and try to send some tokens to the contract, I get a "Gas estimation errored" warning and my transaction always fails. I'm using Remix, but I also tried it on MyEtherWallet. Both unsuccessful. Also I tried moving around with the gas but I was not successful either.
Am I doing this right? Feel free to try it yourself. Just pass in 0xc4375b7de8af5a38a93548eb8453a498222c4ff2
to the constructor, which is the DAI contract on the Kovan network
I would appreciate any help.