Trade Ledger API
Endpoint: /api/trade-ledgers
This API endpoint returns all Trade Ledger information and collection details from the database.
export async function GET() {
try {
const tradeLedgers = await prisma.tradeLedgerCreated.findMany({
orderBy: { timestamp: 'desc' }
});
const serializedTradeLedgers = tradeLedgers.map((ledger) => ({
id: ledger.ledger_id,
collectionId: ledger.collection_id,
collectionName: ledger.name || ledger.collection_id,
currencyType: ledger.currency_type,
createdAt: ledger.timestamp,
currentSupply: ledger.current_supply,
maxSupply: ledger.max_supply,
isMutable: ledger.is_mutable,
hasDenyListAuthority: ledger.has_deny_list_authority,
denyListSize: ledger.deny_list_size,
description: ledger.description,
uri: ledger.uri,
logoUri: ledger.logo_uri,
creator: ledger.creator
}));
return new NextResponse(JSON.stringify(serializedTradeLedgers), {
status: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
}
});
} catch (error) {
console.error("[TradeLedger API] Error:", error);
return new NextResponse(JSON.stringify([]), { status: 500 });
}
}No data fetched yet.
The UI below shows the active trading pools by calling the API and rendering the TradeTable component.
Trade Events API
Endpoint: /api/trade-events?ledgerId=YOUR_LEDGER_ID
This API endpoint returns trade events (sell and buy price history) for a given ledger ID.
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const ledgerId = searchParams.get('ledgerId');
if (!ledgerId) {
return NextResponse.json({ error: 'ledgerId is required' }, { status: 400 });
}
// Fetch OfferAccepted events
const offerAcceptedEvents = await prisma.offerAccepted.findMany({
where: { ledger_id: ledgerId },
orderBy: { timestamp: 'asc' },
select: {
requested_amount: true,
timestamp: true,
offered_asset_ids: true
}
});
// Fetch BuyOfferAccepted events
const buyOfferAcceptedEvents = await prisma.buyOfferAccepted.findMany({
where: { ledger_id: ledgerId },
orderBy: { timestamp: 'asc' },
select: {
offered_amount: true,
timestamp: true,
requested_asset_count: true
}
});
// Process events and format response
const sellPriceHistory = offerAcceptedEvents
.filter(event => event.timestamp && event.requested_amount &&
Array.isArray(event.offered_asset_ids) && event.offered_asset_ids.length > 0)
.map(event => ({
timestamp: event.timestamp,
requested_amount: event.requested_amount,
offered_asset_ids: event.offered_asset_ids,
volume: event.offered_asset_ids.length,
type: 'sell'
}));
const buyPriceHistory = buyOfferAcceptedEvents
.filter(event => event.timestamp && event.offered_amount &&
event.requested_asset_count && parseInt(event.requested_asset_count, 10) > 0)
.map(event => ({
timestamp: event.timestamp,
offered_amount: event.offered_amount,
requested_asset_count: event.requested_asset_count,
volume: parseInt(event.requested_asset_count, 10),
type: 'buy'
}));
const apiResponse = { sellPriceHistory, buyPriceHistory };
return NextResponse.json(apiResponse);
} catch (error: any) {
console.error('[API] Error fetching trade events:', error);
return NextResponse.json({ error: 'Failed to fetch trade events' }, { status: 500 });
}
}No data fetched yet.
No data to display.
Trade Chart Interface (Events API)
Endpoint: /api/trade-events?ledgerId=YOUR_LEDGER_ID
This API endpoint returns trade events (sell and buy price history) for the given ledger ID.
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const ledgerId = searchParams.get('ledgerId');
if (!ledgerId) {
return NextResponse.json({ error: 'ledgerId is required' }, { status: 400 });
}
// Fetch OfferAccepted events
const offerAcceptedEvents = await prisma.offerAccepted.findMany({
where: { ledger_id: ledgerId },
orderBy: { timestamp: 'asc' },
select: {
requested_amount: true,
timestamp: true,
offered_asset_ids: true
}
});
// Fetch BuyOfferAccepted events
const buyOfferAcceptedEvents = await prisma.buyOfferAccepted.findMany({
where: { ledger_id: ledgerId },
orderBy: { timestamp: 'asc' },
select: {
offered_amount: true,
timestamp: true,
requested_asset_count: true
}
});
// Process and format response
const sellPriceHistory = offerAcceptedEvents
.filter(event => event.timestamp && event.requested_amount &&
Array.isArray(event.offered_asset_ids) && event.offered_asset_ids.length > 0)
.map(event => ({
timestamp: event.timestamp,
requested_amount: event.requested_amount,
offered_asset_ids: event.offered_asset_ids,
volume: event.offered_asset_ids.length,
type: 'sell'
}));
const buyPriceHistory = buyOfferAcceptedEvents
.filter(event => event.timestamp && event.offered_amount &&
event.requested_asset_count && parseInt(event.requested_asset_count, 10) > 0)
.map(event => ({
timestamp: event.timestamp,
offered_amount: event.offered_amount,
requested_asset_count: event.requested_asset_count,
volume: parseInt(event.requested_asset_count, 10),
type: 'buy'
}));
const apiResponse = { sellPriceHistory, buyPriceHistory };
return NextResponse.json(apiResponse);
} catch (error: any) {
console.error('[API] Error fetching trade events:', error);
return NextResponse.json({ error: 'Failed to fetch trade events' }, { status: 500 });
}
}No data fetched yet.
The TradeChart below is rendered using the selected ledger ID.
0.00
0.00
0.00
0.00
0.00
Buy Offers/Best Offers API
Endpoint: /api/buy-offers/best-offers?limit=10&ledgerId=YOUR_LEDGER_ID&includeDetails=true
This API endpoint returns the best buy offers (for sellers to accept) for the given ledger ID.
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const ledgerId = searchParams.get('ledgerId');
const limit = searchParams.get('limit');
// Fetch best buy offers from the database
const response = await prisma.buyOfferAccepted.findMany({
where: { ledger_id: ledgerId },
orderBy: { timestamp: 'asc' },
select: {
offer_id: true,
buyer: true,
collection_id: true,
requested_asset_count: true,
original_quantity: true,
offered_currency: true,
offered_amount: true,
status: true,
timestamp: true,
ledger_id: true,
collection: { select: { name: true } }
}
});
// Format response
const offers = response.map((offer) => ({
offer_id: offer.offer_id,
buyer: offer.buyer,
collection_id: offer.collection_id,
requested_asset_count: offer.requested_asset_count,
original_quantity: offer.original_quantity || offer.requested_asset_count,
offered_currency: offer.offered_currency,
offered_amount: offer.offered_amount,
status: offer.status,
timestamp: offer.timestamp,
ledger_id: offer.ledger_id,
collection: { name: offer.collection?.name }
}));
return NextResponse.json({ offers });
} catch (error) {
console.error('Error fetching buy offers:', error);
return NextResponse.json({ error: 'Failed to fetch offers' }, { status: 500 });
}
}No data fetched yet.
No data available.
Trade Offers/Best Offers API
Endpoint: /api/trade-offers/best-offers?limit=10&ledgerId=YOUR_LEDGER_ID
This API endpoint returns sell offers (for buyers to purchase) for the given ledger ID.
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const ledgerId = searchParams.get('ledgerId');
const limit = searchParams.get('limit');
// Fetch sell offers from the database
const response = await prisma.offerCreated.findMany({
where: { ledger_id: ledgerId },
orderBy: { timestamp: 'asc' },
select: {
offer_id: true,
offerer: true,
collection_id: true,
offered_asset_ids: true,
requested_currency: true,
requested_amount: true,
original_quantity: true,
status: true,
timestamp: true,
ledger_id: true,
collection: { select: { name: true } }
}
});
const offers = response.map((offer) => ({
offer_id: offer.offer_id,
offerer: offer.offerer,
collection_id: offer.collection_id,
offered_asset_ids: offer.offered_asset_ids,
requested_currency: offer.requested_currency,
requested_amount: offer.requested_amount,
original_quantity: offer.original_quantity || String(offer.offered_asset_ids.length),
status: offer.status,
timestamp: offer.timestamp,
ledger_id: offer.ledger_id,
collection: { name: offer.collection?.name }
}));
return NextResponse.json({ offers });
} catch (error) {
console.error('Error fetching sell offers:', error);
return NextResponse.json({ error: 'Failed to fetch offers' }, { status: 500 });
}
}No data fetched yet.
No active offers available.
User Offers API
Endpoint: /api/user-offers/[address]
This API endpoint returns all offers (buy/sell/partially_filled) by user
// File: /api/user-offers/[address]/route.ts
import { NextResponse } from 'next/server';
import prisma from '../../lib/prisma';
function serializeBigInts(obj: any): any {
if (typeof obj === 'bigint') return obj.toString();
if (Array.isArray(obj)) return obj.map(serializeBigInts);
if (obj !== null && typeof obj === 'object') {
const newObj: any = {};
for (const key in obj) {
newObj[key] = serializeBigInts(obj[key]);
}
return newObj;
}
return obj;
}
export async function GET(request: Request, { params }: { params: { address: string } }) {
try {
console.log("[/api/user-offers] Fetching offers for address:", params.address);
// Fetch ledger metadata.
const tradeLedgers = await prisma.tradeLedgerCreated.findMany({
select: { ledger_id: true, collection_id: true, name: true, currency_type: true },
});
// Fetch sell offers (user is the offerer).
const sellOffers = await prisma.offerCreated.findMany({
where: { offerer: params.address, status: { in: ['OPEN', 'PARTIALLY_FILLED'] } },
orderBy: { timestamp: 'desc' },
});
// Fetch buy offers (user is the buyer).
const buyOffers = await prisma.buyOfferCreated.findMany({
where: { buyer: params.address, status: { in: ['OPEN', 'PARTIALLY_FILLED'] } },
orderBy: { timestamp: 'desc' },
});
const markedSellOffers = sellOffers.map((offer) => ({ ...offer, type: 'sell' }));
const markedBuyOffers = buyOffers.map((offer) => ({ ...offer, type: 'buy' }));
const allOffers = [...markedSellOffers, ...markedBuyOffers];
const enrichedOffers = allOffers.map((offer: any) => {
const ledger = tradeLedgers.find((l) => l.ledger_id === offer.ledger_id);
if (offer.type === 'sell') {
return {
id: offer.offer_id,
type: 'sell',
user: offer.offerer,
collection: {
id: ledger?.collection_id || '',
name: ledger?.name || 'Unknown Collection',
},
requested_amount: offer.requested_amount,
status: offer.status,
timestamp: offer.timestamp,
trade_ledger_id: offer.ledger_id,
tradeOfferAssets: offer.offered_asset_ids,
};
} else {
return {
id: offer.offer_id,
type: 'buy',
user: offer.buyer,
collection: {
id: ledger?.collection_id || '',
name: ledger?.name || 'Unknown Collection',
},
offered_amount: offer.offered_amount,
status: offer.status,
timestamp: offer.timestamp,
ledger_id: offer.ledger_id,
};
}
});
return NextResponse.json({ success: true, offers: serializeBigInts(enrichedOffers) });
} catch (error) {
console.error("[/api/user-offers] Error:", error);
return NextResponse.json(
{ success: false, error: error instanceof Error ? error.message : "Unknown error" },
{ status: 500 }
);
}
}No data fetched yet.
No offers found.
Transaction History API
Endpoint: /api/transaction-history/$0x9470585d9b80a03d6e86a4615d5a51e9c687cfa0bd1cd162fe8184e89be77087?page=$page
This API endpoint returns sell offers (for buyers to purchase) for the given ledger ID.
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const ledgerId = searchParams.get('ledgerId');
const limit = searchParams.get('limit');
// Fetch sell offers from the database
const response = await prisma.offerCreated.findMany({
where: { ledger_id: ledgerId },
orderBy: { timestamp: 'asc' },
select: {
offer_id: true,
offerer: true,
collection_id: true,
offered_asset_ids: true,
requested_currency: true,
requested_amount: true,
original_quantity: true,
status: true,
timestamp: true,
ledger_id: true,
collection: { select: { name: true } }
}
});
const offers = response.map((offer) => ({
offer_id: offer.offer_id,
offerer: offer.offerer,
collection_id: offer.collection_id,
offered_asset_ids: offer.offered_asset_ids,
requested_currency: offer.requested_currency,
requested_amount: offer.requested_amount,
original_quantity: offer.original_quantity || String(offer.offered_asset_ids.length),
status: offer.status,
timestamp: offer.timestamp,
ledger_id: offer.ledger_id,
collection: { name: offer.collection?.name }
}));
return NextResponse.json({ offers });
} catch (error) {
console.error('Error fetching sell offers:', error);
return NextResponse.json({ error: 'Failed to fetch offers' }, { status: 500 });
}
}No data fetched yet.
Transaction History
No transactions found.
- Previous
- 1(current)
- Next
