Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Jan 13, 2023
1 parent 2d3193e commit e361cef
Show file tree
Hide file tree
Showing 38 changed files with 313 additions and 246 deletions.
7 changes: 5 additions & 2 deletions contracts/interfaces/IExternalRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ interface IExternalRegistry {
)
external
returns (bool success);



function getDIDOwner(bytes32 _did)
external
view
returns (address didOwner);
}
15 changes: 3 additions & 12 deletions contracts/registry/DIDFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ abstract contract DIDFactory is ProvenanceRegistry {
string memory _attributes
)
public
// onlyOwnerProviderOrDelegated(_did)
returns (bool success)
{
return _used(
Expand Down Expand Up @@ -576,8 +575,6 @@ abstract contract DIDFactory is ProvenanceRegistry {
* @return lastUpdatedBy who was the last updating the DID
* @return blockNumberUpdated In which block was the DID updated
* @return providers the list of providers
* @return nftSupply the supply of nfts
* @return mintCap the maximum number of nfts that can be minted
* @return royalties the royalties amount
* @return immutableUrl includes the url to the DDO in immutable storage
* @return nftInitialized if the NFT has been initialized
Expand All @@ -594,8 +591,6 @@ abstract contract DIDFactory is ProvenanceRegistry {
address lastUpdatedBy,
uint256 blockNumberUpdated,
address[] memory providers,
uint256 nftSupply,
uint256 mintCap,
uint256 royalties,
string memory immutableUrl,
bool nftInitialized
Expand All @@ -608,26 +603,22 @@ abstract contract DIDFactory is ProvenanceRegistry {
blockNumberUpdated = didRegisterList
.didRegisters[_did].blockNumberUpdated;
providers = didRegisterList.didRegisters[_did].providers;
nftSupply = didRegisterList.didRegisters[_did].nftSupply;
mintCap = didRegisterList.didRegisters[_did].mintCap;
royalties = didRegisterList.didRegisters[_did].royalties;
immutableUrl = didRegisterList.didRegisters[_did].immutableUrl;
nftInitialized = didRegisterList.didRegisters[_did].nftInitialized;
}

function getDIDSupply(
function getNFTInfo(
bytes32 _did
)
public
view
returns (
uint256 nftSupply,
uint256 mintCap,
address nftContractAddress,
bool nftInitialized
)
{
nftSupply = didRegisterList.didRegisters[_did].nftSupply;
mintCap = didRegisterList.didRegisters[_did].mintCap;
nftContractAddress = didRegisterList.didRegisters[_did].nftContractAddress;
nftInitialized = didRegisterList.didRegisters[_did].nftInitialized;
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/registry/DIDRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ contract DIDRegistry is DIDFactory {
onlyDIDOwner(_did)
returns (bool success)
{
didRegisterList.initializeNftConfig(_did, _royalties > 0 ? defaultRoyalties : IRoyaltyScheme(address(0)));
didRegisterList.initializeNftConfig(_did, _nftAddress, _royalties > 0 ? defaultRoyalties : IRoyaltyScheme(address(0)));
NFT1155Upgradeable _nftInstance;
if (_nftAddress == address(0))
_nftInstance = erc1155;
else
_nftInstance = NFT1155Upgradeable(_nftAddress);

_nftInstance.setNFTAttributes(uint256(_did), 0, _cap,_nftMetadata);
_nftInstance.setNFTAttributes(uint256(_did), 0, _cap, _nftMetadata);

if (_royalties > 0) {
_nftInstance.setTokenRoyalty(uint256(_did), _msgSender(), _royalties);
Expand Down Expand Up @@ -364,7 +364,7 @@ contract DIDRegistry is DIDFactory {
onlyDIDOwner(_did)
returns (bool success)
{
didRegisterList.initializeNft721Config(_did, _royalties > 0 ? defaultRoyalties : IRoyaltyScheme(address(0)));
didRegisterList.initializeNft721Config(_did, _nftContractAddress, _royalties > 0 ? defaultRoyalties : IRoyaltyScheme(address(0)));

NFT721Upgradeable _nftInstance;
if (_nftContractAddress == address(0))
Expand Down
18 changes: 9 additions & 9 deletions contracts/registry/DIDRegistryLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ library DIDRegistryLibrary {
uint8 royalties;
// Flag to control if NFTs config was already initialized
bool nftInitialized;
// Flag to control if NFTs config was already initialized (erc 721)
// bool nft721Initialized;
// Address of the NFT Contract
address nftContractAddress;
// DIDRegistry original creator, this can't be modified after the asset is registered
address creator;
// Checksum associated to the DID
Expand All @@ -38,9 +38,9 @@ library DIDRegistryLibrary {
// Delegates able to register provenance events on behalf of the owner or providers
address[] delegates;
// The NFTs supply associated to the DID
uint256 nftSupply;
// uint256 nftSupply;
// The max number of NFTs associated to the DID that can be minted
uint256 mintCap;
// uint256 mintCap;
address royaltyRecipient;
IRoyaltyScheme royaltyScheme;
// URL to the Metadata in Immutable storage
Expand Down Expand Up @@ -96,39 +96,39 @@ library DIDRegistryLibrary {
* After this initial setup, this data can't be changed anymore for the DID given, even for the owner of the DID.
* The reason of this is to avoid minting additional NFTs after the initial agreement, what could affect the
* valuation of NFTs of a DID already created.
* @dev update the DID registry providers list by adding the mintCap and royalties configuration
* @dev update the DID registry providers list by adding the nftContract and royalties configuration
* @param _self refers to storage pointer
* @param _did refers to decentralized identifier (a byte32 length ID)
* @param _royaltyHandler contract for handling royalties
*/
function initializeNftConfig(
DIDRegisterList storage _self,
bytes32 _did,
// uint256 _cap,
address _nftContractAddress,
IRoyaltyScheme _royaltyHandler
)
internal
{
require(_self.didRegisters[_did].owner != address(0), 'DID not stored');

require(!_self.didRegisters[_did].nftInitialized, 'NFT already initialized');

// _self.didRegisters[_did].mintCap = _cap;
_self.didRegisters[_did].nftContractAddress = _nftContractAddress;
_self.didRegisters[_did].royaltyScheme = _royaltyHandler;
_self.didRegisters[_did].nftInitialized = true;
}

function initializeNft721Config(
DIDRegisterList storage _self,
bytes32 _did,
address _nftContractAddress,
IRoyaltyScheme _royaltyHandler
)
internal
{
require(_self.didRegisters[_did].owner != address(0), 'DID not stored');

require(!_self.didRegisters[_did].nftInitialized, 'NFT already initialized');

_self.didRegisters[_did].nftContractAddress = _nftContractAddress;
_self.didRegisters[_did].royaltyScheme = _royaltyHandler;
_self.didRegisters[_did].nftInitialized = true;
}
Expand Down
8 changes: 6 additions & 2 deletions contracts/royalties/CurveRoyalties.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ contract CurveRoyalties is IRoyaltyScheme, Initializable, Common {
address _token)
external view returns (bool)
{
if (_token != address(registry.erc1155())) {
(address nftContractAddress,) = registry.getNFTInfo(_did);

if (_token != nftContractAddress) {
return false;
}
(uint256 supply, uint256 cap,) = registry.getDIDSupply(_did);
NFT1155Upgradeable _nftInstance = NFT1155Upgradeable(nftContractAddress);
(,uint256 supply,uint256 cap,) = _nftInstance.getNFTAttributes(uint256(_did));

// If there are no royalties everything is good
uint256 rate = royaltyCurve(supply, cap, royalties[_did]);
if (rate == 0) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/templates/BaseEscrowTemplate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ contract BaseEscrowTemplate is AgreementTemplate {
agreementStoreManager.getDIDRegistryAddress()
);

(owner, , , , , providers,,,,,) = didRegistryInstance.getDIDRegister(agreementData.agreementDataItems[_id].did);
(owner, , , , , providers,,,) = didRegistryInstance.getDIDRegister(agreementData.agreementDataItems[_id].did);

if (providers.length > 0) {
accessProvider = providers[0];
Expand Down
17 changes: 13 additions & 4 deletions contracts/token/NFTBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ abstract contract NFTBase is IERC2981Upgradeable, CommonOwnable, AccessControlUp
address indexed _fromAddress,
uint _ercType
);


modifier onlyOperatorOrOwner
{
require(
owner() == _msgSender() || isOperator(_msgSender()),
'Only operator or owner'
);
_;
}

/**
* @dev getNvmConfigAddress get the address of the NeverminedConfig contract
* @return NeverminedConfig contract address
Expand Down Expand Up @@ -167,7 +176,7 @@ abstract contract NFTBase is IERC2981Upgradeable, CommonOwnable, AccessControlUp
string memory _uri
)
public
onlyOwner
onlyOperatorOrOwner
virtual
{
_contractMetadataUri = _uri;
Expand All @@ -185,7 +194,7 @@ abstract contract NFTBase is IERC2981Upgradeable, CommonOwnable, AccessControlUp
)
public
virtual
onlyOwner
onlyOperatorOrOwner
{
AccessControlUpgradeable._setupRole(NVM_OPERATOR_ROLE, account);
}
Expand All @@ -195,7 +204,7 @@ abstract contract NFTBase is IERC2981Upgradeable, CommonOwnable, AccessControlUp
)
public
virtual
onlyOwner
onlyOperatorOrOwner
{
AccessControlUpgradeable._revokeRole(NVM_OPERATOR_ROLE, account);
}
Expand Down
6 changes: 4 additions & 2 deletions contracts/token/erc1155/NFT1155Upgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ contract NFT1155Upgradeable is ERC1155Upgradeable, NFTBase {
}

function mint(address to, uint256 id, uint256 amount, bytes memory data) public {
require(isOperator(_msgSender()) ||
to == owner()
// Only can mint if:
require(isOperator(_msgSender()) || // is contract operator
to == owner() || // is contract owner
_msgSender() == nftRegistry.getDIDOwner(bytes32(id)) // is asset owner
, // Is operator or the NFT owner is _msgSender()
'only operator can mint');

Expand Down
4 changes: 2 additions & 2 deletions contracts/token/erc721/NFT721Upgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ contract NFT721Upgradeable is ERC721Upgradeable, NFTBase {
virtual
{
require(isOperator(_msgSender()) || to == owner(), 'only nft operator can mint');
// require(_nftAttributes[tokenId].nftInitialized, 'NFT not initialized');
require(_nftContractCap == 0 || _counterMinted.current() < _nftContractCap,
'ERC721: Cap exceed'
);

// Update nftSupply
_counterMinted.increment();
_nftAttributes[tokenId].nftSupply = _counterMinted.current();

_nftAttributes[tokenId].nftURI = '';

// Register provenance event
nftRegistry.used(
keccak256(abi.encode(tokenId, _msgSender(), 'mint', 1, block.number)),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nevermined-io/contracts",
"version": "3.0.0-rc2",
"version": "3.0.0-rc3",
"description": "Nevermined implementation of Nevermined in Solidity",
"bugs": {
"url": "https://github.com/nevermined-io/contracts/issues"
Expand Down
4 changes: 2 additions & 2 deletions test/OpenGSN.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ describe('using ethers with OpenGSN', () => {
let balance = await nft.balanceOf(account, did)
assert.strictEqual(20, balance.toNumber())

await didRegistry.burn(did, 5, { from: account })

// await didRegistry.burn(did, 5, { from: account })
await nft.methods['burn(uint256,uint256)'](did, 5, { from: account })
balance = await nft.balanceOf(account, did)
assert.strictEqual(15, balance.toNumber())

Expand Down
7 changes: 3 additions & 4 deletions test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ const utils = {
const nvmConfig = await NeverminedConfig.new()
await nvmConfig.initialize(owner, owner, false)
const royalties = await Royalties.new()
const nft = await NFT.new()
await nft.initialize('')
const didRegistry = await DIDRegistry.new()
await didRegistry.initialize(owner, nft.address, constants.address.zero, nvmConfig.address, royalties.address)
await didRegistry.initialize(owner, constants.address.zero, constants.address.zero, nvmConfig.address, royalties.address)
await royalties.initialize(didRegistry.address)
const nft = await NFT.new()
await nft.initialize(owner, didRegistry.address, 'NFT1155', 'NVM', '')
const conditionStoreManager = await ConditionStoreManager.new()
await conditionStoreManager.initialize(createRole, owner, nvmConfig.address, { from: owner })
await nft.grantOperatorRole(didRegistry.address)
return {
didRegistry,
nvmConfig,
Expand Down
5 changes: 3 additions & 2 deletions test/int/agreement/AccessAgreement_withAuction.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ contract('Access with Auction integration test', (accounts) => {
const { didSeed, agreement, checksum, url } = await prepareAccessAgreement({ timeOutAccess: 10 })

// register DID
await didRegistry.registerMintableDID(
didSeed, checksum, [], url, 10, 0, constants.activities.GENERATED, '', '', { from: creator })
await didRegistry.registerAttribute(didSeed, checksum, [], url, { from: creator })
// await didRegistry.registerMintableDID(
// didSeed, nft.address, checksum, [], url, 10, 0, constants.activities.GENERATED, '', '', { from: creator })

// create agreement
await accessTemplate.createAgreement(initAgreementId, ...Object.values(agreement))
Expand Down
4 changes: 2 additions & 2 deletions test/int/agreement/DynamicAccessAgreement.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ contract('Dynamic Access Template integration test', (accounts) => {

// register DID
await didRegistry.registerMintableDID(
didSeed, checksum, [], url, 10, 0, Activities.GENERATED, '', '', { from: receiver })
didSeed, nft.address, checksum, [], url, 10, 0, Activities.GENERATED, '', '', { from: receiver })

// Mint and Transfer
await didRegistry.mint(agreement.did, 10, { from: receiver })
await nft.methods['mint(uint256,uint256)'](agreement.did, 10, { from: receiver })

await nft.grantOperatorRole(receiver, { from: deployer })
await nft.safeTransferFrom(
Expand Down
8 changes: 4 additions & 4 deletions test/int/agreement/NFTAccessAgreement.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ contract('NFT Access integration test', (accounts) => {

// register DID
await didRegistry.registerMintableDID(
didSeed, checksum, [], url, 10, 0, constants.activities.GENERATED, '', '', { from: sender })
didSeed, nft.address, checksum, [], url, 10, 0, constants.activities.GENERATED, '', '', { from: sender })

// create agreement
await nftAccessTemplate.createAgreement(...Object.values(agreement))

// mint and transfer the nft
await didRegistry.mint(agreement.did, nftAmount, { from: sender })

await nft.grantOperatorRole(sender, { from: deployer })
// mint and transfer the nft
// await didRegistry.mint(agreement.did, nftAmount, { from: sender })
await nft.methods['mint(uint256,uint256)'](agreement.did, nftAmount, { from: sender })

await nft.safeTransferFrom(
sender, receiver, BigInt(agreement.did), nftAmount, '0x', { from: sender })
Expand Down
2 changes: 1 addition & 1 deletion test/int/market/Subscriptions_e2e.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ contract('End to End NFT721 Scenarios', (accounts) => {
did = await didRegistry.hashDID(didSeed, artist)

await didRegistry.registerMintableDID721(
didSeed, checksum, [market], url, royalties, false, constants.activities.GENERATED, '', { from: artist })
didSeed, nft.address, checksum, [market], url, royalties, false, constants.activities.GENERATED, '', { from: artist })
await nft721.setApprovalForAll(transferCondition.address, true, { from: artist })
})

Expand Down
5 changes: 3 additions & 2 deletions test/int/nft/NFT721_e2e.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ contract('End to End NFT721 Scenarios', (accounts) => {
did = await didRegistry.hashDID(didSeed, artist)

await didRegistry.registerMintableDID721(
didSeed, checksum, [], url, royalties, true, constants.activities.GENERATED, '', { from: artist })
didSeed, nft.address, checksum, [], url, royalties, true, constants.activities.GENERATED, '', { from: artist })
})
})

Expand All @@ -510,7 +510,8 @@ contract('End to End NFT721 Scenarios', (accounts) => {

await didRegistry.registerMintableDID721(
didSeed2, checksum, [], url, royalties, false, constants.activities.GENERATED, '', { from: artist })
await didRegistry.mint721(did, { from: artist })
// await didRegistry.mint721(did, { from: artist })
await nft.methods['mint(uint256)'](did, { from: artist })

assert.strictEqual(artist, await nft.ownerOf(did))
})
Expand Down
5 changes: 3 additions & 2 deletions test/int/nft/NFTAccessProofAgreement.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ contract('NFT Access Proof Template integration test', (accounts) => {
did = await didRegistry.hashDID(didSeed, artist)

await didRegistry.registerMintableDID(
didSeed, checksum, [], url, cappedAmount, royalties, constants.activities.GENERATED, '', '', { from: artist })
await didRegistry.mint(did, 5, { from: artist })
didSeed, nft.address, checksum, [], url, cappedAmount, royalties, constants.activities.GENERATED, '', '', { from: artist })
// await didRegistry.mint(did, 5, { from: artist })
await nft.methods['mint(uint256,uint256)'](did, 5, { from: artist })

const balance = await nft.balanceOf(artist, did)
assert.strictEqual(5, balance.toNumber())
Expand Down
4 changes: 2 additions & 2 deletions test/int/nft/NFTAccessSwapAgreement.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ contract('NFT Sales with Access Proof Template integration test', (accounts) =>
did = await didRegistry.hashDID(didSeed, artist)

await didRegistry.registerMintableDID(
didSeed, checksum, [], url, cappedAmount, royalties, constants.activities.GENERATED, '', '', { from: artist })
await didRegistry.mint(did, 5, { from: artist })
didSeed, nft.address, checksum, [], url, cappedAmount, royalties, constants.activities.GENERATED, '', '', { from: artist })
await nft.methods['mint(uint256,uint256)'](did, 5, { from: artist })

const balance = await nft.balanceOf(artist, did)
assert.strictEqual(5, balance.toNumber())
Expand Down

0 comments on commit e361cef

Please sign in to comment.