VeBetter: Build X2Earn Apps

Integration (Solidity)

Learn how to integrate VeBetter reward logic into your own Solidity smart contracts by importing the X2Earn interface and calling the distributeReward function onchain.

Solidity Version

Solidity (onchain) fits fully decentralized apps where rewards must be triggered transparently and interact with other contracts. 

We need to do a couple of things here.

First, we need to import the X2Earn contract into our own contract. A smooth way of doing this is by creating an interface before defining your own contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
// Interface for VeBetterDAO X2EarnRewardsPool
interface IX2EarnRewardsPool {
    function distributeReward(
        bytes32 appId,
        uint256 amount,
        address receiver,
        string memory proof
        // Other parameters...
    ) external;
    
    function availableFunds(bytes32 appId) external view returns (uint256);
}
contract YourContract {
        // Your contract code below this line
}

And then we need to include a function that calls the .distributeReward method from the x2EarnRewardsPool contract. Here is a sample:

// Your contract code above this line
function _distributeReward(address studentAddress) private {
        require(rewardAmount > 0, "Reward amount must be greater than 0");
        require(
            rewardAmount <= x2EarnRewardsPoolContract.availableFunds(appId),
            "Insufficient funds in rewards pool"
        );
 // START: Sample code to check if the action is valid and user can be rewarded
        // Mark as rewarded to prevent double rewards
        rewarded[studentAddress] = true;
        
        // Create proof string with submission info
        string memory proof = string(abi.encodePacked(
            '{"type":"education","submission":"',
            submissions[studentAddress],
            '","institute":"',
            institute,
            '"}'
        ));
 // END: Sample code to check if the action is valid and user can be rewarded
        // Call VeBetterDAO to distribute rewards
        x2EarnRewardsPoolContract.distributeReward(
            appId,
            rewardAmount,
            studentAddress,
            proof
        );
        
        emit RewardDistributed(studentAddress, rewardAmount);
    }