Scripts to support upgrades and ownership of smart contracts. The package contains common used functions for writing hardhat scripts for smart contracts deployment and upgrade.
It also includes helper classes to allow a swift full or partial transfer of ownership of a SKALE smart-contract project. More details in ownership-transfer/README.
To write an upgrade script extend Upgrader class.
import { Upgrader } from "@skalenetwork/upgrade-tools";
class ExampleContractUpgrader extends Upgrader {
getDeployedVersion = async () => {
return await (await this.getExampleContract()).version();
};
setVersion = async (newVersion: string) => {
const exampleContract = await this.getExampleContract();
const setVersionTransaction = {
to: exampleContract.address,
data: exampleContract.interface.encodeFunctionData("setVersion", [newVersion])
};
this.transactions.push(setVersionTransaction);
}
async getExampleContract() {
return await ethers.getContractAt("ExampleContract", this.abi["example_contract_address"] as string);
}
}
async function main() {
const abi = JSON.parse(await fs.readFile(process.env.ABI, "utf-8")) as SkaleABIFile;
const upgrader = new ExampleContractUpgrader(
"ExampleContract",
"1.0.0",
abi,
["ExampleContract"]
);
await upgrader.upgrade();
}
if (require.main === module) {
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
}The Upgrader has 2 abstract functions. It's mandatory to override them:
abstract getDeployedVersion: () => Promise<string | undefined>
abstract setVersion: (newVersion: string) => Promise<void>getDeployedVersionreturns string representing a deployed version of the contractsetVersioncreates a transaction to set a new version of the contract
There are functions that may be overridden to customize an upgrade process
deployNewContracts = () => { return Promise.resolve() };
initialize = () => { return Promise.resolve() };deployNewContractsis called before proxies upgrade and is used to deploy new instancesinitializeis called after proxies upgrade and is used to send initializing transactions