Skip to content

Instantly share code, notes, and snippets.

@larrythecucumber321
Created September 8, 2023 04:13
Show Gist options
  • Save larrythecucumber321/02ce24082c1d87695142ed544531f665 to your computer and use it in GitHub Desktop.
Save larrythecucumber321/02ce24082c1d87695142ed544531f665 to your computer and use it in GitHub Desktop.
RToken upgrade instructions
import hre, { ethers } from 'hardhat'
import { getDeploymentFile, getDeploymentFilename, IDeployments } from './deployment/common'
import { getChainId } from '../common/blockchain-utils'
async function main() {
const rTokenAddresses = [
'0xA0d69E286B938e21CBf7E51D71F6A4c8918f482F',
'0xE72B141DF173b999AE7c1aDcbF60Cc9833Ce56a8',
'0xaCdf0DBA4B9839b96221a8487e9ca660a48212be',
]
for (const rTokenAddress of rTokenAddresses) {
const rtoken = await ethers.getContractAt('IRToken', rTokenAddress)
// Get deployed collateral
const chainId = await getChainId(hre)
const componentDeploymentFilename = getDeploymentFilename(chainId, 'mainnet-2.1.0')
const componentDeployments = <IDeployments>getDeploymentFile(componentDeploymentFilename)
const {
implementations: { components },
} = componentDeployments
const mainAddr = await rtoken.main()
const main = await ethers.getContractAt('IMain', mainAddr)
// Retrieve name and symbol properties
const name = await rtoken.name()
const symbol = await rtoken.symbol()
// Print a heading with the retrieved name and symbol
console.log(`\n## ${name} - ${symbol}\n`)
const header = ['Description', 'Target Contract', 'Method', 'Data']
const body: string[][] = []
for (const [componentContractName, newAddress] of Object.entries(components)) {
// @ts-expect-error dynamic function accesor
const targetContractAddress = await main[componentContractName]()
body.push([
`Upgrade ${componentContractName}`,
getEtherscanMd(targetContractAddress),
'upgradeTo',
getEtherscanMd(newAddress),
])
}
// Print the table for each RToken
printTable(header, body)
}
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})
// Helper function for printing MD table
// This could've come from a library, but I didn't want to add a dependency
const printTable = (headers: string[], body: string[][]) => {
const fmtStrLen = (str: string, len: number) => {
const pre = ' '.repeat(Math.floor((len - str.length) / 2))
const trail = ' '.repeat(Math.ceil((len - str.length) / 2))
return pre.concat(str).concat(trail)
}
const getFieldLength = (fieldOrder: number) => {
let len = headers[fieldOrder].length
body?.forEach((row) => {
const rowLength = row[fieldOrder]?.length ?? 0
len = len > rowLength ? len : rowLength
})
return Math.ceil((len + 2) / 2) * 2
}
const fieldsLengths: number[] = body[0].map((_, idx) => getFieldLength(idx))
const separator = { horizontal: '-', vertical: '|' }
headers.forEach((header, idx) =>
process.stdout.write(separator.vertical + fmtStrLen(header, fieldsLengths[idx]))
)
console.log(separator.vertical)
headers.forEach((_header, _idx) => {
process.stdout.write(separator.vertical + separator.horizontal.repeat(fieldsLengths[_idx]))
})
console.log(separator.vertical)
body.forEach((row) => {
row.forEach((field, idx) =>
process.stdout.write(separator.vertical + fmtStrLen(field ?? '', fieldsLengths[idx]))
)
console.log(separator.vertical)
})
}
const getEtherscanMd = (address: string, name?: string) =>
`[${name || address}](https://etherscan.io/address/${address})`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment