Skip to content

Commit

Permalink
feat: bonding curve contract
Browse files Browse the repository at this point in the history
  • Loading branch information
VanshSahay committed Feb 12, 2025
1 parent 0c8dc77 commit 69d4ffc
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/DatasetBondingCurve.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import {DatasetToken} from "./DeployDataset.sol";

contract DatasetBondingCurve is Ownable {
using Math for uint256;

// The Dataset Token contract
DatasetToken public datasetToken;

// Bonding curve parameters
uint256 public constant PRICE_MULTIPLIER = 1.5 ether; // 1.5x increase per token
uint256 public constant DENOMINATOR = 1 ether;

// Mapping to store initial prices for each token
mapping(uint256 => uint256) public tokenInitialPrices;
// Mapping to store the number of purchases for each token
mapping(uint256 => uint256) public tokenPurchaseCount;

event PriceCalculated(uint256 indexed tokenId, uint256 price);
event InitialPriceSet(uint256 indexed tokenId, uint256 initialPrice);

constructor(
address datasetTokenAddress,
address initialOwner
) Ownable(initialOwner) {
require(
datasetTokenAddress != address(0),
"Invalid dataset token address"
);
datasetToken = DatasetToken(datasetTokenAddress);
}

/**
* @dev Set the initial price for a token's bonding curve
* @param tokenId The ID of the token
* @param initialPrice The initial price in wei
*/
function setInitialPrice(uint256 tokenId, uint256 initialPrice) external {
require(
msg.sender == address(datasetToken),
"Only dataset token contract can set initial price"
);
require(initialPrice > 0, "Initial price must be greater than 0");
require(tokenInitialPrices[tokenId] == 0, "Initial price already set");

tokenInitialPrices[tokenId] = initialPrice;
emit InitialPriceSet(tokenId, initialPrice);
}

/**
* @dev Calculate the price for a specific token based on its bonding curve
* Price = initialPrice * (PRICE_MULTIPLIER ^ purchaseCount)
*/
function calculatePrice(uint256 tokenId) public returns (uint256) {
require(tokenInitialPrices[tokenId] > 0, "Token initial price not set");

uint256 price = tokenInitialPrices[tokenId];
uint256 purchases = tokenPurchaseCount[tokenId];

// Apply multiplier based on number of purchases
for (uint256 i = 0; i < purchases; i++) {
price = (price * PRICE_MULTIPLIER) / DENOMINATOR;
}

emit PriceCalculated(tokenId, price);
return price;
}

/**
* @dev View function to get the current price without emitting an event
*/
function getCurrentPrice(uint256 tokenId) external view returns (uint256) {
require(tokenInitialPrices[tokenId] > 0, "Token initial price not set");

uint256 price = tokenInitialPrices[tokenId];
uint256 purchases = tokenPurchaseCount[tokenId];

// Apply multiplier based on number of purchases
for (uint256 i = 0; i < purchases; i++) {
price = (price * PRICE_MULTIPLIER) / DENOMINATOR;
}

return price;
}

/**
* @dev Record a purchase of a token to update its bonding curve
* @param tokenId The ID of the token that was purchased
*/
function recordPurchase(uint256 tokenId) external {
require(
msg.sender == address(datasetToken),
"Only dataset token contract can record purchases"
);
require(tokenInitialPrices[tokenId] > 0, "Token initial price not set");

tokenPurchaseCount[tokenId]++;
}

/**
* @dev Update the dataset token contract address
* @param newDatasetTokenAddress The new address of the dataset token contract
*/
function updateDatasetTokenAddress(
address newDatasetTokenAddress
) external onlyOwner {
require(
newDatasetTokenAddress != address(0),
"Invalid dataset token address"
);
datasetToken = DatasetToken(newDatasetTokenAddress);
}
}

0 comments on commit 69d4ffc

Please sign in to comment.