I'm fairly new to smart contract development. I am looking for what is the best approach to track total volume sent to a smart contract. The options I have found are;
Emit an event when a transaction with value is sent to the contract
and use ethers.js to filter and aggregate all these events to get
total lifetime volume?
Create a mapping in the smart contract which increments every time a value is sent to the contract?
What would be the best to get this data. The first approach with events would give me the ability to filter on timespans. Does anyone have insight what's the best approach or if there is a better way than the above two.
You have 3 options
First:
you can create variable and add into this variable the value of the transaction and when you need to know all value just return this variable
Second :
If you want to track the volume in real time you can add event listing on this event and then add the result into your db every time someone interact
Third:
you can make queries filtration on the event and callculat all volume
Related
First of all apologies for probably poorly named question but the fact that I can't formulate it better is probably partially a reason why I've not been successful in finding the answer. I am trying to understand where in blockchain data can I find specific value of smart contract method. For instance, as in the screen below, this specific smart contract 0xF59D66c1d593Fb10e2f8c2a6fD2C958792434B9c holds information about the totalAssets and pricePerShare. I can see the values in the Etherscan but now, having access to logs/events and transactions from Ethereum blockchain- can I find these values in either logs/events/transactions? And how would I go about finding that value per block in the past? Any hints how to approach it would be very helpful
To get historical data, you can refer to the smart contract view function indicating the block number at the time of which you want to view the result. For example, for JSON RPC, such a request looks like:
{"jsonrpc":"2.0", "method":"eth_call", "params":[{"to":"contract", "data":"function signature and parameters"}, "block number"], "id":1}") ;
In my company, we are using Event Sourcing pattern to implement a storage for all changes to the price of a booking. Across the company, different services might try to append events to a booking identified by a booking code.
We use DynamoDB to store the event and it does support consistent read. The thing is in the case when a booking is initially made and the very 1st event is created for a booking code, if we fail to save into DynamoDB for whatever reasons, we put the event into a fallback queue and simply return a success to the client to acknowledge that we already received the event. Client can then move on with their business logic flow and in turn, show a success message to end users. The goal is to not block booking creation at all costs.
The problem is, for a very short period of time, when the event is still in the fallback queue, if clients try to fetch the event using the booking code, they will get back an error although we told them that the write on the 1st event was a success earlier. In a way, we're breaking the consistent read promise here.
I'm trying to find a way where we can improve the design and keep this promise while remaining out of the way of the main booking flow (i.e. not blocking the booking on failure).
I'd be very grateful if someone could throw me an idea to look into.
One solution might be to have the fallback queue be durable (i.e. reading from it doesn't remove elements from the queue) up to some retention period (broadly: the maximum allowable time between initial booking and persisting the creation event to DynamoDB) and instead of being a fallback queue be the actual source of truth for which bookings have been created.
Services can then consume this queue: one of these services is responsible for writing the initial creation event to DynamoDB (which is longer-lived than the queue). If that service is falling behind and approaching the retention limit, that's an operational emergency, but you're buying yourself time. Another of these services maintains an in-memory view based on the queue of created bookings which haven't yet made it to Dynamo.
If I have a smart contract that has the addresses of some other smart contracts hardcoded inside it. E.g. maybe my contract has the addresses of some external yield farming vaults that it periodically deposits some balances to.
Now lets say that I want to update that list of addresses and remigrate it without upsetting the operation of the current contract.. what is the best way to do it ?
Ethereum bytecode is immutable, so the simple answer is: You can't update the hardcoded addresses. You'll have to create a new contract and this time save the addresses into variables that you can update later.
I have the following question, lets say a transaction calls a Smart Contract's function, which
changes the contract's state on Ethereum Mainnet. E.g. a counter variable was set from 0 to 1.
Is there an easy way to access the previous state via Web3JS?
Thanks in advance!
Yes, as long as you know where in storage to look. The JSON-RPC method eth_getStorageAt lets you retrieve storage at a given location at an arbitrary block. web3.js exposes this as web3.eth.getStorageAt().
yeah you can use events in smart contracts functions to save every interaction with the smart contract then use web3 and get all the events that happened on the smart contract like that you get all the records of the previous state .
I'm trying to create a contract that will dish out regular inflation to all holders of the token. Every x period, it needs to scan the addresses and calculate their inflation, minting new tokens and adding it to their total.
What is the most cost efficient way to do this? Is it cheapest to iterate over the map and update each address, or better to calculate all values in memory and replace the map? Perhaps there is a better way I'm not thinking of.
Not sure how big the map could get, but cost efficiency is key here.
Thanks!
The cheapest way to do this is to have a smart contract function that allows a wallet holder to "claim" their inflation payment. e.g. instead of iterating over every address, you let the wallet holders to do the inflation calculation just for their address.
The cost to you is 0, just have to ensure the smart contract logic is correct
If your intention is to regularly send token/eth to account addresses I urge you to read up on the withdrawal pattern. If you do not follow the withdrawal pattern, a malicious party can set up a smart contract that will get your transaction stuck during the payout.
Instead of sending funds directly you create a map about how much each address is allowed to withdraw. This is very similar to the map about which address holds how many token.
When transferring out of your contract now, only the transaction of the withdrawing party is affected.