Skip to content

Commit

Permalink
feat: add support for fetching purchased tokenID
Browse files Browse the repository at this point in the history
  • Loading branch information
VanshSahay committed Feb 15, 2025
1 parent db0766b commit 328dcc4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/DeployDataset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ contract DatasetToken is ERC1155, Ownable, ReentrancyGuard {
// The Bonding Curve contract
DatasetBondingCurve public bondingCurve;

// Mapping to track purchased tokens by address
mapping(address => uint256[]) private _purchasedTokens;
mapping(address => mapping(uint256 => bool)) private _hasPurchased;

// Events
event DatasetTokenMinted(
address[] owners,
Expand Down Expand Up @@ -169,6 +173,10 @@ contract DatasetToken is ERC1155, Ownable, ReentrancyGuard {
*/
function purchaseDataset(uint256 tokenId) external payable nonReentrant {
require(isListed[tokenId], "Dataset is not listed for sale");
require(
!_hasPurchased[msg.sender][tokenId],
"Already purchased this dataset"
);
uint256 currentPrice = bondingCurve.getCurrentPrice(tokenId);
require(msg.value == currentPrice, "Incorrect payment amount");

Expand All @@ -188,6 +196,10 @@ contract DatasetToken is ERC1155, Ownable, ReentrancyGuard {
require(success, "Payment transfer failed");
}

// Record the purchase
_purchasedTokens[msg.sender].push(tokenId);
_hasPurchased[msg.sender][tokenId] = true;

// Record the purchase in the bonding curve
bondingCurve.recordPurchase(tokenId);

Expand All @@ -202,6 +214,30 @@ contract DatasetToken is ERC1155, Ownable, ReentrancyGuard {
emit DatasetPurchased(tokenId, msg.sender, sellers, amounts);
}

/**
* @dev Get all token IDs purchased by an address
* @param buyer The address to check
* @return tokens Array of token IDs purchased by the buyer
*/
function getPurchasedTokens(
address buyer
) public view returns (uint256[] memory tokens) {
return _purchasedTokens[buyer];
}

/**
* @dev Check if an address has purchased a specific token
* @param buyer The address to check
* @param tokenId The token ID to check
* @return bool True if the address has purchased the token
*/
function hasPurchased(
address buyer,
uint256 tokenId
) external view returns (bool) {
return _hasPurchased[buyer][tokenId];
}

/**
* @dev Get token IDs by tag
*/
Expand Down
74 changes: 74 additions & 0 deletions test/DatasetToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,78 @@ contract DatasetTokenTest is Test {
"Price should increase by 1.5x after purchase"
);
}

function test_PurchaseTracking() public {
// First mint a token
vm.startPrank(owner);
datasetToken.mintDatasetToken(
shares,
DATASET_NAME,
DATASET_DESC,
CONTENT_HASH,
IPFS_HASH,
PRICE,
TAGS
);

// Mint a second token
datasetToken.mintDatasetToken(
shares,
"Second Dataset",
DATASET_DESC,
CONTENT_HASH,
IPFS_HASH,
PRICE,
TAGS
);
vm.stopPrank();

// Purchase first token with user3
vm.startPrank(user3);
vm.deal(user3, PRICE * 2);
datasetToken.purchaseDataset{value: PRICE}(0);

// Verify purchase tracking
uint256[] memory purchasedTokens = datasetToken.getPurchasedTokens(
user3
);
assertEq(purchasedTokens.length, 1, "Should have one purchased token");
assertEq(purchasedTokens[0], 0, "Should have purchased token 0");
assertTrue(
datasetToken.hasPurchased(user3, 0),
"Should have purchased token 0"
);
assertFalse(
datasetToken.hasPurchased(user3, 1),
"Should not have purchased token 1"
);

// Purchase second token
datasetToken.purchaseDataset{value: PRICE}(1);

// Verify updated purchase tracking
purchasedTokens = datasetToken.getPurchasedTokens(user3);
assertEq(purchasedTokens.length, 2, "Should have two purchased tokens");
assertEq(purchasedTokens[0], 0, "First purchase should be token 0");
assertEq(purchasedTokens[1], 1, "Second purchase should be token 1");
assertTrue(
datasetToken.hasPurchased(user3, 0),
"Should have purchased token 0"
);
assertTrue(
datasetToken.hasPurchased(user3, 1),
"Should have purchased token 1"
);
vm.stopPrank();

// Verify no purchases for other users
uint256[] memory user1Purchases = datasetToken.getPurchasedTokens(
user1
);
assertEq(user1Purchases.length, 0, "User1 should have no purchases");
assertFalse(
datasetToken.hasPurchased(user1, 0),
"User1 should not have purchased token 0"
);
}
}

0 comments on commit 328dcc4

Please sign in to comment.