Web3JS Ethereum Contract Storage History - ethereum

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 .

Related

Why is smart contract interact with smart contract necessary?

I'm new in blockchain development, I wonder why is it necessary to interact with contract from another contract instead of from a personal EOA address?
Besides the delegatecall, I can't imaging any advantage to use contract to call another deployed contract's functions. As a user, I may rather use ethers or web3js through wallet etc to interact with a deployed smart contract instead.
would you please show me some reasons or necessary cases that I should design my project using a smart contract to interact with another smart contract? Thanks a lot!
There are too many use cases:
1- Upgradeable contracts or proxy contracts. Since blockchain technology is evolving so fast, it is so hard to write a contract that will be scalable. With proxy contracts, user calls the proxy contract but proxy calls the main contract. if in the future you have to change the logic of the contract, you deploy a new contract and make the proxy contract call this new contract. This way users will not get affected. You can read more about this pattern here
2- If you ever need to get data from real world, you cannot make an api call because this is not safe and not deterministic. for this reason, we use oracle services which also calls to many other smart contracts behind the scene, gets the results from each, and make a final response to the user. You can read more about oracles here
3- another use case is factory contracts. think about it as a class and users keep make an instance of it. that way instead of factory owner pays for the new contract creation, user will be paying for the deployment. you can read more about factory contracts
4- defi platforms have too many smart contracts and they are interacting each other. They keep the logic separate from each other, so they maintain better and see the missing points better. also putting everything in one contract would make contract code is bloated and make a mess. you can read uniswap
5- another use case to deploy library smart contracts. those are usually mathematical contracts which are deployed once and once u need complex functionalities, instead of you implement in your contract, you use library contracts and use them in your project. You can read about libraries
6- another use case is solving the scalability issues. imagine you have one kitchen and you have to serve all the customers from only one kitchen. As you get more customers, your service will eventually halted. so, there are layer 2 solutions to transfer some of the orders to different kitchens. and this of course happen through smart contract communications. more on layer 2
7- In general you can use inheritance. You deploy some contracts and inherit the logic from those smart contract. that is why you see openzeppelin contracts always inherit from each other
For example there are some standarts in Ethereum ecosystem. ERC20, ERC721, ERC1155 and others. So when you developing new project, instead of writing it from scratch you could install openzeppelin-contracts, import them and inherit. Afterwards you have this "carcass", on the top of which you can implement some additional functionality/custome code, etc. That approach is way more convenient and secure.
Another great example chainlink-contracts. Via them you can read from oracles. Oracles are smart contracts that keep track of cryptocurrencies prices online. Say you want your users to pay 10$ to participate in lottery/some giveaway. Your contract interacts with chainlink contract and makes sure that amount of eth that user paid is greater or equal to 10$ otherwise it returns error.
In solidity there is no true randomness, but again throughout interacting with chainlink contracts we can reach it. Another chainlink feature is automation contracts, check chainlink documentation to learn more(https://docs.chain.link/). To sum up id say that there is a lot of different protocoles/concepts which easier to interact with than to write it again.
"No need to reinvent the wheel"

Can I sign transaction on backend using javascript on ImmutableX chain?

my question is basically written in title, but for more details, I am using node.js, I read immutableX documentation and review there SDKs, but nothing on javascript,
I want to sign the transaction in cloud function(firebase) on immutableX chain.
Main goal is to transfer ERC20 tokens from one wallet to another. In my case, token which I want to transfer is GODS.
Thanks!
Well, I've never used ImmutableX but I think that for transferring ERC20 tokens you can use any library that can interact with Ethereum contracts like web3js
https://piyopiyo.medium.com/how-to-send-erc20-token-with-web3-js-99ed040693ce

Best Way To Track Transaction Volume Sent to a Ethereum Smart Contract

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

Updating a live smart contract

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.

Can we get transaction information recorded in the past block using Solidity in the Smart contract?

I am studying blockchain with Ethereum, and I want to use past transaction data in the Smart contract using Solidity.
If I use Web3.js module in the program written in javascript, I can get these data easily.
But I can't get these data in the Smart contract using Solidity.
Reference of Solidity says that we can get current block number, blockhash, etc., by using "block.number" and "block.blockhash(uint blockNumber)" functions, but doesn't mention getting transaction data.
(http://solidity.readthedocs.io/en/latest/units-and-global-variables.html#special-variables-and-functions)
please help me.
The answer is simple. Unfortunately, you simply can’t access old transaction or block data onchain from Solidity. At most, you can access hashes of last 256 blocks (see blockhash in documentation )
Alternatively, as a workaround you could consider using Oraclize. Oraclize represents way to read offchain data onchain, so you could try to read transaction data from Etherscan web API. The way Oraclize works is that :
You request to Oraclize smart contract what data you want to fetch from internet (some URL)
Oraclize offchain servers then detect your on-chain request
The look up the data you wanted (they'll make some http request to the URL you provided)
Once they get response, they will send transaction to your contract (calling specific callback method) containing data you requested
With such approach however, you are relying that:
EtherScan is up and running
Oraclize is up un running.
If you only care about transaction data related to your smart contracts, another way would be to store that transaction data onchain. Maybe we could gave you some more suggestions if you tell us more about what specific problem are you solving.