I've got an ERC1155 contract and I want it to receive an ERC721 token and give other tokens in exchange for that. Can I somehow add ERC721 Receiver interface?
Yes you can do that. It would be best to start looking here at OpenZeppelins documentation on it: https://docs.openzeppelin.com/contracts/2.x/api/token/erc721#IERC721Receiver
In your method that accepts ERC721 tokens, you will need to make sure that the sender approves your contract to transfer tokens on their behalf or else the transaction will fail.
In conjunction with IERC721Receiver you have to use the safeTransferFrom method for this to work properly.
hope this helps.
Related
I'm building a DeFi application on Ethereum, and I would like to implement the Deposit function. Everything works fine between ETH and a ERC20 token that I built, but I would like to add some tokens like aToken for AAVE or cTokens for Compound that the user will receive after a Deposit call.
So the question is: is it possible to add a function in my smart contract to create multiple tokens without creating a smart contract for each of them? If not, I have to create a different contract for each token I want to add in my app or there is a best method to do it?
Yes this is possible. You can transfer the tokens to the user's address after the Compound Protocol mint operation. This can be made generic using the ERC-20 transfer interface. Be sure to account the amounts users are due and beware of the reentrancy vulnerability.
My ultimate purpose is to swap some tokens across pancakeswap babyswap apeswap atomicly. So I build a contract(called ContractA) to run a bunch of swaps in a transaction. I transfer some USDT token into ContractA. But ContractA is not approved to transfers USDT by the USDT contract.
I known how to approve Metamask address to transfer USDT, but how to do it for a Contract?
In order to control an ERC20 token from a smart contract, first you need to create an instance of it. To do that, first you need to import ERC20 to your contract, and then just create an ERC20 type function giving it the token address you want to manage. It would be something like this:
// Some code...
import "#openzeppelin/contract/token/ERC20/ERC20.sol";
// Some more code...
contract exampleContract {
// Example of an ERC20 token instance
ERC20 USDTToken = ERC20("USDT Contract Address Here");
// Approve USDT
USDTToken.approve(address(this), _amount);
}
Then you will be able to manage the token, always following the ERC20 standard, as you want.
Hope you find this information useful :)
I want to receive some USD coin, for this special wallet is required or can any ethereum address receive it?
Yes, it is possible as USDC follows the ERC20 standard, so you don't need a special wallet for it.
ERC20-tokens have an "abi", which defines the functions that ERC20-tokens use. The abi on https://etherscan.io/address/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48#code is for a Proxy Contract and is not ERC20 compliant, as it does not define a "transfer" function.
To still be able to send USDC tokens from an address, you simply gotta define the transfer function in the abi when creating the transaction. You could even just take the minimal ERC20 abi and delete the USDC abi altogether.
What is the best way to send transaction in Ethereum with ERC-20 token and get balance of account using web3j
Take a look at https://github.com/blk-io/erc20-rest-service it will proxy requests into an ERC20 contract.
I'm a new beginner in the ethereum Blockchain.
I want to implement a smart contract that verifies the authenticity of a user and then send a message to a cloud service (with is the smart contract creator) in case of positive verification to grant access to the user.
My question is it possible for a smart contract to return results of his methods invocation to another blockchain user and how we can do it?
A smart contract cannot make calls outside of the blockchain. i.e., you cannot have it call an http endpoint or something similar.
In ethereum, the usual way of achieving something like this is:
Have the user make a transaction passing relevant data to your smart contract
The smart contract runs whatever verification logic it must on that data
The smart contract logs an Event
You have an ethereum node that your code connects to and listens for these events.