Hi all,
I have a function to cancel an order but I’m stuck on retrieving the value from nested mapping. I understand that mapping are not iterable , so I’ve created a new mapping to get the unique id from my struct as per suggestion from this link :
But it is still not working when I tested it.
Below are the current code for my SC:
// struct
struct Order {
uint256 id;
address trader;
Status status;
bytes32 ticker;
uint256 amount;
uint256 filled;
uint256 price;
uint256 date;
}
// mapping
mapping(bytes32 => mapping(uint256 => Order[])) public s_orderBook;
// to cancel the order based ticker and id
mapping(bytes32 => mapping(uint256 => bool)) public s_orderCancelled;
// this mapping to retrieve the id from struct
mapping(uint256 => uint256) private s_groupIndexMapping;
function createLimitOrder(
bytes32 _ticker,
uint256 _amount,
uint256 _price,
Status status
) external {
Order[] storage orders = s_orderBook[_ticker][uint(status)];
orders.push(
Order(
s_nextOrderId,
msg.sender,
status,
_ticker,
_amount,
0,
_price,
block.timestamp
)
);
// this will be the id to use to retrieve the order id
s_groupIndexMapping[s_nextOrderId] = orders.length - 1;
}
function cancelOrder(bytes32 _ticker, uint256 _id)
public
{
uint orderId = s_groupIndexMapping[_id];
// cancel the order id based on the save index mapping
s_orderCancelled[_ticker][orderId] = true;
}
but somehow my test are still returning false, any suggestions are greatly appreciated
My test script as below
describe("Order Actions", async () => {
let amount = tokens(100);
let tradeAmount = tokens(1);
let price1 = 9;
let transaction: any, result: any;
beforeEach(async () => {
await dex.connect(trader1).depositToken(DAI, amount);
await dex.connect(trader1).depositToken(REP, amount);
transaction = await dex
.connect(trader1)
.createLimitOrder(REP, tradeAmount, price1, Status.BUY);
result = await transaction.wait();
await dex.connect(trader2).depositToken(DAI, amount);
await dex.connect(trader2).depositToken(REP, amount);
transaction = await dex
.connect(trader2)
.createMarketOrder(REP, tradeAmount, Status.SELL);
result = await transaction.wait();
});
describe("Cancelling Orders", async () => {
describe("Success", () => {
beforeEach(async () => {
transaction = await dex.connect(trader1).cancelOrder(REP, 1);
result = await transaction.wait();
transaction = await dex.connect(trader2).cancelOrder(REP, 2);
result = await transaction.wait();
// console.log(result);
});
it("updates canceled orders", async () => {
expect(await dex.s_orderCancelled(REP, 1)).to.equal(true);
expect(await dex.s_orderCancelled(REP, 2)).to.equal(true);
});
});