0

I have a function that have a modifier that it must be the owner before it can cancel the order and I'm using the nested mapping that i've created for this struct as below

//mapping
mapping(bytes32 => mapping(uint256 => Order)) private s_orders;
mapping(bytes32 => mapping(uint256 => bool)) public s_orderCancelled;
//struct
struct Order {
        uint256 id;
        address trader;
        Status status;
        bytes32 ticker;
        uint256 amount;
        uint256 filled;
        uint256 price;
        uint256 date;
    }
//function
function cancelOrder(bytes32 _ticker, uint256 _id) external {
        Order storage orders = s_orders[_ticker][_id];

        if (address(orders.trader) != msg.sender) revert Exchange__NotOwner();
        if (orders.id == _id) revert Exchange__InvalidId();

        s_orderCancelled[_ticker][_id] = true;
    }

And I'm getting an error reverted with custom error 'Exchange__NotOwner() with my test script below:

 beforeEach(async () => {
          await dex.connect(trader1).depositToken(DAI, amount);

          transaction = await dex
            .connect(trader1)
            .createLimitOrder(REP, tradeAmount, price1, Status.BUY);
          result = await transaction.wait();

          transaction = await dex.connect(trader1).cancelOrder(REP, 1);
          result = await transaction.wait();

          await dex.connect(trader2).depositToken(REP, amount);

          transaction = await dex
            .connect(trader2)
            .createMarketOrder(REP, tradeAmount, Status.SELL);
          result = await transaction.wait();

          transaction = await dex.connect(trader2).cancelOrder(REP, 2);
          result = await transaction.wait();
        });

        it("updates cancelled orders", async () => {
          expect(await dex.s_orderCancelled(REP, 1)).to.equal(true);
          expect(await dex.s_orderCancelled(REP, 2)).to.equal(true);
        });
      });

not sure which syntax is wrong here

1 Answer 1

0

you get Exchange__NotOwner() because you did not send your transaction from a specific address, for example, here I send the transition with account[1]:

sampleTranaction = await this.sampleContract.myFunc(
arg1,
arg2,
{ from: account[1] }

attention please before this you must set address for your account[1] as like your contract msg.sender.

3
  • I've added the from syntax like this transaction = await dex .connect(trader1) .createLimitOrder(REP, tradeAmount, price1, Status.BUY, { from: trader1, });. but its giving me invalid address or ENS name
    – 0xhaz
    Commented Oct 31, 2022 at 16:23
  • are you sure about declaring the true address before usage? Commented Nov 1, 2022 at 5:58
  • I've declared it like this account = await ethers.getSigners(); trader1 = account[1]; but it still giving me the same revert error transaction = await dex .connect(trader1) .createLimitOrder(REP, tradeAmount, price1, Status.BUY, { from: trader1.address });
    – 0xhaz
    Commented Nov 1, 2022 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.