Here you can ask questions related to this specific course section.
Idk if anyone else is having this issue, but i kept getting this error message saying that the variable was inmutable and couldnt be read during contract creation time, so it seems that openzeppelin updated some stuff, they moved ERC20Capped to a different folder called extensions, and they also changed the _cap function and since after that change people gets that error message that im getting , here is a link to a discussion, where a team member of openzeppelin explains whats going on a little better than i do xD :
Also if someone wants to get their code to compile xD, heres the new way to do it :
pragma solidity 0.8.2;
import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
contract MyToken is ERC20Capped {
constructor(uint256 initialSupply) ERC20("MyToken", "MTKN") ERC20Capped(100000) {
ERC20._mint(msg.sender, initialSupply);
}
}
@filip i was wondering if this is a legit issue, or if its just me doing something wrong, and so if its a legit issue i think it would be a good idea to leave a message for the people watching that video so that they understand whats going on, cause ooof using truffle is a nightmare
And i was also wondering if the code that i posted is good, or if doing something wrong there, cause i feel that im missing something
Hey @Carlosvh96
Looks like openzeppelin opened an issue (the link to the issue is in the url you posted): OpenZeppelin/openzeppelin-contracts#2580
By reading the link above it seems an issue with the compiler, try to use Solidity < 0.8
Reagards,
Dani
Hey,
Iām struggling with compiling, any ideas how to fix this?
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MKTN"){
}
}
In the terminal I try to compile and I get the belowā¦
truffle(develop)> compile
Compiling your contractsā¦
Error: Could not find a compiler version matching ^0.8.0;. compilers.solc.version option must be a string specifying:
- a path to a locally installed solcjs
- a solc version or range (ex: ā0.4.22ā or ā^0.5.0ā)
- a docker image name (ex: āstableā)
- ānativeā to use natively installed solc
at CompilerSupplier.badInputError (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/compile-solidity/compilerSupplier/index.js:33:1)
at CompilerSupplier.load (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/compile-solidity/compilerSupplier/index.js:75:1)
at loadParser (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/compile-solidity/profiler/loadParser.js:15:29)
at Object.requiredSources (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/compile-solidity/profiler/index.js:19:1)
at Object.sourcesWithDependencies (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/compile-solidity/index.js:68:45)
at necessary (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/compile-solidity/index.js:42:1)
at /opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/workflow-compile/index.js:38:1
at async Promise.all (index 0)
at compile (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/workflow-compile/index.js:28:1)
at Object.compile (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/workflow-compile/index.js:71:47)
at Object.run (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/compile.js:86:1)
at Command.run (/opt/homebrew/lib/node_modules/truffle/build/webpack:/packages/core/lib/command.js:140:1)
truffle(develop)>
Iāve tried changing the version in truffle-config.js to ā^0.8.0ā as well.
If anyone could help me out, that be great!
Why it shows me an error, why I canāt run instance? How instance is not defined?
you forgot to use await
in your instance
variable declaration.
If your migration file have deployed the contract, you just need to add the await
keyword:
let instance = await ContractName.deployed()
then you should be able to call instance
and check all the methods on it.
Carlos Z
Hey @Lane11
There are different errors in your code above
As @thecil pointed out, the .deployed
method is asynchronous therefore you should use the keyword away
to get the instance and assign it to instance
.
In your screenshot there are two different errors:
- Identifier āinstanceā has already been declared
In all programming languages you cannot declare the same variable twice.
When you interact with the truffle console you are just writing javascript code, therefore the language rules apply.
let number = 10;
-> ok
let number = 5;
-> error
You are declaring a variable with the same name and trying to assign the number 5.
let number = 10;
-> ok
number = 5;
-> ok
You have assigned a different value to the variable number
and this is possibile.
The same thing applies to the variable instance
.
Also keep in mind that you have to re-declare instance
every time you restart your truffle console.
Cheers,
Dani
Hello, Iām having an issue compiling the ERC20Capped contract. I followed Filipās instruction but kept on getting an error. Can someone help?
Hi @MrSeven
This happens only if you mint in the constructor, there is an issue opened on github.
https://forum.openzeppelin.com/t/erc20capped-immutable-variables-cannot-be-read-during-contract-creation-time/6174/5
Cheers,
Dani
When I put ERC20._mint
in place of the _mint
syntax the contracts can compile but when I go over the cap it doesnāt give me an error could this be a problem in the coming lessons and making real contracts?
Hello, I created my own ERC20 token using the ERC20 template. I deployed it on truffle and everything seems to work. However I cant get the right logic and order for the following functions: approve, allowance, and transferFrom. I set the accounts[0] as the owner and I transferred tokens from accounts[0] to accounts[1]. This works. However I cant transfer tokens using the transferFrom function from accounts[1] to accounts[2]. Again, can someone tell me what is the correct logic and order for the approve, allowance, and transferFrom functions to make this work.
My contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
contract PracticeToken is ERC20, Ownable {
constructor() ERC20("PracticeToken", "PCT") Ownable() {
_mint(msg.sender, 1000);
}
}
My error message:
Hey @cecilia
In order to troubleshoot your issue I need to check the whole contract, can you please post it?
Thanks,
Dani
Solution for Error Message (āImmutable variables cannot be read during contract creation timeā¦ā)
// contracts/GLDToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
contract GLDToken is ERC20Capped {
constructor(uint256 initialSupply) ERC20("Gold", "GLD") ERC20Capped(1000*10**18) {
ERC20._mint(msg.sender, initialSupply);
}
}
Matter is resolved by adding;
ERC20.
Example;
ERC20._mint(msg.sender, 1000);
My whole contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "../../utils/Context.sol";
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin guidelines: functions revert instead
* of returning `false` on failure. This behavior is nonetheless conventional
* and does not conflict with the expectations of ERC20 applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is Context, IERC20 {
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
/**
* @dev Sets the values for {name} and {symbol}.
*
* The defaut value of {decimals} is 18. To select a different value for
* {decimals} you should overload it.
*
* All three of these values are immutable: they can only be set once during
* construction.
*/
constructor (string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* @dev Returns the name of the token.
*/
function name() public view virtual returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual returns (string memory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5,05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless this function is
* overloaded;
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual returns (uint8) {
return 18;
}
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view override virtual returns (uint256) {
return _totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view override virtual returns (uint256) {
return _balances[account];
}
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) public override virtual returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender) public view override virtual returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) public override virtual returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* Requirements:
*
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
* - the caller must have allowance for ``sender``'s tokens of at least
* `amount`.
*/
function transferFrom(address sender, address recipient, uint256 amount) public override virtual returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
_approve(sender, _msgSender(), currentAllowance - amount);
return true;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
uint256 currentAllowance = _allowances[_msgSender()][spender];
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
_approve(_msgSender(), spender, currentAllowance - subtractedValue);
return true;
}
/**
* @dev Moves tokens `amount` from `sender` to `recipient`.
*
* This is internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `sender` cannot be the zero address.
* - `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
*/
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");
_beforeTokenTransfer(sender, recipient, amount);
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
_balances[sender] = senderBalance - amount;
_balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `to` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
_balances[account] = accountBalance - amount;
_totalSupply -= amount;
emit Transfer(account, address(0), amount);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(address owner, address spender, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be to transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
}
Hi @cecilia
The contract looks good.
In order to use transferFrom
you just have to call the function increaseAllowance
using the account you want to move funds from.
For example, if you want to move tokens from accounts[0] to accounts[1] you will call the increaseAllowance function with accounts[0] and you will pass accounts[1] and the amount as parameters.
function increaseAllowance(address spender, uint256 addedValue)
Once done you can call transferFrom.
Cheers,
Dani
Hello Dani,
I already transferred from accounts[0], which is the owner, to accounts[1]. Iām now trying to transfer from accounts[1] to accounts[2]. This is the order of the functions that I used:
instance.increasedAllowance(accounts[1], 500)
instance.allowance(accounts[0], accounts[1])
instance.approve(accounts[1], 200)
instance.transferFrom(accounts[1], accounts[2], 200)
What am I doing wrong? Any help would be greatly appreciated.
Thank you - Cecilia
Hey @cecilia, hope you are well.
What if the balance is not being update properly when you made a transfer? would be great to also check at your contract code
Carlos Z
@dan-i i removed the immutable keyword when cap was declared in ERC20Capped is this dangerous. Like if the var is still private would it be possible for anyone to change the cap?