Overview
The Custom Function Rule allows you to create sophisticated loyalty rules using JavaScript code. This rule type leverages Stratus functions to evaluate complex conditions, perform calculations, and determine rewards based on custom logic that goes beyond standard rule types.Key Features
- Custom Logic: Write JavaScript functions to implement any evaluation logic
- Dynamic Rewards: Calculate variable reward amounts based on user data or conditions
- External Data: Access external APIs and services using approved modules
- Complex Conditions: Implement multi-step validation and conditional rewards
- Flexible Output: Return points, badges, or multipliers based on your logic
Use Cases
Gamification Logic
Reward users based on complex gaming achievements, streaks, or skill levels.
Social Scoring
Calculate rewards based on social media engagement metrics or community
participation.
Tiered Rewards
Implement dynamic reward structures based on user tier, activity level, or
spending patterns.
Cross-Platform Integration
Combine data from multiple platforms to create unified reward systems.
How It Works
1
Write Your Function
Create a JavaScript function that receives user data and loyalty rule information as input.
Your function must follow the Stratus function syntax and use only approved modules.
2
Define Evaluation Logic
Implement your custom logic to determine if a user qualifies for rewards and
calculate the appropriate reward amount.
3
Return Reward Details
Your function returns a structured object describing the reward to grant
(points, badges, or multipliers).
4
Automatic Execution
The platform automatically executes your function when users attempt to complete the rule.
Function Requirements
Your custom function must:- Accept
(input, output)parameters - Parse the input data containing user and rule information
- Call
output.setResult([rewardObject])with your reward details - Return
output.buildOutput() - Use only approved modules:
axios,lodash,viem, andviem/chains
Functions that don’t follow the required syntax or use unauthorized modules
will fail validation.
Input Data Structure
Your function receives the following input structure:Output Requirements
Your function must return an array with a single reward object containing:- walletAddress: The user’s wallet address to reward
- amount: Points to grant (for fixed point rewards)
- idempotencyKey: Unique identifier to prevent duplicate rewards
- data: Optional JSON string with additional context (max 1KB)
- loyaltyBadgeId: Optional badge to award instead of points
- loyaltyMultiplierAmount: Optional multiplier for range-based rules
Example Use Cases
Dynamic Point Calculation
Conditional Badge Award
Getting Started
1
Learn Function Syntax
Read the Stratus Function Syntax guide to understand the required export patterns and validation rules.
2
Write Your Function
Follow the Function Loyalty Rule Guide for a
complete tutorial on creating custom loyalty rule functions.
3
Test Your Function
Use the function editor in the loyalty rule form to write and test your custom
logic.
4
Deploy and Monitor
Save your rule and monitor function execution through the admin dashboard.
Best Practices
Keep functions simple and focused - Complex logic should be broken into
smaller, testable functions.
Handle errors gracefully - Always include try-catch blocks and meaningful
error messages.
Use idempotency keys - Ensure your functions can be safely retried without
creating duplicate rewards.
Optimize for performance - Minimize external API calls and use efficient
data structures.
Troubleshooting
Function validation errors
Function validation errors
- Ensure your function follows the exact syntax requirements
- Check that you’re only using approved modules
- Verify your output structure matches the required format
Execution failures
Execution failures
- Review function logs for specific error messages - Test your function with sample data before deploying - Ensure all external dependencies are available
Performance issues
Performance issues
- Optimize external API calls and database queries
- Consider caching frequently accessed data
- Monitor function execution time and memory usage
Related Documentation
- Stratus Function Syntax - Learn the required function structure
- Function Loyalty Rule Guide - Complete tutorial for writing loyalty rule functions
- Function Templates - Ready-to-use function examples
- Loyalty API Reference - API documentation for loyalty rules