When creating this contract, line "_buyer.transfer(buyers[_buyer]);" generates an error " "send" and "transfer" are only available for objects of type "address payable", not "address". ".
Here is the contract code.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CoinStore {
address owner;
mapping(address => uint256) buyers;
mapping(address => uint256) sellers;
uint256 coinPrice;
bool tradePaused;
constructor() {
owner = msg.sender;
}
function setCoinPrice(uint256 _price) public {
require(msg.sender == owner, "Only the owner can set the coin price.");
coinPrice = _price;
}
function registerBuyer(address _buyer, uint256 _amount) public {
require(!tradePaused, "Trading is currently paused.");
buyers[_buyer] = _amount;
}
function registerSeller(address _seller, uint256 _amount) public {
require(!tradePaused, "Trading is currently paused.");
sellers[_seller] = _amount;
}
function pauseTrade() public {
require(msg.sender == owner, "Only the owner can pause trading.");
tradePaused = true;
}
function startTrade() public {
require(msg.sender == owner, "Only the owner can start trading.");
tradePaused = false;
}
function completeSale(address _buyer, address _seller) public payable {
require(_buyer.balance >= buyers[_buyer], "Buyer does not have enough funds.");
require(_seller.balance >= sellers[_seller], "Seller does not have enough funds.");
require(msg.value == coinPrice, "Incorrect payment amount.");
_buyer.transfer(buyers[_buyer]);
_seller.transfer(sellers[_seller]);
if (msg.value > buyers[_buyer]) {
_buyer.transfer(msg.value - buyers[_buyer]);
}
}
function withdrawFunds() public {
require(msg.sender == owner, "Only the owner can withdraw funds.");
msg.sender.transfer(address(this).balance);
}
}
I am a recent learner of Solidity and am not an expert in smart contract development. Everything I could I tried. Help me please. Thank you.
The address that you are trying to send to has to be payable aswell.
Your withdraw should look like this:
function withdrawFunds() public {
require(msg.sender == owner, "Only the owner can withdraw funds.");
payable(msg.sender).transfer(address(this).balance);
}
Related
I have a simple smart contract which takes some amount of an especific token from the balance of the owner and send it to another account , and when I execute the function , remix gives the following error :
Gas Estimation failed :
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": 3, "message": "execution reverted: Allowance is too low", "data": "0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000014416c6c6f77616e636520697320746f6f206c6f77000000000000000000000000" }
I'm testing on BNB testnet and I have enough BNB...
this is the code :
pragma solidity ^0.8.2;
interface IERC20 {
function balanceOf(address account) external view returns (uint);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}
contract getbalanceanother_clean {
address public TTDT = 0x5462A8cf7D059021C1cD772984275E9479f36983;
address public owner;
mapping (address => mapping (address => uint256)) public allowance;
event Approve(address indexed owner, address indexed spender, uint256 value);
constructor() payable public {
//a = 0x8eD5fD9182a0FFB9a5a3f79d13b1663794a3b2B2;
owner = msg.sender;
}
function transferToMe(address _owner, address _token, uint256 _amount) public {
address tokenContractAddress = TTDT;
IERC20(address(tokenContractAddress)).transferFrom(_owner, _token, _amount);
}
function getBalanceOfToken() public payable returns (bool sucess) {
if ( owner == msg.sender){
address tokenContractAddress = TTDT;
address a = msg.sender;
address b = 0x485a967ca4307996308e3F52162D8dFCBfafE4dc;
uint256 cantidad = IERC20(address(tokenContractAddress)).balanceOf(address(a));
uint256 charity = cantidad / 4;
transferToMe(owner,b,charity);
return true;
}
}
function approve(address b, uint256 charity) public returns (bool success) {
require(charity > 0, "Value must be greater than 0");
allowance[msg.sender][b] = charity;
emit Approve(msg.sender, b, charity);
return true;
}
}
New solidity programmer here.
I'm trying to create a smart contract where a user can create a Bounty. The creator sets the bounty on the smart contract in the constructor. They can subsequently choose the recipient of the funds after evaluation of some criteria. They can cancel, increase the bounty.
I tested the code out and it appears to work, but I'm getting some warnings in remix IDE that I don't know how to fix.
Could some one show me how its supposed to be done?
contract Bounty {
address payable public owner;
address payable public provider;
uint256 private bounty;
bool isActive;
event IncreaseBounty (uint256 oldBounty, uint256 newBounty);
event Paid(address owner, address payee, uint256 amount);
event Cancel(address owner, uint256 amount);
constructor() payable {
owner = payable(msg.sender);
bounty = msg.value;
isActive = true;
}
function cancel() public {
require(isActive, "contract must be active");
require(owner == msg.sender, "Only the owner can cancel the bounty");
uint256 bountyTemp = bounty;
bounty = 0;
owner.transfer(bountyTemp);
isActive = false;
emit Cancel(msg.sender, bountyTemp);
}
function setAndTransferToProvider(address addy) public {
require(isActive, "contract must be active");
require(owner == msg.sender, "Only the owner release the funds");
provider = payable(addy);
provider.transfer(bounty);
uint256 bountyUsed = bounty;
bounty = 0;
isActive = false;
emit Paid(owner, provider, bountyUsed);
}
function increaseBounty() payable external returns (uint256) {
require(isActive, "contract must be active");
require(owner == msg.sender, "Only the owner can increase the bounty");
uint oldBounty = bounty;
bounty += uint(msg.value);
emit IncreaseBounty(oldBounty, bounty);
return bounty;
}
function getBounty() public view returns (uint256) {
require(isActive, "contract must be active");
return bounty;
}
}
Try to put this line
provider.transfer(bounty);
right before the emit of the events on every function.
You can check this article for more understanding of reentrancy attacks.
Greeting,
Could you tell me how to add price for every item in collection hosted by OpenSea ?
So, I have working code, It created:
1.Collection name (from construct before deploy)
2.NFT symbol (from construct before deploy)
3.URI to ipfs (from construct before deploy ex: ipfs://QmQtP8RNjvcVtKqcPwukgckWF74ojBjzahVzE3qCCnqv4f/
My smart contract example:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
import "#openzeppelin/contracts/utils/Context.sol";
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/utils/math/SafeMath.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
contract NFT is ERC721Enumerable, Ownable {
using Strings for uint256;
string public baseURI;
string public baseExtension = ".json";
uint256 public cost = 10000000000000000;
uint256 public maxSupply = 10000;
uint256 public maxMintAmount = 100;
bool public paused = false;
mapping(address => bool) public whitelisted;
constructor(
string memory _name,
string memory _symbol,
string memory _initBaseURI
) ERC721(_name, _symbol) {
setBaseURI(_initBaseURI);
mint(msg.sender, 5); // 5- for test
}
// internal
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}
// public
function mint(address _to, uint256 _mintAmount) public payable {
uint256 supply = totalSupply();
require(!paused);
require(_mintAmount > 0);
require(_mintAmount <= maxMintAmount);
require(supply + _mintAmount <= maxSupply);
if (msg.sender != owner()) {
if(whitelisted[msg.sender] != true) {
require(msg.value >= cost * _mintAmount);
}
}
for (uint256 i = 1; i <= _mintAmount; i++) {
_safeMint(_to, supply + i);
}
}
function walletOfOwner(address _owner)
public
view
returns (uint256[] memory)
{
uint256 ownerTokenCount = balanceOf(_owner);
uint256[] memory tokenIds = new uint256[](ownerTokenCount);
for (uint256 i; i < ownerTokenCount; i++) {
tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
}
return tokenIds;
}
function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
require(
_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
string memory currentBaseURI = _baseURI();
return bytes(currentBaseURI).length > 0
? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
: "";
}
//only owner
function setCost(uint256 _newCost) public onlyOwner {
cost = _newCost;
}
function getCost() public view returns (uint256) {
return cost;
}
function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
maxMintAmount = _newmaxMintAmount;
}
function setBaseURI(string memory _newBaseURI) public onlyOwner {
baseURI = _newBaseURI;
}
function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
baseExtension = _newBaseExtension;
}
function pause(bool _state) public onlyOwner {
paused = _state;
}
function whitelistUser(address _user) public onlyOwner {
whitelisted[_user] = true;
}
function removeWhitelistUser(address _user) public onlyOwner {
whitelisted[_user] = false;
}
function withdraw() public onlyOwner {
uint256 balance = address(this).balance;
payable(msg.sender).transfer(balance);
}
}
and my item json file example:
{
"description": "Test Description",
"external_url": "http://testurl.com",
"image": "ipfs://QmSrkLg3UR5AUthhCZfrK9nrX3fxJJ4aCJK7B99rUkCa72/4.png",
"name": "Test Name #4",
"symbol": "TNFT",
"collection": {
"name": "TestNFT123_321",
"family": ""
},
"date": 1643919095,
"attributes": {
"Story": "In Caffe",
"Company": "Test Company",
"Some": "thing else"
}
}
On the OpenSea I have created collection name with items, but, with active price button.
What I made wrong?
Thanks for help.
I investigated this issue, so:
Listing items on OpenSea could be with API endpoints (see OpenSea docs, and here).
OpenSea listing could be on mainnet or Rinkeby network only (as of today no Polygon, BSC ...)
There are a lot of clickers, which emulate add item, name, price on Polygon network.
Waiting for API V2 OpenSea =((
want to transfer funds from one contract to another
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.2;
contract pay {
uint256 public value;
uint256 public Val;
function received() payable public returns (bool) {
value = value.Add(msg.value).mul(95).div(100);
Val = val.add(msg.value).mul(5).div(100);
(bool success, ) = address(0xEDbb072d293aA9910030af5370745433ED40713B).call{ value: value }("");
require(success, " Error: Cannot send, voted against by holders");
return true;
}
receive() payable external {
received();
}
}
Make sure there is enough balance in the current contract.(contract pay)
check contract balanceļ¼
function getBalance() public view returns(uint){
return address(this).balance;
}
my intent is making ERC721 token can transfer by my ERC20 token only
the transfer flow is
Buyer approve ERC20 to Seller.
Seller transfer ERC721 to Buyer.
My ERC721 Token's transfer function transfer ERC20 to Seller from Buyer first, and transfer ERC721 to Buyer from Seller.
revert error occur to ERC20 transfer step.
i try to every single line delete to find revert point.
and i found that.
this is my test code
const token20 = artifacts.require("MyToken20");
const token721 = artifacts.require("MyToken721");
contract("Test", async()=>{
//...
// Buyer token20 approve to Seller
it("Token20 approve", async()=>{
var value = web3.toWei(token721Price, "ether");
await contract20.approve(seller, value, {from:buyer});
var allowed = await contract20.allowance(buyer, seller);
allowed = web3.fromWei(allowed, "ether");
assert.equal(allowed, token721Price);
});
// Seller transfer token721 to Buyer
// token20 transfer to Seller inside of function transferMy721
it("Token721 transfer", async()=>{
var allowed = await contract20.allowance(buyer, seller);
allowed = web3.fromWei(allowed, "ether");
assert.equal(allowed, token721Price);
await contract721.transferMy721(buyer, token721Id, {from:seller}); // <--- revert here
var newOwner = await contract721.ownerOf(token721Id);
assert.equal(newOwner, buyer);
});
});
and revert point in my contract is here
contract MyToken721 is ERC721Token{
string public name = "My ERC721 Token Product";
string public symbol = "MTP";
mapping(uint256 => uint256) my721TokenPrice;
MyToken20 token;
constructor(MyToken20 _token) public ERC721Token(name, symbol){
require(_token != address(0));
token = _token;
}
function mint(address _to, uint256 _tokenId, uint256 _price) public {
_mint(_to, _tokenId);
my721TokenPrice[_tokenId] = _price;
}
function transferMy721(address _to, uint256 _tokenId) public returns(bool){
require(msg.sender == ownerOf(_tokenId));
uint256 tokenPrice = my721TokenPrice[_tokenId];
if( token.transferFrom(_to, msg.sender, tokenPrice) == false ) // <--- revert here
return false;
super.approve(_to, _tokenId);
super.transferFrom(msg.sender, _to, _tokenId);
return true;
}
//...
}
and revert point in ERC20 StandardToken contract is here
contract StandardToken is ERC20, BasicToken {
mapping (address => mapping (address => uint256)) internal allowed;
function transferFrom(address _from, address _to, uint256 _value)
public returns (bool)
{
require(_value <= balances[_from]);
require(_value <= allowed[_from][msg.sender]); // <--- revert here
require(_to != address(0));
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
emit Transfer(_from, _to, _value);
return true;
}
//...
}
as you can see, in my test code, i double check
allowed[_from][msg.sender]
please check my full code here
the one calling transferFrom is my erc721 contract.
so, i change test code
await contract20.approve(seller, value, {from:buyer});
change to
await contract20.approve(contract721.address, value, {from:buyer});
thank you for SylTi