SPA Buyback

Git Source

Inherits: Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable

Author: Sperax Foundation

This contract allows users to exchange SPA tokens for USDs tokens.

Users can provide SPA tokens and receive USDs tokens in return based on the current exchange rate.

A percentage of the provided SPA tokens are distributed as rewards, and the rest are burned.

State Variables

veSpaRewarder

address public veSpaRewarder;

rewardPercentage

uint256 public rewardPercentage;

oracle

address public oracle;

Functions

constructor

constructor();

initialize

Contract initializer

function initialize(address _veSpaRewarder, uint256 _rewardPercentage) external initializer;

Parameters

NameTypeDescription

_veSpaRewarder

address

Rewarder's address

_rewardPercentage

uint256

Percentage of SPA to be rewarded

withdraw

Emergency withdrawal function for unexpected situations

Can only be called by the owner

function withdraw(address _token, address _receiver, uint256 _amount) external onlyOwner;

Parameters

NameTypeDescription

_token

address

Address of the asset to be withdrawn

_receiver

address

Address of the receiver of tokens

_amount

uint256

Amount of tokens to be withdrawn

updateRewardPercentage

Changes the reward percentage

Example value for _newRewardPercentage = 5000 for 50%

function updateRewardPercentage(uint256 _newRewardPercentage) external onlyOwner;

Parameters

NameTypeDescription

_newRewardPercentage

uint256

New Reward Percentage

updateVeSpaRewarder

Update veSpaRewarder address

function updateVeSpaRewarder(address _newVeSpaRewarder) external onlyOwner;

Parameters

NameTypeDescription

_newVeSpaRewarder

address

is the address of desired veSpaRewarder

updateOracle

Update oracle address

function updateOracle(address _newOracle) external onlyOwner;

Parameters

NameTypeDescription

_newOracle

address

is the address of desired oracle

buyUSDs

Function to buy USDs for SPA for frontend

function buyUSDs(uint256 _spaIn, uint256 _minUSDsOut) external;

Parameters

NameTypeDescription

_spaIn

uint256

Amount of SPA tokens

_minUSDsOut

uint256

Minimum amount out in USDs

getSPAReqdForUSDs

Calculates and returns SPA amount required for _usdsAmount

function getSPAReqdForUSDs(uint256 _usdsAmount) external view returns (uint256);

Parameters

NameTypeDescription

_usdsAmount

uint256

USDs amount the user wants

Returns

NameTypeDescription

uint256

Amount of SPA required

buyUSDs

Buy USDs for SPA if you want a different receiver

function buyUSDs(address _receiver, uint256 _spaIn, uint256 _minUSDsOut) public nonReentrant;

Parameters

NameTypeDescription

_receiver

address

Receiver of USDs

_spaIn

uint256

Amount of SPA tokens

_minUSDsOut

uint256

Minimum amount out in USDs

distributeAndBurnSPA

Sends available SPA in this contract to rewarder based on rewardPercentage and burns the rest

function distributeAndBurnSPA() public;

getUsdsOutForSpa

Returns the amount of USDS for SPA amount in

function getUsdsOutForSpa(uint256 _spaIn) public view returns (uint256);

Parameters

NameTypeDescription

_spaIn

uint256

Amount of SPA tokens

Returns

NameTypeDescription

uint256

Amount of USDs user will get

_getUsdsOutForSpa

Returns the amount of USDS for SPA amount in

function _getUsdsOutForSpa(uint256 _spaIn) private view returns (uint256, uint256);

Parameters

NameTypeDescription

_spaIn

uint256

Amount of SPA tokens

Returns

NameTypeDescription

uint256

Amount of USDs user will get

uint256

_getOracleData

Retrieves price data from the oracle contract for SPA and USDS tokens.

function _getOracleData() private view returns (uint256, uint256, uint256, uint256);

Returns

NameTypeDescription

uint256

The price of USDS in SPA, the price of SPA in USDS, and their respective precisions.

uint256

uint256

uint256

_isValidRewardPercentage

Checks if the provided reward percentage is valid.

The reward percentage must be a non-zero value and should not exceed the maximum percentage value.

function _isValidRewardPercentage(uint256 _rewardPercentage) private pure;

Parameters

NameTypeDescription

_rewardPercentage

uint256

The reward percentage to validate.

Events

BoughtBack

event BoughtBack(
    address indexed receiverOfUSDs, address indexed senderOfSPA, uint256 spaPrice, uint256 spaAmount, uint256 usdsAmount
);

Withdrawn

event Withdrawn(address indexed token, address indexed receiver, uint256 amount);

SPARewarded

event SPARewarded(uint256 spaAmount);

SPABurned

event SPABurned(uint256 spaAmount);

RewardPercentageUpdated

event RewardPercentageUpdated(uint256 newRewardPercentage);

VeSpaRewarderUpdated

event VeSpaRewarderUpdated(address newVeSpaRewarder);

OracleUpdated

event OracleUpdated(address newOracle);

Errors

CannotWithdrawSPA

error CannotWithdrawSPA();

InsufficientUSDsBalance

error InsufficientUSDsBalance(uint256 toSend, uint256 bal);