Frontend structure
From the repository root, navigate to the auction frontend.
To run the frontend, use Node.js 22.22.2 or newer and install its dependencies:
Specifying the contract
We have a config file that reads the network, auction contract account ID, and RPC URL from environment variables. Copy.env.example to .env.local and set NEXT_PUBLIC_AUCTION_CONTRACT to your deployed, initialized auction. Set NEXT_PUBLIC_NETWORK_ID to testnet or mainnet, and optionally set NEXT_PUBLIC_RPC_URL to use a different RPC provider.
NEXT_PUBLIC_ variables are included in the frontend at build time, so rebuild after changing them.
config.js
Setting up wallets
To be able to fully interact with the contract - send bids and claim the auction - you’ll need awallet to sign transactions. Wallets securely store your private keys and allow you to sign transactions without exposing your private key to the frontend. We use @hot-labs/near-connect to let users choose and connect a wallet.
We abstract the wallet connection in our near.js file by exposing methods to complete various tasks. Feel free to explore the file to see how NearConnector restores a connected account and listens for sign-in and sign-out events.
The wallet object is initialized in the _app.js file and added to the global context along with the account that is signed in to make it easier to access anywhere in the application. The connector is loaded in the browser, and the effect removes its event listeners when it is cleaned up.
_app.js
context.js
navigation component to call the respective methods in the near.js file.
Navigation.jsx
near.js
Displaying the highest bid
To get the highest bid from the auction and who made it we callget_highest_bid. This function returns the highest bid in yoctoNEAR as a string. We use formatUnits(highestBidData.bid, 24) to display it in NEAR. The helper uses integer arithmetic and returns a decimal string, preserving every yoctoNEAR.
index.js
near.js
viewMethod uses JsonRpcProvider.callFunction from near-api-js to read the contract through the configured RPC provider. Reading state does not require a connected wallet. The default testnet endpoint is https://test.rpc.fastnear.com; there are many different providers available.
We then pass the information about the highest bidder into the LastBid component to display the bid amount and the bidder’s account Id.
index.js
LastBid.jsx
Updating the highest bid
Someone else could have placed a higher bid since the page was loaded. To keep the display up to date, we fetch the contract information every 20 seconds usingsetInterval. A second interval updates the countdown until the next refresh, and both intervals are cleared when the component unmounts.
index.js
Auction end time
The contract stores the end time of the auction in nanoseconds since the Unix epoch (1 January 1970 00:00:00 UTC). TheTimer component converts this value to milliseconds using BigInt, compares it with Date.now(), and displays the time left in days, hours, minutes, and seconds.
Timer.jsx
Making a bid
To make a bid we make a call to the contract using thebid function. We specify the deposit amount in yoctoNEAR which will be the bid amount. The input box takes the bid amount in NEAR as a decimal string. We use parseUnits(amount, 24) to convert it to a yoctoNEAR string without using JavaScript floating-point arithmetic. We also attach 300 Tgas to the transaction; unused gas is refunded.
Here, since the user is changing the state of the contract, not just viewing it, the user needs to sign the transaction. Thus the wallet will pop up displaying the transaction details.
index.js
near.js
Claiming the auction
Once the auction is over (the current time is greater than the end time) the auction can be claimed. At this point, the timer will be hidden and a button to claim the auction will be displayed. Once clicked theclaim function will be called on the auction contract to send the highest bid in NEAR to the auctioneer. The page reloads after a successful bid or claim to show the updated state, and errors are displayed through notifications.
index.js
Conclusion
In this part of the tutorial, we have implemented a simple frontend for a NEAR contract. Along the way, you have learned how to usenear-connect to sign the user in and out, how to view the contract’s state, how to sign and send transactions.
While we can see the highest bid, we may want to see the auction’s bidding history. Since the contract only stores the most recent bid (to reduce storage costs), we need to use an indexer to pull historical data. In the next part of the tutorial, we’ll look at querying historical data using an API endpoint.