Skip to content

Commit

Permalink
Publish contracts to sourcify after migration (#9220)
Browse files Browse the repository at this point in the history
* Publish contracts to sourcify after migration

* Add useLiteralContent flag

* Addressing some comments

* Fix to work with a report.json file getting network and paths as parameter

* Fix comments

* Fix tests and add note
  • Loading branch information
Enrique Ruiz authored and martinvol committed May 13, 2022
1 parent d9c843a commit f7fad50
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"gas": "yarn run test --gas",
"build:sol": "ts-node ./scripts/build.ts --solidity ${BUILD_DIR:-./build}",
"build": "yarn build:sol && yarn build:ts",
"sourcify-publish": "ts-node ./scripts/sourcify-publish.ts",
"migrate": "./scripts/bash/migrate.sh",
"set_block_gas_limit": "./scripts/bash/set_block_gas_limit.sh",
"verify": "./scripts/bash/verify_contracts.sh",
Expand Down
114 changes: 114 additions & 0 deletions packages/protocol/scripts/sourcify-publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/* tslint:disable no-console */
import FormData from 'form-data'
import fs from 'fs'
import fetch from 'node-fetch'
import path from 'path'
import Web3 from 'web3'

/*
* A script that reads the artifacts from the build/contracts directory and publish using the sourcify api.
*
* NOTE: this script hasn't been tested yet. It should be manually tested and
* potentially adjusted before including it in the release process.
*
* Expects the following flags:
* network: The network for which artifacts should be
* build_artifacts_path: Path containing the artifacts to publish
* proposal_path: Path to the proposal file
*
* Run using yarn run sourcify-publish, e.g.:
* yarn run sourcify-publish \
* --network alfajores --build_artifacts_path ./build/contracts --proposal_path ./proposal.json
*
* report.json is generated by the build script with this format:
* [{
* "contract": "<Contract>Proxy",
* "function": "_setImplementation",
* "args": "[<Contract>]",
* },{
* "contract": "Registry",
* "function": "setAddressFor",
* "args": ["<Contract>", "<ContractProxy address>"]
* }]
*
*/

interface BuildOptions {
network: string
buildArtifactsPath: string
proposalPath: string
}

async function main(buildTargets: BuildOptions) {
const artifactBasePath = buildTargets.buildArtifactsPath || './build/contracts'
const artifactPaths = fs.readdirSync(artifactBasePath)
const reportPath = buildTargets.proposalPath || './proposal.json'
const report = require(path.join(process.cwd(), reportPath))
const network = buildTargets.network
const web3 = new Web3('http://localhost:8545')
const chainId = await web3.eth.getChainId()

console.log('Uploading sources & metadata')
console.log('============================')
console.log(artifactPaths)

const artifacts = artifactPaths.map((a) => require(path.join(process.cwd(), artifactBasePath, a)))

if (
!isEqual(
artifacts.map((a) => a.contractName),
report.map((a) => a.contract)
)
) {
if (report.length !== artifactPaths.length) {
throw new Error(
`There are ${report.length} addresses to publish, but there are ${artifactPaths.length} artifacts.`
)
} else {
throw new Error(`Contracts names are not equal`)
}
}

for (const r of report) {
const artifact = artifacts.find((a) => a.contractName === r.contract)

console.log()
console.log(artifact.contractName)
console.log('-'.repeat(artifact.contractName.length))

const address = r.contract.includes('Proxy') ? r.args[0] : r.args[1]
const formData = new FormData()
formData.append(
'files',
fs.createReadStream(artifactBasePath + '/' + artifact.contractName + '.json')
)
formData.append('address', address)
formData.append('chain', chainId)

await fetch('https://sourcify.dev/server', {
method: 'POST',
body: formData,
})
.then((res) => res.json())
.then((json) =>
fetch(
`https://${network}-blockscout.celo-testnet.org/address/${json.result[0].address}/contracts`
)
)
}
console.log('Finished.')
}

const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)

const minimist = require('minimist')
const argv = minimist(process.argv.slice(3), {
string: ['network', 'build_artifacts_path', 'proposal_path'],
})

main(argv)
.then(() => process.exit(0))
.catch((err) => {
console.log(err)
process.exit(1)
})
2 changes: 2 additions & 0 deletions packages/protocol/truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ module.exports = {
version: SOLC_VERSION,
settings: {
evmVersion: 'istanbul',
metadata: { useLiteralContent: true },
},
},
},
Expand All @@ -234,6 +235,7 @@ if (process.argv.includes('--gas')) {
version: SOLC_VERSION,
settings: {
evmVersion: 'istanbul',
metadata: { useLiteralContent: true },
},
},
},
Expand Down

0 comments on commit f7fad50

Please sign in to comment.