Smart contract ERC721 - Mint all tokens to contract address - smartcontracts

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.

Related

How to call a function from smart contract A on a token ID received from smart contract B?

I have two ERC721 smart contracts A and B. I have successfully minted a token ID on contract A and transferred it to contract B address (to transfer it to a contract address and not a wallet address I used IERC721Receiver). From here, is there a way for contract's B functions, which take a token ID as argument, to be called on the token ID received from A that now belongs to B?
For example, if Contract A was:
contract ContractA is ERC721 {
...
function mint(address _to, uint256 _mintAmount) public payable {
for (uint256 i = 1; i <= _mintAmount; i++) {
_safeMint(_to, supply + i);
}
}
}
and contract B was:
contract ContractB is ERC721 {
...
function exampleFunction(uint256 tokenId) public payable {
// Do something with tokenId
}
}
How can I call exampleFunction(6) on ContractB if token ID #6 was transferred from ContractA to ContractB (and not minted on ContractB)?
It just seems to me like there is no way to use methods from ERC721 contracts on token IDs that are not minted from the same contract where the methods are implemented.
Anything helps, so thank you in advance!
I am able to see that ContractB owns the token transferred to it by ContractA, but only on ContractA's ownerOf() method. I cannot do anything with that token on ContractB's methods, even though it now belongs to it.
You can call your exampleFunction() from your onERC721Received() implementation.
However you will not be able to do anything with the token as it will not have been transferred to you yet. The onERC721Received is purely to check that the contract supports receiving ERC721 tokens.

Created custom ERC20 contract, balanceOf msg.sender is zero

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

send ERC-20 tokens with solidity to user

as you know, ERC-20 network has many tokens like USDT, SHIB, LINK, . . .
I want to create a website , when user need to buy USDT token I should send the USDT token in his wallet or use need to send USDT to another wallet on the blockchain and I want do all of these things into the blockchain, and the user could see the detail of his USDT transaction :
USDT Transactions
now I have a big question about transfer tokens in ERC-20 network.
i write this codes in remix and solidity :
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol';
contract DEX {
struct Token {
string token;
address contractAddress;
}
mapping(string => Token) public tokens;
string[] public tokenLis;
address public admin;
constructor() {
admin = msg.sender;
}
function addToken(string memory token, address contractAddress) external {
tokens[token] = Token(ticker,contractAddress);
tokenLis.push(ticker);
}
function getTokenAddress(string memory token) external view returns(address moemory) {
return tokens[token].contractAddress;
}
function sendToken(string memory token, address from , uint256 amount)
external
{
IERC20(tokens[token].contractAddress).transferFrom(
from,
address(this),
amount
);
}
}
I want to add dynamically tokens to my website and smart contract, for this write this codes :
struct Token {
string token;
address contractAddress;
}
mapping(string => Token) public tokens;
function addToken(string memory token, address contractAddress) external {
tokens[token] = Token(ticker,contractAddress);
tokenLis.push(ticker);
}
I called addToken with this info :
Token : USDT
contractAddress : 0xdac17f958d2ee523a2206206994597c13d831ec7 ( USDT mainnet contract address )
it's work and add success USDT in tokens .
now I want to send some USDT to the user with this function ( imported the openzepelin IERC20) :
function sendToken(string memory ticker , address from , uint256 amount)
external
{
IERC20(tokens[ticker].contractAddress).transferFrom(
from,
address(this),
amount
);
}
now when I want to transfer amount from remix address one to remixaddress to into the USDT contract address it show me this error :
What is the problem? how can I solve this error?
Your contract is on a different network (Remix VM emulator) than the USDT contract (Ethereum mainnet), so they cannot interact. You can fork the mainnet and connect to the forked network from your Remix IDE, see this answer for more details.
Then you're probably going to run into another issue within the sendToken() function (since you don't cover it in your question, I'm assuming you haven't performed this step). In order to successfully invoke transferFrom(), the function caller (in this case your contract address) needs to be approved by the token owner (in this case the from variable) to spend their tokens. You can find more info in the ERC-20 specification to the approve() function.
Summary: The from address needs to invoke the approve() method on the token contract directly (not through your or any other contract), passing it your contract address as the first param, so that your contract is able to pull their tokens.

Smart Contract - BEP20: transfer amount exceeds allowance

I'm new in solidity and I'm trying to swap tokens from "Address A" to "Address B".
I used the functions approve and transferFrom, but I'm still getting the error: "Error: VM Exception while processing transaction: reverted with reason string 'BEP20: transfer amount exceeds allowance'"
Could you please help me with this issue?
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.3;
import "./CryptoPlinkoBall.sol";
import "./CryptoPlinko.sol";
import "hardhat/console.sol";
contract TokenSwap {
address admin;
address public owner;
address private _token;
constructor(address token) {
admin = msg.sender;
_token = token;
}
function swapTokens(address recipient, uint256 amount) external {
BEP20(_token).approve(msg.sender, amount);
BEP20(_token).allowance(msg.sender, address(this));
BEP20(_token).transferFrom(msg.sender, recipient, amount);
}
}
When you call BEP20(_token).approve(msg.sender, amount); you are approving the user to move that amount of tokens that the contract owns if you want to transfer the tokens from the user, the user should have called the token contract and approved the amount before calling this function, if you are doing the frontend that will interact with the contract you will need to put the call to the token contract first then the call to this contract
The approve must be mined before the transferFrom gets called.You can't do both on the same call, meanning the approve should occur before going into the swapTokens function.

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)"));
}
}