-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFTWallet.sol
89 lines (67 loc) · 3.43 KB
/
NFTWallet.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract CustomNFT is ERC721, Ownable {
constructor() ERC721("CustomNFT", "CNFT") {}
// Mapping to store ERC-20 balances for each token ID
mapping(uint256 => mapping(address => uint256)) private erc20Balances;
// Event to log when ERC-20 tokens are received
event ERC20Received(uint256 tokenId, address from, uint256 amount);
// Event to log when ERC-20 tokens are sent
event ERC20Sent(uint256 tokenId, address to, uint256 amount);
// Event to log when Ether is received
event EtherReceived(uint256 tokenId, address from, uint256 amount);
// Event to log when Ether is sent
event EtherSent(uint256 tokenId, address to, uint256 amount);
// Function to mint a new token and create a unique address for it
function mint() external onlyOwner {
uint256 tokenId = totalSupply() + 1;
_safeMint(msg.sender, tokenId);
}
// Function to check ERC-20 balance for a specific token ID
function getERC20Balance(uint256 tokenId, address erc20Token) external view returns (uint256) {
return erc20Balances[tokenId][erc20Token];
}
// Function to receive ERC-20 tokens
function receiveERC20(uint256 tokenId, address erc20Token, uint256 amount) external {
require(_isApprovedOrOwner(msg.sender, tokenId), "Not approved or owner");
require(amount > 0, "Invalid amount");
// Transfer ERC-20 tokens to the contract
IERC20(erc20Token).transferFrom(msg.sender, address(this), amount);
// Update the ERC-20 balance for the token ID
erc20Balances[tokenId][erc20Token] += amount;
// Emit an event
emit ERC20Received(tokenId, nftOwner(tokenId), amount);
}
// Function to send ERC-20 tokens to a specific address
function sendERC20(uint256 tokenId, address erc20Token, uint256 amount) external {
require(_isApprovedOrOwner(msg.sender, tokenId), "Not approved or owner");
require(amount > 0, "Invalid amount");
require(erc20Balances[tokenId][erc20Token] >= amount, "Insufficient ERC-20 balance");
// Transfer ERC-20 tokens to the owner's address
IERC20(erc20Token).transfer(nftOwner(tokenId), amount);
// Update the ERC-20 balance for the token ID
erc20Balances[tokenId][erc20Token] -= amount;
// Emit an event
emit ERC20Sent(tokenId, nftOwner(tokenId), amount);
}
// Function to receive Ether
receive() external payable {
emit EtherReceived(0, msg.sender, msg.value);
}
// Function to send Ether to a specific address
function sendEther(uint256 tokenId, uint256 amount) external {
require(_isApprovedOrOwner(msg.sender, tokenId), "Not approved or owner");
require(amount > 0 && amount <= address(this).balance, "Invalid amount");
// Transfer Ether to the owner's address
payable(nftOwner(tokenId)).transfer(amount);
// Emit an event
emit EtherSent(tokenId, nftOwner(tokenId), amount);
}
// Internal function to get the owner's address for a given token ID
function nftOwner(uint256 tokenId) internal view returns (address) {
return ownerOf(tokenId);
}
}