approve is required in ERC20 for a Dex? - solidity

According to the take advantage function, it is used to use your tokens in an external contract based on the permissions and the amount that you indicate
But:
When implementing an ERC20 is it necessary to implement approve? To use it on a Dex?
With the ethereum fees high enough to make a simple swap. If I don't implement it, what can happen?

The approve function allows an external address to spend tokens from a specific address on its behalf. If you do not implement it, then transferFrom function will not work, since you always need to approve the token usage for an specific spender. Thus, the only way to transfer tokens will be calling the function 'transfer', which won't be accepted by a DEX. DEXes use the transferFrom function in order to interact with an address’s tokens on its behalf.

Let's say Dex contract has a staking functionality:
function stakeTokens(uint256 _amount,address _token) public{
require(_amount>0,"Amount must be more than 0");
/*call transferFrom from erc20. transfer only works if it is being called from the wallet who owns the tokens. If we dont own the token we use transferFrom. We have to have abi to call the transferFrom. so we need IERC20 interface*/
// In order to interact with a contract we need abi
// IERC20(_token) initilaizes the contract
IERC20(_token).transferFrom(msg.sender,address(this),_amount);
// ... more logic down here
}
approve is used to authorize the DEX to make transfers on behalf of the token owner up to the approved amount. How DEX contract will withdraw from your contract? You have to first give permission with approve

Related

Are there smart contracts in Etherium that can be called to buy other cryptocurrencies?

I want to buy Arweave coins when my contract solidety payable function is called. So I wonder are there any Smart Contracts with solidety API that provide exchange services?
I look for a function like: buy('wallet_address_to_transfer_coins_to_on_other_network', 'token_mnemonic', Amount); function that can be called within my solidety payable function and could for example sequre tokens for NFT data storedge space.
So are there cryptocurrency exchanges with solidity API?
if you want the tokens in other chain maybe you could try with a bridge, if not you could try with any dex in which the coin you want is listed, or maybe you could need both for the thing you want to

ERC721 Smart contract revoke approval

I've some question about ERC721 processing. I'd like to make an user to give aproval to an external smart contract for transfering one of its NFT.
To do it I'm using approve(to, tokenId).
Then, in some situation, the contract should be able to revoke its own autorisation by calling approve(address(0), tokenId)
(The NFT owner calls a cancel method from the smart contract that perform some operations and revokes its own permission on the token)
At this step, I got the following error:
ERC721: approve caller is not owner nor approved
My understanding is that in order to make the contract able to call approve, the NFT owner should have approved it with setApprovalForAll(operator, _approved)?
What about others NFT from the same collection owned by the user ? Would the contract be able to manage them too ?
I'd like to limit as much as possible the smart contract's permissions and stick to a very specific NFT transfer (with its token_id)
Can someone enlighten me on the right way to do it ?
It sounds like you have 2 contracts and an EOA involved in this process. You want the NFT contract to give another contract permission to transfer tokens, as well as remove that permission. You should be following this order:
1.) EOA calls approve(to, tokenID) on the NFT contract
2.) NFT contract has a function that calls approve(address(0), tokenID)
3.) Non-NFT contract calls the function in step 2
--> rever error
You would indeed get this error if Non-NFT contract has not been given approval permission for tokenID. Even though the token owner is calling that function, the context of the call reads the Non-NFT contract address as msg.sender. You could avoid this actually if the function inside the Non-NFT contract made a delegatecall instead of a regular call.
You do not need to use setApprovalForAll if you are only working with one NFT. You can give the contract approval by calling approve() and then revoke it with the method you stated above.

Is smart contract receive, hold and send different kinds of ERC-20 tokens

I'm creating a smart contract using solidity and publishing it to the Mumbai Test Network.
Right now when I'm interacting with the contract on the client-side using ethers and metamask it's automatically using the MATIC token.
I want the smart contract to be able to receive different kinds of ERC-20 tokens and afterward be able to transfer them.
const parsedAmount = ethers.utils.parseUnits(amount.toString(), 'ether');
const contract = await tokenContract.createTransfer({ value: parsedAmount });
For example, I want to be able to send the test token shown in the picture.
Link to the faucet
I've searched online and read docs but couldn't find an answer for this problem...
If needed, I will add any info you think will clarify this question!
Thanks in advance :)
The ERC-20 standard doesn't define any way to let the receiving contract know about a transfer that is not initiated by the receiver. It "only" emits an event but that's not readable from onchain.
All ERC-20 balances are stored in the respective token contracts. For example, if an address holds 10 USDC, this information is stored on the USDC contract - no matter if the holder is an end user address or a contract.
Combined these two things together, you can send a transaction to the USDC (or any other ERC-20) token contract, invoking the transfer() function where the receiver is your contract. This will effectively transfer USDC from the user to your contract address.
const usdcContract = new ethers.Contract(usdcAddress, usdcAbi, metamaskSigner);
const to = "0x123..."; // your contract address
const amount = "1000000"; // 1 token with six decimals
await usdcContract.transfer(to, amount);

Migrate my users ERC1155 tokens to ERC721

I have my ERC1155 token contract deployed on Ethereum network
i want my token holders to make the token to 721 standard and burn the old tokens
how can i achieve that using a proxy contract
need some proper direction on this , thanks
So looking to migrate the OpenSea ERC1155 tokens to the new ERC721 tokens. The existing contract has 135 tokens.
a proxy contract won't let you do this migration for a couple of reasons, first a proxy contract just let you update the address of the contract where the actual logic is, but it has some limitations, like that the proxy contract should be deployed from the beginning you can't just deploy a proxy an expect that it changes or modify an existing contract, but let's say you have an upgreadable erc1155, for the limitation of the proxy you will probably have a lot of problems doing this update, because you can't change the way that the variables are defined or stored, you can't delete any existing function (you can leave it empty instead),and other things
probably you will need to deploy a new contract that will be the erc721, then you will have to make another contract that makes the swap, receive a token from the old contract and gives the token of the new contract, is on you how the contract will get the new tokens if it will mint the tokens or if it will have it, also you have to considere what you will do with the old tokens because if the old contract do not let the tokens to be burned you can't, but you could lock it in the same contract or something similar that is almost the same as burning it

How much nfts can be transferd on 1 smart contract?

I can't figure out if i need to Deploy a smart contract for each transfer? or can 1 smart contract serve several transactions? Thanks ahead
You don't need to deploy another smart contract to transfer NFTs.
Info about each token ownership is stored in its collection contract. So you just need to execute the safeTransferFrom() function on the collection contract from the current owner address, passing it following params:
current owner (_from)
receiver (_to)
token ID (_tokenId)
There are several ways to execute the function from a regular (non-contract) address, for example using the UI of EtherScan/BSCScan under the Contract -> Write Contract section on the detail page of the collection contract address.
Note: All assuming the collection contract follows the ERC-721 standard.