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.
Related
// 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);
}
}
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)
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");
}
}
I am learning solidity and testing out accepting payment of a fixed amount using a smart contract.
I took this code from a tutorial website and im testing it in Remix.
pragma solidity ^0.8.7;
contract DonateContract {
address payable public owner;
//contract settings
constructor() {
owner = payable(msg.sender);
}
//public function to make donate
function donate() public payable {
(bool success,) = owner.call{value: 10000 wei}("");
require(success, "Failed to send money");
}
}
I want to transfer a fixed amount of 10000 WEI.
The message sender has a 100 ETH balance.
The owner is just an address, not a contract.
It gives the error "Failed to send money" every time.
The problem here is that by sending a fixed wei amount to the owner, you do not know if the contract will have enough balance to make the transaction. To do so, I would suggest maybe transferring the msg.value amount, so that you ensure that all the eth sent to the contract is redirected to the owner's balance. It would be something like this:
(bool success,) = owner.call{value: msg.value}("");
Hope you find this information useful :)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract transfertot{
//address public address1=0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; it is owner address sample
address public address2=0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db; // it is reciever address sample
address payable public owner;
constructor()payable {
owner=payable(msg.sender);
}
uint public msgvalue=msg.value;//contract value
uint balance1;//owner balance
uint balance2;//reciever balance
uint[] balance1arr;
uint[] balance2arr;
function transfer1(address payable _address,uint _priceGwei)payable public {
require(_priceGwei<=(msgvalue/10**9),"balance is less than msgvalue");//working in gwei
_address.transfer(_priceGwei*10**9);
balance1=owner.balance/(10**9);
balance2=address2.balance/(10**9);
balance1arr.push(balance1);
balance2arr.push(balance2);
msgvalue-=_priceGwei*10**9;
}
function ownerbalancearr()public view returns(uint[] memory){
return balance1arr;
}
function recieverbalancearr()public view returns(uint[] memory){
return balance2arr;
}
}
//when you want to deploy add some gwei to value in deploy and run transactions panel`
//the contract value is differ from owner value
//you can check the`enter code here` output in this code
//good day to you`
I have the following smart contract:
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./MyCoinSupply.sol";
contract MyCoinDEX
{
IERC20 public token;
event Bought(uint256 amount);
event Sold(uint256 amount);
constructor() public
{
token = new MyCoinSupply();
}
function getSenderAddress() public view returns (address) // for debugging purposes
{
return (msg.sender);
}
function getAddress() public view returns (address)
{
return address(this);
}
function getTokenAddress() public view returns (address)
{
return address(token);
}
function buy() payable public // send ether and get tokens in exchange; 1 token == 1 ether
{
uint256 amountTobuy = msg.value;
uint256 dexBalance = token.balanceOf(address(this));
require(amountTobuy > 0, "You need to send some ether");
require(amountTobuy <= dexBalance, "Not enough tokens in the reserve");
token.transfer(msg.sender, amountTobuy);
emit Bought(amountTobuy);
}
function sell(uint256 amount) public // send tokens to get ether back
{
require(amount > 0, "You need to sell at least some tokens");
uint256 allowance = token.allowance(msg.sender, address(this));
require(allowance >= amount, "Check the token allowance");
token.transferFrom(msg.sender, address(this), amount);
// https://stackoverflow.com/questions/67341914/error-send-and-transfer-are-only-available-for-objects-of-type-address-payable
payable(msg.sender).transfer(amount);
emit Sold(amount);
}
}
if I call the buy() method from truffle console, it executes without any exceptions:
truffle(development)> MyCoinDEX.buy({value: 1})
I verified that the account calling the buy() method receives the token. However, the balance of Ether in Ganache for the account calling the buy() method doesn't decrease. So essentially, the account is getting tokens for free.
What's going on here? How do I fix it?
I am not sure but it would be necessary to investigate about the balance of the contract account. The gas for token.transfer might be paid by the contract account balance, since the contract account is the transaction sender for token contract.
Or the balance decrement would be unnoticeable because it is too small.
I would be happy to know about the answer if you have found out.