-
Notifications
You must be signed in to change notification settings - Fork 1
/
FrankMeV2.sol
67 lines (51 loc) · 2.82 KB
/
FrankMeV2.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.25;
contract FrankMeV2 {
struct DigitalAsset {
string id;
uint256 sellerId;
uint256 price;
bool exist;
string linkId;
uint256 timestamp;
uint256 downloads;
}
mapping(string => DigitalAsset) private digitalAssets;
mapping(uint256 => string[]) private digitalAssetsBySeller;
mapping(string => string) private DigitalAssetBylinkId;
event AssetAdded(string indexed id, uint256 sellerId, uint256 price, string linkId, uint256 timestamp);
function addDigitalAsset(string memory _id, uint256 _sellerId, uint256 _price, string memory _linkId) public {
require(!digitalAssets[_id].exist, "Asset with this ID already exists");
require(!digitalAssets[DigitalAssetBylinkId[_linkId]].exist, "Asset with this linkId already exists");
digitalAssets[_id] = DigitalAsset({
id: _id,
sellerId: _sellerId,
price: _price,
exist: true,
linkId: _linkId,
timestamp: block.timestamp,
downloads: 0
});
digitalAssetsBySeller[_sellerId].push(_id);
DigitalAssetBylinkId[_linkId] =_id;
emit AssetAdded(_id, _sellerId, _price, _linkId, digitalAssets[_id].timestamp);
}
function getDigitalAssetsBySeller(uint256 _sellerId) external view returns (DigitalAsset[] memory) {
require(digitalAssetsBySeller[_sellerId].length > 0, "Seller does not exist");
DigitalAsset[] memory assets = new DigitalAsset[](digitalAssetsBySeller[_sellerId].length);
for (uint i = 0; i < digitalAssetsBySeller[_sellerId].length; i++) {
string memory assetId = digitalAssetsBySeller[_sellerId][i];
assets[i] = digitalAssets[assetId];
}
return assets;
}
function getDigitalAssetBylinkId(string memory _linkId) external view returns (string memory id, uint256 sellerId, uint256 price, bool exist, uint256 timestamp, uint256 downloads) {
require(digitalAssets[DigitalAssetBylinkId[_linkId]].exist, "Asset with this linkId does not exist");
return (digitalAssets[DigitalAssetBylinkId[_linkId]].id, digitalAssets[DigitalAssetBylinkId[_linkId]].sellerId, digitalAssets[DigitalAssetBylinkId[_linkId]].price, digitalAssets[DigitalAssetBylinkId[_linkId]].exist, digitalAssets[DigitalAssetBylinkId[_linkId]].timestamp, digitalAssets[DigitalAssetBylinkId[_linkId]].downloads);
}
function purchase(string memory _linkId) public returns (string memory _id){
require(digitalAssets[DigitalAssetBylinkId[_linkId]].exist, "Asset with this linkId does not exist");
digitalAssets[DigitalAssetBylinkId[_linkId]].downloads++;
return (digitalAssets[DigitalAssetBylinkId[_linkId]].id);
}
}