-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add weight manager #19
Open
CoderZhi
wants to merge
1
commit into
main
Choose a base branch
from
weight_manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import "./interfaces/IWeightManager.sol"; | ||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
contract WeightManager is Ownable, IWeightManager { | ||
mapping(address => mapping(uint256 => uint256)) public weights; | ||
mapping(address => address) public operators; | ||
|
||
modifier onlyOperator(address _nft) { | ||
if (operators[_nft] != msg.sender) { | ||
revert NotOperator(); | ||
} | ||
_; | ||
} | ||
|
||
function addOperator(address _nft, address _operator) external onlyOwner { | ||
if (operators[_nft] != address(0)) revert DuplicateOperator(); | ||
operators[_nft] = _operator; | ||
emit OperatorSet(_nft, _operator); | ||
} | ||
|
||
function changeOperator(address _nft, address _operator) external onlyOperator(_nft) { | ||
operators[_nft] = _operator; | ||
emit OperatorSet(_nft, _operator); | ||
} | ||
|
||
function setWeight(address _nft, uint256 _tokenId, uint256 _weight) external onlyOperator(_nft) { | ||
weights[_nft][_tokenId] = _weight; | ||
} | ||
|
||
function weight(address _nft, uint256 _tokenId) external view override returns (uint256) { | ||
if (operators[_nft] == address(0)) { | ||
return 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default weight should be 100 |
||
} | ||
return weights[_nft][_tokenId]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
interface IWeightManager { | ||
error DuplicateOperator(); | ||
error NotOperator(); | ||
event OperatorSet(address indexed nft, address indexed operator); | ||
|
||
function weight(address nft, uint256 tokenId) external view returns (uint256); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,16 @@ | ||
pragma solidity ^0.8.0; | ||
|
||
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | ||
import {IWeightedNFT} from "../interfaces/IWeightedNFT.sol"; | ||
|
||
contract TestDeviceNFT is IWeightedNFT, ERC721 { | ||
mapping(uint256 => uint256) public weightOf; | ||
contract TestDeviceNFT is ERC721 { | ||
|
||
constructor(string memory name_, string memory symbol_) ERC721(name_, symbol_) { | ||
_mint(msg.sender, 1); | ||
_mint(msg.sender, 2); | ||
_mint(msg.sender, 3); | ||
weightOf[1] = 1 ether; | ||
weightOf[2] = 2 ether; | ||
weightOf[3] = 3 ether; | ||
} | ||
|
||
function weight(uint256 tokenId) external view returns (uint256) { | ||
return weightOf[tokenId]; | ||
} | ||
|
||
function nft() external view override returns (address) { | ||
return address(this); | ||
} | ||
|
||
function setWeight(uint256 _tokenId, uint256 _weight) public { | ||
weightOf[_tokenId] = _weight; | ||
} | ||
|
||
function mint(address to, uint tokenId) external { | ||
_mint(to, tokenId); | ||
weightOf[tokenId] = 1 ether; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check weight is equals to zero or greater than 100