-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish contracts to sourcify after migration (#9220)
* 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
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters