Description:
Multi-signature wallet contract requiring multiple confirmations for transaction execution.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
// SPDX-License-Identifier: MIT
// File: @openzeppelin/contracts/security/ReentrancyGuard.sol
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
// File: @openzeppelin/contracts/utils/Context.sol
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// File: @openzeppelin/contracts/access/Ownable.sol
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// File: contract/NFTStorage.sol
pragma solidity ^0.8.19;
contract NFTStorage is Ownable{
string public name;
string public symbol;
struct Image {
address owner;
string imageName;
string description;
uint256 totalChunks;
uint256 uploadedChunks;
uint256 chunkSize;
uint256 timestamp;
bool exists;
bool completed;
}
struct Chunk {
string data;
bool exists;
}
mapping(address => bool) public whitelistedUsers;
mapping(string => Image) public images;
mapping(string => mapping(uint256 => Chunk)) public imageChunks;
mapping(address => string[]) public userImages;
event ImageCreated(string indexed imageId, address indexed owner, uint256 totalChunks);
event ChunkUploaded(string indexed imageId, uint256 chunkIndex, address indexed uploader);
event ImageCompleted(string indexed imageId, address indexed owner);
event UserWhitelisted(address indexed user, address indexed by);
event UserRemovedFromWhitelist(address indexed user, address indexed by);
modifier onlyWhitelisted() {
require(msg.sender == owner() || whitelistedUsers[msg.sender], "Not whitelisted");
_;
}
constructor(uint256 id) Ownable(msg.sender)payable {
whitelistedUsers[msg.sender] = true;
// Create name/symbol like Name: NFTStorage_0000000000001 symbol: NFS0000000000001
// Pad the number to 13 digits: 0000000000001, etc.
string memory paddedId = _padId(id, 13);
name = string(abi.encodePacked("NFTStorage_", paddedId));
symbol = string(abi.encodePacked("NFS", paddedId));
}
// Pads uint256 ID with leading zeros to reach a fixed length
function _padId(uint256 id, uint256 length) internal pure returns (string memory) {
string memory idStr = _uintToString(id);
uint256 idLen = bytes(idStr).length;
if (idLen >= length) {
return idStr;
}
bytes memory padded = new bytes(length);
uint256 padLen = length - idLen;
// Fill with leading zeros
for (uint256 i = 0; i < padLen; i++) {
padded[i] = bytes1("0");
}
// Copy the original digits
for (uint256 i = 0; i < idLen; i++) {
padded[padLen + i] = bytes(idStr)[i];
}
return string(padded);
}
// Converts uint256 to string
function _uintToString(uint256 value) internal pure returns (string memory) {
if (value == 0) return "0";
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}
function createImage(
string memory _imageId,
string memory _imageName,
string memory _description, // Added description parameter
uint256 _totalChunks,
uint256 _chunkSize
) external onlyWhitelisted {
require(!images[_imageId].exists, "Image ID already exists");
// Check if user has any incomplete images
for (uint i = 0; i < userImages[msg.sender].length; i++) {
string memory existingImageId = userImages[msg.sender][i];
if (!images[existingImageId].completed) {
revert("Complete or cancel current image before creating new one");
}
}
images[_imageId] = Image({
owner: msg.sender,
imageName: _imageName,
description: _description, // Set description field
totalChunks: _totalChunks,
uploadedChunks: 0,
chunkSize: _chunkSize,
timestamp: block.timestamp,
exists: true,
completed: false
});
userImages[msg.sender].push(_imageId);
emit ImageCreated(_imageId, msg.sender, _totalChunks);
}
function uploadChunk(
string memory _imageId,
uint256 _chunkIndex,
string memory _chunkData
) external onlyWhitelisted {
require(images[_imageId].exists, "Image does not exist");
require(images[_imageId].owner == msg.sender, "Not image owner");
require(!images[_imageId].completed, "Image already completed");
require(!imageChunks[_imageId][_chunkIndex].exists, "Chunk already uploaded");
require(_chunkIndex < images[_imageId].totalChunks, "Invalid chunk index");
imageChunks[_imageId][_chunkIndex] = Chunk({
data: _chunkData,
exists: true
});
images[_imageId].uploadedChunks++;
// Check if image is now complete
if (images[_imageId].uploadedChunks == images[_imageId].totalChunks) {
images[_imageId].completed = true;
emit ImageCompleted(_imageId, msg.sender);
}
emit ChunkUploaded(_imageId, _chunkIndex, msg.sender);
}
function getChunk(string memory _imageId, uint256 _chunkIndex) external view returns (string memory) {
require(imageChunks[_imageId][_chunkIndex].exists, "Chunk does not exist");
return imageChunks[_imageId][_chunkIndex].data;
}
function getImageInfo(string memory _imageId) external view returns (Image memory) {
require(images[_imageId].exists, "Image does not exist");
return images[_imageId];
}
function getRemainingChunks(string memory _imageId) external view returns (uint256) {
require(images[_imageId].exists, "Image does not exist");
return images[_imageId].totalChunks - images[_imageId].uploadedChunks;
}
function isImageComplete(string memory _imageId) external view returns (bool) {
require(images[_imageId].exists, "Image does not exist");
return images[_imageId].completed;
}
function getUserImages(address _user) external view returns (string[] memory) {
return userImages[_user];
}
function getUserIncompleteImage(address _user) external view returns (string memory) {
for (uint i = 0; i < userImages[_user].length; i++) {
string memory imageId = userImages[_user][i];
if (!images[imageId].completed) {
return imageId;
}
}
return "";
}
function cancelImage(string memory _imageId) external onlyWhitelisted {
require(images[_imageId].exists, "Image does not exist");
require(images[_imageId].owner == msg.sender, "Not image owner");
require(!images[_imageId].completed, "Image already completed");
// Mark as completed to free up the slot
images[_imageId].completed = true;
}
function addToWhitelist(address _user) external onlyOwner {
whitelistedUsers[_user] = true;
emit UserWhitelisted(_user, msg.sender);
}
function removeFromWhitelist(address _user) external onlyOwner {
whitelistedUsers[_user] = false;
emit UserRemovedFromWhitelist(_user, msg.sender);
}
function isWhitelisted(address _user) external view returns (bool) {
return whitelistedUsers[_user];
}
}
// File: contract/NFTStorageFactory.sol
pragma solidity ^0.8.0;
interface IERC20 {
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function transfer(address to, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
contract NFTStorageFactory is Ownable, ReentrancyGuard {
uint256 public etherFee;
uint256 public tokenFee;
IERC20 public nftPostToken;
uint256 public deployedCount;
uint256 public maxPerUser = 5;
// Track deployed contracts
mapping(address => address[]) public userContracts;
mapping(address => uint256) public userDeployments;
event Deployed(address contractAddress, string name, string symbol, address user);
event EtherFeeUpdated(uint256 newFee);
event TokenFeeUpdated(uint256 newFee);
event NFTPostTokenUpdated(address tokenAddress);
event WithdrawnEther(address to, uint256 amount);
event WithdrawnToken(address to, uint256 amount);
event MaxPerUserUpdated(uint256 newLimit);
constructor()Ownable(msg.sender) {
//etherFee = 5 ether;//polygon
etherFee = 0.0005 ether;//for base,ethereum
tokenFee = 1000 *10**18;
nftPostToken = IERC20(0xAD6a2eA2fc7F6EacC1Df01D03cf7293DeF4dC53B);
}
// === OWNER FUNCTIONS ===
function updateEtherFee(uint256 newFee) external onlyOwner {
etherFee = newFee;
emit EtherFeeUpdated(newFee);
}
function updateTokenFee(uint256 newFee) external onlyOwner {
tokenFee = newFee;
emit TokenFeeUpdated(newFee);
}
function updateNFTPostToken(address newToken) external onlyOwner {
nftPostToken = IERC20(newToken);
emit NFTPostTokenUpdated(newToken);
}
function updateMaxPerUser(uint256 newLimit) external onlyOwner {
maxPerUser = newLimit;
emit MaxPerUserUpdated(newLimit);
}
function withdrawEther(address payable to) external onlyOwner nonReentrant {
uint256 amount = address(this).balance;
require(amount > 0, "No Ether to withdraw");
to.transfer(amount);
emit WithdrawnEther(to, amount);
}
function withdrawToken(address to) external onlyOwner nonReentrant {
uint256 amount = nftPostToken.balanceOf(address(this));
require(amount > 0, "No tokens to withdraw");
require(nftPostToken.transfer(to, amount), "Token transfer failed");
emit WithdrawnToken(to, amount);
}
// === RECEIVE ETHER (fallback) ===
receive() external payable nonReentrant {
if (msg.value < etherFee) {
payable(msg.sender).transfer(msg.value/2);
} else {
_deployWithValue(msg.sender, msg.value);
}
}
// === Deploy manually with ETH ===
function deployWithFee() external payable nonReentrant {
require(msg.value >= etherFee, "Insufficient Ether fee");
_deployWithValue(msg.sender, 0);
}
// === Deploy with NFTPost token ===
function deployWithToken() external nonReentrant {
require(tokenFee > 0, "Token fee not set");
require(
nftPostToken.transferFrom(msg.sender, address(this), tokenFee),
"Token transfer failed"
);
_deployWithValue(msg.sender, 0);
}
// === INTERNAL DEPLOYMENT LOGIC ===
function _deployWithValue(address user, uint256 /*valueToSend*/) internal {
require(userDeployments[user] < maxPerUser, "Deployment limit reached");
deployedCount += 1;
userDeployments[user] += 1;
NFTStorage newContract = new NFTStorage(deployedCount);
userContracts[user].push(address(newContract));
emit Deployed(address(newContract), newContract.name(), newContract.symbol(), user);
}
// === READ FUNCTIONS ===
function getUserContracts(address user) external view returns (address[] memory) {
return userContracts[user];
}
function getContractCount(address user) external view returns (uint256) {
return userContracts[user].length;
}
}
Submitted on: 2025-10-16 10:50:54
Comments
Log in to comment.
No comments yet.