attempting to understand gas limits, and why I'm getting infinite gas and extremely high gas fees for the constructor function - solidity

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
contract Coin {
address public minter;
mapping(address => uint) public balances;
event Sent(address from, address to, uint amount);
constructor() {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
balances[receiver] += amount;
}
error InsufficientBalance(uint requested, uint available);
function send(address receiver, uint amount) public {
if (amount > balances[msg.sender])
revert InsufficientBalance({
requested: amount,
available: balances[msg.sender]
});
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
While scrolling and attempting to understand ALL aspects of solidity, I can't understand gas fees. Why they say infinite and why it says the "constructor" function is so high.

Remix IDE contains an estimator that is still in active development (February 2023), and it's not perfect yet.
In some cases it's simply not able to estimate the amount of gas that the function is going to consume, and then it shows Infinity or undefined.
For example, it can currently estimate simple assigning to the value, but it's not yet able to estimate increasing the value:
But it doesn't mean that the actual consumption is going to be infinite.

Related

How to deposit eth to contract writen in solidity on front-end app using useDapp

Given i have this smart contract in remix
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract DepositWithdraw {
mapping(address => uint256) private balances;
event Deposit(address indexed depositor, uint256 amount);
event Withdrawal(address indexed withdrawer, uint256 amount);
function deposit() public payable {
balances[msg.sender] += msg.value;
emit Deposit(msg.sender, msg.value);
}
function withdraw(uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
emit Withdrawal(msg.sender, amount);
}
function balanceOf(address account) public view returns (uint256) {
return balances[account];
}
}
Deposit by remix ide works, but when I'm trying to deposit using useDapp lib in javascript by using useContractFunction I can't pass the amount to deposit. When I'm redirected to meta mask to accept transaction there is no amount shown.
my front-end code:
import { Contract } from 'ethers';
import { Interface } from 'ethers/lib/utils';
import { useContractFunction } from '#usedapp/core';
const contractAddress = // my contract address;
const ABI = new Interface(
//...copied contract ABI from remix
);
const contract = new Contract(contractAddress, ABI);
const { send } = useContractFunction(contract, 'deposit');
// send(value) will error that deposit function argument length is no correct (it want 0 arguments)
tried adding third argument with options to useContractFunction but no success.
When i call send without arguments it will process to metamask but no amount shown, only gas fees
any ideas?

payments ( pre set value) in solidity

i am developing a Movie renting smartcontract. Where the owner can add new movies, clients can search movies and pay for the movies they select.Adding and searching is working to my liking.
problem: i want to develop the pay function as such- where it takes one argument( the title of the movie) and clients has to pay the exact amount set by the owner, he can not pay less then the price.
for example: owner add a movie: title-titanic,price-10 eth. when client use the pay function he put the title titanic and pay 10 eth. if he tries to pay less or more the transaction will not be successful.
here is the code
pragma solidity 0.8.7;
// SPDX-License-Identifier: MIT
contract Movie{
address public owner;
struct Move{
uint year;
uint price;
}
mapping (string => Move ) public movieInfo;
mapping(uint => Move) amount;
constructor() payable{
owner= msg.sender;
}
function addMovie(string memory _title, uint _year, uint _price) public {
require( msg.sender==owner);
movieInfo[_title]= Move(_year, _price);
}
function pay(string memory _title) public payable{
}
function totalFunds() public view returns(uint){
return address(this).balance;
}
}
Don't think that this kind of stuff is good to store into the blockchain ATM, due to the highly cost of storing.
In case you would like to have it:
mapping(string -> address[]) paidUsers;
function pay(string memory _title) public payable {
require(msg.value == movieInfo[_title].price, "Invalid price for the film");
paidUsers[_title].push(msg.sender);
}

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

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!

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);
}
}

Solidity Syntax error - SENT

I'm learning Solidity from official documentation and stack on an exercise where I create simple coin:
pragma solidity ^0.4.20; // should actually be 0.4.21
contract Coin {
// The keyword "public" makes those variables
// readable from outside.
address public minter;
mapping (address => uint) public balances;
// Events allow light clients to react on
// changes efficiently.
event Sent(address from, address to, uint amount);
// This is the constructor whose code is
// run only when the contract is created.
function Coin() public {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
if (msg.sender != minter) return;
balances[receiver] += amount;
}
function send(address receiver, uint amount) public {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
When i try to compile i got a syntax error on the last line:
emit Sent(msg.sender, receiver, amount);
I tried to compile it in Remix and VS Code but got the same error message.
Can somebody help me pls?
The emit keyword was added in Solidity 0.4.21. Prior to that version, you emit events by using just the event name.
Sent(msg.sender, receiver, amount);
You can view the proposal here.