...

/

Implementing Payments in the Auction

Implementing Payments in the Auction

Learn how to add payments support to the Auction smart contract.

Now that we’ve learned how to use payments in Solidy, we'll add payment support in the Auction smart contract. After this lesson, users of our smart contract will be able to send bids to the auction and terminate the auction once it is over.

Adding new fields

To implement payment support in our smart contract, we first need to add a few additional fields and methods to keep track of the state of the bidding process.

Press + to interact
// Total bid amount for each user
mapping(address => uint) public totalBids;
// Highest bidder in the auction
address public highestBidder;
// Total number of bids in the auction
uint public totalBidsNum;

The most important field here is the totalBids mapping that we'll use to keep track of the bid amounts for each user. All the users’ bids will be stored in the total Ether balance of the Auction smart contract, so we need this additional data structure to keep track of each user’s contribution amount. We'll also keep track of the account that made the largest bid in the highestBidder field and the total number of bids made during the auction in totalBidsNum.

We'll add two additional modifiers to enforce that some operations can be done only before the deadline, while others can only be executed after the deadline.

Press + to interact
modifier beforeDeadline {
require(block.timestamp <= deadline, "Deadline has passed");
_;
}
modifier afterDeadline {
require(block.timestamp > deadline, "Deadline has not yet passed");
_;
}

In both modifiers, we compare the current block’s timestamp with the auction deadline and ...