Assertion Error Invalid number of parameters

0 passing (5s)
  1 failing

  1) Contract: Dex
       should only be possible for owner to add tokens:
     AssertionError: Failed with Error: Invalid number of parameters for "addToken". Got 3 expected 2!
      at passes (node_modules/truffle-assertions/index.js:142:11)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at Context.<anonymous> (test/walletTest.js:9:9)

const Dex = artifacts.require("Dex");
const Link = artifacts.require("Link");
const truffleAssert = require('truffle-assertions');

contract("Dex", accounts => {
     it("should only be possible for owner to add tokens", async () => {
        let dex = await Dex.deployed();
        let link = await Link.deployed();
        await truffleAssert.passes(
               dex.addToken(web3.utils.fromUtf8("Link"), link.address, {form: accounts[0]})
        )
        await truffleAssert.reverts(
         dex.addToken(web3.utils.fromUtf8("Link"), link.address, {form: accounts[1]})
  )
     })
}) 

I understand that the line:

dex.addToken(web3.utils.fromUtf8("Link"), link.address, {form: accounts[0]})
has 3 parameters given in it but I copied exactly what was on the course yet Filips code passes. Does anyone have an idea what to do?

cheers

1 Like

Could you please share your contract too?

Carlos Z

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

contract Wallet is Ownable{
using SafeMath for uint256;

modifier tokenExist(bytes32 ticker){
    require(tokenMapping[ticker].tokenAddress != address(0));
    _;
}

struct Token{
    bytes32 ticker;
    address tokenAddress;
}

mapping(bytes32 => Token)public tokenMapping;
mapping(address => mapping(bytes32 => uint256))public balances;

bytes32[]public tokenList;

function addToken(bytes32 ticker, address tokenAddress) external onlyOwner {
    tokenMapping[ticker] = Token(ticker, tokenAddress);
    tokenList.push(ticker);
}

function deposit(uint256 amount, bytes32 ticker )external {
    IERC20(tokenMapping[ticker].tokenAddress).transferFrom(msg.sender, address(this), amount);
    balances[msg.sender][ticker] = balances[msg.sender][ticker].add(amount);

}

function withdraw(bytes32 ticker, uint256 amount)external tokenExist(ticker){
    require(balances[msg.sender][ticker] >= amount);

    balances[msg.sender][ticker] = balances[msg.sender][ticker].sub(amount);
    IERC20(tokenMapping[ticker].tokenAddress).transfer(msg.sender, amount);
}

}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Link is ERC20 {

    constructor() ERC20("Chainlink", "Link") {
        _mint(msg.sender, 1000);
    }

}




// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
pragma experimental ABIEncoderV2;

import "./Wallet.sol";

contract Dex is Wallet {
     
      enum Side { 
        BUY,
        SELL
     }

     struct Order {
        uint id;
        address trader;
        bool buyOrder;
        bytes32 ticker;
        uint amount;
        uint price;
     }

     mapping(bytes32 => mapping(uint => Order[]))public orderBook;

     function getOrderBook(bytes32 ticker, Side side)view public returns(Order[] memory){
          return orderBook[ticker][uint(side)];
     }

    

    /*function createLimitOrder()public{

    }*/


}

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Migrations {
  address public owner = msg.sender;
  uint public last_completed_migration;

  modifier restricted() {
    require(
      msg.sender == owner,
      "This function is restricted to the contract's owner"
    );
    _;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }
}

const Link = artifacts.require("Link");
const Dex = artifacts.require("Dex");

module.exports = async function (deployer, network, accounts) {
  await deployer.deploy(Link);
  let dex = await Dex.deployed()
  let link = await Link.deployed()
  await link.approve(dex.address, 500)
  dex.addToken(web3.utils.fromUtf8("Link"), link.address)
  /*await dex.deposit(100, web3.utils.fromUtf8("Link")) */  
  let balanceOfLink = await dex.balances(accounts[0], web3.utils.fromUtf8("Link"));
  console.log(balanceOfLink);
};

const Link = artifacts.require("Link");
const Dex = artifacts.require("Dex");

module.exports = async function (deployer, network, accounts) {
  await deployer.deploy(Link);
  let dex = await Dex.deployed()
  let link = await Link.deployed()
  await link.approve(dex.address, 500)
  dex.addToken(web3.utils.fromUtf8("Link"), link.address)
  /*await dex.deposit(100, web3.utils.fromUtf8("Link")) */  
  let balanceOfLink = await dex.balances(accounts[0], web3.utils.fromUtf8("Link"));
  console.log(balanceOfLink);
};

Hi Carlos,

Have you had time to look at this issue?

Kind regards,
Oscar

1 Like

Update**

I’ve managed to get the test to pass but only when commenting out the deposit call in the token migration file.

await dex.deposit(100, web3.utils.fromUtf8("LINK"))

Here is the full file:

const Link = artifacts.require("Link");
const Dex = artifacts.require("Dex");

module.exports = async function(deployer, network, accounts) {
  await deployer.deploy(Link);
  let dex = await Dex.deployed()
  let link = await Link.deployed()
  await link.approve(dex.address, 500)
  dex.addToken(web3.utils.fromUtf8("LINK"), link.address)
  await dex.deposit(100, web3.utils.fromUtf8("LINK"))   
  let balanceOfLink = await dex.balances(accounts[0], web3.utils.fromUtf8("LINK"));
  console.log(balanceOfLink);
1 Like

how do i include the line of code so that it all works together?

1 Like

Sorry for the late reply man, i might suggest to keep the migrations files as default by only deploying the contract, any other action like approve or addToken can be made on the unit tests.

You could take a look at my old project, im not using truffleAssertions because further in the course is not need it any more, but off course is good to know.

https://github.com/thecil/Project-Dex

Carlos Z.