update nft contract

This commit is contained in:
osmannyildiz 2023-12-16 18:19:50 +03:00
parent 94a6b488ed
commit 88c5aa6ec8
1 changed files with 69 additions and 37 deletions

View File

@ -2,60 +2,57 @@
pragma solidity ^0.8.0; pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol"; import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/Base64.sol";
contract Certificate is ERC721, Ownable { contract NftCertificate is ERC721 {
uint256 public nextCertificateId; using Strings for uint256;
uint256 public nextTokenId;
// Mapping to store additional information for each certificate
mapping(uint256 => CertificateInfo) private certificateInfoMapping; mapping(uint256 => CertificateInfo) private certificateInfoMapping;
// Mapping to check if a certificate with a given name and wallet address has been issued // course id => student address => is cert issued
mapping(string => mapping(address => bool)) private issuedCertificates; mapping(string => mapping(address => bool)) private issuedCertificates;
struct CertificateInfo { struct CertificateInfo {
string userName; string studentFullName;
string surName; string courseName;
string email; string instructorFullName;
string companyName;
} }
constructor() ERC721("Certificate", "CERT") Ownable(msg.sender) {} constructor() ERC721("CubLearn Certificate", "CUBCERT") {}
function mintCertificate( function mintCertificate(
uint256 certificateId, string memory courseId,
string memory userName, string memory studentFullName,
string memory surName, string memory courseName,
string memory email, string memory instructorFullName
string memory companyName
) external { ) external {
// Check if the certificate with the given ID has already been issued require(!issuedCertificates[courseId][msg.sender], "Certificate already issued.");
require(!issuedCertificates[userName][msg.sender], "Certificate already issued");
_safeMint(msg.sender, certificateId); uint256 tokenId = nextTokenId;
_storeCertificateInfo(tokenId, studentFullName, courseName, instructorFullName);
// Store additional information using a mapping _safeMint(msg.sender, tokenId);
_storeCertificateInfo(certificateId, userName, surName, email, companyName);
// Mark the certificate as issued // Mark the certificate as issued
issuedCertificates[userName][msg.sender] = true; issuedCertificates[courseId][msg.sender] = true;
// Update the nextCertificateId only if the mint is successful nextTokenId++;
nextCertificateId = certificateId + 1;
} }
function _storeCertificateInfo( function _storeCertificateInfo(
uint256 tokenId, uint256 tokenId,
string memory userName, string memory studentFullName,
string memory surName, string memory courseName,
string memory email, string memory instructorFullName
string memory companyName
) internal { ) internal {
certificateInfoMapping[tokenId] = CertificateInfo({ certificateInfoMapping[tokenId] = CertificateInfo({
userName: userName, studentFullName: studentFullName,
surName: surName, courseName: courseName,
email: email, instructorFullName: instructorFullName
companyName: companyName
}); });
} }
@ -63,13 +60,48 @@ contract Certificate is ERC721, Ownable {
external external
view view
returns ( returns (
string memory userName, CertificateInfo memory certInfo
string memory surName,
string memory email,
string memory companyName
) )
{ {
CertificateInfo storage info = certificateInfoMapping[tokenId]; return certificateInfoMapping[tokenId];
return (info.userName, info.surName, info.email, info.companyName); }
function _getSvg(uint256 tokenId) internal view returns (string memory) {
CertificateInfo storage certInfo = certificateInfoMapping[tokenId];
string memory svg = string(
abi.encodePacked(
unicode"data:image/svg+xml;utf8,<svg width='1024' height='1024' version='1.1' viewBox='0 0 270.93 270.93' xmlns='http://www.w3.org/2000/svg'><g stroke-width='1.75'><rect width='270.93' height='270.93' fill='#fff'/><text x='15.531887' y='33.715561' fill='#000000' font-family='Avenir' font-size='25.4px' letter-spacing='0px' xml:space='preserve'><tspan x='15.531887' y='33.715561' fill='#000000' font-family='Avenir' stroke-width='1.75'>Cub</tspan></text><text x='149.14455' y='33.867962' fill='#000000' font-family='Avenir' font-size='12.7px' letter-spacing='0px' xml:space='preserve'><tspan x='149.14455' y='33.867962' fill='#000000' font-family='Avenir' font-size='12.7px' stroke-width='1.75'>#",
tokenId.toString(),
unicode"</tspan></text><g font-family='Avenir' letter-spacing='0px'><text x='63.156933' y='33.715561' fill='#a11235' font-size='25.4px' xml:space='preserve'><tspan x='63.156933' y='33.715561' fill='#a11235' font-family='Avenir' stroke-width='1.75'>Learn</tspan></text><g><text x='16.209221' y='92.678009' fill='#a11235' font-size='11.289px' xml:space='preserve'><tspan x='16.209221' y='92.678009' font-size='11.289px' stroke-width='1.75'>ÖĞRENCİ</tspan></text><text x='15.531888' y='116.36742' fill='#000000' font-size='22.578px' xml:space='preserve'><tspan x='15.531888' y='116.36742' fill='#000000' font-size='22.578px' stroke-width='1.75'>",
certInfo.studentFullName,
"</tspan></text><text x='16.209221' y='143.1333' fill='#a11235' font-size='11.289px' xml:space='preserve'><tspan x='16.209221' y='143.1333' font-size='11.289px' stroke-width='1.75'>KURS</tspan></text><text x='15.531888' y='166.82274' fill='#000000' font-size='22.578px' xml:space='preserve'><tspan x='15.531888' y='166.82274' fill='#000000' font-size='22.578px' stroke-width='1.75'>",
certInfo.courseName,
unicode"</tspan></text><text x='16.209221' y='193.58873' fill='#a11235' font-size='11.289px' xml:space='preserve'><tspan x='16.209221' y='193.58873' font-size='11.289px' stroke-width='1.75'>EĞİTMEN</tspan></text><text x='15.531888' y='217.27815' fill='#000000' font-size='22.578px' xml:space='preserve'><tspan x='15.531888' y='217.27815' fill='#000000' font-size='22.578px' stroke-width='1.75'>",
certInfo.instructorFullName,
"</tspan></text></g></g></g></svg>"
)
);
return svg;
}
function tokenURI(uint256 tokenId)
external
view
override
returns (string memory)
{
bytes memory json = abi.encodePacked(
'{',
'"name": "CubLearn Certificate #', tokenId.toString(), '",',
'"image_data": "', _getSvg(tokenId), '"',
'}'
);
return string(
abi.encodePacked(
"data:application/json;base64,",
Base64.encode(json)
)
);
} }
} }