Does updating/redeploying a smart contract on Flow reset its state on mainnet? - cadence

Following this official guide here
https://developers.flow.com/flow/dapp-development/mainnet-deployment#updatere-deploy-a-contract-on-mainnet-using-the-cli
I would like to make some updates to a smart contract I've got on Mainnet and there's a bunch of existing NFTs in there in an array that lives on the contract, would updating the contract via the CLI following the guide above just... erase those NFTs and reset the contract's data?

Great question! Updating a contract only updates the code deployed in the contract. It does not update any objects or other state stored in the contract. This is why you can't do any updates to a contract such as adding or removing fields, changing the type of a field, and other such changes. See the upgrade docs for more information: https://developers.flow.com/cadence/language/contract-updatability

Related

How to create a custodial wallet per user on a plateform with Solidity?

I'm new in the blockchain community, and i have to realize a web 3.0 project.
In this project, we have an ERC20, and for each user who sign up on our platform, I have to create a custodial wallet attached to this user.
User A want to be able to send tokens to User B.
I didn't find something concrete on google... so I'm maybe going in the wrong direction.
My question is: Is it possible to do that type of custodial wallet with smart contract in Solidity, and can you explain me how ?
In other to achieve this you will need 3 smart contracts:
Factory: This is the smart contract that has a function deployWallet that can only be called by a certain address, most likely the deployer address. What this does is deploy a new instance of another contract WalletProxy and store the address in a mapping to a UUID string which you use to identify each customer in your off-chain DB.
WalletImplementation: This contract holds the action you want your wallets to perform, e.g transferERC20, stake, swap, etc., and can be anything. It's going to be a contract you can always swap out and use another one with a more updated function, but be careful you need to understand how upgrades work in smart contracts and design Version 1 well. This contract will only be deployed once for every new version created.
WalletProxy: This is the contract you deploy every time a new wallet is created by calling the deployWallet function in the Factory.sol contract, only callable by a certain address. It serves as a wallet for each user and it's only a proxy contract that uses delegatecall to call functions from WalletImplementation, so in the future, if there is any update like WalletImplementation V2 it will always have access to it. The tricky part is also writing it in such a way that only a certain address can call all of the deployed wallet proxy contracts.
Reference Contracts:
I created the following contracts for the same demonstration purposes when asked how to create a custodian wallet using smart contracts.
Factory.sol
WalletImplementation.sol
WalletProxy.sol
I also did a live session where I built a simple exchange using the pattern described above. You can also go through the full codebase here https://github.com/CeloTAs/cXchange

how to interact with an already deployed smart contract

I was reading this contract that I found in the solidity documentation and I assume this contract is unique and deployed by the auction house. All users can make an offer. From an implementation point of view, how does a user call the methods of this contract using web3.js if he didn't deploy it?
You do not need to deploy contract for calling its methods using web3.js.
Only if the methods need owner rights you could not be able to call its methods, but otherwise, you can call methods of the smart contract.
Also you can use Etherscan for calling contract methods. Navigate to the Contract section of your selected smart contract, then in sections Read Contract and Write Contract you can call contracts' methods.

Where Bytecode is stored

The question is very simple.
Using as a reference the following image:
As I understend the Ethereum Blockchain only stores the thinks that are inside the boxes (PREVHASH, STATE_ROOT, TIMSTAMP, NUMBER) and for each account (NONCE, BALANCE, CODEHASH, STORAGE_ROOT).
So, where the code (In this case the bytecode) of an Smart Contract is stored?
Contracts live on the blockchain in an Ethereum-specific binary format (EVM bytecode) that is executed by the Ethereum Virtual Machine (EVM).
The compiled EVM code is sent off to the blockchain with a contract creation transaction while the additional metadata created as part of compiling a smart contract will ideally live on the decentralised cloud as publicly verifiable metadata complementing the code on the blockchain.
In the process of deploying a smart contract, we are actually sending a transaction to the empty address with the EVM code generated as part of compiling the smart contract as data of the transaction.
After some time, your transaction should appear included in a block confirming that the state it brought about is a consensus. Your contract now lives on the blockchain at the address created for the smart contract.
Contract addresses have bytecode associated with them and there is no one externally controlling the private keys behind the contract. Contracts keep a CRAB log instead of a CRUD database (Create read append burn vs Create read update delete).
As we have the code of smart contract part of the data of the transaction that created the smart contract and also we have the new address for the smart contract, Ethereum blockchain can actually find out the actual byte code to be executed by searching for the first transaction that has the output pointing to the contract address.

Where ethereum contact instance state is stored?

Does ethereum contact instance state stored in blockchain?
And when an updated contract is deployed ,how to restore previous instance state into the new contact instance?
Look at this answer for information on how the state is stored.
Migrating the state to a new version of your contract is the developer’s responsibility. If the data for your contract is tightly coupled (resides in the same contract), then you have to extract the data manually and deploy it with the new version.
The more accepted way is to create a separate contract for the data and pass the address into your contract that is responsible for executing the business logic.

Can we add changes to the smart contract once it has been deployed?

I am a newbie and am learning about the blockchain and smart contracts.
So my question is can we add changes(features) to the smart contract code once it has been deployed?
and is it possible to create an ERC 20 token that is based on proof of stake?
Thanks
No, the very foundation of a blockchain is its immutability, meaning that once a set of data is sent to the blockchain, it can never be modified or changed in any way. Therefore, once you push a smart contract to the Ethereum blockchain, it cannot be changed or manipulated in any way.
It's not entirely possible due to the fact that an ERC20 token is embedded into Ethereum's blockchain. So, when miners are mining the Ethereum blockchain, they're essentially mining every ERC20 token as well.