pragma solidity ^0.6.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC20/IERC20.sol";
contract BlockSwap {
IERC20 public token1;
address public owner1;
IERC20 public token2;
address public owner2;
constructor(
address _token1,
address _owner1,
address _token2,
address _owner2
) public {
token1 = IERC20(_token1);
owner1 = owner1;
token2 = IERC20(_token2);
owner2 = owner2;
}
function swap(uint _amount1, uint _amount2) public{
require(msg.sender == owner1 || msg.sender == owner2, "Not Authorized");
require(
token1.allowance(owner1, address(this)) >= _amount1,
"Token 1 allowance to low"
);
require(
token2.allowance(owner2, address(this)) >= _amount2,
"Token 2 allowance to low"
);
//transfer tokens
//transfer tokens
//Token1, owner1, amount1 -> owner2
_safeTransferFrom(token1, owner1, owner2, _amount1);
//token2, owner2, _amount2, -> owner1
_safeTransferFrom(token2, owner2, owner1, _amount2);
}
function _safeTransferFrom (
IERC20 token,
address sender,
address recipient,
uint amount
) private {
bool sent = token.transferFrom (sender, recipient, amount);
require(sent, "token transfer failed");
}
}
Hi @Tedmcm,
I got the error the first time I compiled on Remix. I have since tried again (including in a private browser) and can’t get the error, the contract compiles with two warnings for unused function parameters.
Can you try again?
You may want to update the import to the latest version of OpenZeppelin Contracts: https://blog.openzeppelin.com/openzeppelin-contracts-3-4/
Hi, THX for helping, I have tried to reload remix, exit out of remix completely and nothing works… it gives me the same error… I attached a screen shot… Im not sure why I get that error…It worked yesterday and now im getting errors in alot of my contracts… when I run it now I get the exact same error… also, im not sure what you mean by update the import version? I have tried looking for import versions and cant find it… ?? Is there a resource/page that has imports? The actual ones to use in the code? Like below? when I look in your GitHub all I see is the latest ones… its confusing what ones to use, all the coding/teaching examples I see people using are much older and I would like to see some docs on how/why/which ones I should be using??
import “https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC20/IERC20.sol”;
THX again for the help…
Hi @Tedmcm,
In the GitHub import, we specify a release tag to use (we should only use an official release of OpenZeppelin Contracts). Currently you are using v3.0.0
, notice the v3.0.0
in the URL:
import “https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC20/IERC20.sol”;
The current version is OpenZeppelin Contracts 3.4, so we can use v3.4.0
:
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0/contracts/token/ERC20/IERC20.sol";
I can compile your contract using Solidity 0.6.0 and Solidity 0.6.12.
I updated the import to OpenZeppelin Contracts 3.4
pragma solidity ^0.6.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0/contracts/token/ERC20/IERC20.sol";
contract BlockSwap {
IERC20 public token1;
address public owner1;
IERC20 public token2;
address public owner2;
constructor(
address _token1,
address _owner1,
address _token2,
address _owner2
) public {
token1 = IERC20(_token1);
owner1 = owner1;
token2 = IERC20(_token2);
owner2 = owner2;
}
function swap(uint _amount1, uint _amount2) public{
require(msg.sender == owner1 || msg.sender == owner2, "Not Authorized");
require(
token1.allowance(owner1, address(this)) >= _amount1,
"Token 1 allowance to low"
);
require(
token2.allowance(owner2, address(this)) >= _amount2,
"Token 2 allowance to low"
);
//transfer tokens
//transfer tokens
//Token1, owner1, amount1 -> owner2
_safeTransferFrom(token1, owner1, owner2, _amount1);
//token2, owner2, _amount2, -> owner1
_safeTransferFrom(token2, owner2, owner1, _amount2);
}
function _safeTransferFrom (
IERC20 token,
address sender,
address recipient,
uint amount
) private {
bool sent = token.transferFrom (sender, recipient, amount);
require(sent, "token transfer failed");
}
}
I only get two warnings about unused function parameters and when using 0.6.12 a warning about not having a SPDX License Identifier:
THX a lot for the help!! That works!
Hi, Question, Im keep getting massive amounts of errors in my contracts that worked before, i compiled stuff last night and now today they dont work? are you guys changed github?? Its all my erc20 related contracts… how do i know what imports to use??? Its like litterally i compile some simple token contracts last night and then when i compiled your changes today and then go to another contract they dont work? is there some kind of kernal type issue that when i compile a contract with one version of import and then when i go to another contract it remembers the last compile? I cant figure out why so many of my old contracts just stopped working? Im spending hours trying to figure out why they stopped working? Is there somewhere i can go for help with this or read up about it? Its incredibly frustrating…
This is the most bizarre thing i have ever seen! every time i change something in one contract all my other contracts are effected… i went back and changed the version to the 2.5.1 like you said… and then those erc20 contracts worked… but now when i go back to the original contract you were helping me with, the token swap one you sent me the code for last night… now it wont work… .this is nuts… i dont know what is happening… its like the kernals/code is mixing from one contract and when i compile one contract all my other ones are effected… its bizarre… now when i go back to compile your code again it wont work, I tried it this morning and it worked, then i changed the version to 2.5.1 in those erc20 contracts i told you about… and now this contract and others dont work…
this is continuing from the above post… its like when i compile an erc20 contract… any one… it then transfers over to any other contract i try to compile… and it errors… its like the version is being held… inbedded somehow in remix… and it then carries over into any other contract that version and it errors… its like a kernal in jupter lab or something… once i compile a erc version… all the contracts from that point inherit it… bizarre… i don know what to do… every time we fix one thing i corrupts all my other files with that version and then the others dont work… idk…
I have been trying to figure this out all day… when i completely exit remix… turn off my computer… and go back in to remix… and compile a erc20 contract… what ever that version is… it gets inherited into all my contracts that i try to compile after that… i dont know how it is happening… but i can see in the error for the contracts i try to compile after that the error is saying its using the version from the first compile… i have tried using the ones you gave me 2.5.1… and then when i try to compile my token swap which is 6.0.0… it errors with pragma 5.0. in the error… if i shut off my computer and then run an example token i got from your site with a version of 7… and then try the token swap it errors and in the error it has a 7 pragma error??? How can i fix this… ?? I Its like the pragma/versions from the erc20 contracts are being inherited to all my other contracts…
Hi @Tedmcm,
Each time you compile, check the version of the Solidity compiler, the pragma in your contract and the version of OpenZeppelin Contracts that you are using.
You may want to start from scratch with Remix and check each contract.
I tried a couple of different versions of OpenZeppelin Contracts and Solidity compiler in Remix but wasn’t able to reproduce your issue:
OpenZeppelin Contracts 3.4 compiled with Solidity 0.6.12
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0/contracts/token/ERC20/ERC20.sol";
contract Token is ERC20 {
constructor () ERC20("Token", "TKN") public {
_mint(msg.sender, 1000000 * (10 ** uint256(decimals())));
}
}
OpenZeppelin Contracts 3.4 Solidity 0.7 version compiled with Solidity 0.7.6
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0-solc-0.7/contracts/token/ERC20/ERC20.sol";
contract Token is ERC20 {
constructor () ERC20("Token", "TKN") {
_mint(msg.sender, 1000000 * (10 ** uint256(decimals())));
}
}
OpenZeppelin Contracts 4.0 Beta compiled with Solidity 0.8.1
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.0.0-beta.0/contracts/token/ERC20/ERC20.sol";
contract Token is ERC20 {
constructor () ERC20("Token", "TKN") {
_mint(msg.sender, 1000000 * (10 ** uint256(decimals())));
}
}