From 74acb2d5aeaaeb016958239a862c84a69ab51334 Mon Sep 17 00:00:00 2001 From: Uladzislau Hubar Date: Mon, 11 Dec 2023 13:43:22 +0100 Subject: [PATCH 1/4] Changed the way tokenURI is constructed for ContentAssetStorageV2, added possibility to set asset storage parameters through HubController --- abi/ContentAssetStorageV2.json | 18 -------- contracts/v1/HubController.sol | 14 +++++- .../v2/storage/assets/ContentAssetStorage.sol | 44 +++++-------------- deploy/019_deploy_content_asset_storage_v2.ts | 19 +++++++- test/v2/unit/ContentAssetStorageV2.test.ts | 6 ++- utils/helpers.ts | 2 +- 6 files changed, 46 insertions(+), 57 deletions(-) diff --git a/abi/ContentAssetStorageV2.json b/abi/ContentAssetStorageV2.json index 2c4d39db..418789ef 100644 --- a/abi/ContentAssetStorageV2.json +++ b/abi/ContentAssetStorageV2.json @@ -5,11 +5,6 @@ "internalType": "address", "name": "hubAddress", "type": "address" - }, - { - "internalType": "string", - "name": "blockchainName_", - "type": "string" } ], "stateMutability": "nonpayable", @@ -183,19 +178,6 @@ "stateMutability": "view", "type": "function" }, - { - "inputs": [], - "name": "blockchainName", - "outputs": [ - { - "internalType": "string", - "name": "", - "type": "string" - } - ], - "stateMutability": "view", - "type": "function" - }, { "inputs": [ { diff --git a/contracts/v1/HubController.sol b/contracts/v1/HubController.sol index 9f3826e8..ab667f32 100644 --- a/contracts/v1/HubController.sol +++ b/contracts/v1/HubController.sol @@ -156,7 +156,19 @@ contract HubController is Named, Versioned, ContractStatus, Ownable { function _forwardCalls(GeneralStructs.ForwardCallInputArgs[] calldata forwardCallsData) internal { for (uint i; i < forwardCallsData.length; ) { - address contractAddress = hub.getContractAddress(forwardCallsData[i].contractName); + address contractAddress; + + // Try to get the contract address using getContractAddress + try hub.getContractAddress(forwardCallsData[i].contractName) returns (address addr) { + contractAddress = addr; + } catch { + // If getContractAddress fails, try getAssetStorageAddress + try hub.getAssetStorageAddress(forwardCallsData[i].contractName) returns (address addr) { + contractAddress = addr; + } catch { + revert("Failed to get contract or asset storage address"); + } + } for (uint j; j < forwardCallsData[i].encodedData.length; ) { forwardCall(contractAddress, forwardCallsData[i].encodedData[j]); unchecked { diff --git a/contracts/v2/storage/assets/ContentAssetStorage.sol b/contracts/v2/storage/assets/ContentAssetStorage.sol index 2f5c28d0..d9af213e 100644 --- a/contracts/v2/storage/assets/ContentAssetStorage.sol +++ b/contracts/v2/storage/assets/ContentAssetStorage.sol @@ -13,21 +13,18 @@ contract ContentAssetStorageV2 is ContentAssetStorage, IERC4906 { using Strings for address; using Strings for uint256; - string private constant _VERSION = "2.0.0"; + string private constant _VERSION = "2.0.1"; // Interface ID as defined in ERC-4906. This does not correspond to a traditional interface ID as ERC-4906 only // defines events and does not include any external function. bytes4 private constant ERC4906_INTERFACE_ID = bytes4(0x49064906); - string public blockchainName; - uint256 internal _tokenId = 1; string public tokenBaseURI; - constructor(address hubAddress, string memory blockchainName_) ContentAssetStorage(hubAddress) { - blockchainName = blockchainName_; - } + // solhint-disable-next-line no-empty-blocks + constructor(address hubAddress) ContentAssetStorage(hubAddress) {} function version() external pure override returns (string memory) { return _VERSION; @@ -47,41 +44,22 @@ contract ContentAssetStorageV2 is ContentAssetStorage, IERC4906 { } function lastTokenId() public view virtual returns (uint256) { - if (_tokenId <= 0) revert ContentAssetErrors.NoMintedAssets(); + if (_tokenId == 1) revert ContentAssetErrors.NoMintedAssets(); unchecked { return _tokenId - 1; } } - /** - * @dev See {IERC721Metadata-tokenURI}. - */ - function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { - string memory base = tokenBaseURI; - string memory _ual = string( - abi.encodePacked( - "did:dkg:", - blockchainName, - ":", - Strings.toString(block.chainid), - "/", - address(this).toHexString(), - "/", - Strings.toString(tokenId) - ) - ); - - // If there is no base URI, return the Knowledge Asset UAL. - if (bytes(base).length == 0) { - return _ual; - } + function setBaseURI(string memory baseURI) external virtual onlyHubOwner { + tokenBaseURI = baseURI; - return string.concat(base, _ual); + if (_tokenId > 1) { + emit BatchMetadataUpdate(1, lastTokenId()); + } } - function setBaseURI(string memory baseURI) external virtual onlyHubOwner { - tokenBaseURI = baseURI; - emit BatchMetadataUpdate(0, lastTokenId()); + function _baseURI() internal view virtual override returns (string memory) { + return tokenBaseURI; } } diff --git a/deploy/019_deploy_content_asset_storage_v2.ts b/deploy/019_deploy_content_asset_storage_v2.ts index 02e1cb95..be92f66b 100644 --- a/deploy/019_deploy_content_asset_storage_v2.ts +++ b/deploy/019_deploy_content_asset_storage_v2.ts @@ -12,14 +12,29 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { console.log('Deploying ContentAssetStorage V2...'); - await hre.helpers.deploy({ + const ContentAssetStorage = await hre.helpers.deploy({ newContractName: 'ContentAssetStorageV2', newContractNameInHub: 'ContentAssetStorage', passHubInConstructor: true, setContractInHub: false, setAssetStorageInHub: true, - additionalArgs: [hre.network.name.split('_')[0]], }); + + const encodedData = ContentAssetStorage.interface.encodeFunctionData('setBaseURI', [ + `did:dkg:${hre.network.name.split('_')[0]}:${hre.network.config.chainId}/${ContentAssetStorage.address}/`, + ]); + + if (hre.network.config.environment == 'development') { + const { deployer } = await hre.getNamedAccounts(); + + const hubControllerAddress = hre.helpers.contractDeployments.contracts['HubController'].evmAddress; + const HubController = await hre.ethers.getContractAt('HubController', hubControllerAddress, deployer); + + const setBaseURITx = await HubController.forwardCall(ContentAssetStorage.address, encodedData); + await setBaseURITx.wait(); + } else { + hre.helpers.setParametersEncodedData.push(['ContentAssetStorage', [encodedData]]); + } }; export default func; diff --git a/test/v2/unit/ContentAssetStorageV2.test.ts b/test/v2/unit/ContentAssetStorageV2.test.ts index 654510e0..b7fb6035 100644 --- a/test/v2/unit/ContentAssetStorageV2.test.ts +++ b/test/v2/unit/ContentAssetStorageV2.test.ts @@ -74,7 +74,7 @@ describe('@v2 @unit ContentAssetStorageV2', function () { // Test for successful deployment it('Should deploy successfully with correct initial parameters', async function () { expect(await ContentAssetStorageV2.name()).to.equal('ContentAssetStorage'); - expect(await ContentAssetStorageV2.version()).to.equal('2.0.0'); + expect(await ContentAssetStorageV2.version()).to.equal('2.0.1'); }); // Test for ERC4906 interface support @@ -109,7 +109,9 @@ describe('@v2 @unit ContentAssetStorageV2', function () { await expect( HubController.forwardCall( ContentAssetStorageV2.address, - ContentAssetStorageV2.interface.encodeFunctionData('setBaseURI', ['https://dkg.resolver.origintrail.io/']), + ContentAssetStorageV2.interface.encodeFunctionData('setBaseURI', [ + `https://dkg.resolver.origintrail.io/did:dkg:hardhat:31337/${ContentAssetStorageV2.address.toLowerCase()}/`, + ]), ), ).to.emit(ContentAssetStorageV2, 'BatchMetadataUpdate'); diff --git a/utils/helpers.ts b/utils/helpers.ts index 5c5f5e02..5f3c2b47 100644 --- a/utils/helpers.ts +++ b/utils/helpers.ts @@ -213,7 +213,7 @@ export class Helpers { await this.updateDeploymentsJson(nameInHub, newContract.address); - return await this.hre.ethers.getContractAt(nameInHub, newContract.address, deployer); + return await this.hre.ethers.getContractAt(newContractName, newContract.address, deployer); } public async updateContractParameters(contractName: string, contract: Contract) { From 7f24df971f6af63111253eb9ef45fdaf19e87c21 Mon Sep 17 00:00:00 2001 From: Uladzislau Hubar Date: Mon, 11 Dec 2023 13:58:03 +0100 Subject: [PATCH 2/4] Shortened revert message in the HubController --- contracts/v1/HubController.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/v1/HubController.sol b/contracts/v1/HubController.sol index ab667f32..ed8470dd 100644 --- a/contracts/v1/HubController.sol +++ b/contracts/v1/HubController.sol @@ -166,7 +166,7 @@ contract HubController is Named, Versioned, ContractStatus, Ownable { try hub.getAssetStorageAddress(forwardCallsData[i].contractName) returns (address addr) { contractAddress = addr; } catch { - revert("Failed to get contract or asset storage address"); + revert("Failed to get contract address"); } } for (uint j; j < forwardCallsData[i].encodedData.length; ) { From 03a0468c68da0ea3eb4239b4228e3a3602a9083f Mon Sep 17 00:00:00 2001 From: Uladzislau Hubar Date: Mon, 11 Dec 2023 14:01:39 +0100 Subject: [PATCH 3/4] Bumped version to 4.1.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3ba968a4..4713dc92 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dkg-evm-module", - "version": "4.1.0", + "version": "4.1.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "dkg-evm-module", - "version": "4.1.0", + "version": "4.1.1", "license": "Apache-2.0", "dependencies": { "@openzeppelin/contracts": "^4.9.3", diff --git a/package.json b/package.json index 44485cd2..8e2de2d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dkg-evm-module", - "version": "4.1.0", + "version": "4.1.1", "description": "Smart contracts for OriginTrail V6", "main": "index.ts", "files": [ From e8b23cbb13f76f87e41b9eae040610c9a7e86d86 Mon Sep 17 00:00:00 2001 From: Uladzislau Hubar Date: Tue, 12 Dec 2023 09:53:12 +0100 Subject: [PATCH 4/4] Added 'ParameterChanged' event for Log2PLDSF --- abi/Log2PLDSF.json | 19 +++++++++++++++++++ contracts/v1/scoring/log2pldsf.sol | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/abi/Log2PLDSF.json b/abi/Log2PLDSF.json index c38ea491..a7759b1d 100644 --- a/abi/Log2PLDSF.json +++ b/abi/Log2PLDSF.json @@ -21,6 +21,25 @@ "name": "PRBMathUD60x18__LogInputTooSmall", "type": "error" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "string", + "name": "parameterName", + "type": "string" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "parameterValue", + "type": "uint256" + } + ], + "name": "ParameterChanged", + "type": "event" + }, { "inputs": [], "name": "a", diff --git a/contracts/v1/scoring/log2pldsf.sol b/contracts/v1/scoring/log2pldsf.sol index 4c7bf13d..d014a417 100644 --- a/contracts/v1/scoring/log2pldsf.sol +++ b/contracts/v1/scoring/log2pldsf.sol @@ -15,6 +15,8 @@ import {PRBMathUD60x18} from "@prb/math/contracts/PRBMathUD60x18.sol"; contract Log2PLDSF is IScoreFunction, Indexable, Named, HubDependent, Initializable { using PRBMathUD60x18 for uint256; + event ParameterChanged(string parameterName, uint256 parameterValue); + uint8 private constant _ID = 1; string private constant _NAME = "Log2PLDSF"; @@ -102,41 +104,61 @@ contract Log2PLDSF is IScoreFunction, Indexable, Named, HubDependent, Initializa function setDistanceMappingCoefficient(uint256 distanceRangeMax) external onlyHubOwner { distanceMappingCoefficient = type(uint256).max / distanceRangeMax; + + emit ParameterChanged("distanceMappingCoefficient", distanceMappingCoefficient); } function setStakeRangeMax(uint96 stakeRangeMax_) external onlyHubOwner { stakeRangeMax = stakeRangeMax_; + + emit ParameterChanged("stakeRangeMax", stakeRangeMax); } function setMultiplier(uint32 multiplier_) external onlyHubOwner { multiplier = multiplier_; + + emit ParameterChanged("multiplier", multiplier); } function setLogArgumentConstant(uint32 logArgumentConstant_) external onlyHubOwner { logArgumentConstant = logArgumentConstant_; + + emit ParameterChanged("logArgumentConstant", logArgumentConstant); } function setA(uint32 a_) external onlyHubOwner { a = a_; + + emit ParameterChanged("a", a); } function setStakeExponent(uint32 stakeExponent_) external onlyHubOwner { stakeExponent = stakeExponent_; + + emit ParameterChanged("stakeExponent", stakeExponent); } function setB(uint32 b_) external onlyHubOwner { b = b_; + + emit ParameterChanged("b", b); } function setC(uint32 c_) external onlyHubOwner { c = c_; + + emit ParameterChanged("c", c); } function setDistanceExponent(uint32 distanceExponent_) external onlyHubOwner { distanceExponent = distanceExponent_; + + emit ParameterChanged("distanceExponent", distanceExponent); } function setD(uint32 d_) external onlyHubOwner { d = d_; + + emit ParameterChanged("d", d); } }