-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVault.sol
57 lines (43 loc) · 1.68 KB
/
Vault.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
// SPDX-License-Identifier: GNU-GPL v3.0 or later
pragma solidity 0.8.20;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
contract Vault is ERC1155 {
mapping(uint256 => uint256) public idToRequiredEth;
mapping(uint256 => bool) public payed;
mapping(uint256 => uint256) public nftPrice;
uint256 public fnftsCreated = 0;
function getNftPrice(uint256 id) public view returns (uint256) {
return nftPrice[id];
}
function getRquire(uint256 id) public view returns (uint256) {
return idToRequiredEth[id];
}
function getNextId() public view returns (uint256) {
return fnftsCreated;
}
constructor() payable ERC1155("") {}
function create(uint256 nftAmount, uint256 value) public returns (uint256) {
require(value > 0, "value should be greater than 0");
uint256 id = getNextId();
idToRequiredEth[id] = value * nftAmount;
nftPrice[id] = value;
mint(msg.sender, id, nftAmount);
return id;
}
function payEth(uint256 id) public payable {
require(payed[id] == false, "already payed");
require(msg.value == idToRequiredEth[id], "incorrect eth amount");
payed[id] = true;
}
function withdraw(uint256 id) public {
require(payed[id] == true, "did not unlocked, (completed pay)");
uint256 nftAmount = balanceOf(msg.sender, id);
_burn(msg.sender, id, nftAmount);
(bool success,) = msg.sender.call{value: nftPrice[id] * nftAmount}("");
require(success, "Transfer failed.");
}
function mint(address user, uint256 id, uint256 amount) internal {
_mint(user, id, amount, "");
fnftsCreated++;
}
}