// File: @openzeppelin/contracts/security/ReentrancyGuard.sol
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
// File: @openzeppelin/contracts/utils/Context.sol
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// File: @openzeppelin/contracts/access/Ownable.sol
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// File: contracts/AuraSoulsV1.sol
pragma solidity >=0.8.2 <0.9.0;
contract AuraSoulsV1 is ReentrancyGuard, Ownable {
address public protocolFeeDestination;
address public lpBucketFeeDestination;
uint256 public protocolFeePercent;
uint256 public subjectFeePercent;
uint256 public lpBucketFeePercent;
event Trade(
address trader,
address subject,
bool isBuy,
uint256 soulAmount,
uint256 ethAmount,
uint256 protocolEthAmount,
uint256 subjectEthAmount,
uint256 lpBucketEthAmount,
uint256 supply
);
// SoulsSubject => (Holder => Balance)
mapping(address => mapping(address => uint256)) public soulsBalance;
// SoulsSubject => Supply
mapping(address => uint256) public soulsSupply;
// Track total creator earnings
mapping(address => uint256) public creatorEarnings;
// Constructor
constructor(
address _owner,
address _protocolFeeDestination,
address _lpBucketFeeDestination
) Ownable(_owner) ReentrancyGuard() {
protocolFeeDestination = _protocolFeeDestination;
lpBucketFeeDestination = _lpBucketFeeDestination;
}
// Setters for fee and destinations
function setProtocolFeePercent(uint256 _feePercent) public onlyOwner {
require(_feePercent <= 1 ether, "Fee percent must be <= 100%");
protocolFeePercent = _feePercent;
}
function setSubjectFeePercent(uint256 _feePercent) public onlyOwner {
require(_feePercent <= 1 ether, "Fee percent must be <= 100%");
subjectFeePercent = _feePercent;
}
function setLpBucketFeePercent(uint256 _feePercent) public onlyOwner {
require(_feePercent <= 1 ether, "Fee percent must be <= 100%");
lpBucketFeePercent = _feePercent;
}
function setProtocolFeeDestination(address _feeDestination) public onlyOwner {
protocolFeeDestination = _feeDestination;
}
function setLpBucketFeeDestination(address _feeDestination) public onlyOwner {
lpBucketFeeDestination = _feeDestination;
}
function cumulativeSum(uint256 n) internal pure returns (uint256) {
return (n * (n + 1) * (2 * n + 1)) / 6;
}
function getPrice(uint256 supply, uint256 amount)
public
pure
returns (uint256)
{
uint256 start = supply == 0 ? 0 : supply - 1;
uint256 end = supply + amount - 1;
uint256 sum1 = cumulativeSum(start);
uint256 sum2 = cumulativeSum(end);
uint256 summation = sum2 - sum1;
return (summation * 1 ether) / 16000;
}
function getBuyPriceAfterFee(address soulsSubject, uint256 amount)
public
view
returns (uint256)
{
uint256 price = getPrice(soulsSupply[soulsSubject], amount);
return applyFees(price, true);
}
function getSellPriceAfterFee(address soulsSubject, uint256 amount)
public
view
returns (uint256)
{
uint256 price = getPrice(soulsSupply[soulsSubject] - amount, amount);
return applyFees(price, false);
}
function applyFees(uint256 price, bool isBuy)
internal
view
returns (uint256 finalPrice)
{
uint256 protocolFee = (price * protocolFeePercent) / 1 ether;
uint256 subjectFee = (price * subjectFeePercent) / 1 ether;
uint256 lpBucketFee = (price * lpBucketFeePercent) / 1 ether;
if (isBuy) {
finalPrice = price + protocolFee + subjectFee + lpBucketFee;
} else {
finalPrice = price - protocolFee - subjectFee - lpBucketFee;
}
}
function buySouls(address soulsSubject, uint256 amount)
public
payable
nonReentrant
{
uint256 supply = soulsSupply[soulsSubject];
require(supply > 0 || soulsSubject == msg.sender, "Only the souls' subject can buy the first soul");
uint256 price = getPrice(soulsSupply[soulsSubject], amount);
uint256 totalPrice = applyFees(price, true);
require(msg.value == totalPrice, "Incorrect payment amount");
// Update state
soulsBalance[soulsSubject][msg.sender] += amount;
soulsSupply[soulsSubject] += amount;
creatorEarnings[soulsSubject] += (price * subjectFeePercent) / 1 ether;
emit Trade(
msg.sender,
soulsSubject,
true,
amount,
price,
(price * protocolFeePercent) / 1 ether,
(price * subjectFeePercent) / 1 ether,
(price * lpBucketFeePercent) / 1 ether,
soulsSupply[soulsSubject]
);
// Transfer fees
distributeFees(
(price * protocolFeePercent) / 1 ether,
(price * subjectFeePercent) / 1 ether,
(price * lpBucketFeePercent) / 1 ether,
soulsSubject
);
}
function sellSouls(address soulsSubject, uint256 amount)
public
nonReentrant
{
uint256 supply = soulsSupply[soulsSubject];
require(supply > amount, "Cannot sell the last soul");
uint256 price = getPrice(soulsSupply[soulsSubject] - amount, amount);
uint256 totalPayout = applyFees(price, false);
require(
soulsBalance[soulsSubject][msg.sender] >= amount,
"Insufficient souls"
);
// Update state
soulsBalance[soulsSubject][msg.sender] -= amount;
soulsSupply[soulsSubject] -= amount;
creatorEarnings[soulsSubject] += (price * subjectFeePercent) / 1 ether;
emit Trade(
msg.sender,
soulsSubject,
false,
amount,
price,
(price * protocolFeePercent) / 1 ether,
(price * subjectFeePercent) / 1 ether,
(price * lpBucketFeePercent) / 1 ether,
soulsSupply[soulsSubject]
);
// Transfer funds
distributeFees(
(price * protocolFeePercent) / 1 ether,
(price * subjectFeePercent) / 1 ether,
(price * lpBucketFeePercent) / 1 ether,
soulsSubject
);
(bool success, ) = msg.sender.call{value: totalPayout}("");
require(success, "Unable to send payout");
}
function distributeFees(
uint256 protocolFee,
uint256 subjectFee,
uint256 lpBucketFee,
address soulsSubject
) internal {
(bool success1, ) = protocolFeeDestination.call{value: protocolFee}("");
(bool success2, ) = soulsSubject.call{value: subjectFee}("");
(bool success3, ) = lpBucketFeeDestination.call{value: lpBucketFee}("");
require(success1 && success2 && success3, "Fee transfer failed");
}
}
{
"compilationTarget": {
"AuraSoulsV1.sol": "AuraSoulsV1"
},
"evmVersion": "cancun",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_protocolFeeDestination","type":"address"},{"internalType":"address","name":"_lpBucketFeeDestination","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"OwnableInvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"OwnableUnauthorizedAccount","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"trader","type":"address"},{"indexed":false,"internalType":"address","name":"subject","type":"address"},{"indexed":false,"internalType":"bool","name":"isBuy","type":"bool"},{"indexed":false,"internalType":"uint256","name":"soulAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"ethAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"protocolEthAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"subjectEthAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"lpBucketEthAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"supply","type":"uint256"}],"name":"Trade","type":"event"},{"inputs":[{"internalType":"address","name":"soulsSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"buySouls","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"creatorEarnings","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"soulsSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getBuyPriceAfterFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"supply","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"soulsSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getSellPriceAfterFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lpBucketFeeDestination","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lpBucketFeePercent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"protocolFeeDestination","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"protocolFeePercent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"soulsSubject","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"sellSouls","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_feeDestination","type":"address"}],"name":"setLpBucketFeeDestination","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_feePercent","type":"uint256"}],"name":"setLpBucketFeePercent","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_feeDestination","type":"address"}],"name":"setProtocolFeeDestination","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_feePercent","type":"uint256"}],"name":"setProtocolFeePercent","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_feePercent","type":"uint256"}],"name":"setSubjectFeePercent","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"soulsBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"soulsSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"subjectFeePercent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]