-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAssetFactory.sol
136 lines (107 loc) · 3.92 KB
/
AssetFactory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// SPDX-FileCopyrightText: 2024 Fondazione LINKS
//
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/proxy/Clones.sol";
import "../interfaces/IAsset.sol";
// Uncomment this line to use console.log
// import "hardhat/console.sol";
contract AssetFactory is Ownable {
uint256 private count; // counter of deployed NFT
address private assetContract;
address[] public assets;
mapping(address => address) public assetToOwner;
mapping(address => uint) ownerAssetCount;
struct AssetData {
string name;
string symbol;
string proofId;
string did;
string assetId;
string license; // as SPDX
}
event AssetContractUpdated (address indexed _assetContract);
event NftMinted (
address istanceAddress,
address assetContract,
address owner,
string name,
string symbol,
string proofId
);
constructor (address _assetContract) Ownable(msg.sender) {
count = 0;
updateAssetContract(_assetContract);
}
function tokenize (AssetData memory _assetData) public returns(address instanceAddr) {
require(msg.sender != address(0), "address(0) cannot be an owner");
instanceAddr = Clones.clone(assetContract);
require(instanceAddr != address(0), "Failed to deploy new instance of Asset contract");
assets.push(instanceAddr);
assetToOwner[instanceAddr] = msg.sender;
ownerAssetCount[msg.sender]++;
count += 1;
IAsset asset = IAsset(instanceAddr);
require(asset.initialize(
msg.sender,
address(this),
_assetData.name,
_assetData.symbol,
_assetData.proofId,
_assetData.did,
_assetData.assetId,
_assetData.license
) == true, "Factory: Could not initialize New NFT contract");
require(asset.balanceOf(msg.sender) == 1, "NFT not minted");
emit NftMinted(instanceAddr, assetContract, msg.sender, _assetData.name, _assetData.symbol, _assetData.proofId);
}
function updateAssetContract(address _assetContract) internal onlyOwner {
require(_assetContract != address(0), "Address(0) NOT allowed");
require(isContract_(_assetContract), "Provided address is NOT a valid contract address");
assetContract = _assetContract;
emit AssetContractUpdated(_assetContract);
}
// Address.isContract has been deprecated from openzeppelin v5.x
function isContract_(address account) internal view onlyOwner returns (bool){
uint256 size;
// solhint-disable-next-line no-inline-assembly
assembly {
size := extcodesize(account)
}
return size > 0;
}
// get the number of the created nfts
function getCount() external view returns(uint256) {
return count;
}
function getAssets() external view returns(address[] memory) {
return assets;
}
function getCreatorMintedNfts(address _owner) external view returns(address[] memory) {
address[] memory ownerAssets = new address[](ownerAssetCount[_owner]);
uint256 j = 0;
for(uint256 i = 0; i < count; i++) {
if(_owner == assetToOwner[assets[i]]){
ownerAssets[j] = assets[i];
j++;
}
}
return ownerAssets;
}
function getAssetContract() external view returns(address) {
return assetContract;
}
/**
* @dev fallback function
* this is a default fallback function in which receives
* the collected ether.
*/
fallback() external payable {}
/**
* @dev receive function
* this is a default receive function in which receives
* the collected ether.
*/
receive() external payable {}
}