0

Currently, I'm working on a project to mint an ERC1155 token. but I need to make a transfer before the mint. Imagine minting this NFT, the user should pay $10, so I have to convert $10 to the ETH amount after that transfer it to the owner of ERC1155, then mint it. I'm using solidity.

pragma solidity ^0.8.27;

import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import {ERC1155Burnable} from "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Burnable.sol";
import {ERC1155Pausable} from "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Pausable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";


contract Cafe3 is ERC1155, Ownable, ERC1155Pausable, ERC1155Burnable {
uint256 public mintRate;
uint256 public tokenUrlRate;

mapping (uint256 => string) private _tokenURIs;

constructor()
    ERC1155("")
    Ownable(msg.sender)
{
    mintRate = 0.0001 ether;
    tokenUrlRate = 0.00001 ether;
}

function setMintRate(uint256 _rate) public onlyOwner{
    mintRate = _rate;
}
function setTokenUrlRate(uint256 _rate) public onlyOwner{
    tokenUrlRate = _rate;
}

function setURI(string memory newuri) public onlyOwner {
    _setURI(newuri);
}

function uri(uint256 tokenId) override public view   returns (string memory) { 
    return(_tokenURIs[tokenId]); 
}

function setTokenUri(uint256 tokenId, string memory tokenURI)  public payable 
{
    require(msg.sender.balance >= tokenUrlRate, "not enough ether sent for uri token");
    _tokenURIs[tokenId] = tokenURI;
}  
function pause() public onlyOwner {
    _pause();
}

function unpause() public onlyOwner {
    _unpause();
}

function mint(address payable account, uint256 id, uint256 amount, bytes memory data)
    payable public 
{
    uint256 cost = mintRate * amount;        
    require(msg.sender.balance >= cost, "not enough ether sent");

    address owner = owner();
    (bool success,) = owner.call{value: cost}("");
    require(success, "Payment failed.");

    _mint(account, id, amount, data);        
}

// The following functions are overrides required by Solidity.

function _update(address from, address to, uint256[] memory ids, uint256[] memory values)
    internal
    override(ERC1155, ERC1155Pausable)
{
    super._update(from, to, ids, values);
}
}

1 Answer 1

0

You're on the right track. You just have to replace msg.sender.balance with msg.value in your require condition.

Replace:

require(msg.sender.balance >= cost, "not enough ether sent");

with:

require(msg.value >= cost, "not enough ether sent");

Then, according to the calculated cost (i.e., mintRate * amount), you've to pass the corresponding value (in Wei) to the VALUE field in Remix IDE.

If amount is 1, the cost would be 0.0001 * 1 i.e., 0.0001 Ether or 100000000000000 Wei.

mint

value

Now, when you execute the mint function, the passed value (i.e., 100000000000000 Wei) will be sent to the owner address.

P.S., If you want the user to pay in USD and then convert it to ETH before transferring to the owner, you need to use an oracle like Chainlink Price Feed in your smart contract.

You can refer to this part of the Cyfrin Updraft Solidity Smart Contract Development Course to understand how to integrate Chainlink Price Feed into the smart contract. This will allow you to fetch real-world price data and input the amount in ETH equivalent to the intended USD value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.