In this example, we assume that you are building a smart contract where users can submit some sustainable action that is being approved/rejected by some moderator. If the action is approved the user can claim his rewards. No backend is involved.
Your contract should look like this:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
// can find here: https://github.com/vechain/vebetterdao-contracts/tree/main/contracts/interfaces
import "./interfaces/IX2EarnRewardsPool.sol";
/**
* Example contract with key functionality to interact with the x2Earn rewards pool
* and distribute rewards.
* You can customize this contract in the way
*/
contract MySustainableAppContract {
IX2EarnRewardsPool public x2EarnRewardsPool;
bytes32 public VBD_APP_ID;
// a dummy mapping pretending you have a way to track and
// validate user actions on chain.
mapping(uint256 actionId => ActionStruct) public sustainableActions;
mapping(uint256 actionId => bool) public rewardsClaimed;
// You will need to setup the address of the X2EarnRewardsPool contract
// and your APP_ID on VeBetterDAO
constructor(IX2EarnRewardsPool _x2EarnRewardsPool, bytes32 _VBD_APP_ID) {
x2EarnRewardsPool = _x2EarnRewardsPool;
VBD_APP_ID = _VBD_APP_ID;
}
/**
* @notice A function that allows a user to claim a reward for a specific
* sustainable action that they performed.
*
* IMPORTANT: the address of this contract must be set as a distributor
* of your APP in order to move funds from the X2EarnRewardsPool contract.
* You can set this on the VeBetterDAO governance app.
*/
function claimReward(uint256 _actionId) external {
// ... some code to check if the action is valid and user can claim
// If distributeReward fails, it will revert
x2EarnRewardsPool.distributeReward(
VBD_APP_ID,
actions[_actionId].rewardAmount,
msg.sender, // this is the user calling the claimReward function
"" // proof and impacts not provided
);
rewardClaimed[_actionId] = true;
emit RewardClaimed(_actionId, msg.sender);
}
}
The address of this contract must be set as a distributor of your APP in order to move funds from the X2EarnRewardsPool contract.
You can set this on the VeBetterDAO governance app.