Ether is not transferring in this code from one account to other.? - ethereum

// SPDX-License-Identifier: MIT
pragma solidity >0.5.0 < 0.9.0;
contract TestPay {
function payEther()payable public{
// user will pay to contract
}
function transferEther(address payable _to,uint amt) payable public{
_to.transfer(amt);
}
}

Related

Solidity: Call Deposit function with value

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

unable to send eth to deployed smart contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract Steezy {
function contractAddress() public view returns (address asd){
return address(this);
}
function buy() public payable{
uint256 listenPrice = 25000000000000000;
(bool successs, ) = address(this).call{value: listenPrice}("");
require(successs, "Failed");
}
}
I was trying to send some eth to the deployed smart contract but it gives me Failed execution reverted
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract Steezy {
function contractAddress() public view returns (address asd){
return address(this);
}
function buy() public payable{
uint256 listenPrice = 25000000000000000;
(bool successs, ) = address(this).call{value: listenPrice}("");
require(successs, "Failed");
}
fallback() external payable {}
}
You were missing the fallback function try this out. It should work

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.

Send BNB or ETH to get back token automatically Solidity

I am working on a contract with openzeppelin and remix ide.
In this contract I have written a fallback function to get ether from consumer and send them the token automatically.
here is my code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelincontracts/blob/master/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
address public owner;
uint256 public value;
constructor() ERC20("MtToken", "TKN"){
owner = msg.sender;
_mint(owner, 1000000 ether);
increaseAllowance(owner, 1000000 ether);
}
fallback() external payable{
value += msg.value;
if(msg.value > (0.01 ether)){
transferFrom(owner, msg.sender, (1000 ether));
}
}
}
but when I try to send BNB or ETH in test net to get back the token transaction fails.

How to receive funds during minting to a specific address in solidity (Openzeppeling)?

I am trying to receive the funds into a specific address during mint process. This is my code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-
contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
contract NFTContract is ERC1155 {
uint256 public constant Jack = 0;
constructor() ERC1155("") {
_mint(msg.sender, Jack, 0, "");
}
function mint(address account, uint256 id, uint256 amount) public payable {
payable(address(0xdD870fA1b7C4700F2BD7f44238821C26f7392148)).transfer(100000000000000000);
_mint(account, id, amount, "");
}
}
But I get an error saying:
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.
How do I receive a payment during the mint?
Just needed to do regular transfer:
addr.transfer(amount)