Skip to content

Commit

Permalink
Fixing comments
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Stefanov <[email protected]>
  • Loading branch information
stefan-stefanooov committed Nov 9, 2023
1 parent 71b40e4 commit 473ff62
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 46 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": {}
}
3 changes: 1 addition & 2 deletions contracts/solidity/simple-auction/SimpleAuction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ contract SimpleAuction {
}

function bid() external payable {

if (block.timestamp > auctionEndTime)
revert('AuctionAlreadyEnded');

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

if (highestBid != 0) {
Expand Down
102 changes: 61 additions & 41 deletions test/solidity/simple-auction/simple-auction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const { expect } = require('chai')
const { ethers } = require('hardhat')
const Constants = require('../../constants')
const Utils = require('../../hts-precompile/utils')
const {
genericPoll
} = require('../../../utils/helpers')

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

Expand All @@ -11,8 +33,8 @@ describe('Solidity Simple Auction example', function () {
contractShortLived, bidAmountSmall, initialEvent
const CONTRACT_DURATION = 10000000000
const CONTRACT_SHORT_DURATION = 1
const TRANSACTION_VALUE = '100'
const TRANSACTION_VALUE_SMALL = '10'
const TRANSACTION_VALUE = '1000'
const TRANSACTION_VALUE_SMALL = '100'

before(async function () {
signers = await ethers.getSigners()
Expand All @@ -25,7 +47,6 @@ describe('Solidity Simple Auction example', function () {

bidAmount = ethers.utils.parseUnits(TRANSACTION_VALUE, 'gwei')
bidAmountSmall = ethers.utils.parseUnits(TRANSACTION_VALUE_SMALL, 'gwei')
await sleep(5000)
})

beforeEach(async function () {
Expand All @@ -34,11 +55,11 @@ describe('Solidity Simple Auction example', function () {
await contract.deployed();

const trx = await contract.bid({ value: bidAmountSmall })
const receipt = await trx.wait()
const receipt = await trx.wait(1)
initialEvent = receipt.events[0].event
})

it('should confirm "bid" function works', async function () {
it('should confirm "bid" function works', async function () {
const highestBid = await contract.highestBid()
const highestBidder = await contract.highestBidder()

Expand All @@ -48,30 +69,17 @@ describe('Solidity Simple Auction example', function () {
})

it('should confirm bid not high enough scenario works: BidNotHighEnough', async function () {
try {
const tr = await contract.bid({ value: bidAmountSmall })
const receipt = await tr.wait();
expect(receipt).to.not.be.null
} catch (error) {
hasError = true
expect(error.code).to.be.equal('CALL_EXCEPTION')
}

expect(hasError).to.equal(true)
await expect(
contract.callStatic.bid({ value: 1 })
).to.eventually.be.rejected
.and.have.property('reason', 'BidNotHighEnough')
})

it('should revert a bid with "AuctionAlreadyEnded" error', async function () {
try {
console.log("Waiting for the next block")
await sleep(5000)
console.log("Done")
const tr = await contractShortLived.bid({ value: bidAmountSmall })
await tr.wait()
} catch (error) {
hasError = true
}

expect(hasError).to.equal(true)
it('should revert a bid with "AuctionAlreadyEnded" error', async function () {
await expect(
contractShortLived.callStatic.bid({ value: bidAmountSmall })
).to.eventually.be.rejected
.and.have.property('reason', 'AuctionAlreadyEnded')
})

it('should confirm "withdraw" function works', async function () {
Expand All @@ -83,24 +91,32 @@ describe('Solidity Simple Auction example', function () {
expect(initialHighestBidder, `Initial Highest bidder to be: ${initialHighestBidder}`).to.equal(wallet.address)

const tr = await contract.connect(wallet1).bid({ value: bidAmount })
await tr.wait()

await sleep(1000)
const newHighestBidder = await contract.highestBidder()
await tr.wait(2)

const newHighestBidder = await genericPoll(
await contract.highestBidder(),
res => res === wallet1.address,
3000,
"New Highest bidder to be: --Wallet1--"
)
expect(newHighestBidder, "New Highest bidder to be: --Wallet1--").to.equal(wallet1.address)
const currentContractBalance = await ethers.provider.getBalance(contract.address)


const currentContractBalance = await ethers.provider.getBalance(contract.address)
const combined = bidAmount.add(bidAmountSmall)
expect(currentContractBalance, "The contract balance to be the combined of the two transactions").to.equal(combined)

// Call the withdraw function with the previous highest bidder's address
const withdrawTx = await contract.connect(wallet).withdraw()
await withdrawTx.wait()
await withdrawTx.wait(2)

await sleep(1000)
// Check that the amount of Ether returned to the previous highest bidder is correct
const newContractBalance = await ethers.provider.getBalance(contract.address)
expect(newContractBalance, "The new balance to be bidAmount").to.equal(bidAmount)
const newContractBalance = await genericPoll(
ethers.provider.getBalance(contract.address),
res => res.eq(bidAmount),
3000,
`The new balance to be: ${bidAmount}`
)
expect(newContractBalance, `The new balance to be: ${bidAmount}`).to.equal(bidAmount)
})

it('should confirm "auctionEnd" function works', async function () {
Expand All @@ -109,10 +125,14 @@ describe('Solidity Simple Auction example', function () {
expect(previousContractBalance, `Initial Contract balance to be: ${bidAmountSmall}`).to.equal(bidAmountSmall)

const tr = await contractShortLived.connect(wallet).auctionEnd()
await tr.wait()

await sleep(1000)
const contractBalance = await ethers.provider.getBalance(contract.address)
await tr.wait(2)

const contractBalance = await genericPoll(
ethers.provider.getBalance(contract.address),
res => res.eq(bidAmountSmall),
1000,
`Contract balance after 'auctionEnd' to be: ${bidAmountSmall}`
)
expect(contractBalance, `Contract balance after "auctionEnd" to be: ${bidAmountSmall}`).to.equal(bidAmountSmall)
})
})
Loading

0 comments on commit 473ff62

Please sign in to comment.