How to send crypto to an upgradeable contract using a UUPS proxy?

56 Views Asked by At

We've decided to use the UUPS proxy module offered by OpenZeppelin for our contracts to enable future updates.

However, our contract implementation requires users to deposit crypto for some functionality to work.

We'd like the crypto our users send to ideally stay in the same place and not require movement for every implementation upgrade.

Is this possible? If so, should our users send crypto to the proxy or the implementation contract for this to work?

Thanks!

Referencing https://docs.openzeppelin.com/contracts/4.x/api/proxy and https://docs.openzeppelin.com/upgrades-plugins/1.x/

2

There are 2 best solutions below

1
Mohit Kapadiya On BEST ANSWER

Another Question :

If we were to deploy a v2 implementation and update the proxy, then our user deposits would be split between the v1 and v2 implementation contracts. Is there any way to avoid this? Or would we need to instruct our users to manually migrate their funds to the v2 implementation?

Answer :

contract MigrationContract

// The address of the v1 implementation contract.
address public v1ImplementationContract;

// The address of the v2 implementation contract.
address public v2ImplementationContract;

constructor(address _v1ImplementationContract, 
address_v2ImplementationContract) {
    v1ImplementationContract = _v1ImplementationContract;
    v2ImplementationContract = _v2ImplementationContract;
}

// Migrates the user's deposit from the v1 implementation contract to 
the v2 implementation contract.

function migrate() public {
// Get the user's deposit from the v1 implementation contract.
uint256 deposit = v1ImplementationContract.balanceOf(msg.sender);

// Transfer the user's deposit to the v2 implementation contract.
v2ImplementationContract.transfer(msg.sender, deposit);
}

Upgrade Proxy to v2:

After a sufficient migration period or when most users have moved their funds, you can proceed with upgrading the proxy contract to point to the v2 implementation.

// Upgrade the proxy to v2 implementation
function upgradeToV2() external onlyAdmin {
    // Update the proxy to point to v2 implementation
    _upgradeTo(v2Implementation);
}
2
Mohit Kapadiya On

Yes, it is possible to keep the crypto deposits of your users in the same place even when upgrading your contract implementation. To do this, you should have your users send crypto to the proxy contract.

The UUPS proxy module works by intercepting all calls to the proxy contract and redirecting them to the current implementation contract. This means that any crypto deposits sent to the proxy contract will be automatically stored in the implementation contract.

When you upgrade your contract implementation, you will simply need to deploy a new implementation contract and update the proxy contract to point to the new implementation contract. The proxy contract will then continue to intercept all calls and redirect them to the new implementation contract.

Here is a step-by-step guide on how to implement this:

  1. Deploy your initial implementation contract.
  2. Deploy a UUPS proxy contract and initialize it to point to your initial implementation contract.
  3. Have your users send crypto to the proxy contract address.
  4. When you need to upgrade your contract implementation, deploy a new implementation contract.
  5. Update the proxy contract to point to the new implementation contract.
  6. This approach will ensure that the crypto deposits of your users are always stored in the current implementation contract, even when you upgrade the implementation contract.

Here are some additional things to keep in mind:

Make sure that your proxy contract is properly implemented and that it is secure. Test your contract upgrade process thoroughly before deploying it to production.