Created custom ERC20 contract, balanceOf msg.sender is zero - solidity

I've created a ERC20 contract in Remix:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
_mint(msg.sender, 1002000);
}
}
Then I deployed it:
But the balance of msg.sender is zero:
Does anyone know what's wrong?

I just tried your code and it works perfectly like intended.
It is important, that you compile the correct contract using Remix, see my attached picture. You need to choose the contract "MyToken". I guess you could have deployed the contract "ERC20 - #openzeppelin/contracts/token/ERC20/ERC20.sol" which would lead to the behaviour you mentioned.
compile "MyToken" in Remix

Related

How can I get metadata and my image to show up on OpenSea Testnet?

I followed along with Remix's beginner NFT course and successfully deployed a few NFTs using the Goerli testnet and their provided IPFS data. I uploaded my own image and metadata and can see it on IPFS but neither the metadata nor the image is populating on OpenSea.
Here is the code for the contract I am deploying:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "#openzeppelin/contracts#4.4.0/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts#4.4.0/access/Ownable.sol";
contract Donation is ERC721, Ownable {
constructor() ERC721("Donation", "DONO") {}
function _baseURI() internal pure override returns (string memory) {
return "https://ipfs.io/ipfs/QmXZKcU9WDZxvXvxoAL4YdZVR5Ssj97ayEYRPqYBHrRSb2";
}
function safeMint(address to, uint256 tokenId) public onlyOwner {
_safeMint(to, tokenId);
}
}
Please see the URL that I return for my metadata and subsequent link to my image. Is there anything you see that is immediately wrong that would indicate why nothing is populating (in the JSON file, code, or otherwise)?
For ERC721, the _baseURI will essentially be the base to be combined with the NFT's token id, so for example NFT with token ID 0 will have the tokenURI of:
https://ipfs.io/ipfs/QmXZKcU9WDZxvXvxoAL4YdZVR5Ssj97ayEYRPqYBHrRSb2/0
which in this case after checking is invalid as https://ipfs.io/ipfs/QmXZKcU9WDZxvXvxoAL4YdZVR5Ssj97ayEYRPqYBHrRSb2 should be the valid tokenURI itself. OpenZeppelin designed the ERC721 contract this way as it is the most gas-efficient way to create a standard ERC721 NFT. However, the drawback is that it made it difficult to provide modified URI for each token ID.
If you like to set tokenURI with different base for different token ID, then you should instead check ERC721URIStorage in the contract wizard. This way, the ERC721 NFT contract will have the _setTokenURI(tokenId, uri) function, which allows you to modify the tokenURI for different token ID. However, keep in mind that this will be very expensive on the user side as string inputs cost a lot in EVMs.
Hope this helps~

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.

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.

Can't transfer ETH from swap smart contract to user

I try to send back ether when user sell token in swap, and i have that error:
My solidity pragma: pragma solidity >=0.4.22 <0.9.0;
project:/contracts/ANQSwap.sol:33:9: ParserError: Expected primary expression.
payable(address(msg.sender)).transfer(etherToSendBack);
It's my function code:
function sellTokens(uint256 _value) public {
require(anteqToken.balanceOf(msg.sender) >= _value, "You doesn't have enought AnteqToken.");
uint256 etherToSendBack = _value/rate;
require(address(this).balance >= etherToSendBack, "AnteqToken Swap doesn't have enought Ether to buy yours token.");
anteqToken.transferFrom(msg.sender, address(this), _value);
payable(msg.sender).transfer(etherToSendBack);
}
And I too try
payable(address(msg.sender)).transfer(etherToSendBack);
Fixed error
I added one pragma version to all .sol file
pragma solidity ^0.8.0
payable(msg.sender).transfer(etherToSendBack);
Code above works.
I leave this question for other dev if they encountern on similar problem.

safeTransfer with token erc1155 in solidity 0.8

I get this error
ERC1155: transfer to non ERC1155Receiver implementer when try to transfer to a smart contract I found this doc https://docs.openzeppelin.com/contracts/4.x/api/token/erc1155 but still don't know how to fix this do I have to abstract IERC1155Receiver interface in my holder token 1155
The receiving contract needs to implement the onERC1155BatchReceived() function based on the ERC-721 definition.
pragma solidity ^0.8;
contract MyContract {
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes memory _data) external returns(bytes4) {
// here you can (but don't have to) define your own logic - emit an event, set a storage value, ...
// this is the required return value described in the EIP-721
return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
}
}