Skip to content

Commit

Permalink
@synthetixio/contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
noisekit committed Jul 19, 2022
1 parent 0c6050c commit d7a3c7c
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 28 deletions.
5 changes: 5 additions & 0 deletions contracts/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"@typescript-eslint/no-var-requires": "off"
}
}
80 changes: 80 additions & 0 deletions contracts/codegen.js
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions contracts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('Import each contract directly from @synthetixio/contracts/build');
22 changes: 22 additions & 0 deletions contracts/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"repository": "github:Synthetixio/js-monorepo",
"license": "MIT",
"devDependencies": {
"ethers": "^5.6.8",
"prettier": "^2.6.2",
"synthetix": "^2.74.1"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "root",
"version": "2.74.1",
"workspaces": [
"contracts",
"packages/contracts-interface",
"packages/optimism-networks",
"packages/transaction-notifier",
Expand Down
22 changes: 0 additions & 22 deletions packages/contracts-interface/codegen.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/contracts-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -36,6 +36,7 @@
"url": "https://github.com/Synthetixio/js-monorepo/issues"
},
"dependencies": {
"@synthetixio/contracts": "workspace:*",
"ethers": "^5.5.3",
"synthetix": "2.74.1"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts-interface/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 2 additions & 2 deletions packages/contracts-interface/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 21 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d7a3c7c

Please sign in to comment.