xga.fyi

How to Create a Bid

Creating and submitting a bid in an XGA auction.

This guide will show you how to create and submit a bid for an XGA auction using the TypeScript SDK.

Prerequisites

Bidding Implementation

Create src/bidding.ts

Create a file named src/bidding.ts with the following code:

import { XGAConnection } from './connection.js'; // Assuming you have this from the reference
import { ethers } from 'ethers';

export interface BidParams {
  slot: number;
  bidPrice: bigint;  // Price per unit in wei
  quantity: number;  // Number of units requested
}

export class XGAAuctioneer {
  private connection: XGAConnection;
  private auctioneerAddress: string;

  // Simplified ABI for example - in practice, use full ABI
  private auctioneerABI = [
    "function getBid(uint256 slot) external view returns (uint256[] memory packedBids)",
    "function submitBid(uint256 slot, uint256 packedBid) external",
    "function packBid(uint256 bidPrice, uint256 quantity, uint256 bidderId) external pure returns (uint256)",
    "function getAuctionStatus(uint256 slot) external view returns (uint8 status)"
  ];

  constructor(connection: XGAConnection, auctioneerAddress: string) {
    this.connection = connection;
    this.auctioneerAddress = auctioneerAddress;
  }

  private getContract(): ethers.Contract {
    return new ethers.Contract(
      this.auctioneerAddress,
      this.auctioneerABI,
      this.connection.getWallet()
    );
  }

  async getCurrentSlot(): Promise<number> {
    const provider = this.connection.getProvider();
    const currentBlock = await provider.getBlockNumber();

    // XGA auctions are for future slots
    // This is simplified - actual slot calculation involves genesis time
    return currentBlock + 32; // Approximately 1 epoch ahead
  }

  async createBid(params: BidParams): Promise<string> {
    try {
      const contract = this.getContract();

      // For this tutorial, we'll use bidder ID 1
      const bidderId = 1;

      console.log(`Creating bid for slot ${params.slot}:`);
      console.log(`- Price: ${ethers.formatEther(params.bidPrice)} ETH per unit`);
      console.log(`- Quantity: ${params.quantity} units`);

      // Pack the bid
      const packedBid = await contract.packBid(
        params.bidPrice,
        params.quantity,
        bidderId
      );

      console.log(`Packed bid: ${packedBid.toString()}`);

      // Submit the bid
      const tx = await contract.submitBid(params.slot, packedBid);
      console.log(`Transaction submitted: ${tx.hash}`);

      // Wait for confirmation
      const receipt = await tx.wait();
      console.log(`Bid confirmed in block ${receipt.blockNumber}`);

      return tx.hash;
    } catch (error) {
      console.error('Bid creation failed:', error);
      throw error;
    }
  }

  async checkAuctionStatus(slot: number): Promise<string> {
    const contract = this.getContract();
    const status = await contract.getAuctionStatus(slot);

    const statusMap: { [key: number]: string } = {
      0: 'Not Started',
      1: 'Bidding Open',
      2: 'Bidding Closed',
      3: 'Processing',
      4: 'Finalized'
    };

    return statusMap[status] || 'Unknown';
  }
}

Example Usage

Run the Bidding Example

To run this code, you can create a script that does the following:

import { createConnection } from './connection.js'; // Assuming you have this
import { XGAAuctioneer, BidParams } from './bidding.js';
import { ethers } from 'ethers';

async function runBiddingExample(): Promise<void> {
  const connection = await createConnection();

  // Replace with actual auctioneer contract address
  const auctioneerAddress = "0x86Bc75A43704E38f0FD94BdA423C50071fE17c99";
  const auctioneer = new XGAAuctioneer(connection, auctioneerAddress);

  // Get target slot
  const targetSlot = await auctioneer.getCurrentSlot();
  console.log(`Target slot: ${targetSlot}`);

  // Check auction status
  const status = await auctioneer.checkAuctionStatus(targetSlot);
  console.log(`Auction status: ${status}`);

  if (status === 'Bidding Open') {
    // Create a conservative bid
    const bidParams: BidParams = {
      slot: targetSlot,
      bidPrice: ethers.parseEther("0.01"), // 0.01 ETH per unit
      quantity: 1 // 1 unit of blockspace
    };

    await auctioneer.createBid(bidParams);
  } else {
    console.log('Bidding not currently open for this slot');
  }
}

runBiddingExample().catch(console.error);