Decentralized Auction
A screenshot of the dapp.
Description
A decentralized application for auctioning items on the Ethereum blockchain. Features of the dapp include the following:
- Setting a start price and end price of an item.
- Specify the duration of the auction (in minutes).
- Immediately withdraw funds after the auction has ended.
- Users bidding over the buy price are refunded the difference.
Prerequisites
To run and interact with the dapp, you'll need to install the dependencies below. Click on the links for further instructions.
- Node.js - requires the npm package manager that comes with Node.js.
- Truffle - tool for developing and deploying smart contracts on the blockchain.
- Ganache - to run a personal Ethereum blockchain.
- MetaMask - to interact with the Ethereum blockchain via a web browser.
Running the Dapp
1. Open Ganache
To run a local Ethereum blockchain, launch Ganache with the default rpc address http://127.0.0.1:7545
- Mac: open ganache from the applications directory.
- Windows: open either PowerShell or Command Prompt, enter
cd ganache
and thennpm start
2. Clone, install and deploy
Open up a new terminal or shell window and then clone this repository.
$ git clone https://github.com/takJohn/decentralized-auction.git
Change to the directory of the cloned repository and install the required packages.
$ cd decentralized-auction
$ npm install
Deploy the smart contract onto the local Ethereum blockchain.
$ truffle migrate
Run the light-server to view the application inside a browser.
$ npm run dev
This should open up your browser and automatically direct it to http://localhost:3000/
- if it doesn't then just fire up your browser and type in the address manually. The browser should now be displaying the loading page. To view the auction interface, you'll need to log into your MetaMask account and refresh the page.
3. Using MetaMask
Make sure the MetaMask plugin is enabled first before following these steps.
- Click on the MetaMask icon.
- Select Restore from seed phrase .
- Copy the mnemonic from Ganache and paste it into the Wallet Seed box.
- Enter a password and click OK.
- Click on the Main Network menu and select Custom RPC.
- Enter
http://127.0.0.1:7545
as the New RPC URL and then click Save.
To use the auction, you'll want to setup a few more accounts - on the Accounts page you can click on the switch accounts icon at the top to import more accounts. Click on Import Account and paste in more private keys from various addresses generated by Ganache.
Using the Dapp
You can change the details of the auction by modifying the parameters inside migrations/2_deploy_contracts.js (see comments in the code for more information).
When the dapp is launched, your web browser will open a page that displays content relevant to the current active user.
Admin (seller)
By default the first address listed in Ganache is the owner of the contract and therefore has admin rights.
- By clicking on any of the buttons on the auction page, you'll trigger the MetaMask pop-up to appear. Click on Submit to confirm the details of the transaction.
- You can cancel the auction whilst the auction is in progress. This allows all bidders to withdraw their funds.
- Providing there were bidders during the auction then the Withdraw button becomes available once the auction is over.
- The auction ends when the buy price has been met or when the End Auction button has been clicked.
- Unfortunately you'll need to click on the End Auction button when the time limit has been reached (see Future Improvements at the bottom of this page).
User (bidder)
Accounts not belonging to the contract owner can interact with the dapp as a standard user meaning you'll be able to bid for the item.
- By clicking on any of the buttons on the auction page, you'll trigger the MetaMask pop-up to appear. Click on Submit to confirm the details of the transaction.
- When you make a bid, your funds becomes locked into the contract and can only be unlocked when another bidder outbids you.
- The Withdraw button becomes available when you have funds to withdraw. This can happen if you placed a bid that's higher than the buying price or if someone else outbids you.
Troubleshooting
These are just some of the problems you may encounter when running the dapp.
Truffle migration error
Error: Attempting to run transaction which calls a contract function...
If you've ran truffle migrate
before then you'll need to run truffle migrate --reset
instead or delete the json files from the build/contracts directory.
MetaMask transaction error
Error: the tx doesn't have the correct nonce.
This is an issue with running on a private network and occurs when the blockchain is shutdown with MetaMask still connected to it. The easiest solution is to restart Ganache with a new mnemonic phrase and then reimport all the accounts before running the dapp again.
Missing MetaMask window
If a MetaMask transaction window doesn't appear when making a submission to the page then it's likely that a MetaMask pop-up already exists and needs to be closed first in order to proceed.
Page not updating
It should only take a few seconds when making any kind of submission on the page. Occasionally events will stop registering and the browser will become stuck or out of sync, unfortunately this is a known bug with MetaMask and you may have to restart Ganache as well as your browser.
Testing
The test script test/auction.js contains a bidding scenario where multiple parties are bidding all the way up to the item finally being bought. To run the script, you need to open up a terminal or shell window and change to the directory decentralized-auction then execute truffle test
Acknowledgements
- Using OpenZeppelin's SafeMath for a number of math operations.
- The auction contract is based on the simple open auction example.
Future Improvements
- Use Ethereum Alarm Clock to automatically trigger fund withdrawals when the auction is over.