Skip to content

Commit

Permalink
feat: update Dataset transfer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
VanshSahay committed Feb 14, 2025
1 parent 9b184aa commit db0766b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
6 changes: 1 addition & 5 deletions src/DeployDataset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,14 @@ contract DatasetToken is ERC1155, Ownable, ReentrancyGuard {
DatasetMetadata storage metadata = _tokenMetadata[tokenId];
uint256 totalAmount = msg.value;

// Transfer ownership shares and payments
// Distribute payments to owners
for (uint256 i = 0; i < metadata.owners.length; i++) {
address owner = metadata.owners[i].owner;
require(balanceOf(owner, tokenId) > 0, "Owner has no tokens");

// Calculate owner's share of the payment
uint256 ownerShare = (totalAmount * metadata.owners[i].percentage) /
10000;

// Transfer token
_safeTransferFrom(owner, msg.sender, tokenId, 1, "");

// Transfer payment
(bool success, ) = owner.call{value: ownerShare}("");
require(success, "Payment transfer failed");
Expand Down
41 changes: 30 additions & 11 deletions test/DatasetToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,14 @@ contract DatasetTokenTest is Test {
// Verify metadata
(
string memory name, // description
// contentHash
// ipfsHash
,
,
,
uint256 price,
// contentHash
// ipfsHash
uint256 price, // tags

) = // tags
datasetToken.getDatasetMetadata(0);
) = datasetToken.getDatasetMetadata(0);

assertEq(name, DATASET_NAME, "Name should match");
assertEq(price, PRICE, "Price should match");
Expand Down Expand Up @@ -123,14 +122,34 @@ contract DatasetTokenTest is Test {
vm.deal(user3, PRICE);
datasetToken.purchaseDataset{value: PRICE}(0);

// Verify token ownership transfer
assertEq(datasetToken.balanceOf(user3, 0), 2);
assertEq(datasetToken.balanceOf(user1, 0), 0);
assertEq(datasetToken.balanceOf(user2, 0), 0);
// Verify token ownership remains unchanged
assertEq(
datasetToken.balanceOf(user1, 0),
1,
"User1 should still own the token"
);
assertEq(
datasetToken.balanceOf(user2, 0),
1,
"User2 should still own the token"
);
assertEq(
datasetToken.balanceOf(user3, 0),
0,
"User3 should not own the token"
);

// Verify payment distribution
assertEq(user1.balance, user1InitialBalance + ((PRICE * 7000) / 10000)); // 70%
assertEq(user2.balance, user2InitialBalance + ((PRICE * 3000) / 10000)); // 30%
assertEq(
user1.balance,
user1InitialBalance + ((PRICE * 7000) / 10000),
"User1 should receive 70% of payment"
);
assertEq(
user2.balance,
user2InitialBalance + ((PRICE * 3000) / 10000),
"User2 should receive 30% of payment"
);
}

function test_GetTokensByTag() public {
Expand Down

0 comments on commit db0766b

Please sign in to comment.