Skip to content

Commit

Permalink
Changed the way tokenURI is constructed for ContentAssetStorageV2, ad…
Browse files Browse the repository at this point in the history
…ded possibility to set asset storage parameters through HubController
  • Loading branch information
u-hubar committed Dec 11, 2023
1 parent 46f5605 commit 74acb2d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 57 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
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 or asset storage address");

Check warning on line 169 in contracts/v1/HubController.sol

View workflow job for this annotation

GitHub Actions / lint

Error message for revert is too long
}
}
for (uint j; j < forwardCallsData[i].encodedData.length; ) {
forwardCall(contractAddress, forwardCallsData[i].encodedData[j]);
unchecked {
Expand Down
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
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 74acb2d

Please sign in to comment.