Car auction network: A Hello World example with Hyperledger Fabric Node SDK and IBM Block chain Starter Plan. Use Hyperledger Fabric to invoke chaincode while storing results and data in the starter plan
File Structure :
/ (root)
├── app.js
├── carauction.js
├── ledger.json
├── package.json
└── public/
└── index.html
Package.json
{
"name": "car-auction-simulation",
"version": "1.0.0",
"description": "Simulated Car Auction Blockchain using Fabric Node SDK structure with HTML frontend",
"main": "app.js",
"type": "module",
"dependencies":
{
"express": "^4.19.2",
"body-parser": "^1.20.2"
}
}
App.js
import express from "express";
import bodyParser from "body-parser";
import { CarAuctionContract } from "./carauction.js";
const app = express();
app.use(bodyParser.json());
app.use(express.static("public"));
const contract = new CarAuctionContract();
// Initialize ledger
(async () => {
await contract.initLedger();
})();
// API ROUTES
app.get("/api/cars", async (req, res) => {
res.json(await contract.queryAllCars());
});
app.post("/api/register", async (req, res) => {
const { carId, model, owner } = req.body;
try {
const result = await contract.registerCar(carId, model, owner);
res.json({ message: result });
} catch (e) {
res.status(400).json({ error: e.message });
}
});
app.post("/api/bid", async (req, res) => {
const { carId, bidder, amount } = req.body;
try {
const updated = await contract.placeBid(carId, bidder, amount);
res.json(updated);
} catch (e) {
res.status(400).json({ error: e.message });
}
});
app.get("/api/highestBid", async (req, res) => {
res.json(await contract.getHighestBid());
});
// Root route
app.get("/", (req, res) => {
res.sendFile("/index.html", { root: "public" });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () =>
console.log(`🚗 Car Auction app running on port ${PORT}`)
);
Index.html
🚗
Car Auction Blockchain Simulation
🚗
Car Auction Blockchain Simulation
Highest Bid
Ledger.json (empty, so the data will be stored and appear here also after entries)
[]
Carauction .js
import fs from "fs";
const LEDGER_FILE = "./ledger.json";
export class CarAuctionContract {
constructor() {
if (!fs.existsSync(LEDGER_FILE)) {
fs.writeFileSync(LEDGER_FILE, JSON.stringify([]));
}
}
async initLedger() {
const cars = [
{ carId: "CAR1", model: "Tesla Model 3", owner: "Alice", bid: 0 },
{ carId: "CAR2", model: "BMW X5", owner: "Bob", bid: 0 }
];
fs.writeFileSync(LEDGER_FILE, JSON.stringify(cars, null, 2));
return "Ledger initialized";
}
async queryAllCars() {
return JSON.parse(fs.readFileSync(LEDGER_FILE));
}
async queryCar(carId) {
const cars = JSON.parse(fs.readFileSync(LEDGER_FILE));
const car = cars.find(c => c.carId === carId);
if (!car) throw new Error(`Car ${carId} not found`);
return car;
}
async placeBid(carId, bidder, amount) {
const cars = JSON.parse(fs.readFileSync(LEDGER_FILE));
const car = cars.find(c => c.carId === carId);
if (!car) throw new Error(`Car ${carId} not found`);
if (parseInt(amount) <= car.bid)
throw new Error(`Bid must be higher than ${car.bid}`);
car.bid = parseInt(amount);
car.owner = bidder;
fs.writeFileSync(LEDGER_FILE, JSON.stringify(cars, null, 2));
return car;
}
async registerCar(carId, model, owner) {
const cars = JSON.parse(fs.readFileSync(LEDGER_FILE));
const exists = cars.find(c => c.carId === carId);
if (exists) throw new Error("Car ID already exists!");
cars.push({ carId, model, owner, bid: 0 });
fs.writeFileSync(LEDGER_FILE, JSON.stringify(cars, null, 2));
return `Car ${carId} registered successfully.`;
}
async getHighestBid() {
const cars = JSON.parse(fs.readFileSync(LEDGER_FILE));
const topCar = cars.reduce((prev, curr) =>
prev.bid > curr.bid ? prev : curr
);
return topCar;
}
}
Commands to Execute :
- npm install
- node app.js
- Click “Open in New Tab” (top right)
-
Output :
This data will also get stored in json format in “ledger.json” file