Skip to content

Commit

Permalink
Added possibility to mine Paranet KnowledgeAsset for someone
Browse files Browse the repository at this point in the history
  • Loading branch information
u-hubar committed Jul 16, 2024
1 parent 490fb7f commit f5669f7
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 66 deletions.
7 changes: 6 additions & 1 deletion abi/ContentAssetV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,12 @@
"inputs": [
{
"internalType": "address",
"name": "originalSender",
"name": "minter",
"type": "address"
},
{
"internalType": "address",
"name": "payer",
"type": "address"
},
{
Expand Down
76 changes: 76 additions & 0 deletions abi/Paranet.json
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,82 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "paranetKAStorageContract",
"type": "address"
},
{
"internalType": "uint256",
"name": "paranetKATokenId",
"type": "uint256"
},
{
"components": [
{
"internalType": "bytes32",
"name": "assertionId",
"type": "bytes32"
},
{
"internalType": "uint128",
"name": "size",
"type": "uint128"
},
{
"internalType": "uint32",
"name": "triplesNumber",
"type": "uint32"
},
{
"internalType": "uint96",
"name": "chunksNumber",
"type": "uint96"
},
{
"internalType": "uint16",
"name": "epochsNumber",
"type": "uint16"
},
{
"internalType": "uint96",
"name": "tokenAmount",
"type": "uint96"
},
{
"internalType": "uint8",
"name": "scoreFunctionId",
"type": "uint8"
},
{
"internalType": "bool",
"name": "immutable_",
"type": "bool"
}
],
"internalType": "struct ContentAssetStructs.AssetInputArgs",
"name": "knowledgeAssetArgs",
"type": "tuple"
},
{
"internalType": "address",
"name": "miner",
"type": "address"
}
],
"name": "mintKnowledgeAssetFor",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "name",
Expand Down
34 changes: 27 additions & 7 deletions contracts/v2/assets/ContentAsset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {Versioned} from "../../v1/interface/Versioned.sol";
import {ContentAssetStructs} from "../../v1/structs/assets/ContentAssetStructs.sol";
import {ServiceAgreementStructsV1} from "../../v1/structs/ServiceAgreementStructsV1.sol";
import {ContentAssetErrors} from "../errors/assets/ContentAssetErrors.sol";
import {ParanetErrors} from "../errors/paranets/ParanetErrors.sol";
import {HASH_FUNCTION_ID} from "../../v1/constants/assets/ContentAssetConstants.sol";
import {LOG2PLDSF_ID, LINEAR_SUM_ID} from "../../v1/constants/ScoringConstants.sol";

Expand Down Expand Up @@ -47,7 +48,7 @@ contract ContentAssetV2 is Named, Versioned, HubDependentV2, Initializable {
event AssetUpdatePaymentIncreased(address indexed assetContract, uint256 indexed tokenId, uint96 tokenAmount);

string private constant _NAME = "ContentAsset";
string private constant _VERSION = "2.1.0";
string private constant _VERSION = "2.2.0";

Assertion public assertionContract;
HashingProxy public hashingProxy;
Expand Down Expand Up @@ -103,6 +104,7 @@ contract ContentAssetV2 is Named, Versioned, HubDependentV2, Initializable {
function createAsset(ContentAssetStructs.AssetInputArgs calldata args) external returns (uint256) {
return
_createAsset(
msg.sender,
msg.sender,
args.assertionId,
args.size,
Expand All @@ -116,12 +118,14 @@ contract ContentAssetV2 is Named, Versioned, HubDependentV2, Initializable {
}

function createAssetFromContract(
address originalSender,
address minter,
address payer,
ContentAssetStructs.AssetInputArgs calldata args
) external onlyContracts returns (uint256) {
return
_createAsset(
originalSender,
minter,
payer,
args.assertionId,
args.size,
args.triplesNumber,
Expand All @@ -145,6 +149,7 @@ contract ContentAssetV2 is Named, Versioned, HubDependentV2, Initializable {
) external returns (uint256) {
return
_createAsset(
msg.sender,
msg.sender,
assertionId,
size,
Expand Down Expand Up @@ -455,13 +460,28 @@ contract ContentAssetV2 is Named, Versioned, HubDependentV2, Initializable {
revert ContentAssetErrors.AssetExpired(tokenId);
}

ParanetKnowledgeAssetsRegistry pkar = paranetKnowledgeAssetsRegistry;

if (pkar.isParanetKnowledgeAsset(keccak256(abi.encodePacked(contentAssetStorageAddress, tokenId)))) {
bytes32 paranetId = pkar.getParanetId(keccak256(abi.encodePacked(contentAssetStorageAddress, tokenId)));

// Add additional tokenAmount to the UpdatingKnowledgeAssets in the KnowledgeMinersRegistry
paranetKnowledgeMinersRegistry.addUpdatingKnowledgeAssetUpdateTokenAmount(
msg.sender,
paranetId,
keccak256(abi.encodePacked(contentAssetStorageAddress, tokenId, unfinalizedState)),
tokenAmount
);
}

sasV1.addUpdateTokens(msg.sender, agreementId, tokenAmount);

emit AssetUpdatePaymentIncreased(contentAssetStorageAddress, tokenId, tokenAmount);
}

function _createAsset(
address originalSender,
address minter,
address payer,
bytes32 assertionId,
uint128 size,
uint32 triplesNumber,
Expand All @@ -474,18 +494,18 @@ contract ContentAssetV2 is Named, Versioned, HubDependentV2, Initializable {
ContentAssetStorage cas = contentAssetStorage;

uint256 tokenId = cas.generateTokenId();
cas.mint(originalSender, tokenId);
cas.mint(minter, tokenId);

assertionContract.createAssertion(assertionId, size, triplesNumber, chunksNumber);
cas.setAssertionIssuer(tokenId, assertionId, originalSender);
cas.setAssertionIssuer(tokenId, assertionId, minter);
cas.setMutability(tokenId, immutable_);
cas.pushAssertionId(tokenId, assertionId);

address contentAssetStorageAddress = address(cas);

serviceAgreementV1.createServiceAgreement(
ServiceAgreementStructsV1.ServiceAgreementInputArgs({
assetCreator: originalSender,
assetCreator: payer,
assetContract: contentAssetStorageAddress,
tokenId: tokenId,
keyword: abi.encodePacked(contentAssetStorageAddress, assertionId),
Expand Down
127 changes: 74 additions & 53 deletions contracts/v2/paranets/Paranet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ contract Paranet is Named, Versioned, ContractStatusV2, Initializable {
);

string private constant _NAME = "Paranet";
string private constant _VERSION = "2.1.3";
string private constant _VERSION = "2.2.0";

ParanetsRegistry public paranetsRegistry;
ParanetServicesRegistry public paranetServicesRegistry;
Expand Down Expand Up @@ -284,50 +284,16 @@ contract Paranet is Named, Versioned, ContractStatusV2, Initializable {
uint256 paranetKATokenId,
ContentAssetStructs.AssetInputArgs calldata knowledgeAssetArgs
) external returns (uint256) {
ParanetsRegistry pr = paranetsRegistry;

// Check if Paranet exists
// If not: Throw an error
if (!pr.paranetExists(keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)))) {
revert ParanetErrors.ParanetDoesntExist(paranetKAStorageContract, paranetKATokenId);
}

// Check if Knowledge Miner has profile
// If not: Create a profile
if (!paranetKnowledgeMinersRegistry.knowledgeMinerExists(msg.sender)) {
paranetKnowledgeMinersRegistry.registerKnowledgeMiner(msg.sender);
}

// Check if Knowledge Miner is registert to paranet
// If not: Register it
if (
!pr.isKnowledgeMinerRegistered(
keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)),
msg.sender
)
) {
pr.addKnowledgeMiner(keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)), msg.sender);
}

// Mint Knowledge Asset
uint256 knowledgeAssetTokenId = contentAsset.createAssetFromContract(msg.sender, knowledgeAssetArgs);

_updateSubmittedKnowledgeAssetMetadata(
paranetKAStorageContract,
paranetKATokenId,
address(contentAssetStorage),
knowledgeAssetTokenId,
knowledgeAssetArgs.tokenAmount
);

emit KnowledgeAssetSubmittedToParanet(
paranetKAStorageContract,
paranetKATokenId,
address(contentAssetStorage),
knowledgeAssetTokenId
);
return _mintKnowledgeAsset(paranetKAStorageContract, paranetKATokenId, knowledgeAssetArgs, msg.sender);
}

return knowledgeAssetTokenId;
function mintKnowledgeAssetFor(
address paranetKAStorageContract,
uint256 paranetKATokenId,
ContentAssetStructs.AssetInputArgs calldata knowledgeAssetArgs,
address miner
) external returns (uint256) {
return _mintKnowledgeAsset(paranetKAStorageContract, paranetKATokenId, knowledgeAssetArgs, miner);
}

function submitKnowledgeAsset(
Expand Down Expand Up @@ -376,7 +342,7 @@ contract Paranet is Named, Versioned, ContractStatusV2, Initializable {
paranetKnowledgeMinersRegistry.registerKnowledgeMiner(msg.sender);
}

// Check if Knowledge Miner is registert to paranet
// Check if Knowledge Miner is registered in the paranet
// If not: Register it
if (
!pr.isKnowledgeMinerRegistered(
Expand All @@ -392,7 +358,8 @@ contract Paranet is Named, Versioned, ContractStatusV2, Initializable {
paranetKATokenId,
knowledgeAssetStorageContract,
knowledgeAssetTokenId,
remainingTokenAmount
remainingTokenAmount,
msg.sender
);

emit KnowledgeAssetSubmittedToParanet(
Expand Down Expand Up @@ -420,12 +387,66 @@ contract Paranet is Named, Versioned, ContractStatusV2, Initializable {
);
}

function _mintKnowledgeAsset(
address paranetKAStorageContract,
uint256 paranetKATokenId,
ContentAssetStructs.AssetInputArgs calldata knowledgeAssetArgs,
address miner
) internal returns (uint256) {
ParanetsRegistry pr = paranetsRegistry;

// Check if Paranet exists
// If not: Throw an error
if (!pr.paranetExists(keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)))) {
revert ParanetErrors.ParanetDoesntExist(paranetKAStorageContract, paranetKATokenId);
}

// Check if Knowledge Miner has profile
// If not: Create a profile
if (!paranetKnowledgeMinersRegistry.knowledgeMinerExists(miner)) {
paranetKnowledgeMinersRegistry.registerKnowledgeMiner(miner);
}

// Check if Knowledge Miner is registered in the paranet
// If not: Register it
if (
!pr.isKnowledgeMinerRegistered(
keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)),
miner
)
) {
pr.addKnowledgeMiner(keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)), miner);
}

// Mint Knowledge Asset
uint256 knowledgeAssetTokenId = contentAsset.createAssetFromContract(miner, msg.sender, knowledgeAssetArgs);

_updateSubmittedKnowledgeAssetMetadata(
paranetKAStorageContract,
paranetKATokenId,
address(contentAssetStorage),
knowledgeAssetTokenId,
knowledgeAssetArgs.tokenAmount,
miner
);

emit KnowledgeAssetSubmittedToParanet(
paranetKAStorageContract,
paranetKATokenId,
address(contentAssetStorage),
knowledgeAssetTokenId
);

return knowledgeAssetTokenId;
}

function _updateSubmittedKnowledgeAssetMetadata(
address paranetKAStorageContract,
uint256 paranetKATokenId,
address knowledgeAssetStorageContract,
uint256 knowledgeAssetTokenId,
uint96 tokenAmount
uint96 tokenAmount,
address miner
) internal {
ParanetsRegistry pr = paranetsRegistry;
ParanetKnowledgeMinersRegistry pkmr = paranetKnowledgeMinersRegistry;
Expand All @@ -435,7 +456,7 @@ contract Paranet is Named, Versioned, ContractStatusV2, Initializable {
keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)),
knowledgeAssetStorageContract,
knowledgeAssetTokenId,
msg.sender
miner
);

// Add Knowledge Asset Metadata to the ParanetsRegistry
Expand All @@ -450,22 +471,22 @@ contract Paranet is Named, Versioned, ContractStatusV2, Initializable {

// Add Knowledge Asset Metadata to the KnowledgeMinersRegistry
pkmr.addSubmittedKnowledgeAsset(
msg.sender,
miner,
keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)),
keccak256(abi.encodePacked(knowledgeAssetStorageContract, knowledgeAssetTokenId))
);
pkmr.addCumulativeTracSpent(
msg.sender,
miner,
keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)),
tokenAmount
);
pkmr.addUnrewardedTracSpent(
msg.sender,
miner,
keccak256(abi.encodePacked(paranetKAStorageContract, paranetKATokenId)),
tokenAmount
);
pkmr.incrementTotalSubmittedKnowledgeAssetsCount(msg.sender);
pkmr.addTotalTracSpent(msg.sender, tokenAmount);
pkmr.incrementTotalSubmittedKnowledgeAssetsCount(miner);
pkmr.addTotalTracSpent(miner, tokenAmount);
}

function _processUpdatedKnowledgeAssetStatesMetadata(
Expand Down
Loading

0 comments on commit f5669f7

Please sign in to comment.