Hi All,
I am also facing a weird problem. Absolutely clueless, why truffle is not recognising couple of functions in my wallet.sol. I have defined the depositEth and withdrawEth functions in my wallet.sol. My dex contract is inheriting from the wallet. And I am calling these two functions from my wallettest.js.
But truffle is telling me:
TypeError: dex.depositEth is not a function
TypeError: dex.withdrawEth is not a function
The screenshot of the error is as below:
My codes are as below:
wallet.sol
pragma solidity >=0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../node_modules/@openzeppelin/contracts/utils/math/SafeMath.sol";
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
contract Wallet is Ownable{
using SafeMath for uint256;
struct Token{
bytes32 ticker;
address tokenAddress;
}
mapping (bytes32 => Token) public tokenMapping;
bytes32[] public tokenList;
mapping (address => mapping(bytes32 => uint256)) public balances;
modifier tokenExists(bytes32 _ticker){
require(tokenMapping[_ticker].tokenAddress != address(0), 'Token does not exist');
_;
}
function addToken(bytes32 _ticker, address _tokenAddress) onlyOwner external {
tokenMapping[_ticker] = Token(_ticker, _tokenAddress);
tokenList.push(_ticker);
}
function deposit(uint _amount, bytes32 _ticker) tokenExists(_ticker) external {
IERC20(tokenMapping[_ticker].tokenAddress).transferFrom(msg.sender, address(this), _amount);
balances[msg.sender][_ticker] = balances[msg.sender][_ticker].add(_amount);
}
function withdraw(uint amount, bytes32 ticker) tokenExists(ticker) external {
require(balances[msg.sender][ticker] >= amount, "Balance is not sufficient");
balances[msg.sender][ticker] = balances[msg.sender][ticker].sub(amount);
IERC20(tokenMapping[ticker].tokenAddress).transfer(msg.sender, amount);
}
function depositEth() payable external {
balances[msg.sender][bytes32("ETH")] = balances[msg.sender][bytes32("ETH")].add(msg.value);
}
function withdrawEth(uint amount) external {
require(balances[msg.sender][bytes32("ETH")] >= amount, "Balance is not sufficient");
balances[msg.sender][bytes32("ETH")] = balances[msg.sender][bytes32("ETH")].sub(amount);
msg.sender.call{value:amount}("");
}
}
token.sol
pragma solidity >=0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Link is ERC20 {
constructor() ERC20("ChainLink", "LINK") public {
_mint(msg.sender, 1000);
}
}
wallettest.js
const Dex = artifacts.require("Dex")
const Link = artifacts.require("Link")
const truffleAssert = require('truffle-assertions');
contract ("Dex", accounts =>{
it("should be possible only for owner to add token", async () =>{
let dex = await Dex.deployed()
let link = await Link.deployed()
await truffleAssert.passes(
dex.addToken(web3.utils.fromUtf8("LINK"), link.address, {from: accounts[0]})
)
await truffleAssert.reverts(
dex.addToken(web3.utils.fromUtf8("LINK"), link.address, {from: accounts[1]})
)
})
it("should handle deposits correctly", async () =>{
let dex = await Dex.deployed()
let link = await Link.deployed()
await link.approve(dex.address, 500)
await dex.deposit(100, web3.utils.fromUtf8("LINK"))
let balance = await dex.balances(accounts[0], web3.utils.fromUtf8("LINK"));
assert.equal( balance.toNumber(),100)
})
it("should handle faulty withdrawals correctly", async () =>{
let dex = await Dex.deployed()
let link = await Link.deployed()
await truffleAssert.reverts(dex.withdraw(500, web3.utils.fromUtf8("LINK")))
})
it("should handle correct withdrawals correctly", async () =>{
let dex = await Dex.deployed()
let link = await Link.deployed()
await truffleAssert.passes(dex.withdraw(100, web3.utils.fromUtf8("LINK")))
})
it("should handle correct withdrawals correctly", async () =>{
let dex = await Dex.deployed()
let link = await Link.deployed()
await truffleAssert.passes(dex.withdraw(100, web3.utils.fromUtf8("LINK")))
})
it("should have enough balance to place the order", async () =>{
let dex = await Dex.deployed()
let link = await Link.deployed()
let balance = await dex.balances(accounts[0], web3.utils.fromUtf8("LINK"));
await truffleAssert.passes(dex.withdraw(100, web3.utils.fromUtf8("LINK")))
})
it("should deposit correct amount of Eth", async () =>{
let dex = await Dex.deployed()
let link = await Link.deployed()
let balance = await dex.balances(accounts[0], web3.utils.fromUtf8("ETH"));
dex.depositEth({value: 1000});
assert.equal(balancenew.toNumber(), 1000);
})
it("should withdraw correct amount of Eth", async () =>{
let dex = await Dex.deployed()
let link = await Link.deployed()
let balance = await dex.balances(accounts[0], web3.utils.fromUtf8("ETH"));
dex.withdrawEth(500);
let balancenew = await dex.balances(accounts[0], web3.utils.fromUtf8("ETH"));
assert.equal(balancenew.toNumber(), (balance.toNumber() - 500));
})
it("should not allow over deposition of Eth", async () =>{
let dex = await Dex.deployed()
let link = await Link.deployed()
})
})
Please, can somebody help?
Thanks