This document describes the campaign creation flow in Trivela, including both off-chain record creation and optional on-chain contract deployment.
Campaigns in Trivela can exist in two modes:
- Off-chain only: Campaign metadata stored in the backend database without blockchain anchoring
- On-chain anchored: Campaign linked to a deployed Soroban campaign contract for trustless, decentralized operation
- Database Schema:
campaignstable withcontract_idfield (nullable) - API Endpoints:
POST /api/v1/campaigns- Create new campaignPUT /api/v1/campaigns/:id- Update existing campaign (including contract ID)
- Validation: Zod schemas enforce contract ID format (
C[A-Z2-7]{55})
- CreateCampaign.jsx: Admin form for campaign creation with optional on-chain deployment
- stellar.js:
initializeCampaignContract()function for contract initialization - TransactionStatus.jsx: Displays transaction hash and Stellar Expert link
User fills form → POST /api/v1/campaigns → Campaign record created in SQLite
Required fields:
name(string, required)description(string, optional)rewardPerAction(number, optional, default: 0)
Optional fields:
contractId(string, nullable) - Stellar contract ID in formatC...slug,startDate,endDate,active,featured,hidden,hiddenReason
When "Initialize contract on-chain" is enabled:
1. Create off-chain record
↓
2. Check wallet connection
↓
3. Call initializeCampaignContract(walletAddress, contractId)
↓
4. Build transaction: contract.call('initialize', admin_address)
↓
5. Prepare transaction (assemble auth + resources)
↓
6. Sign with wallet (Freighter/other)
↓
7. Submit to Soroban RPC
↓
8. Poll for confirmation (max 40 attempts, 1.5s interval)
↓
9. Update campaign record with contract ID
↓
10. Redirect to campaign detail page
Transaction Flow:
- Build: Create
TransactionBuilderwithinitializeoperation - Prepare:
server.prepareTransaction()adds auth entries and resource fees - Sign: Wallet signs the XDR
- Submit:
server.sendTransaction()broadcasts to network - Poll: Wait for
status !== 'NOT_FOUND'
The campaign contract must be initialized with an admin address before use:
pub fn initialize(env: Env, admin: Address) -> Result<(), Error>This sets:
- Admin address (for privileged operations)
- Schema version
- Initial nonce (for replay protection)
Fields:
- Admin API Key (password) - Required for all operations
- Edit existing campaign (select) - Optional, loads campaign for editing
- Campaign name (text) - Required
- Description (textarea) - Optional
- Reward per action (number) - Optional, default 0
- Contract ID (text) - Optional, Stellar contract address
- Initialize contract on-chain (checkbox) - Only shown when contract ID is provided
States:
- Creating... - Submitting form
- Creating campaign record... - POST request in progress
- Checking wallet connection... - Verifying wallet
- Initializing contract on-chain... - Transaction being signed/submitted
- Contract initialized successfully! - Transaction confirmed
- Success - Campaign created (with optional deployment confirmation)
After successful deployment, shows:
- ✓ Success badge
- Transaction hash (shortened, with copy button)
- "View on Stellar Expert" link
-
Wallet not connected
- Message: "Wallet not connected. Please connect your wallet to deploy on-chain."
- Solution: Connect wallet before enabling deployment
-
Invalid contract ID
- Message: "contractId must be a valid Stellar contract ID (C...)"
- Solution: Ensure contract ID matches pattern
C[A-Z2-7]{55}
-
Transaction failed
- Message: "Contract initialization failed on-chain."
- Solution: Check wallet balance, contract state, and network status
-
Transaction timeout
- Message: "Initialization transaction could not be confirmed in time."
- Solution: Check Stellar Expert for transaction status, may need to retry
- If on-chain deployment fails, the off-chain campaign record is still created
- Users can manually update the
contractIdlater via the edit form - Contract initialization can be retried independently
-
Deploy campaign contract WASM using
stellar-cli:stellar contract deploy \ --wasm target/wasm32-unknown-unknown/release/campaign.wasm \ --source ADMIN_SECRET_KEY \ --network testnet
-
Copy the returned contract ID (starts with
C) -
Enter contract ID in the "Contract ID" field
-
Enable "Initialize contract on-chain" checkbox
A backend service could handle WASM upload and contract creation:
POST /api/v1/contracts/deploy
{
"wasmHash": "...",
"contractType": "campaign"
}
This would return a contract ID that can be used in campaign creation.
- Admin API Key: Stored in session storage only, never persisted
- Wallet Signature: User must explicitly approve contract initialization
- Nonce Protection: Contract uses nonce-based replay protection for admin operations
- Contract Ownership: Only the initializing wallet can perform admin operations
- Create campaign without contract ID
- Create campaign with contract ID (no deployment)
- Create campaign with contract ID and deployment
- Edit existing campaign to add contract ID
- Verify transaction appears on Stellar Expert
- Test error handling (wallet disconnected, invalid contract ID)
- Test deployment failure recovery
See backend/src/integration/campaigns.test.js for API tests.
- Full Contract Deployment: Upload WASM + create contract + initialize in one flow
- Contract Configuration: Set time windows, max cap, Merkle root during creation
- Multi-signature Admin: Support multiple admin addresses
- Contract Verification: Verify contract code matches expected WASM hash
- Gas Estimation: Show estimated XLM cost before deployment
- Batch Deployment: Deploy multiple campaigns in one transaction