The constructor should be payable if you send value - solidity

I'm running this code on remix IDE. Everything is working fine except the function transfertocontracts(uint amount) public. I'm trying to transfer some ethers for e.g. 10 to my contract and then later using this function function Transfer_Contract_Amount() public I will transfer all the amount of contract to specific address.
The problem is that when I run function transfertocontracts(uint amount) public I'm getting this Error:
Note: The constructor should be payable if you send value. debug the transaction to get more information.
contract SLA {
address seller;
event DepositFunds(address from, uint amount);
constructor() payable public {
seller = msg.sender;
}
function transfertocontracts(uint amount) public {
address(this).transfer(amount);
}
function seePerson_Amount() public view returns(uint) {
return seller.balance;
}
function seeContract_Amount() public view returns(uint) {
return address(this).balance;
}
function Transfer_Contract_Amount() public {
seller.transfer(address(this).balance);
}
}

You can send ether only by using the value attribute which you can set while interacting with the Smart Contract. Below is the working code
pragma solidity >=0.4.22 <0.6.0;
contract SLA{
address payable seller;
event DepositFunds(address from, uint amount);
constructor() payable public {
seller = msg.sender;
}
function transfertocontracts(uint amount) payable public{
}
function seePerson_Amount() public view returns(uint){
return seller.balance;
}
function seeContract_Amount() public view returns(uint){
return address(this).balance;
}
function Transfer_Contract_Amount() payable public{
seller.transfer(address(this).balance);
}
}
To check you can run it on remix and on the run tab on the left side you can putt the value of ether to send and run the transfertocontracts function further to store the value its a good idea to use the msg.value.

Related

Inheriting multiple ERC721 extensions

I would like to use both ERC721URIStorage for setting token URI easily but I would also like the methods in ERC721Enumberable to fetch the number of tokens made by an address.
I get this error when trying to use both :
Derived contract must override function "_beforeTokenTransfer". Two or more base classes define function with same name and parameter types.
Derived contract must override function "_burn". Two or more base classes define function with same name and parameter types.
Derived contract must override function "supportsInterface". Two or more base classes define function with same name and parameter types.
Derived contract must override function "tokenURI". Two or more base classes define function with same name and parameter types.
is this just not possible at all or is there a way to Override the duplicated functions?
Below code overrides the overlapping functions from the two extensions
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
contract NameOfContract is ERC721URIStorage, ERC721Enumerable {
// Contract Code ....
function _beforeTokenTransfer(address from, address to, uint256 tokenId)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId);
}
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
super._burn(tokenId);
}
function tokenURI(uint256 tokenId)
public
view
override(ERC721, ERC721URIStorage)
returns (string memory)
{
return super.tokenURI(tokenId);
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}
Update
_beforeTokenTransfer has a new parameter and argument
Now it is like
function _beforeTokenTransfer(address from, address to, uint256 tokenId, uint256 batchSize)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId, batchSize);
}

Exchange ETH for WETH via smart contract function call

I'm attempting to write a simple solidity contract that can:
receive ETH
convert ETH to WETH
display ETH balance
display WETH balance
ETH operations appear to work as expected however whenever I try to call any of the WETH functions I receive an error and the transaction is reverted.
Is there something wrong with the way I am trying to instantiate or interact with the WETH token?
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
interface IERC20 {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
}
interface IWETH is IERC20 {
function deposit() external payable;
function withdraw(uint) external;
}
contract Scrath {
address private weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
function sendEth() external payable {}
function ethBalance() public view returns(uint256 _balance) {
_balance = address(this).balance;
return _balance;
}
function convertToWeth() external payable {
uint256 eth = ethBalance();
IWETH(weth).deposit{value: eth}();
}
function wethBalance() external view returns(uint256 _balance) {
_balance = IWETH(weth).balanceOf(address(this));
return _balance;
}
}

Solidity: WETH can't be extracted

I can convert ETH to WETH via WETH smart contract and deposit it in my smart contract address, but when I want to convert WETH in my smart contract address to ETH via WETH smart contract and withdraw it to my wallet address, I can't do it.
pragma solidity ^0.5.0;
interface IWETH {
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function withdraw(uint256 value) external payable;
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract testwithdraw {
uint256 public ETHAnt;
address public WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
function deposit() public payable{
ETHAnt = msg.value;
IWETH(WETH).deposit.value(ETHAnt)();
}
function getContractBalance(address _to) public view returns(uint) {
uint256 Balance = IERC20(WETH).balanceOf(_to);
return Balance;
}
function withdraw(uint256 _value) public payable {
IWETH(WETH).withdraw.value(_value);
}
}
When using the withdraw function, it tells me that it ran successfully. But I see that the from in the log is actually my wallet address, and the to is my smart contract address. What's going on here?
status true Transaction mined and execution succeed
transaction hash 0x2859b5188b12cb2f0849a26777a5d2d0972e0e2ce000b548b80f3e5ae3b85b32
from 0xAb2942DEcDEa92A43d84DE7097a2e8df373e41Ab
to testwithdraw.withdraw(uint256) 0xa5f43d3A0C3ee2A99511A14698F0DB082491C751
gas 22323 gas
transaction cost 22323 gas
hash 0x2859b5188b12cb2f0849a26777a5d2d0972e0e2ce000b548b80f3e5ae3b85b32
input 0x2e1...00000
decoded input {
"uint256 _value": "1000000000000000000000"
}
decoded output -
logs []
val 0 wei
When you're withdrawing ETH using IWETH(WETH).withdraw.value(_value), it withdraws ETH from WETH contract and stores them on your contract. In order to be able to withdraw received funds, you need to add something like
payable(msg.sender).transfer(address(this).balance) in your withdraw function.

When (and when not) to use the new keyword in Solidity

I have two contracts in Solidity like so (based upon a tutorial):
pragma solidity >=0.4.16 <0.9.0;
contract ERC20Token {
string public name;
mapping(address => uint) public balances;
function mint() public {
balances[tx.origin]++;
}
}
contract MyContract {
address payable wallet;
address public token;
constructor(address payable _wallet, address _token) {
wallet = _wallet;
token = _token;
}
function buyToken() public payable {
ERC20Token _token = ERC20Token(address(token));
_token.mint();
wallet.transfer(msg.value);
}
}
My question is why they use ERC20Token _token = ERC20Token(address(token)); in order to access the class and call he .mint() function.
My base reflex would be to do the following instead:
ERC20Token _token = new ERC20Token();
_token.mint();
This is because the ERC20Token contract does not have a constructor, so I'm unsure where the RC20Token(address(token)) constructor parameters come from.
The new ERC20Token() expression deploys a new ERC20Token contract and returns its (newly deployed) address.
Since your ERC20Token doesn't have any constructor (and constructor params), you don't need to pass any params. But if it did have constructor params, you'd need to pass them. Example:
contract ERC20Token {
constructor (string memory _name, string memory _symbol) {
}
}
address deployedTo = new ERC20Token("MyToken", "MyT");
The ERC20Token _token = ERC20Token(<address>) creates a helper object allowing the _token to be treated as a contract implementing the ERC20Token interface, so you can execute and call its public and external functions.
It's the address of the external contract that you're passing - not the constructor params.
contract ERC20Token {
function mint() public {
}
}
ERC20Token _token = ERC20Token(address(token));
_token.mint();

In Remix - Solidity IDE, How to pass arguments?

I'm studying smart contracts on solidity and I ran into a problem. Every time I try to create this contract, my arguments are not confirmed.
I expected a "OreOreCoin" to come out when I chose name, but instead I get an empty string.
and
This my code:
pragma solidity ^0.4.8;
contract OreOreCoin{
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
mapping (address => uint256) public balanceOf;
event Transfer(address indexed from, address indexed to, uint256 value);
function OreOreCoin(uint256 _supply, string _name, string _symbol, uint8
_demicals){
balanceOf[msg.sender] = _supply;
name = _name;
symbol = _symbol;
decimals = _demicals;
totalSupply = _supply;
}
function transfer(address _to, uint256 _value){
if(balanceOf[msg.sender] < _value) throw;
if(balanceOf[_to] + _value < balanceOf[_to]) throw;
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
Transfer(msg.sender,_to,_value);
}
}
What could be the problem?
Don’t quote the entire parameter list. In doing so, you're sending a single string parameter into the constructor that is converted to a uint256 for _supply and the rest are defaulted. You can confirm this by looking at the details of the transaction in the Remix UI.
The parameter list should just be:
10000,”OreOreCoin”,”oc”,0