Commit dec07f3
authored
Create SmartContractRegistry.sol
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract SmartContractRegistry {
struct Contract {
string name;
string version;
address owner;
string sourceCode;
string documentation;
bool isVerified;
uint256 creationTime;
ContractStatus status;
address[] authorizedUsers;
uint256 lastUpdateTime;
}
struct ContractVersion {
string version;
string sourceCode;
uint256 deploymentTime;
string changeLog;
}
enum ContractStatus {
Draft,
UnderReview,
Active,
Deprecated,
Paused
}
// State variables
mapping(bytes32 => Contract) public contracts;
mapping(bytes32 => ContractVersion[]) public contractVersions;
mapping(address => bytes32[]) public userContracts;
mapping(address => bool) public administrators;
uint256 public totalContracts;
address public owner;
bool public paused;
// Events
event ContractCreated(bytes32 indexed contractId, string name, address owner);
event ContractUpdated(bytes32 indexed contractId, string version);
event ContractStatusChanged(bytes32 indexed contractId, ContractStatus newStatus);
event UserAuthorized(bytes32 indexed contractId, address user);
event UserRevoked(bytes32 indexed contractId, address user);
event AdministratorAdded(address administrator);
event AdministratorRemoved(address administrator);
// Modifiers
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
modifier onlyAdmin() {
require(administrators[msg.sender], "Only administrators can call this function");
_;
}
modifier contractExists(bytes32 contractId) {
require(contracts[contractId].owner != address(0), "Contract does not exist");
_;
}
modifier notPaused() {
require(!paused, "Contract is paused");
_;
}
modifier onlyContractOwner(bytes32 contractId) {
require(contracts[contractId].owner == msg.sender, "Only contract owner can call this function");
_;
}
constructor() {
owner = msg.sender;
administrators[msg.sender] = true;
paused = false;
}
// Core functions
function createContract(
string memory name,
string memory version,
string memory sourceCode,
string memory documentation
) external notPaused returns (bytes32) {
require(bytes(name).length > 0, "Name cannot be empty");
require(bytes(version).length > 0, "Version cannot be empty");
require(bytes(sourceCode).length > 0, "Source code cannot be empty");
bytes32 contractId = keccak256(abi.encodePacked(name, msg.sender, block.timestamp));
require(contracts[contractId].owner == address(0), "Contract ID already exists");
Contract storage newContract = contracts[contractId];
newContract.name = name;
newContract.version = version;
newContract.owner = msg.sender;
newContract.sourceCode = sourceCode;
newContract.documentation = documentation;
newContract.isVerified = false;
newContract.creationTime = block.timestamp;
newContract.status = ContractStatus.Draft;
newContract.lastUpdateTime = block.timestamp;
// Add initial version
ContractVersion memory initialVersion = ContractVersion({
version: version,
sourceCode: sourceCode,
deploymentTime: block.timestamp,
changeLog: "Initial version"
});
contractVersions[contractId].push(initialVersion);
// Update user contracts
userContracts[msg.sender].push(contractId);
totalContracts++;
emit ContractCreated(contractId, name, msg.sender);
return contractId;
}
function updateContract(
bytes32 contractId,
string memory newVersion,
string memory newSourceCode,
string memory changeLog
) external contractExists(contractId) onlyContractOwner(contractId) notPaused {
require(bytes(newVersion).length > 0, "Version cannot be empty");
require(bytes(newSourceCode).length > 0, "Source code cannot be empty");
Contract storage contractToUpdate = contracts[contractId];
contractToUpdate.version = newVersion;
contractToUpdate.sourceCode = newSourceCode;
contractToUpdate.lastUpdateTime = block.timestamp;
contractToUpdate.isVerified = false;
// Add new version
ContractVersion memory newContractVersion = ContractVersion({
version: newVersion,
sourceCode: newSourceCode,
deploymentTime: block.timestamp,
changeLog: changeLog
});
contractVersions[contractId].push(newContractVersion);
emit ContractUpdated(contractId, newVersion);
}
function changeContractStatus(
bytes32 contractId,
ContractStatus newStatus
) external contractExists(contractId) onlyAdmin {
contracts[contractId].status = newStatus;
emit ContractStatusChanged(contractId, newStatus);
}
function authorizeUser(
bytes32 contractId,
address user
) external contractExists(contractId) onlyContractOwner(contractId) {
require(user != address(0), "Invalid user address");
require(!isUserAuthorized(contractId, user), "User already authorized");
contracts[contractId].authorizedUsers.push(user);
emit UserAuthorized(contractId, user);
}
function revokeUser(
bytes32 contractId,
address user
) external contractExists(contractId) onlyContractOwner(contractId) {
require(isUserAuthorized(contractId, user), "User not authorized");
Contract storage contractToUpdate = contracts[contractId];
for (uint i = 0; i < contractToUpdate.authorizedUsers.length; i++) {
if (contractToUpdate.authorizedUsers[i] == user) {
// Remove user by replacing with last element and popping
contractToUpdate.authorizedUsers[i] = contractToUpdate.authorizedUsers[contractToUpdate.authorizedUsers.length - 1];
contractToUpdate.authorizedUsers.pop();
break;
}
}
emit UserRevoked(contractId, user);
}
// Admin functions
function addAdministrator(address newAdmin) external onlyOwner {
require(newAdmin != address(0), "Invalid administrator address");
require(!administrators[newAdmin], "Already an administrator");
administrators[newAdmin] = true;
emit AdministratorAdded(newAdmin);
}
function removeAdministrator(address admin) external onlyOwner {
require(admin != owner, "Cannot remove contract owner");
require(administrators[admin], "Not an administrator");
administrators[admin] = false;
emit AdministratorRemoved(admin);
}
function togglePause() external onlyOwner {
paused = !paused;
}
// View functions
function getContract(bytes32 contractId) external view returns (
string memory name,
string memory version,
address owner,
bool isVerified,
uint256 creationTime,
ContractStatus status,
uint256 lastUpdateTime
) {
Contract storage contractData = contracts[contractId];
require(contractData.owner != address(0), "Contract does not exist");
return (
contractData.name,
contractData.version,
contractData.owner,
contractData.isVerified,
contractData.creationTime,
contractData.status,
contractData.lastUpdateTime
);
}
function getContractVersions(bytes32 contractId) external view returns (ContractVersion[] memory) {
return contractVersions[contractId];
}
function getUserContracts(address user) external view returns (bytes32[] memory) {
return userContracts[user];
}
function isUserAuthorized(bytes32 contractId, address user) public view returns (bool) {
Contract storage contractData = contracts[contractId];
if (contractData.owner == user) return true;
for (uint i = 0; i < contractData.authorizedUsers.length; i++) {
if (contractData.authorizedUsers[i] == user) return true;
}
return false;
}
function getContractCount() external view returns (uint256) {
return totalContracts;
}
}
```1 parent d293d0e commit dec07f3
1 file changed
+258
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
0 commit comments