Skip to content

Commit

Permalink
Adding tests for the Simple Auction smart contract. (#532)
Browse files Browse the repository at this point in the history
* Adding tests for the Simple Auction smart contract.
---------

Signed-off-by: Stefan Stefanov <[email protected]>
  • Loading branch information
stefan-stefanooov authored Nov 21, 2023
1 parent 8190545 commit 39e16fa
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../build-info/18a2f5e432afb04d112aa7797a8e064f.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "SimpleAuction",
"sourceName": "contracts/solidity/simple-auction/SimpleAuction.sol",
"abi": [
{
"inputs": [
{
"internalType": "uint256",
"name": "biddingTime",
"type": "uint256"
},
{
"internalType": "address payable",
"name": "beneficiaryAddress",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "AuctionAlreadyEnded",
"type": "error"
},
{
"inputs": [],
"name": "AuctionEndAlreadyCalled",
"type": "error"
},
{
"inputs": [],
"name": "AuctionNotYetEnded",
"type": "error"
},
{
"inputs": [],
"name": "BidNotHighEnough",
"type": "error"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "winner",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "AuctionEnded",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "recepient",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "FundReturned",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "bidder",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "HighestBidIncreased",
"type": "event"
},
{
"inputs": [],
"name": "auctionEnd",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "auctionEndTime",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "beneficiary",
"outputs": [
{
"internalType": "address payable",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "bid",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"name": "highestBid",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "highestBidder",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "withdraw",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x608060405234801561001057600080fd5b5060405161054438038061054483398101604081905261002f91610062565b610039824261009f565b600155600080546001600160a01b0319166001600160a01b0392909216919091179055506100c6565b6000806040838503121561007557600080fd5b825160208401519092506001600160a01b038116811461009457600080fd5b809150509250929050565b808201808211156100c057634e487b7160e01b600052601160045260246000fd5b92915050565b61046f806100d56000396000f3fe6080604052600436106100705760003560e01c80633ccfd60b1161004e5780633ccfd60b146100d15780634b449cba146100f657806391f901571461011a578063d57bde791461013a57600080fd5b80631998aeef146100755780632a24f46c1461007f57806338af3eed14610094575b600080fd5b61007d610150565b005b34801561008b57600080fd5b5061007d610297565b3480156100a057600080fd5b506000546100b4906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b3480156100dd57600080fd5b506100e6610373565b60405190151581526020016100c8565b34801561010257600080fd5b5061010c60015481565b6040519081526020016100c8565b34801561012657600080fd5b506002546100b4906001600160a01b031681565b34801561014657600080fd5b5061010c60035481565b6001544211156101a75760405162461bcd60e51b815260206004820152601360248201527f41756374696f6e416c7265616479456e6465640000000000000000000000000060448201526064015b60405180910390fd5b34600354106101f85760405162461bcd60e51b815260206004820152601060248201527f4269644e6f7448696768456e6f75676800000000000000000000000000000000604482015260640161019e565b60035415610233576003546002546001600160a01b03166000908152600460205260408120805490919061022d908490610412565b90915550505b6002805473ffffffffffffffffffffffffffffffffffffffff1916339081179091553460038190556040805192835260208301919091527ff4757a49b326036464bec6fe419a4ae38c8a02ce3e68bf0809674f6aab8ad300910160405180910390a1565b6001544210156102ba5760405163044cee2960e41b815260040160405180910390fd5b60055460ff16156102de57604051630c39fb9f60e31b815260040160405180910390fd5b6005805460ff19166001179055600254600354604080516001600160a01b03909316835260208301919091527fdaec4582d5d9595688c8c98545fdd1c696d41c6aeaeb636737e84ed2f5c00eda910160405180910390a1600080546003546040516001600160a01b039092169281156108fc029290818181858888f19350505050158015610370573d6000803e3d6000fd5b50565b33600090815260046020526040812054801561040a57336000818152600460205260408082208290555183156108fc0291849190818181858888f1935050505061040a5733600081815260046020908152604091829020849055815192835282018390527ff0f0519cd10c4b06281b3bcd73769ee2071e9f11d6a5cfa92b565d51c5643083910160405180910390a1600091505090565b600191505090565b8082018082111561043357634e487b7160e01b600052601160045260246000fd5b9291505056fea2646970667358221220439d4d437409a51fb88c7adfffc36d1dc0b4c573bddb18345ee7d53f4a16eab164736f6c63430008140033",
"deployedBytecode": "0x6080604052600436106100705760003560e01c80633ccfd60b1161004e5780633ccfd60b146100d15780634b449cba146100f657806391f901571461011a578063d57bde791461013a57600080fd5b80631998aeef146100755780632a24f46c1461007f57806338af3eed14610094575b600080fd5b61007d610150565b005b34801561008b57600080fd5b5061007d610297565b3480156100a057600080fd5b506000546100b4906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b3480156100dd57600080fd5b506100e6610373565b60405190151581526020016100c8565b34801561010257600080fd5b5061010c60015481565b6040519081526020016100c8565b34801561012657600080fd5b506002546100b4906001600160a01b031681565b34801561014657600080fd5b5061010c60035481565b6001544211156101a75760405162461bcd60e51b815260206004820152601360248201527f41756374696f6e416c7265616479456e6465640000000000000000000000000060448201526064015b60405180910390fd5b34600354106101f85760405162461bcd60e51b815260206004820152601060248201527f4269644e6f7448696768456e6f75676800000000000000000000000000000000604482015260640161019e565b60035415610233576003546002546001600160a01b03166000908152600460205260408120805490919061022d908490610412565b90915550505b6002805473ffffffffffffffffffffffffffffffffffffffff1916339081179091553460038190556040805192835260208301919091527ff4757a49b326036464bec6fe419a4ae38c8a02ce3e68bf0809674f6aab8ad300910160405180910390a1565b6001544210156102ba5760405163044cee2960e41b815260040160405180910390fd5b60055460ff16156102de57604051630c39fb9f60e31b815260040160405180910390fd5b6005805460ff19166001179055600254600354604080516001600160a01b03909316835260208301919091527fdaec4582d5d9595688c8c98545fdd1c696d41c6aeaeb636737e84ed2f5c00eda910160405180910390a1600080546003546040516001600160a01b039092169281156108fc029290818181858888f19350505050158015610370573d6000803e3d6000fd5b50565b33600090815260046020526040812054801561040a57336000818152600460205260408082208290555183156108fc0291849190818181858888f1935050505061040a5733600081815260046020908152604091829020849055815192835282018390527ff0f0519cd10c4b06281b3bcd73769ee2071e9f11d6a5cfa92b565d51c5643083910160405180910390a1600091505090565b600191505090565b8082018082111561043357634e487b7160e01b600052601160045260246000fd5b9291505056fea2646970667358221220439d4d437409a51fb88c7adfffc36d1dc0b4c573bddb18345ee7d53f4a16eab164736f6c63430008140033",
"linkReferences": {},
"deployedLinkReferences": {}
}
77 changes: 77 additions & 0 deletions contracts/solidity/simple-auction/SimpleAuction.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract SimpleAuction {
address payable public beneficiary;
uint public auctionEndTime;
address public highestBidder;
uint public highestBid;

mapping(address => uint) pendingReturns;
bool ended;

event HighestBidIncreased(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
event FundReturned(address recepient, uint amount);

/// The auction has already ended.
error AuctionAlreadyEnded();
/// There is already a higher or equal bid.
error BidNotHighEnough();
/// The auction has not ended yet.
error AuctionNotYetEnded();
/// The function auctionEnd has already been called.
error AuctionEndAlreadyCalled();

constructor(
uint biddingTime,
address payable beneficiaryAddress
) {
auctionEndTime = block.timestamp + biddingTime;
beneficiary = beneficiaryAddress;
}

function bid() external payable {
if (block.timestamp > auctionEndTime)
revert('AuctionAlreadyEnded');

if (highestBid >= msg.value)
revert('BidNotHighEnough');

if (highestBid != 0) {
pendingReturns[highestBidder] += highestBid;
}

highestBidder = msg.sender;
highestBid = msg.value;
emit HighestBidIncreased(msg.sender, msg.value);
}

/// Withdraw a bid that was overbid.
function withdraw() external returns (bool) {
uint amount = pendingReturns[msg.sender];
if (amount > 0) {
pendingReturns[msg.sender] = 0;
if (!payable(msg.sender).send(amount)) {
pendingReturns[msg.sender] = amount;
emit FundReturned(msg.sender, amount);
return false;
}
}
return true;
}

/// End the auction and send the highest bid
/// to the beneficiary.
function auctionEnd() external {
if (block.timestamp < auctionEndTime)
revert AuctionNotYetEnded();
if (ended)
revert AuctionEndAlreadyCalled();

ended = true;
emit AuctionEnded(highestBidder, highestBid);

beneficiary.transfer(highestBid);
}
}
1 change: 0 additions & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,3 @@ module.exports = {
},
},
}

1 change: 1 addition & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const Contract = {
Panic: 'Panic',
ReentrancyGuardTestSender: 'ReentrancyGuardTestSender',
ReentrancyGuardTestReceiver: 'ReentrancyGuardTestReceiver',
SimpleAuction: 'SimpleAuction',
}

const CALL_EXCEPTION = 'CALL_EXCEPTION'
Expand Down
1 change: 0 additions & 1 deletion test/solidity/errors/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,4 @@ describe('@solidityequiv2 Solidity Errors', function () {
expect(receipt.events[0].args.code).to.equal(18)
expect(receipt.events[0].args.message).to.equal('panic')
})

})
Loading

0 comments on commit 39e16fa

Please sign in to comment.