Is it possible to get a list of token holders for a given ERC20 token from within another solidity contract?
Since "balances" are stored in a mapping in most ERC20 contracts, I do not think it is possible, since you can't get a list of keys for a mapping in solidity.
Is there anything I missed? Or is this just impossible?
Thanks!
It is not possible to get a list of ERC20 token holders directly from a contract.
You are correct in that you cannot do this because you cannot get a list of keys for a mapping in Solidity, therefore it is impossible without external intervention.
With that said, there are many people who need this functionality and perform tasks to achieve this. The biggest example I can think of is airdropping tokens to various accounts based on their holdings of another token. The way that most people do this is to read all of the token holders from the blockchain and store it in a local database. From there, they will implement a gas-efficient function that takes in the addresses as a parameter and performs actions on them that way.
It is not possible to accomplish what you desire using only the blockchain, but using a combination of on-chain/off-chain logic can achieve your goals.
It maybe possible.
If you make a function that can pick sender and receiver when transfer function is called, it's possible.
mapping(address => bool) _holderList;
function transferFrom(address from, address to, uint256 amount) {
checkHolderList(from);
checkHolderList(to);
_transfer(from, to, amount);
}
function checkHolderList(address _address) {
require(balanceOf(_address) > 0, "this address can't be holder");
require(_holderList[address] != true, "this address is already in holder list");
_holderList[_address] = true;
}
Related
I have to develop an NFT minting contract with a whitelist system where we can set a variable amount of mint allowance per address.
It would be easy to achieve by setting manually in the contract with something like:
mapping(address => uint) allowancePerAddress
But as it's on Ethereum this solution would not be very cost efficient as we would need to store a couple hundreds of entries manually.
Is there another way, through signature for example to achieve this so it would be cost efficient for both the project and the minter ?
Signatures serve for checking the integrity of a message and also validate the emitter of the message, so Im not sure how would you accomplish using signatures, mainly because you'll need to set a value for each address, and keep track of them in the contract.
What you can do is create a constant that would be the default allowance, and when someone mints their first token you can update their allowance value during that transaction.
Yet, if what you want is to set up a custom value for each address, and you have a high amount of them, you can either do it on deploy, on the constructor or create a function that receives an array of Struct {address, allowance}, and set every address on a loop.
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.
I do not have much experience with Solidity programming. I want to create a smart contract for a new digital asset, say Cryptodoggies. I want to know if there is a way to prevent users from being able to resell/transfer their cryptodoggies.
You could have a variable (boolean I think would be more suitable) on your contract and use it in an access modifier for the transfer function so that the transfer function would require for this variable to be true. Then by making this variable false (by a function accessible only to you) you can stop users from transferring coins. Of course, this modifier would have to be used for every function that enables users to transfer tokens.
It would be something like that pausable ERC-20 from openzeppelin. You can find it here.
I want to create a smart contract which people can transfer tokens without ether in their wallet.
Suppose A want to transfer ERC20 tokens to B, but he does not have ether in his wallet.
Does third party C can send the transaction for A and therefore pay the gas? Is it possible to create a function in the contract for this usgae?
I have searched online for a soloution and could not find one.
This is a key issue of Ethereum dApp development, but also of tokens. Here is a very old thread on Ethereum Stack Exchange, and also this one.
There are 2 options with their pros and cons:
Use signatures
Every function in your smart contract must have signature parameter.
People who want to interact with the smart contract must sign the function parameters with their account's private key and send it to the smart contract owner (via any communication channel).
The owner then submits the parameters along with the signature to the blockchain, paying for gas. The signature guarantees that the message was approved by the user.
Refund used gas at the end of the transaction. A modifier refundGasCost can be used for this (see below).
But (1) is really hard to apply to token transfers where you just don't know who uses the token and (2) does not really address the issue.
There is a lot happening recently, there is this blog post about How to save your Ethereum Dapp users from paying gas for transactions, and since you ask about tokens, there is an ERC that suggests to Pay transfers in tokens instead of gas, in one transaction which would be nice if you have tokens but no ETH.
I hope this helps.
Exactly this case is already defined in the ERC20 standard. Its this function:
function transferFrom(address from, address to, uint tokens) public returns (bool success);
But before party C could use it and send tokens from A to B, A would have to approve C to do this via the following function, which is also defined in the ERC20 standard:
function approve(address spender, uint tokens) public returns (bool success);
No, in a standard ERC20 token contract, the token holder must initiate at least one transaction (to call transfer() or approve()), and a transaction must always be paid for by its initiator.
this https://theethereum.wiki/w/index.php/ERC20_Token_Standard eth wiki describes erc20 standard as a set of functions and attributes a token needs to have implemented. some of them are pretty self explanatory like
function transfer(address to, uint tokens) public returns (bool success);
which takes coins from your wallet and transfers it to somebody elses.
But on the other hand
function approve(address spender, uint tokens) public returns (bool success);
or
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
How am I supposed to know what is the logic behind these methods? are there any extra docs describing it? and last but not least: what are upsides of tokens being ERC20 compliant?
ERC20 standard contains a set of functions that were proposed EIP-20 which were reviewed and voted on by community. As per abstract states the following:
This standard provides basic functionality to transfer tokens, as well as allow tokens to be approved so they can be spent by another on-chain third party.
See here for more some analogous examples on what approve and allowance do, but basically these are functions that allow an account owner to approve moving a fixed amount of tokens from their account to another account. approve allows you to authorize an address to move a fixed amount, while allowance simply returns this amount.
Looking at the EIP and ERC20 documentation can be a bit daunting at once, but one you start playing with the functions it makes a lot more sense. For quickest way to start testing I'd suggest using Ethereum's remix.
If you think the documentation on that wiki is not enough, you can look at the original EIP and look at all of the discussions involved that led to the final version, along with links to additional documentation and code examples to further explain the intention of each function. Note that all of the ERCs have a corresponding EIP, so you can refer to those for all of the other token standards.
what are upsides of tokens being ERC20 compliant?
The upside is that others will know how to work with your token.