From d7a3c7cdca9f40afbff81963f2579e8fb0302338 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 19 Jul 2022 13:36:15 +0800 Subject: [PATCH] @synthetixio/contracts --- contracts/.eslintrc | 5 ++ contracts/codegen.js | 80 +++++++++++++++++++++++ contracts/index.js | 1 + contracts/package.json | 22 +++++++ package.json | 1 + packages/contracts-interface/codegen.js | 22 ------- packages/contracts-interface/package.json | 3 +- packages/contracts-interface/src/index.ts | 4 +- packages/contracts-interface/src/types.ts | 4 +- yarn.lock | 22 ++++++- 10 files changed, 136 insertions(+), 28 deletions(-) create mode 100644 contracts/.eslintrc create mode 100644 contracts/codegen.js create mode 100644 contracts/index.js create mode 100755 contracts/package.json delete mode 100644 packages/contracts-interface/codegen.js diff --git a/contracts/.eslintrc b/contracts/.eslintrc new file mode 100644 index 000000000..4c0e41d54 --- /dev/null +++ b/contracts/.eslintrc @@ -0,0 +1,5 @@ +{ + "rules": { + "@typescript-eslint/no-var-requires": "off" + } +} diff --git a/contracts/codegen.js b/contracts/codegen.js new file mode 100644 index 000000000..3f52e5756 --- /dev/null +++ b/contracts/codegen.js @@ -0,0 +1,80 @@ +const path = require('path'); +const fs = require('fs'); +const ethers = require('ethers'); +const prettier = require('prettier'); + +const synthetixPath = path.dirname(require.resolve('synthetix')); +const deployed = path.join(synthetixPath, 'publish/deployed'); +const networks = fs + .readdirSync(deployed, { withFileTypes: true }) + .filter((dirent) => dirent.isDirectory()) + .map((dirent) => dirent.name); + +function generateTargets(network) { + const deployment = require(`synthetix/publish/deployed/${network}/deployment.json`); + fs.mkdirSync(`build/${network}/deployment/targets`, { recursive: true }); + if (!deployment.targets) { + return; + } + + Object.entries(deployment.targets).forEach(([key, val]) => { + fs.writeFileSync( + `build/${network}/deployment/targets/${key}.ts`, + prettier.format( + Object.entries(val) + .filter(([name]) => ['name', 'source', 'address'].includes(name)) + .map(([name, value]) => `export const ${name} = ${JSON.stringify(value, null, 2)};`) + .join('\n'), + { parser: 'typescript' } + ), + 'utf8' + ); + }); +} + +function generateSources(network) { + const deployment = require(`synthetix/publish/deployed/${network}/deployment.json`); + fs.mkdirSync(`build/${network}/deployment/sources`, { recursive: true }); + if (!deployment.sources) { + return; + } + + Object.entries(deployment.sources).forEach(([key, val]) => { + const iface = new ethers.utils.Interface(val.abi); + val.abi = iface.format(ethers.utils.FormatTypes.full); + fs.writeFileSync( + `build/${network}/deployment/sources/${key}.ts`, + prettier.format( + Object.entries(val) + .filter(([name]) => ['abi'].includes(name)) + .map(([name, value]) => `export const ${name} = ${JSON.stringify(value, null, 2)};`) + .join('\n'), + { parser: 'typescript' } + ), + 'utf8' + ); + }); +} + +function generateSynths(network) { + const synths = require(`synthetix/publish/deployed/${network}/synths.json`); + fs.mkdirSync(`build/${network}`, { recursive: true }); + fs.writeFileSync( + `build/${network}/synths.ts`, + prettier.format( + [ + 'export enum Synths {', + ...synths.map(({ name }) => ` ${name} = ${JSON.stringify(name, null, 2)},`), + '}', + ].join('\n'), + { parser: 'typescript' } + ), + 'utf8' + ); +} + +networks.forEach((network) => { + generateTargets(network); + generateSources(network); + generateSynths(network); +}); diff --git a/contracts/index.js b/contracts/index.js new file mode 100644 index 000000000..b1a43cab6 --- /dev/null +++ b/contracts/index.js @@ -0,0 +1 @@ +throw new Error('Import each contract directly from @synthetixio/contracts/build'); diff --git a/contracts/package.json b/contracts/package.json new file mode 100755 index 000000000..6162698d9 --- /dev/null +++ b/contracts/package.json @@ -0,0 +1,22 @@ +{ + "name": "@synthetixio/contracts", + "publishConfig": { + "access": "public" + }, + "main": "index.js", + "scripts": { + "build": "node codegen.js" + }, + "files": [ + "build" + ], + "version": "1.0.0", + "author": "Nikita ", + "repository": "github:Synthetixio/js-monorepo", + "license": "MIT", + "devDependencies": { + "ethers": "^5.6.8", + "prettier": "^2.6.2", + "synthetix": "^2.74.1" + } +} diff --git a/package.json b/package.json index a22ef93f7..3934bbe43 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "root", "version": "2.74.1", "workspaces": [ + "contracts", "packages/contracts-interface", "packages/optimism-networks", "packages/transaction-notifier", diff --git a/packages/contracts-interface/codegen.js b/packages/contracts-interface/codegen.js deleted file mode 100644 index c867c2f07..000000000 --- a/packages/contracts-interface/codegen.js +++ /dev/null @@ -1,22 +0,0 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ -const fs = require('fs'); - -try { - fs.mkdirSync(__dirname + '/generated/'); -} catch {} - -for (const network of ['mainnet', 'mainnet-ovm', 'kovan', 'kovan-ovm']) { - const synths = require(`synthetix/publish/deployed/${network}/synths.json`); - - const genTs = ['export enum Synths {']; - - for (const synth of synths) { - if (!synth.name) continue; - - genTs.push(` ${synth.name} = '${synth.name}',`); - } - - genTs.push('}'); - - fs.writeFileSync(__dirname + `/generated/${network}.ts`, genTs.join('\n')); -} diff --git a/packages/contracts-interface/package.json b/packages/contracts-interface/package.json index 0865701e9..4b206e627 100644 --- a/packages/contracts-interface/package.json +++ b/packages/contracts-interface/package.json @@ -15,7 +15,7 @@ "src" ], "scripts": { - "build": "node ./codegen.js && yarn build-browser && yarn build-node", + "build": "yarn build-browser && yarn build-node", "build-node": "tsc -p tsconfig.node.json", "build-browser": "webpack-cli --mode=production --max-old-space-size=4096", "examples:node": "ts-node --project tsconfig.node.json ./examples/signer-example.js", @@ -36,6 +36,7 @@ "url": "https://github.com/Synthetixio/js-monorepo/issues" }, "dependencies": { + "@synthetixio/contracts": "workspace:*", "ethers": "^5.5.3", "synthetix": "2.74.1" }, diff --git a/packages/contracts-interface/src/index.ts b/packages/contracts-interface/src/index.ts index bcc066e1b..e3d7e489e 100644 --- a/packages/contracts-interface/src/index.ts +++ b/packages/contracts-interface/src/index.ts @@ -34,8 +34,8 @@ import { NetworkNameById, } from './types'; -import { Synths as MainnetSynths } from '../generated/mainnet'; -import { Synths as OptimismSynths } from '../generated/mainnet-ovm'; +import { Synths as MainnetSynths } from '@synthetixio/contracts/build/mainnet/synths'; +import { Synths as OptimismSynths } from '@synthetixio/contracts/build/mainnet-ovm/synths'; import { ERRORS } from './constants'; diff --git a/packages/contracts-interface/src/types.ts b/packages/contracts-interface/src/types.ts index 375493f93..eba49dd63 100644 --- a/packages/contracts-interface/src/types.ts +++ b/packages/contracts-interface/src/types.ts @@ -11,8 +11,8 @@ import { networkToChainId, } from 'synthetix'; -import { Synths } from '../generated/mainnet'; -import { Synths as OptimismSynths } from '../generated/mainnet-ovm'; +import { Synths } from '@synthetixio/contracts/build/mainnet/synths'; +import { Synths as OptimismSynths } from '@synthetixio/contracts/build/mainnet-ovm/synths'; export const NetworkIdByName = { mainnet: 1, diff --git a/yarn.lock b/yarn.lock index 985c5fc33..501c943ef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2262,6 +2262,7 @@ __metadata: "@babel/plugin-transform-runtime": ^7.13.10 "@babel/preset-env": ^7.13.10 "@babel/preset-typescript": ^7.13.0 + "@synthetixio/contracts": "workspace:*" "@types/node": ^17.0.43 chokidar-cli: ^2.1.0 eslint: ^7.26.0 @@ -2276,6 +2277,16 @@ __metadata: languageName: unknown linkType: soft +"@synthetixio/contracts@workspace:*, @synthetixio/contracts@workspace:contracts": + version: 0.0.0-use.local + resolution: "@synthetixio/contracts@workspace:contracts" + dependencies: + ethers: ^5.6.8 + prettier: ^2.6.2 + synthetix: ^2.74.1 + languageName: unknown + linkType: soft + "@synthetixio/optimism-networks@workspace:*, @synthetixio/optimism-networks@workspace:packages/optimism-networks": version: 0.0.0-use.local resolution: "@synthetixio/optimism-networks@workspace:packages/optimism-networks" @@ -9543,6 +9554,15 @@ __metadata: languageName: node linkType: hard +"prettier@npm:^2.6.2": + version: 2.7.1 + resolution: "prettier@npm:2.7.1" + bin: + prettier: bin-prettier.js + checksum: 55a4409182260866ab31284d929b3cb961e5fdb91fe0d2e099dac92eaecec890f36e524b4c19e6ceae839c99c6d7195817579cdffc8e2c80da0cb794463a748b + languageName: node + linkType: hard + "pretty-error@npm:^2.1.1": version: 2.1.2 resolution: "pretty-error@npm:2.1.2" @@ -11057,7 +11077,7 @@ __metadata: languageName: node linkType: hard -"synthetix@npm:2.74.1": +"synthetix@npm:2.74.1, synthetix@npm:^2.74.1": version: 2.74.1 resolution: "synthetix@npm:2.74.1" dependencies: