Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V8 Paranet #310

Open
wants to merge 3 commits into
base: v8-contracts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions contracts/KnowledgeCollection.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {KnowledgeCollectionStorage} from "./storage/KnowledgeCollectionStorage.s
import {ShardingTableStorage} from "./storage/ShardingTableStorage.sol";
import {IdentityStorage} from "./storage/IdentityStorage.sol";
import {ParametersStorage} from "./storage/ParametersStorage.sol";
import {ParanetKnowledgeAssetsRegistry} from "./storage/paranets/ParanetKnowledgeAssetsRegistry.sol";
import {ParanetKnowledgeMinersRegistry} from "./storage/paranets/ParanetKnowledgeMinersRegistry.sol";
import {ParanetsRegistry} from "./storage/paranets/ParanetsRegistry.sol";
import {KnowledgeCollectionLib} from "./libraries/KnowledgeCollectionLib.sol";
import {TokenLib} from "./libraries/TokenLib.sol";
import {IdentityLib} from "./libraries/IdentityLib.sol";
Expand All @@ -29,6 +32,9 @@ contract KnowledgeCollection is INamed, IVersioned, HubDependent {
IERC20 public tokenContract;
ParametersStorage public parametersStorage;
IdentityStorage public identityStorage;
ParanetKnowledgeAssetsRegistry public paranetKnowledgeAssetsRegistry;
ParanetKnowledgeMinersRegistry public paranetKnowledgeMinersRegistry;
ParaneRegistry public paraneRegistry;

constructor(address hubAddress) HubDependent(hubAddress) {}

Expand All @@ -42,6 +48,13 @@ contract KnowledgeCollection is INamed, IVersioned, HubDependent {
tokenContract = IERC20(hub.getContractAddress("Token"));
parametersStorage = ParametersStorage(hub.getContractAddress("ParametersStorage"));
identityStorage = IdentityStorage(hub.getContractAddress("IdentityStorage"));
paranetKnowledgeAssetsRegistry = ParanetKnowledgeAssetsRegistry(
hub.getContractAddress("ParanetKnowledgeAssetsRegistry")
);
paranetKnowledgeMinersRegistry = ParanetKnowledgeMinersRegistry(
hub.getContractAddress("ParanetKnowledgeMinersRegistry")
);
paranetsRegistry = ParanetsRegistry(hub.getContractAddress("ParanetsRegistry"));
}

function name() public pure virtual returns (string memory) {
Expand Down Expand Up @@ -99,6 +112,7 @@ contract KnowledgeCollection is INamed, IVersioned, HubDependent {
_addTokens(tokenAmount, paymaster);
}

//TODO: If KC part of paranet update
function updateKnowledgeCollection(
uint256 id,
string calldata updateOperationId,
Expand Down Expand Up @@ -183,6 +197,22 @@ contract KnowledgeCollection is INamed, IVersioned, HubDependent {

_validateTokenAmount(byteSize, epochs, tokenAmount, false);
_addTokens(tokenAmount, paymaster);

ParanetKnowledgeAssetsRegistry pkar = paranetKnowledgeAssetsRegistry;
knowledgeCollectionStorageAddress = address(kcs);
if (pkar.isParanetKnowledgeAsset(keccak256(abi.encodePacked(knowledgeCollectionStorageAddress, id)))) {
ParanetKnowledgeMinersRegistry pkmr = paranetKnowledgeMinersRegistry;

bytes32 paranetId = pkar.getParanetId(keccak256(abi.encodePacked(knowledgeCollectionStorageAddress, id)));

// Add Knowledge Asset Token Amount Metadata to the ParanetsRegistry
paranetsRegistry.addCumulativeKnowledgeValue(paranetId, tokenAmount);

// Add Knowledge Asset Token Amount Metadata to the KnowledgeMinersRegistry
pkmr.addCumulativeTracSpent(msg.sender, paranetId, tokenAmount);
pkmr.addUnrewardedTracSpent(msg.sender, paranetId, tokenAmount);
pkmr.addTotalTracSpent(msg.sender, tokenAmount);
}
}

function increaseKnowledgeCollectionTokenAmount(uint256 id, uint96 tokenAmount, address paymaster) external {
Expand All @@ -201,6 +231,23 @@ contract KnowledgeCollection is INamed, IVersioned, HubDependent {
kcs.setTokenAmount(id, oldTokenAmount + tokenAmount);

_addTokens(tokenAmount, paymaster);

ParanetKnowledgeAssetsRegistry pkar = paranetKnowledgeAssetsRegistry;
knowledgeCollectionStorageAddress = address(kcs);
if (pkar.isParanetKnowledgeAsset(keccak256(abi.encodePacked(knowledgeCollectionStorageAddress, id)))) {
ParanetKnowledgeMinersRegistry pkmr = paranetKnowledgeMinersRegistry;

bytes32 paranetId = pkar.getParanetId(keccak256(abi.encodePacked(knowledgeCollectionStorageAddress, id)));

// Add Knowledge Asset Token Amount Metadata to the ParanetsRegistry
paranetsRegistry.addCumulativeKnowledgeValue(paranetId, tokenAmount);

// Add Knowledge Asset Token Amount Metadata to the KnowledgeMinersRegistry
// Question is there some problem when paymaster is used ???
pkmr.addCumulativeTracSpent(msg.sender, paranetId, tokenAmount);
pkmr.addUnrewardedTracSpent(msg.sender, paranetId, tokenAmount);
pkmr.addTotalTracSpent(msg.sender, tokenAmount);
}
}

function _verifySignatures(
Expand Down
165 changes: 91 additions & 74 deletions contracts/paranets/Paranet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {IVersioned} from "../interfaces/IVersioned.sol";
import {ParanetLib} from "../libraries/ParanetLib.sol";
import {ProfileLib} from "../libraries/ProfileLib.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {ERC1155Delta} from "../tokens/ERC1155Delta.sol";

contract Paranet is INamed, IVersioned, ContractStatus, IInitializable {
event ParanetRegistered(
Expand Down Expand Up @@ -842,83 +843,91 @@ contract Paranet is INamed, IVersioned, ContractStatus, IInitializable {
// return knowledgeAssetTokenId;
// }

// function submitKnowledgeAsset(
// address paranetKAStorageContract,
// uint256 paranetKATokenId,
// address knowledgeAssetStorageContract,
// uint256 knowledgeAssetTokenId
// ) external onlyKnowledgeAssetOwner(knowledgeAssetStorageContract, knowledgeAssetTokenId) {
// ParanetsRegistry pr = paranetsRegistry;
// bytes32 paranetId = keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId));

// if (!pr.paranetExists(paranetId)) {
// revert ParanetLib.ParanetDoesntExist(paranetKAStorageContract, paranetKATokenId);
// }
function submitKnowledgeAsset(
address paranetKAStorageContract,
uint256 paranetKATokenId,
address knowledgeAssetStorageContract,
uint256 knowledgeAssetTokenId
) external onlyKnowledgeAssetOwner(knowledgeAssetStorageContract, knowledgeAssetTokenId) {
ParanetsRegistry pr = paranetsRegistry;
bytes32 paranetId = keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId));

// ParanetLib.MinersAccessPolicy minersAccessPolicy = pr.getMinersAccessPolicy(paranetId);
if (!pr.paranetExists(paranetId)) {
revert ParanetLib.ParanetDoesntExist(paranetKAStorageContract, paranetKATokenId);
}

// // Check if paranet is curated and if knowledge miner is whitelisted
// if (
// minersAccessPolicy == ParanetLib.MinersAccessPolicy.CURATED &&
// !pr.isKnowledgeMinerRegistered(paranetId, msg.sender)
// ) {
// revert ParanetLib.ParanetCuratedMinerDoesntExist(paranetId, msg.sender);
// } else if (minersAccessPolicy == ParanetLib.MinersAccessPolicy.OPEN) {
// // Check if Knowledge Miner has profile
// // If not: Create a profile
// if (!paranetKnowledgeMinersRegistry.knowledgeMinerExists(msg.sender)) {
// paranetKnowledgeMinersRegistry.registerKnowledgeMiner(msg.sender);
// }
ParanetLib.MinersAccessPolicy minersAccessPolicy = pr.getMinersAccessPolicy(paranetId);

// // Check if Knowledge Miner is registered on paranet
// if (!pr.isKnowledgeMinerRegistered(paranetId, msg.sender)) {
// pr.addKnowledgeMiner(paranetId, msg.sender);
// }
// }
// Check if paranet is curated and if knowledge miner is whitelisted
if (
minersAccessPolicy == ParanetLib.MinersAccessPolicy.CURATED &&
!pr.isKnowledgeMinerRegistered(paranetId, msg.sender)
) {
revert ParanetLib.ParanetCuratedMinerDoesntExist(paranetId, msg.sender);
} else if (minersAccessPolicy == ParanetLib.MinersAccessPolicy.OPEN) {
// Check if Knowledge Miner has profile
// If not: Create a profile
if (!paranetKnowledgeMinersRegistry.knowledgeMinerExists(msg.sender)) {
paranetKnowledgeMinersRegistry.registerKnowledgeMiner(msg.sender);
}

// if (
// paranetKnowledgeAssetsRegistry.isParanetKnowledgeAsset(
// keccak256(abi.encodePacked(knowledgeAssetStorageContract, knowledgeAssetTokenId))
// )
// ) {
// revert ParanetLib.KnowledgeAssetIsAPartOfOtherParanet(
// knowledgeAssetStorageContract,
// knowledgeAssetTokenId,
// paranetKnowledgeAssetsRegistry.getParanetId(
// keccak256(abi.encodePacked(knowledgeAssetStorageContract, knowledgeAssetTokenId))
// )
// );
// }
// Check if Knowledge Miner is registered on paranet
if (!pr.isKnowledgeMinerRegistered(paranetId, msg.sender)) {
pr.addKnowledgeMiner(paranetId, msg.sender);
}
}

// uint96 remainingTokenAmount = serviceAgreementStorageProxy.getAgreementTokenAmount(
// hashingProxy.callHashFunction(
// HASH_FUNCTION_ID,
// abi.encodePacked(
// address(contentAssetStorage),
// knowledgeAssetTokenId,
// abi.encodePacked(
// address(contentAssetStorage),
// contentAssetStorage.getAssertionIdByIndex(knowledgeAssetTokenId, 0)
// )
// )
// )
// );
if (
paranetKnowledgeAssetsRegistry.isParanetKnowledgeAsset(
keccak256(abi.encodePacked(knowledgeAssetStorageContract, knowledgeAssetTokenId))
)
) {
revert ParanetLib.KnowledgeAssetIsAPartOfOtherParanet(
knowledgeAssetStorageContract,
knowledgeAssetTokenId,
paranetKnowledgeAssetsRegistry.getParanetId(
keccak256(abi.encodePacked(knowledgeAssetStorageContract, knowledgeAssetTokenId))
)
);
}
// This needs to have separet logiic for new and old assets
uint96 remainingTokenAmount = 0;
try ERC1155Delta(knowledgeAssetStorageContract).isOwnerOf(msg.sender, knowledgeAssetTokenId) returns (
bool isOwner
) {
KnowledgeCollectionStorage kcs = KnowledgeCollectionStorage(knowledgeAssetStorageContract);
remainingTokenAmount = kcs.getTokenAmount(knowledgeAssetTokenId);
} catch {
remainingTokenAmount = serviceAgreementStorageProxy.getAgreementTokenAmount(
hashingProxy.callHashFunction(
HASH_FUNCTION_ID,
abi.encodePacked(
address(contentAssetStorage),
knowledgeAssetTokenId,
abi.encodePacked(
address(contentAssetStorage),
contentAssetStorage.getAssertionIdByIndex(knowledgeAssetTokenId, 0)
)
)
)
);
}

// _updateSubmittedKnowledgeAssetMetadata(
// paranetKAStorageContract,
// paranetKATokenId,
// knowledgeAssetStorageContract,
// knowledgeAssetTokenId,
// remainingTokenAmount
// );
_updateSubmittedKnowledgeAssetMetadata(
paranetKAStorageContract,
paranetKATokenId,
knowledgeAssetStorageContract,
knowledgeAssetTokenId,
remainingTokenAmount
);

// emit KnowledgeAssetSubmittedToParanet(
// paranetKAStorageContract,
// paranetKATokenId,
// knowledgeAssetStorageContract,
// knowledgeAssetTokenId
// );
// }
emit KnowledgeAssetSubmittedToParanet(
paranetKAStorageContract,
paranetKATokenId,
knowledgeAssetStorageContract,
knowledgeAssetTokenId
);
}

// function processUpdatedKnowledgeAssetStatesMetadata(
// address paranetKAStorageContract,
Expand Down Expand Up @@ -1069,9 +1078,17 @@ contract Paranet is INamed, IVersioned, ContractStatus, IInitializable {
uint256 knowledgeAssetTokenId
) internal view virtual {
require(hub.isAssetStorage(knowledgeAssetStorageContract), "Given address isn't KA Storage");
require(
IERC721(knowledgeAssetStorageContract).ownerOf(knowledgeAssetTokenId) == msg.sender,
"Caller isn't the owner of the KA"
);
try ERC1155Delta(knowledgeAssetStorageContract).isOwnerOf(msg.sender, knowledgeAssetTokenId) returns (
bool isOwner
) {
require(isOwner, "Caller isn't the owner of the KA");
// TODO: Check for each KA in KC
} catch {
try IERC721(knowledgeAssetStorageContract).ownerOf(knowledgeAssetTokenId) returns (address owner) {
require(owner == msg.sender, "Caller isn't the owner of the KA");
} catch {
revert("Caller isn't the owner of the KA");
}
}
}
}