The Publication Contract manages the on-chain publishing of services and projects on the Offer Hub platform. It provides a decentralized registry for all service and project publications with data validation, user-specific counters, and event emission for off-chain indexing.
┌─────────────────────┐ ┌──────────────────────┐
│ User Publications │────│ Data Validation │
└─────────────────────┘ └──────────────────────┘
│ │
▼ ▼
┌─────────────────────┐ ┌──────────────────────┐
│ Publication Storage│ │ Event System │
│ • Service Posts │ │ • Creation Events │
│ • Project Posts │ │ • Off-chain Sync │
│ • User Counters │ │ • Indexing Support │
└─────────────────────┘ └──────────────────────┘
publish(env: Env, user: Address, publication_type: Symbol, title: String, category: String, amount: i128, timestamp: u64) -> Result<u32, ContractError>
Publishes a new service or project on-chain.
Parameters:
user: Publisher addresspublication_type: "service" or "project"title: Publication titlecategory: Category classificationamount: Associated payment amounttimestamp: Publication timestamp
Returns: Unique publication ID for the user
Validation:
- Title length requirements
- Amount positivity
- Category validation
- User authorization
Retrieves a specific publication.
Returns:
PublicationData {
publication_type: Symbol,
title: String,
category: String,
amount: i128,
timestamp: u64,
}const publishService = async (
userAddress: string,
serviceData: {
title: string,
category: string,
price: string,
description: string
}
) => {
const publicationId = await publicationContract.publish({
user: userAddress,
publication_type: 'service',
title: serviceData.title,
category: serviceData.category,
amount: serviceData.price,
timestamp: Math.floor(Date.now() / 1000)
});
// Store detailed data off-chain with reference to on-chain publication
await storeServiceDetails({
publicationId,
description: serviceData.description,
userAddress
});
return publicationId;
};// Listen for publication events
publicationContract.events.publication_created.subscribe((event) => {
console.log(`New ${event.publication_type} published by ${event.user}`);
// Trigger off-chain indexing
indexPublication({
userId: event.user,
publicationId: event.id,
type: event.publication_type
});
});- User submits publication → Contract validates data
- On-chain storage → Publication stored with unique ID
- Event emission → Off-chain services notified
- Database sync → Detailed data stored off-chain with reference
- User authorization required for all publications
- Input validation for all data fields
- Rate limiting through gas costs
- Data integrity with immutable on-chain records
For complete implementation details, see the full contract code and validation logic.