Solidity Function That Restricts The Public To Mint Just One ERC721 Token (NFT) With Mint Price of Zero? - solidity

I would like to to request some help in developing a mint function that restricts a public user to mint only ONE NFT with a mint price of 0 (excluding gas fees) - in the case of a promotional giveaway. Is this even possible? I would welcome any suggestions, even a supplementary centralised solution..
Here is my function so far. At the moment, the only way I can restrict the number of free minting NFT is if the owner of the contract executes the minting. But I would like the public user to execute this function, escpecially if the number of free NFTs is a lot and hence associated gas fee. Its based on the OpenZeppelin Contracts:
contract MyTestContract is ERC721, ERC721Enumerable, Ownable {
bool public preLaunchActive = false;
uint256 public maxGiveAway = 3;
function myPreLaunchGiveAway(uint amount, address to) public onlyOwner {
require(preLaunchActive, "preLaunchActive state is not active.");
require(amount <= maxGiveAway, "No more available.");
require(amount > 0, "Amount must be greater than 0.");
for (uint i = 0; i < amount; i++) {
uint256 tokenId = totalSupply();
if (tokenId < maxGiveAway) {
_safeMint(to, tokenId);
}
}
maxGiveAway = maxGiveAway.sub(amount);
}
}

require(balanceOf(msg.sender) <= maxGiveAway,"No more available.!");
Will let user mint allowed number of max give away!

Related

Getting Block Hash of another contract

I've seen some problems with calling functions from other contracts but I believe my problem is fairly genuine to demand a separate question if only to be negated in its possibility.
So I am trying to call a contract within another contract. Is it possible to get the blockhash of a particular block number of the callee contract within my caller? If so how?
Every syntax I've attempted fails for some reason.
Contract A
enter code here
contract DiceGame {
uint256 public nonce = 0;
uint256 public prize = 0;
event Roll(address indexed player, uint256 roll);
event Winner(address winner, uint256 amount);
constructor() payable {
resetPrize();
}
function resetPrize() private {
prize = ((address(this).balance * 10) / 100);
}
function rollTheDice() public payable {
require(msg.value >= 0.002 ether, "Failed to send enough value");
bytes32 prevHash = blockhash(block.number - 1);
bytes32 hash = keccak256(abi.encodePacked(prevHash, address(this), nonce));
uint256 roll = uint256(hash) % 16;
console.log('\t'," Dice Game Roll:",roll);
nonce++;
prize += ((msg.value * 40) / 100);
emit Roll(msg.sender, roll);
if (roll > 2 ) {
return;
}
uint256 amount = prize;
(bool sent, ) = msg.sender.call{value: amount}("");
require(sent, "Failed to send Ether");
resetPrize();
emit Winner(msg.sender, amount);
}
receive() external payable { }
}
Contract B
enter code here
contract RiggedRoll is Ownable {
DiceGame public diceGame;
constructor(address payable diceGameAddress) {
diceGame = DiceGame(diceGameAddress);
}
//Add withdraw function to transfer ether from the rigged contract to an address
//Add riggedRoll() function to predict the randomness in the DiceGame contract and only roll when it's going to be a winner
function riggedRoll(bytes32 riggedHash) public payable {
riggedHash = address(diceGame).blockhash(block.number-1); //I am aware this syntax is broken but I am not able to find a legitimate one to access the data from contract A.
}
//Add receive() function so contract can receive Eth
receive() external payable { }
}
A contract doesn't know when it was last called, unless you explicitly store this information.
Then you can get the block hash using the native blockhash() function (accepts the block number as a parameter).
contract Target {
uint256 public lastCalledAtBlockNumber;
// The value is stored only if you invoke the function using a (read-write) transaction.
// If you invoke the function using a (read-only) call, then it's not stored.
function foo() external {
lastCalledAtBlockNumber = block.number;
}
}
bytes32 blockHash = blockhash(block.number);

How to send ETH to smart contract?

I want an address to send ETH to the smart contract in the RSVP function.
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract MeetingCreation {
struct Meeting {
address meeting_creator;
uint256 meetingID;
string name;
string description;
uint256 start_date;
uint256 end_date;
uint256 max_tickets;
uint256 ticket_price;
uint256 current_tickets_sold;
address[] attendees;
}
function sendETHtoContract(uint256 j) public payable {
}
function RSVP (uint256 MEETINGID) public payable {
//get the Meeting ID
Meeting storage m = meetings[MEETINGID];
require(m.current_tickets_sold < m.max_tickets,
"No more tickets available"
);
require(msg.sender.balance >= m.ticket_price,
"Not enough funds"
);
console.log("funds before paying", msg.sender.balance);
console.log("funds in smart contract before paying", address(this).balance);
//Attempt #1 To send ETH to contract
sendETHtoContract(m.ticket_price);
//Attempt #2 To send ETH to contract
(bool success, ) = address(this).call{value: m.ticket_price}("");
//Attempt #3 to send ETH to contract
payable(address(this).transfer(msg.value));
console.log("funds AFTEr paying", msg.sender.balance);
console.log("funds in smart contract AFTER paying", address(this).balance);
address[] storage adr;
adr = m.attendees;
adr.push(msg.sender);
m.current_tickets_sold += 1;
}
Both sendETHtoContract and (bool, success) result in not sending any ETH when I tried 3 different addresses.
funds before paying 9999949127225257315696
funds in smart contract before paying 0
funds AFTEr paying 9999949127225257315696
funds in smart contract AFTER paying 0
funds before paying 9999955000801324655336
funds in smart contract before paying 0
funds AFTEr paying 9999955000801324655336
funds in smart contract AFTER paying 0
When I tried using the transfer function, this is the error I get when the contract tries to compile:
"send" and "transfer" are only available for objects of type "address payable", not "address".
I assume that you can delete your attempt for the simple reason that when you add payable modifier, it handle the transfer about ETH from address to smart contract and viceversa.
Said this, I think that you're not enhancing msg.value field. If you using remix IDE, before call RSVP function go into DEPLOY & RUN TRANSACTIONS section and put into VALUE textbox a value that you want sending to smart contract and then you can call RSVP function.
I adjust your smart contract delete your attempts. Try it:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract MeetingCreation {
struct Meeting {
address meeting_creator;
uint256 meetingID;
string name;
string description;
uint256 start_date;
uint256 end_date;
uint256 max_tickets;
uint256 ticket_price;
uint256 current_tickets_sold;
address[] attendees;
}
mapping(uint => Meeting) meetings;
function RSVP (uint256 MEETINGID) public payable {
//get the Meeting ID
Meeting storage m = meetings[MEETINGID];
require(m.current_tickets_sold < m.max_tickets,
"No more tickets available"
);
require(msg.sender.balance >= m.ticket_price,
"Not enough funds"
);
address[] storage adr;
adr = m.attendees;
adr.push(msg.sender);
m.current_tickets_sold += 1;
}
// NOTE: Get smart contract balance
function getSmartContractBalance() external view returns(uint) {
return address(this).balance;
}
}

Sending ERC20 tokens using the transfer function

I'm pretty new to programing in solidity and I'm currently trying to run a simple smart contract in Remix, seen bellow:
pragma solidity ^0.8.0;
import "github.com/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
contract Swap {
address public owner;
uint256 public balance;
event TransferReceived(address _from, uint _amount);
event TransferSent(address _from, address _destAddr, uint _amount);
constructor() {
owner = msg.sender;
}
receive() payable external {
balance += msg.value;
emit TransferReceived(msg.sender, msg.value);
}
function withdraw(uint amount, address payable destAddr) public {
require(msg.sender == owner, "Only owner can withdraw funds");
require(amount <= balance, "Insufficient funds");
destAddr.transfer(amount);
balance -= amount;
emit TransferSent(msg.sender, destAddr, amount);
}
function transferERC20(IERC20 token, address to, uint256 amount) public {
require(msg.sender == owner, "Only owner can withdraw funds");
uint256 erc20balance = token.balanceOf(address(this));
require(amount <= erc20balance, "balance is low");
token.transfer(to, amount);
emit TransferSent(msg.sender, to, amount);
}
}
While I can successfully send BNB and call the withdraw function giving the value sent and my wallet address in the BSC testnet, I'm having issues when running the transferERC20 function. The only output that I get when calling this method is the following message:
Gas estimation errored with the following message (see below). The
transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": -32000, "message": "execution
reverted" }
I've tried several different addresses that I found in the testnet.bscscan website for BNB while making sure that the contract had enough funds for transfering, but I had no success.
Can someone suggest what might be going wrong in my contract/setup? Am I making this transfer properly?
fix constrcutor
constructor() {
// payable allows payment of ether with a call.
owner = payable(msg.sender);
}
make sure those require statements are satisfied
require(msg.sender == owner, "Only owner can withdraw funds");
require(amount <= balance, "Insufficient funds");
check that you are connected to correct network

How to add "transaction fee" to ERC20 or BEP20 smart contracts?

There's new tokens that "charge a transaction fee" aside the usual ETH gas fee. I'm talking about a transaction fee that subtracts from the token itself. Let's say 100 tokens are transferred. 1 token goes into a central account as "transaction fee". It's doable since coins like $GRUMPY have it but I can't find the function on the code that would allow me to do that.
intrested here too.
I have something simple see _transfer function below.
Problem is it will create 2 transactions and more Gas Fees to pay.
Dont know if you splitt and send the fee to the contract itself if this reduce fees.
This is just an idea not testet. Intrested in other solutions from the community.
function _transfer(address sender, address recipient, uint256 amount) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
address FeeReceiverAdr = 0xE037D395bB24C69bD0347F4e1652199BF60a41d1;
address payable FeeReceiver = payable(FeeReceiverAdr); // Correct since Solidity >= 0.6.0
_beforeTokenTransfer(sender, recipient, amount);
uint bpsfee = 1618;
uint bps = 100000;
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
_balances[sender] = senderBalance - amount;
_balances[recipient] += taxedValue;
_balances[FeeReceiver] += fee;
emit Transfer(sender, recipient, taxedValue);
emit Transfer(sender, FeeReceiver, fee);
}
I found a solution that may help! In case anyone was still looking around for clarity. Here is a code snippet for enabling ERC20 % distribution. Code has not been audited, so please use it at your own risk, but this should be enough to start experimenting.
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract TransfertTokenAndPercentageToTargetAddress{
// pay 1% of all transactions to target address
//test address = testnet
address payable target = 0x326Da5Aa845675a1967e1F7E31A32a4412eD47DD;
// state variables for your token to track balances and to test
mapping (address => uint) public balanceOf;
uint public totalSupply;
// create a token and assign all the tokens to the creator to test
constructor(uint _totalSupply) public {
totalSupply = _totalSupply;
balanceOf[msg.sender] = totalSupply;
}
// the token transfer function with the addition of a 1% share that
// goes to the target address specified above
function transfer(address _to, uint amount) public {
// calculate the share of tokens for your target address
uint shareForX = amount/100;
// save the previous balance of the sender for later assertion
// verify that all works as intended
uint senderBalance = balanceOf[msg.sender];
// check the sender actually has enough tokens to transfer with function
// modifier
require(senderBalance >= amount, 'Not enough balance');
// reduce senders balance first to prevent the sender from sending more
// than he owns by submitting multiple transactions
balanceOf[msg.sender] -= amount;
// store the previous balance of the receiver for later assertion
// verify that all works as intended
uint receiverBalance = balanceOf[_to];
// add the amount of tokens to the receiver but deduct the share for the
// target address
balanceOf[_to] += amount-shareForX;
// add the share to the target address
balanceOf[target] += shareForX;
// check that everything works as intended, specifically checking that
// the sum of tokens in all accounts is the same before and after
// the transaction.
assert(balanceOf[msg.sender] + balanceOf[_to] + shareForX ==
senderBalance + receiverBalance);
}
}

Why Sender address emitted in event differs from saved in storage?

The wallet address that is sent through event differs from the one stored in contract
Hi, I have a contract that is deployed to development network through truffle.
I trigger function that looks like this:
struct Round {
bool isValue;
uint32 id;
RoundState state;
address[] addresses;
RoundBet[] bets;
mapping(address => bool) betUsers;
mapping(address => uint256) userBets;
uint256 winTicket;
uint256 amount;
uint256 lastTicket;
address winner;
}
.....
event roundBet(
address user,
uint256 amount,
uint256 start,
uint256 end
);
......
function test() payable public {
Round storage round = roundsHistory[currentRound];
require(round.isValue == true);
require(round.state == RoundState.started);
require(msg.value >= MIN_BET);
uint256 amount = msg.value - msg.value % MIN_STEP;
if(!round.betUsers[msg.sender]){
round.addresses.push(msg.sender);
round.betUsers[msg.sender] = true;
}
round.userBets[msg.sender] += amount;
uint256 sticket = round.lastTicket + 1;
uint256 eticket = sticket + amount;
uint256 length = round.bets.push(RoundBet(true, sticket, eticket, msg.sender, amount));
round.amount += amount;
round.lastTicket = eticket;
if(round.addresses.length == 2){
round.state = RoundState.running;
emit roundTimerStart(currentRound);
}
emit roundBet(msg.sender,amount, sticket, eticket);
}
AS you can see I emit roundBet event at the end of function call. The problem is that value of "user" of that event differs from msg.sender that is stored in round.addresses(values stored in round.addresses - is currect and the one emited - is wrong)
If you are using Metamask, keep in mind that it does not switch the account set as msg.sender to the contract, it seems to use the first account (0) to sign every transaction.
We encountered the same problem during a school project.
First of all about platform. It was tron not ethereum. May be in ethereum there is no such issue.
So what I did:
I do not pass address in event. I save it in internal structure
In event I do pass index of saved address in structure
I've wrote a separate method that returns address (and other usefull info) from internal structures by its index.
So by using this workaround I was able to get needed information from contract.