-
Notifications
You must be signed in to change notification settings - Fork 0
/
CovalentDemo.sol
59 lines (44 loc) · 2.02 KB
/
CovalentDemo.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol";
contract CovalentDemo is ERC721URIStorage{
//maps tokenIds to item indexes
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
mapping(uint256 => uint256) private itemIndex;
mapping(uint256 => uint256) private salePrice;
constructor(string memory _name, string memory _symbol) ERC721(_name, _symbol) {
}
function setSale(uint256 tokenId, uint256 price) public {
address owner = ownerOf(tokenId);
require(owner != address(0), "setSale: nonexistent token");
require(owner == msg.sender, "setSale: msg.sender is not the owner of the token");
salePrice[tokenId] = price;
}
function buyTokenOnSale(uint256 tokenId) public payable {
uint256 price = salePrice[tokenId];
require(price != 0, "buyToken: price equals 0");
require(msg.value == price, "buyToken: price doesn't equal salePrice[tokenId]");
address payable owner = payable((ownerOf(tokenId)));
approve(address(this), tokenId);
salePrice[tokenId] = 0;
transferFrom(owner, msg.sender, tokenId);
owner.transfer(msg.value);
}
function mintWithIndex(address to, uint256 index, string memory tokenURI) public {
_tokenIds.increment();
uint256 tokenId = _tokenIds.current();
itemIndex[tokenId] = index;
_mint(to, tokenId);
//Here, we will set the metadata hash link of the token metadata from Pinata
_setTokenURI(tokenId, tokenURI);
}
function getItemIndex(uint256 tokenId) public view returns (uint256) {
return itemIndex[tokenId];
}
function getSalePrice(uint256 tokenId) public view returns (uint256) {
return salePrice[tokenId];
}
}