Skip to content

Commit

Permalink
Merge pull request #205 from OriginTrail/improvement/asset-storage-me…
Browse files Browse the repository at this point in the history
…tadata-update

Changed the way tokenURI is constructed for ContentAssetStorageV2
  • Loading branch information
0xbraindevd authored Dec 13, 2023
2 parents c5685c6 + e8b23cb commit 24d3e23
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 60 deletions.
18 changes: 0 additions & 18 deletions abi/ContentAssetStorageV2.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
"internalType": "address",
"name": "hubAddress",
"type": "address"
},
{
"internalType": "string",
"name": "blockchainName_",
"type": "string"
}
],
"stateMutability": "nonpayable",
Expand Down Expand Up @@ -183,19 +178,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "blockchainName",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down
19 changes: 19 additions & 0 deletions abi/Log2PLDSF.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 13 additions & 1 deletion contracts/v1/HubController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 address");
}
}
for (uint j; j < forwardCallsData[i].encodedData.length; ) {
forwardCall(contractAddress, forwardCallsData[i].encodedData[j]);
unchecked {
Expand Down
22 changes: 22 additions & 0 deletions contracts/v1/scoring/log2pldsf.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
}
}
44 changes: 11 additions & 33 deletions contracts/v2/storage/assets/ContentAssetStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
19 changes: 17 additions & 2 deletions deploy/019_deploy_content_asset_storage_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
6 changes: 4 additions & 2 deletions test/v2/unit/ContentAssetStorageV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 24d3e23

Please sign in to comment.