From 00843dffa2f725788ac3c280bc1b2e5a5a528e4b Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 6 Jul 2022 11:58:19 +0200 Subject: [PATCH] updagre proxy in blocchain.js --- apps/remix-ide/src/blockchain/blockchain.js | 19 +++++++++++++++++++ .../src/lib/constants/uups.ts | 14 ++++++++++++++ .../src/lib/openzeppelin-proxy.ts | 16 +++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/apps/remix-ide/src/blockchain/blockchain.js b/apps/remix-ide/src/blockchain/blockchain.js index baba1ad2e1..76e2707ca4 100644 --- a/apps/remix-ide/src/blockchain/blockchain.js +++ b/apps/remix-ide/src/blockchain/blockchain.js @@ -179,6 +179,25 @@ export class Blockchain extends Plugin { this.runTx(args, confirmationCb, continueCb, promptCb, finalCb) } + async upgradeProxy(proxyAddress, data, newImplementationContractObject) { + const args = { useCall: false, data, to: proxyAddress } + const confirmationCb = (network, tx, gasEstimation, continueTxExecution, cancelCb) => { + // continue using original authorization given by user + continueTxExecution(null) + } + const continueCb = (error, continueTxExecution, cancelCb) => { continueTxExecution() } + const promptCb = (okCb, cancelCb) => { okCb() } + const finalCb = (error, txResult, address, returnValue) => { + if (error) { + const log = logBuilder(error) + + return this.call('terminal', 'logHtml', log) + } + return this.call('udapp', 'resolveContractAndAddInstance', newImplementationContractObject, address) + } + this.runTx(args, confirmationCb, continueCb, promptCb, finalCb) + } + async getEncodedFunctionHex (args, funABI) { return new Promise((resolve, reject) => { txFormat.encodeFunctionCall(args, funABI, (error, data) => { diff --git a/libs/remix-core-plugin/src/lib/constants/uups.ts b/libs/remix-core-plugin/src/lib/constants/uups.ts index 2fb521a999..977d3769a8 100644 --- a/libs/remix-core-plugin/src/lib/constants/uups.ts +++ b/libs/remix-core-plugin/src/lib/constants/uups.ts @@ -88,4 +88,18 @@ export const UUPSfunAbi = { type: "constructor", outputs: [], stateMutability: "payable" +} + +export const UUPSupgradeAbi = { + "inputs": [ + { + "internalType": "address", + "name": "newImplementation", + "type": "address" + } + ], + "name": "upgradeTo", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" } \ No newline at end of file diff --git a/libs/remix-core-plugin/src/lib/openzeppelin-proxy.ts b/libs/remix-core-plugin/src/lib/openzeppelin-proxy.ts index 2de757f4d2..c909fac990 100644 --- a/libs/remix-core-plugin/src/lib/openzeppelin-proxy.ts +++ b/libs/remix-core-plugin/src/lib/openzeppelin-proxy.ts @@ -1,6 +1,6 @@ import { Plugin } from '@remixproject/engine'; import { ContractABI, ContractAST, DeployOption } from '../types/contract'; -import { UUPS, UUPSABI, UUPSBytecode, UUPSfunAbi } from './constants/uups'; +import { UUPS, UUPSABI, UUPSBytecode, UUPSfunAbi, UUPSupgradeAbi } from './constants/uups'; const proxyProfile = { name: 'openzeppelin-proxy', @@ -74,4 +74,18 @@ export class OpenZeppelinProxy extends Plugin { implementationContractObject.name = proxyName this.blockchain.deployProxy(data, implementationContractObject) } + + async upgradeUUPSProxy (proxyAddress: string, newImplAddress: string, newImplementationContractObject): Promise { + const fnData = await this.blockchain.getEncodedParams([newImplAddress], UUPSupgradeAbi) + const proxyName = 'ERC1967Proxy' + const data = { + contractABI: UUPSABI, + contractName: proxyName, + funAbi: UUPSupgradeAbi, + funArgs: [newImplAddress], + linkReferences: {}, + dataHex: fnData.replace('0x', '') + } + this.blockchain.upgradeProxy(proxyAddress, data, newImplementationContractObject) + } }