I want to "listen" to a specific ERC20 address balance changes. I can fetch the address's current balance using balanceOf, but from that moment I need to know about changes.
How can this be done? should I listen to the Transfer event. If so, how can I know the balance when I get the event.
Thanks!
Since retrieving data from Ethereum doesn't cost you any gas, I think the simplest way is to call balanceOf when you detect a Transfer event with that specific address equals to from or to field.
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 want to airdrop and presale my won token
so in the next step I want to lock them until my IDO data come
and unlock in this time 30% of user wallet balances
and next month 50 %
what is the best way to do this
If you want your token to be sent to users when certain date comes automatically without backend or any users or you doing the transactions, it is not possible. Blockchain operations cannot be executed without calling a transaction using scripts by user or backend.
You can write the following airdrop contract to achieve desired result:
Users send stable coins to your airdrop contract. Addresses of these users should be stored in some place. You can store addresses and amounts of token buyers automatically (in arrays on the blockchain - address[] buyers, uint256[] amounts).
If you want users to click on button on website to receive available amount of tokens, your airdrop contract should have a method to send right amount of your token to caller if the caller sent stable coins before. This method should check, if msg.sender is in buyers (check in array can be expensive, you can create a buyers map only for this check) and if airdrop date has come (you can save timestamps in seconds in some array and check if any date has come via comparison with block.timestamp). If both requirements are met, contract sends tokens from your balance (transferFrom) or from its balance (transfer) following the exchange rate.
If you want send the transaction by yourself to give all tokens to buyers, you can make the method, which can be called only by you (Ownable will be very helpful). Inside this method, contract goes through buyers array and send amount[buyerIndex]*exchangeRate to each of them. This method doesn't require users to click on any buttons, but can be very expensive in gas and hence for you.
I'm developing a new dapp and I'm wondering with current Ethereum state of network, which are correct parameters to send along with web3, i.e.
myconytact.methods.myfunction(<params>).send({from:address,?????})
my problem is gas, gas limit and so on. I should use estimateGas? and the put gas:gas in the object passed in send()?
Generally, you never need to put in gas or gas limit, as those are set by the user in his or her wallet when confirming the transaction.
If you are using the ERC-721 standard, what is the prefer method determining which tokens the address owns in a DAPP.
Currently I'm request all the Transfer Events for an address and basically sorting them into transfer in and transfer out, and then using that to determine which tokens the user owns.
Is there a simpler way I missed.
Transfer events may be emitted also by contracts that are not ERC-721 tokens, or some noname tokens that you might not be interested in.
The actual token ownership is stored in the tokens contracts (and not the DAPP contract).
So your current approach is pretty much as straightforward as it can get, if you want to automatically keep track of all tokens that the address currently owns (and some false positives as well).
Note: This is also similar to the approach of Etherscan, which listens to all Transfer event logs and if the sender contract is listed in their database of tokens, they use the event log data to update balances of the sender and recipient.
If you're willing/able to create and maintain a list of tokens that you want to follow, I'd recommend a simpler approach:
Define the list of followed token contract addresses (e.g. ECF or RARI)
For each of these token contracts, call balanceOf(<your_dapp_address>) that returns amount of tokens that the <your_dapp_address> currently owns.
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.