Web3 solidity order pool operation

The previous article was set for some reasons to enter your visible needs. Friends who need it can private message me.
insert image description here
From the program we wrote before, the exchange is nothing more than a token custody, but it will be more professional. In
this article, let’s continue to look at one of the exchanges. function called swimming pool

For example, we exchange 100grToken for 1ETH
on the premise that our tokens can already be valued.
For example, if you want to use RMB against the US dollar, the exchange rate between them must be stipulated,
but our place itself is a simulation environment,
and it is not reasonable to operate directly.
So here we directly develop a swimming pool function or order pool

We can create orders by ourselves. For example, Xiao Ming released a list of you who are willing to exchange 100grToken for 1 ETH. Our
unfinished orders will be in our swimming pool. If someone thinks it is appropriate to click this confirmation, the background will operate and link him and the person who issued the order. The corresponding data is deducted to complete the order
, but it may be released by XX, but because of the exchange rate change, the previous one is not suitable, and it can be cancelled.

Here we need to create three functions
makeOrder to create an order
cancelOrder to cancel an order
fillorder to fill an order

It is still a standard process to start our ganache environment.
insert image description here
Then we still have a problem here. We can't find the number of tokens of the user well.
Then we need to add a balanceof function in Exchange.sol. We specify to query the corresponding address of the corresponding user under the tokens Value
Returns a value of uint256 digital type
and then sets the public function scope to be a public view setting. It just reads the information on our blockchain
insert image description here
and any of our orders need to contain the following information
id This is the most basic data structure Things must have an id to mark and delete the order.
The user who created the order must also judge the authority when it is displayed on the interface. If the current user can delete and manage the order,
then what token is used to exchange another token
for time storage order created by

These members must be necessary.
We have to sort out the positive thinking. We have no server or database, so your life is in the data structure of the chain, which is the data storage.
Then we declare such an order structure in the Exchange.sol contract.

 //订单结构体
 struct _Order{
    
    
     uint256 id;
     address user;
     
     address tokenGet;
     uint256 amountGet;

     address tokenGive;
     uint256 amountGive;

     uint256 timestamp;
 }

Here our data structure corresponds to the above two kinds of token quantity and address id, user address and time, but
insert image description here
this form obviously only supports us to create one order.
If we want to create more than one, this method must be increased.

The simpler way is naturally to use an array of type _Order

_Order[] orderlist;

But for the convenience of querying by id here, I choose to directly

mapping(uint256=> _Order) public orders;

Define a mapping object property and the key is a uint256 number type corresponding to the order id and the value is an _Order object.
insert image description here
Friends who have studied js should understand the difference between the two before.
The array is like this

[1,2,3]

And the object form we are using now is like this

{
    
    
    0: 1,
    1: 2,
    2: 3
}

But it’s nothing more than the key of our object. We use the id of the order.
We can find him in the form of object [order id]

Then we create a

uint256 public orderCount;

The number type is simply to record the quantity of the order
insert image description here
, and then we can write the
makeOrder below to create an order.
But after the content of the previous exchanges, I think everyone has developed a good habit of event recording
, so we also need to create an order here. To record
we define an event

event Order(uint256 id,address user,address tokenGet,uint256 amountGet,address tokenGive,uint256 amountGive,uint256 timestamp);

insert image description here
Then we write the makeOrder function

function makeOrder(address _tokenGet,uint256 _amountGet,address _tokenGive,uint256 _amountGive) public {
    
    
    orderCount = orderCount.add(1);
    orders[orderCount] = _Order(orderCount,msg.sender,_tokenGet,_amountGet,_tokenGive,_amountGive,block.timestamp);
    emit Order(orderCount,msg.sender,_tokenGet,_amountGet,_tokenGive,_amountGive,block.timestamp);
}

Then our parameter accepts the address and quantity of the two tokens, and then we get the orderCount and add one to it. For example, the first time we come in, it is 0, we add 1 to it, so that it is 1, and then we copy the current order id, which is orderCount second
. The orderCount of the second entry is 1, plus one, it becomes 2,
and then our users still use msg
.

After adding the data, call the Order we just wrote to record the order information.
Well, we don’t have the conditions for testing. In order to ensure that our code is ok, we execute it on the terminal.

truffle compile

insert image description here
Ok no syntax issues as for testing etc I got all the order operations done we are doing some more

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/132316003