SocialBets
Overview
The SocialBets contract manages social betting functionality within a prediction market ecosystem. Social betting is a feature that allows users to place bets in GPM prediction markets directly from social media platforms. It implements daily spending limits, gas drops for new users, and integrates with oracle contracts for buying positions in prediction markets.
Key Features and Considerations
-
Social Betting: Allows designated social spenders to place bets on behalf of users.
-
Daily Spending Limits: Implements daily spending limits for social bets to manage risk.
-
Gas Drops for New Users: Provides initial gas to new users to facilitate their first interactions.
-
Integration with Oracle Contracts: Works with authorized oracle contracts for buying positions in prediction markets.
-
Flexible Spending Caps: Implements both daily and total spending caps per user.
-
Upgradeable: The contract is designed to be upgradeable using the UUPS pattern.
-
Owner Controls: The contract owner can adjust various parameters like spending limits, gas drop amounts, and authorized spenders/oracles.
Contract Details
- License: MIT
- Solidity Version: ^0.8.24
- Inheritance: Initializable, OwnableUpgradeable, UUPSUpgradeable
- Source Code: SocialBets
Data Structures
Mappings
mapping(address => bool) public oracleContracts;
Tracks authorized oracle contract addresses.
mapping(uint256 => mapping(address => uint256)) public dailySocialSpendings;
Tracks daily social spending for each user. The outer key is the day (timestamp / 86400), and the inner key is the user's address.
mapping(address => bool) public gasDrops;
Tracks whether a user has received an initial gas drop.
mapping(address => uint256) public userTotalSpendings;
Tracks the total amount spent by each user.
Sets
EnumerableSet.AddressSet private socialSpenderSet;
A set to store addresses authorized as social spenders.
State Variables
uint256 public maxBuyAmount;
uint256 public maxDailySocialSpending;
uint256 public initialGasDrop;
uint256 public maxSpendingCapPerUser;
maxBuyAmount
: Maximum amount allowed for a single buy.maxDailySocialSpending
: Maximum daily spending limit for social bets.initialGasDrop
: Amount of initial gas (in wei) given to new users.maxSpendingCapPerUser
: Maximum total spending cap per user.
Events
MaxSpendingCapPerUserUpdated
event MaxSpendingCapPerUserUpdated(uint256 maxSpendingCap)
maxSpendingCap
: The new maximum spending cap per user.
Emitted when the maximum spending cap per user is updated. This event is triggered in the setMaxSpendingCapPerUser() function.
MaxSocialSpendingUpdated
event MaxSocialSpendingUpdated(uint256 maxSpending)
maxSpending
: The new maximum daily social spending limit.
Emitted when the maximum daily social spending limit is updated. This event is triggered in the setMaxDailySocialSpending() function.
SocialTokenSpent
event SocialTokenSpent(address user, uint256 amount, uint256 dayId)
user
: The address of the user who spent the tokens.amount
: The amount of tokens spent in wei.dayId
: The day identifier (timestamp // 86400) when the tokens were spent.
Emitted when tokens are spent on social bets. This event is triggered in the buyPosition() function.
InitialGasDropUpdated
event InitialGasDropUpdated(uint256 initialGasDrop)
initialGasDrop
: The new initial gas drop amount in wei.
Emitted when the initial gas drop amount is updated. This event is triggered in the updateInitialGasDrop() function.
InitialGasDrop
event InitialGasDrop(address user, uint256 amount)
user
: The address of the user receiving the initial gas drop.amount
: The amount of gas (in wei) dropped to the user.
Emitted when a user receives an initial gas drop. This event is triggered in the buyPosition() function when a user receives their first gas drop.
SocialSpendersUpdated
event SocialSpendersUpdated(address[] spenders, bool[] status)
spenders
: An array of addresses that have been updated as social spenders.status
: An array of boolean values indicating whether each address was added (true) or removed (false) as a social spender.
Emitted when the list of social spenders is updated. This event is triggered in the updateSocialSpenders() function.
Function Signatures and Descriptions
initialize
function initialize(address initialOwner) initializer public
initialOwner
: The address that will be set as the owner of the contract.
Initializes the contract, setting up the initial owner.
setMaxSpendingCapPerUser
function setMaxSpendingCapPerUser(uint256 _newSpendingCap) public onlyOwner
_newSpendingCap
: The new maximum amount in wei for a user's total spending.
Sets the maximum spending cap per user.
getMaxDailySpending
function getMaxDailySpending(address user) public view returns (uint256)
user
: The address of the user for whom to calculate the maximum daily spending.
Calculates the maximum daily spending for a user based on their total spendings and the cap.
getAvailableSpending
function getAvailableSpending(address spender) public view returns (uint256)
spender
: The address of the user for whom to calculate the available spending.
Calculates the available spending for a user on the current day.
updateInitialGasDrop
function updateInitialGasDrop(uint256 _initialGasDrop) external onlyOwner
_initialGasDrop
: The new initial gas drop amount in wei.
Updates the initial gas drop amount.
setMaxDailySocialSpending
function setMaxDailySocialSpending(uint256 _maxDailySocialSpending) public onlyOwner
_maxDailySocialSpending
: The new maximum amount in wei for daily social spending.
Sets the maximum daily social spending limit.
addOracleContract
function addOracleContract(address _oracleContract) public onlyOwner
_oracleContract
: The address of the oracle contract to add.
Adds an oracle contract to the list of authorized oracles.
updateMaxBuyAmount
function updateMaxBuyAmount(uint256 _maxBuyAmount) public onlyOwner
_maxBuyAmount
: The new maximum amount in wei for a single transaction.
Updates the maximum buy amount for a single transaction.
updateSocialSpenders
function updateSocialSpenders(address[] calldata _spenders, bool[] calldata _status) external onlyOwner
_spenders
: An array of addresses to update as social spenders._status
: An array of boolean values indicating whether each address was added (true) or removed (false) as a social spender.
Updates the list of social spenders.
getSocialSpenders
function getSocialSpenders() public view returns (address[] memory)
Retrieves the list of current social spenders. Returns an array of addresses that are currently authorized as social spenders.
buyPosition
function buyPosition(
bytes32 questionId,
uint256 outcomeIndex,
uint256 minOutcomeTokensToBuy,
uint256 amount,
address to,
PredictionsOracle _oracleContract
) external payable onlySocialSpender
questionId
: The ID of the question in the prediction market.outcomeIndex
: The index of the outcome in the question.minOutcomeTokensToBuy
: The minimum number of tokens required to buy the outcome.amount
: The amount of tokens to spend on the outcome.to
: The address of the user who will receive the position._oracleContract
: The address of the oracle contract used for buying the position.
Buys a position in a prediction market on behalf of a user. It can only be called by an authorized social spender address. The final position will be held by the user to
. The function also checks if the user has received an initial gas drop and if not, it will send them a gas stipend.
emergencyWithdraw
function emergencyWithdraw() external onlyOwner
Allows the owner to withdraw all balance in case of emergency.
Modifiers
onlySocialSpender
modifier onlySocialSpender()
Restricts function access to authorized social spenders only.
Usage Example
// Initialize the contract
socialBets.initialize(ownerAddress);
// Set up parameters
socialBets.setMaxSpendingCapPerUser(1000 ether);
socialBets.setMaxDailySocialSpending(100 ether);
socialBets.updateInitialGasDrop(0.01 ether);
socialBets.updateMaxBuyAmount(10 ether);
// Add an oracle contract
socialBets.addOracleContract(oracleAddress);
// Update social spenders
address[] memory spenders = new address[](1);
spenders[0] = spenderAddress;
bool[] memory status = new bool[](1);
status[0] = true;
socialBets.updateSocialSpenders(spenders, status);
// Buy a position (called by a social spender)
socialBets.buyPosition(
questionId,
0, // outcome index
1 ether, // min outcome tokens to buy
5 ether, // amount to spend
userAddress, // recipient of the position
PredictionsOracle(oracleAddress)
);
The SocialBets contract provides a mechanism for social betting within the prediction market ecosystem. It allows authorized entities to place bets on behalf of users, implements spending limits to manage risk, and integrates with the broader system through oracle contracts. The contract includes features like initial gas drops for new users and flexible spending caps, making it suitable for a variety of social betting scenarios.