I have this command, I don't know what it means:
startUNIX = block.timestamp.add(365 days);
Full code:
block.timestamp is a read-only global variable returning the datetime of when the block exeucting this function was mined.
The add() function is most likely from the SafeMath library added through the "using for" expression. Example:
library Safemath {
function add(uint256 a, uint256 b) public pure returns (uint256) {
// ...
}
}
contract MyContract {
using SafeMath for uint256;
}
days is a Solidity unit, effectively just calculating the amount of seconds from the number of days, so it's easier to calculate time difference.
Related
I'm trying to retrieve the total supply of an ERC1155 SmartContract. This code was made by my friend and has been deployed. He took the code from OpenZeppelin with Burnable and Pausable presets.
The function totalSupply that I am trying to retrieve exists in the code. But, when I see the ABI, the totalSupply does not included.
(Sorry for small screenshot. Please see this screenshot in the new tab)
MyErc1155Contract compiled
This is where to get the ABI
Full code is here: click_this_to_go_to_pastebin
Then I am trying to write smartcontract to retrieve the totalSupply;
pragma solidity ^0.8.0;
contract TotSup {
uint256 public hasilBal;
uint256 public hasil;
function balanceOf(address erc1155_address,
address user_address,
uint256 nft_id) public {
(bool successA, bytes memory resultA) =
erc1155_address.call(abi.encodeWithSignature(
"balanceOf(address,uint256)",
user_address,
nft_id
));
hasil = abi.decode(resultA, (uint256));
}
function bal(address erc1155_address, uint256 nft_id) public {
(bool successA, bytes memory resultA) =
erc1155_address.call(
abi.encodeWithSignature("totalSupply(uint256)", nft_id
));
hasilBal = abi.decode(resultA, (uint256));
}
}
I test my idea with writing another function balanceOf and it works perfectly.
And for the totalSupply in function bal is fail.
Is it possible to retrieve or calling function which is not included in the ABI? If yes, how to achieve that? And last, why call balanceOf still need for paying gas for just retrieving the data?
When a function exists in the code when you compile the code it reflects in the ABI, Without the function signature, you can't call the function.
As for your second answer, balanceOf in TotSup will cost you gas as it is not a view function.
I have a ERC20 smart contract with edited transfer function
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
if(_transactionMaxValue > 0){
require(_transactionMaxValue >= amount, "You can not transfer more than 1000000 tokens at once!");
}
transfer(recipient, amount);
}
I have added if statement in transfer function, in case user indicates limit per single transaction before deploying. I was wondering if this would affect gas fees when transferring tokens, in order to decide weather to leave the "universal" ERC20 smart contract template with indictable transaction limit or compose new one, with no if statement, if no transaction limit was indicated.
I was wondering if this would affect gas fees when transferring tokens
Adding the (if and require) conditions increases the total amount of gas used, but the increase is small in the context of gas usage of other operations of the parent transfer() function. It's because in the overriding function, you're performing "just" one more storage read and few other operations in memory.
Execution of your transfer() function costs 54,620 gas (including the parent function; assuming the require() condition doesn't fail). While execution of just the parent transfer() function costs 52,320 gas.
You need to use super.transfer(recipient, amount); if you want to invoke the parent transfer() function. Without the super keyword, you'd lock the script in an infinite recursion, always invoking itself.
Also, in order to correctly override the parent function, you need to state the override modifier (and virtual if you are planning to override this function as well) before the public visibility modifier, and to return the value as declared.
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
uint256 _transactionMaxValue = 1000000;
constructor() ERC20("MyToken", "MyT") {
_mint(msg.sender, 1000000);
}
// moved the `override virtual` before the `public` modifier
function transfer(address recipient, uint256 amount) override virtual public returns (bool) {
if(_transactionMaxValue > 0){
require(_transactionMaxValue >= amount, "You can not transfer more than 1000000 tokens at once!");
}
// added `return super.`
return super.transfer(recipient, amount);
}
}
Everything #Petr Hejda suggested is correct.
Additional improvement here would be to make a require statement fail reason string smaller and more precise, as it will result in the smaller bytecode, and therefore cheaper deployment.
I have this code in my solidity contract:
uint256 constant maxNum = 10000;
function mintNewFull(uint16 tokenId) public {
require (0 <= tokenId && tokenId < maxNum;
// do other stuff
}
And I called it using this code in ethers which worked:
contractWithSigner.mintNewFull(3);
But then later when I changed it to trying to mint with tokenID 11:
contractWithSigner.mintNewFull(11);
It didn't work. And I tried and every number under 10 seems to work and numbers greater than that don't.
Is it some uint16 uint256 problem? Should my constant maxNum be changed to uint16, is it impossible to call using ethers a function with a uint16 parameter? I have no idea how to pass in a uimt16 instead of a uint256 because I couldn't find how to declare parameter types in the ethers docs. It seems like everyone just uses numbers or strings so that's confusing (especially when I will later have to pass in an array.)
I figured it out!!! Turns out it was something in the
// do other stuff
part where I was causing an integer overflow.
Yayyy I'm so happy :):)
When I try to mint inside of constructor using ERC20Capped from OpenZeppelin 4 like
contract SomeERC20 is ERC20Capped {
constructor (
string memory name,
string memory symbol,
uint256 cap,
uint256 initialBalance
)
ERC20(name, symbol)
ERC20Capped(cap)
{
_mint(_msgSender(), initialBalance);
}
}
the error
Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it
appears.
What should I do?
cap is immutable in ERC20Capped thus it cannot be read during the mint process in the constructor. It was done on purpose to lower gas costs. You can either mint outside of the constructor or use the _mint function from common ERC20 like this
contract SomeERC20 is ERC20Capped {
constructor (
string memory name,
string memory symbol,
uint256 cap,
uint256 initialBalance
)
ERC20(name, symbol)
ERC20Capped(cap)
{
require(initialBalance <= cap, "CommonERC20: cap exceeded"); // this is needed to know for sure the cap is not exceded.
ERC20._mint(_msgSender(), initialBalance);
}
}
It is advised to add a check for the initialSupply to be lower than the cap The check is originally done in the _mint function of ERC20Capped but not in the ERC20 and since you are using the latter the check is omitted.
Hello I have a simple implementation for a ERC223 Token. I use this repository: ERC223-token-standard
Here is my contract Code:
pragma solidity ^0.5.8;
import "./ERC223-token-standard-development/token/ERC223/ERC223Burnable.sol";
import "./ERC223-token-standard-development/token/ERC223/ERC223Mintable.sol";
contract MyToken is ERC223Burnable, ERC223Mintable{
}
The first strange Thing is that I get this warning when compiling:
Warning: Function state mutability can be restricted to view
function mint(address account, uint256 amount) public onlyMinter returns (bool) {
^ (Relevant source part starts here and spans across multiple lines).
I think this is weird because to my understanding it changes the state of the contract and should not be a view function
However after creating my contract and using the mint function the totalSupply is still 0 even though isMinter(myaddr) returns true. I thought that I was just doing something wrong in the truffle testfile but after I pushed my code into Remix I still get the same warning and the total supply is still 0.
I hope you can help me with this issue.
thanks for your question, it looks like a bug in your _mint function:
function mint(address account, uint256 amount) public onlyMinter returns (bool) {
_totalSupply.add(amount);
balances[msg.sender].add(amount);
return true;
}
Let's see how add method of SafeMath lib works:
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
First of all, its pure function, which doesn't modify the state of contract. It just returns amount of addition. So, it's expected behavior for your code ‘cause you don't save a result
But if you take a look at open-zeppelin's implementation of _mint method, they change contract's state
function _mint(address account, uint256 amount) internal {
require(account != address(0), "ERC20: mint to the zero address");
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
I believe it's better to use OpenZeppelin's contracts for your development 'cause they have a lot of auditors
P.S. Also you don't use address account variable in _mint() and always add amount to msg.sender (yourself) in your implementation