I would like to utilize the blockchain for my project. I would like put logic of exchanging goods&services into the smart contract to keep all parties safe about their intents and reduce fraud.
I successfully design, deploy and mint my custom token which extends ERC721 standard (I use openzeppelin implementation). At this moment I am working on the contract which receives offers and demands and do exchange between users in case all criteria meets. The issue is how to operate with ERC721 token within this contract (aka marketplace) when you have received just an address of this token.
Remix has similar use case under EnglishAuction name.
IERC721 public nft;
uint public nftId;
address payable public seller;
uint public endAt;
bool public started;
bool public ended;
address public highestBidder;
uint public highestBid;
mapping(address => uint) public bids;
constructor(
address _nft,
uint _nftId,
uint _startingBid
) {
**nft = IERC721(_nft);**
This sample code is able to recreate token by an address of it. I had checked and didn't found this or similar functionality in RFC721 openzeppelin implementation.
Could you please show the right way to operate with openzeppelin token? Is there an alternative way to achieve my goal?
Thanks!
Code to reproduce
pragma solidity ^0.8.4;
import "@openzeppelin/contracts@4.7.2/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts@4.7.2/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts@4.7.2/access/Ownable.sol";
import "@openzeppelin/contracts@4.7.2/utils/Counters.sol";
contract SwapValue is ERC721, ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;
constructor() ERC721("SwapValue", "SWV") {}
function _baseURI() internal pure override returns (string memory) {
return "";
}
function safeMint(address to, string memory uri) public onlyOwner {
uint256 tokenId = _tokenIdCounter.current();
_tokenIdCounter.increment();
_safeMint(to, tokenId);
_setTokenURI(tokenId, uri);
}
// The following functions are overrides required by Solidity.
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
super._burn(tokenId);
}
function tokenURI(uint256 tokenId)
public
view
override(ERC721, ERC721URIStorage)
returns (string memory)
{
return super.tokenURI(tokenId);
}
}
pragma solidity ^0.8.2;
// import "./Model.sol"
import { User } from "./Model.sol";
import "../contract/ISwapChain.sol";
import "../contract/SwapValue.sol";
contract SwapChain is ISwapChain, {
// mapping (address => User) users;
uint256 _usersCounter = 0;
mapping(address => SwapValue[]) _users;
constructor() {}
function registerUser(address user) public override(address) returns (bool) {
require(_users[user] == [], "User should not be registered yet or have any tokens.");
_users[user] = [];
_usersCounter++;
return true;
}
function registerOffer(address user, address nft) public external override(address, address) returns (bool) {
// TODO add SwapValue based on nft address to the _users[user]
}
}
Environment
Remix 0.25.1 , solidity 0.8.2, 0.8.4