Web3 solidity writes the cancellation order function of cancelorder and sorts out the logic of telling

In the above Web3 solidity order pool operation, we described the basic concept of the order pool and manually wrote the operation of creating an order.
Recently, we still start the ganache environment first,
insert image description here
and then we open the project.
insert image description here
Above we wrote the function of makeOrder to create an order,
but it is also It brings up a question, how can we kill it if we don’t want it after we create it?
We have splice in js
but not in solidity

It only has push pop and can only delete the last one,
or it is a form similar to deleting an object and directly deletes it with delete, but after the deletion, this position will be vacant, and we need to move the back to move forward

As for this article, we won’t talk about this operation
because we can’t let him appear on the interface again when we cancel the order, but there is no need to delete him because we need to record
apps like our Jingdong Meituan, you cancel the order but you are in your own Canceled orders can still be seen in the order

So whether it is completed, created or cancelled, it will exist in our orders,
insert image description here
so we can add a field
insert image description here
type to _Order. If we use the uint256 digital type to facilitate our enumeration operations,
the name is orderState, which is convenient for everyone. As the name implies,
we can completely Can be marked as 0 Created 1 Completed 2 Canceled

In fact, many foreigners like to directly build an array to directly store canceled orders. Anyway, this thing is mainly a solution. Everyone has their own way of writing.

Here we don't want this orderState,
let's define one above

mapping(uint256=>bool) public orderCancel;

We create a mapping object, the key is a number type to store the id of the order, and the value is a Boolean type to store whether it has been deleted or
insert image description here
not. Next, we will write this cancelorder function to cancel the order.
Write the code as follows

//取消订单  方法接受一个参数  订单id  代理名叫  _id
function cancelorder(uint256 _id) public {
    
    
    //根据传进来的订单id 获取到当前订单的对象
    _Order memory myorder = orders[_id];
    //判断id有没有取错
    require(myorder.id == _id);
    //将删除的数据的id 存入orderCancel  值赋值为 true 表示已经取消
    orderCancel[_id] = true;
    //最后 调用函数记录一下取消订单的事件
    emit Cancel(myorder.id,msg.sender,myorder.tokenGet,myorder.amountGet,myorder.tokenGive,
    myorder.amountGive,block.timestamp);
}

This function accepts an id, you tell me which order you want to cancel, right? I want us to delete the function. If you don’t give any conditions, it’s useless. Then we use the id to find the corresponding id
in the orders collection we wrote before. After finding out the objects,
we use require to judge whether their ids are the same to avoid problems with the things we took out.
Then we record a data key for the orderCancel just created. The id value of the order is true, indicating that it has been cancelled.

Then we finally call Cancel to record the event of canceling the order.
The parameter here uses myorder, which is the order object we found in the collection through the id,
but here our Cancel has not been written yet.

Here we directly create an exactly the same one called Cancel under Order,
which is also very simple. Anyway, our event is named randomly.
insert image description here
Finally, we still don’t have a complete environment test for the time being. At least now we can see if there is any problem with the grammar.
We execute it on the terminal.

truffle compile

insert image description here
Compiled at least now there is no problem with the syntax

Supongo que te gusta

Origin blog.csdn.net/weixin_45966674/article/details/132650583
Recomendado
Clasificación