Can erc20 type in erc1155 be listed on uniswap or exchanges? - solidity

An example from Alchemy,
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "#openzeppelin/contracts/token/ERC1155/ERC1155.sol";
contract AwesomeGame is ERC1155 {
uint256 public constant GOLD = 0;
uint256 public constant SWORD = 1;
uint256 public constant CROWN = 2;
constructor() ERC1155("https://awesomegame.com/assets/{id}.json") {
_mint(msg.sender, GOLD, 10**18, "");
_mint(msg.sender, SWORD, 1000, "");
_mint(msg.sender, CROWN, 1, "");
}
}
Here, GOLD is an erc20 type and just wondering if it can have some monetary value like erc20 and be listed on uniswap or exchanges? and two side questions,
_mint(msg.sender, GOLD, 10**18, "");
is this minting only 1 token?
is SWORD still NFT? I think it's fungible with 1k quantity and it should not be non-fungible token (NFT)

First of all, you have to understand why ERC-1155 was introduced in the first place. In short, it was introduced to cater to the need for in-game currency and items.
Yes it will but why would you be needing that many decimals for GOLD? I've seen people setting 4 at max.
As per ethereum.org, If the supply is only 1, treat it as NFT.
and to answer your main question No you can't add it. You can see more detail discussion on forum.openzeppelin.

Related

Native coin MATIC has a Contract Address on the Polygon Network?

I am currently developing a donation smart contract for the Polygon blockchain.
The donation contract should receive many types of coins like (MATIC, USDT, USDC, etc.).
I first made the donate function like the below:
function donate(address donor, address token, bool isNativeCoin, uint256 amount);
But later, I noticed that the Polygon network has MATIC address differently from other evm-based chains.
So I removed the isNativeCoin flag variable in the method params.
function donate(address donor, address token, uint256 amount);
But I don't know that is real MATIC address is and how Polygon is different from other chains.
Here is my research about Polygon MATIC and WMATIC address.
MATIC
https://mumbai.polygonscan.com/address/0x0000000000000000000000000000000000001010
https://polygonscan.com/address/0x0000000000000000000000000000000000001010
WMATIC
https://mumbai.polygonscan.com/address/0x9c3c9283d3e44854697cd22d3faa240cfb032889#code
https://polygonscan.com/address/0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270
If I import 0x0000000000000000000000000000000000001010 as a token in my metamask wallet, it shows the same balance with Native MATIC balance. It's so interesting.
Here is the full mockup code.
address MATIC_TOKEN = 0x0000000000000000000000000000000000001010;
DonationInfo[] donations;
function _safeTransferMatic(address to, uint256 amount) internal {
(bool success, ) = to.call{value: amount}("");
require(success, "MATIC_TRANSFER_FAILED");
}
function donate(address donor, address token, uint256 amount) {
donations.push(DonationInfo(donor, token, amount);
}
function sendDonation(uint256 donateIndex) {
if (donations[donateIndex].token == MATIC_TOKEN) {
_safeTransferMatic(charityPartner, donations[donateIndex].amount);
} else {
IERC20(donations[donateIndex].token).safeTransfer(charityPartner,donations[donateIndex].amount);
}
}
Is the above code correct or should I use isNativeCoin flag variable?
Could you explain the Polygon MATIC address?
0x0000000000000000000000000000000000001010 is a precompile that supports some EIP-20 functions, such as balanceOf(address), name(), symbol(), and decimals(). It does not support functions like transfer(address, uint256), approve(address, uint256), or transferFrom(address, address, uint256).
Basically, it's a special address that isn't a contract that supports some, but not all EIP-20 functions.

Solidity, Wrapping Eth from a contract

I'm trying to wrap Eth from an smart contract as I want to swap weth later in uniswap but I don't know how to import the WETH code from goerli scan WETH = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6;.
Following internet examples I accomplished wrapping Eth but all influencers just import a ERC20 library to create a new one. I don't understand why they are using this as they are not interacting with the right weth contract.
Here is the code I used but is just creating a new token. Can anyone give me some advice?
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;
pragma abicoder v2;
import '#uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol';
import '#uniswap/v3-periphery/contracts/libraries/TransferHelper.sol';
import '#openzeppelin/contracts/token/ERC20/ERC20.sol';
contract SwapExamples is ERC20 {
// For the scope of these swap examples,
// we will detail the design considerations when using `exactInput`, `exactInputSingle`, `exactOutput`, and `exactOutputSingle`.
// It should be noted that for the sake of these examples we pass in the swap router as a constructor argument instead of inheriting it.
// More advanced example contracts will detail how to inherit the swap router safely.
// This example swaps DAI/WETH9 for single path swaps and DAI/USDC/WETH9 for multi path swaps.
ISwapRouter public immutable swapRouter;
address payable [] private s_Wallets;
uint256 public walletA = address(this).balance;
// Router = 0xE592427A0AEce92De3Edee1F18E0157C05861564
address public constant WETH = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6; //0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
// For this example, we will set the pool fee to 0.3%.
uint24 public constant poolFee = 3000;
uint256 public UsdOut;
constructor(ISwapRouter _swapRouter) ERC20("Wrapped Ether", "WETH") {//ERC20("Wrapped Ether", "WETH")
swapRouter = _swapRouter;
}
function Deposit() public payable {
s_Wallets.push(payable(msg.sender));
}
function Mint() external payable {
_mint(address(this), address(this).balance);
}
}
You need not create a new ERC20 token.
Calling:
WETH.deposit.value(msg.value)();
helps you wrap your ETH and you don't need to import WETH code from anywhere.
After wrapping, you can then move on to swapping WETH for any other token on Uniswap like you said.

Ethereum lottery smart contract reverts due to insufficient funds. Where does my code consumes that much gas?

I am experimenting with solidity and I faced an issue for what I could not find a solution.
The program should let addresses buy ticket at a preset price, and the owner can start the "roll the dice" function which randomly selects the winner and transfer the funds to that address.
I beleive that this program would be easier with mapping instead of array, but getting experience with array was the main purpose of this program.
The error happens when the user calls buyTicket function. Based on the response I beleive the contract comsumes too much gas. Can someone tell me why it doesnt work? I appreciate any other helping comment regarding the rest of the code:)
Thanks in advance!
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Lottery {
constructor () {
owner = msg.sender;
}
address[] public Players;
address private owner;
uint public ticketPrice;
uint public price;
uint public nonce;
uint public variations;
address payable winner;
bool hasTicketAnswer;
event Winner(address _winner);
event PriceSet(uint _setPrice);
event TicketBought();
function setTicketPrice(uint _ticketPrice) public {
require(msg.sender == owner, "Only Owner...");
ticketPrice = _ticketPrice;
emit PriceSet(_ticketPrice);
}
function hasTicket(address _sender) private returns(bool) {
hasTicketAnswer = false;
for (uint i = 0; i < Players.length; i++) {
if (Players[i] == _sender) hasTicketAnswer = true;
}
return hasTicketAnswer;
}
function buyTicket() external payable {
require(ticketPrice > 0, "Price did not set, be patient...");
require(hasTicket(msg.sender) == false, "You cannot have two tickets...");
require(msg.sender.balance <= ticketPrice, "Insufficient funds...");
payable(address(this)).transfer(ticketPrice);
Players.push(address(msg.sender));
price += msg.value;
emit TicketBought();
}
function checkBalance() public view returns(uint) {
return address(this).balance;
}
function rollTheDice() public payable {
variations = Players.length;
winner = payable(Players[uint(keccak256(abi.encodePacked(msg.sender, nonce, block.timestamp))) % variations]);
winner.transfer(price);
emit Winner(winner);
}
receive () external payable {
}
}
Besides probably finding the problem, I've read some things that I'd like to comment on.
Your problem
The reason why you're getting the "Insufficient funds" error is because the condition is returning false. You're asking the msg.sender balance to be less than or equal (<=) to ticketPrice, when it should be more than or equal (>=).
Let's say Alice has a balance of 0.05 ETH and interacts with the contract whose ticket price is 0.001 ETH...
require(0.05 ETH <= 0.001 ETH) // Reverting...
Observations
I'm curious if you're intentionally coding the buyTicket function in that way. What it actually does is checking if the msg.sender has enough ETH to buy a ticket in its wallet, which doesn't mean that the user is effectively sending ETH in the transaction (the amount of wei sent in the transaction can be checked with msg.value, you can read more about it here).
My last observation is the payable(address(this)).transfer(ticketPrice) line of code, because it's not necessary to do so, once a payable function receives ETH, it is saved into the contract... In that line you're just making the Bob's contract to send ETH to the Bob's contract, which just wastes gas without reason
I hope I've helped with you and if I wasn't completely clear in any thing I've said don't doubt in asking me

Smart contract ERC721 - Mint all tokens to contract address

Is it possible to mint all tokens to the contract address as soon as the contract is deployed?
I have a collection of 100 NFTs which need to be visible immediately under the collection address in OpenSea but I see that the NFTs appear there only when minted to a wallet address.
My initial idea was to mint everything to the owner's address so that all NFTs would be available under the collection...
Any idea on how to implement it?
You can mint tokens during deployment, the contract address is available through the address(this) expression.
pragma solidity ^0.8;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
contract MyCollection is ERC721 {
constructor() ERC721("CollectionName", "Symbol") {
// mint 100 tokens to the contract address on deployment
for (uint i = 1; i <= 100; i++) {
_mint(address(this), i);
}
}
}
It depends on implementation of OpenSea and other marketplaces if they accept such NFTs minted on deployment - or if they don't recognize these tokens.

Token balance check is not working in Solidity smart contract

here is my code snippet and i have no idea why its not working
function Testdeposit(address _assetAddress) public returns (uint256 status)
{
//IERC20 erc20 = IERC20(_assetAddress);
//erc20.transferFrom(senderAddress, address(this), _amount));
//uint256 amount = erc20.allowance(senderAddress, address(this));
uint256 amount = IERC20(_assetAddress).balanceOf(address(this));
return amount;
}
i am using a standard IERC20 interface. just it dsnt have an event to emit. i assume it to return me 0 if there is no balance but its gvng me erro. Transcation not going through.
Any suggestions??
It is not working because you may have not overridden the IERC20 functions. At least as they should. I would highly recommend using the ERC20 standard instead, just for how easy it is to implement.