How to guides
How to Monitor Auctions
A goal-oriented guide to monitoring XGA auctions and checking bid results.
Create src/monitoring.ts
Create a file named src/monitoring.ts
with the following code:
import { XGAConnection } from './connection.js'; // Assuming you have this
import { ethers } from 'ethers';
export class AuctionMonitor {
private connection: XGAConnection;
private auctioneerAddress: string;
constructor(connection: XGAConnection, auctioneerAddress: string) {
this.connection = connection;
this.auctioneerAddress = auctioneerAddress;
}
async monitorAuction(slot: number): Promise<void> {
console.log(`Monitoring auction for slot ${slot}...`);
const provider = this.connection.getProvider();
// Set up event listener for auction events
const contract = new ethers.Contract(
this.auctioneerAddress,
[
"event AuctionFinalized(uint256 indexed slot, uint256 clearingPrice)",
"event BidAccepted(uint256 indexed slot, address indexed bidder, uint256 quantity)",
],
provider
);
// Listen for auction finalization
contract.on("AuctionFinalized", (auctionSlot, clearingPrice) => {
if (auctionSlot.toString() === slot.toString()) {
console.log(`Auction ${slot} finalized!`);
console.log(`Clearing price: ${ethers.formatEther(clearingPrice)} ETH per unit`);
}
});
// Listen for bid acceptance
contract.on("BidAccepted", (auctionSlot, bidder, quantity) => {
if (auctionSlot.toString() === slot.toString()) {
const myAddress = this.connection.getWallet().address;
if (bidder.toLowerCase() === myAddress.toLowerCase()) {
console.log(`Your bid was accepted!`);
console.log(`Quantity won: ${quantity.toString()} units`);
}
}
});
console.log('Event listeners set up. Monitoring...');
}
async getBidResults(slot: number): Promise<void> {
// This would query the contract for final results
console.log(`Checking results for slot ${slot}...`);
// Implementation would depend on actual contract interface
// For tutorial purposes, we'll simulate
setTimeout(() => {
console.log('Bid results:');
console.log('- Status: Auction completed');
console.log('- Your bid: Partially filled');
console.log('- Quantity won: 1 unit');
console.log('- Final price: 0.008 ETH per unit');
}, 2000);
}
}
Example Usage
Run the Monitoring Example
You can use this class in a script to monitor an auction you've bid on:
import { createConnection } from './connection.js'; // Assuming you have this
import { AuctionMonitor } from './monitoring.js';
async function runMonitoringExample(slot: number): Promise<void> {
const connection = await createConnection();
const auctioneerAddress = "0x86Bc75A43704E38f0FD94BdA423C50071fE17c99"; // Replace with actual address
const monitor = new AuctionMonitor(connection, auctioneerAddress);
await monitor.monitorAuction(slot);
// Check results after some time
setTimeout(async () => {
await monitor.getBidResults(slot);
process.exit(0);
}, 10000);
}
// Example: monitor slot 12345
runMonitoringExample(12345).catch(console.error);