Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f745965

Browse files
committedJul 14, 2024·
feat: initial import
0 parents  commit f745965

File tree

15 files changed

+4979
-0
lines changed

15 files changed

+4979
-0
lines changed
 

‎.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: andreas-timm

‎.github/workflows/test.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
env:
9+
BASH_ENV: ~/.profile
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
defaults:
15+
run:
16+
working-directory: ./scripts
17+
steps:
18+
- uses: actions/checkout@v4
19+
- run: curl https://get.volta.sh | bash
20+
- run: npm i ci
21+
- run: npm run test

‎.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.idea/
2+
/node_modules/
3+
/dist/
4+
tmp/

‎LICENSE

Lines changed: 395 additions & 0 deletions
Large diffs are not rendered by default.

‎README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Solidity preprocessor
2+
3+
[![test](https://github.com/andreas-timm/solidity-preprocessor/actions/workflows/test.yml/badge.svg)](https://github.com/andreas-timm/solidity-preprocessor/actions/workflows/test.yml)
4+
5+
## Motivation
6+
7+
8+
## Usage
9+
```solidity
10+
// TestNft1155.sol
11+
// SPDX-License-Identifier: UNLICENSED
12+
13+
pragma solidity ^0.8.25;
14+
15+
import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
16+
17+
contract TestNft1155 is ERC1155 {
18+
constructor(string memory uri_) ERC1155(uri_) {
19+
_mint(address(0), 1, 100, "");
20+
}
21+
}
22+
```
23+
24+
```ts
25+
import { preprocessor } from "@andreas-timm/solidity-preprocessor";
26+
27+
const res = await preprocessor({path: 'contracts/TestNft1155.sol', replace: [
28+
['_mint(address(0), 1, 100, "")', '_mint(0x630C6C3180d3b4B6912644D046f6769dA3e54843, 1, 10000, "")']
29+
]});
30+
31+
console.log(res.bytecode);
32+
```
33+
34+
## License
35+
[![CC BY 4.0][cc-by-shield]][cc-by]
36+
37+
This work is licensed under a [Creative Commons Attribution 4.0 International License][cc-by].
38+
39+
[![CC BY 4.0][cc-by-image]][cc-by]
40+
41+
[cc-by]: http://creativecommons.org/licenses/by/4.0/
42+
[cc-by-image]: https://i.creativecommons.org/l/by/4.0/88x31.png
43+
[cc-by-shield]: https://img.shields.io/badge/License-CC%20BY%204.0-lightgrey.svg
44+
45+
- [LICENSE](https://github.com/andreas-timm//blob/main/LICENSE)
46+
- Author: [Andreas Timm](https://github.com/andreas-timm)

‎contracts/TestNft1155.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
3+
pragma solidity ^0.8.25;
4+
5+
import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
6+
7+
contract TestNft1155 is ERC1155 {
8+
constructor(string memory uri_) ERC1155(uri_) {
9+
_mint(address(0), 1, 100, "");
10+
_mint(address(1), 1, 100, "");
11+
}
12+
}

‎hardhat.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { HardhatUserConfig } from 'hardhat/config'
2+
3+
const hardhatConfig: HardhatUserConfig = {}
4+
5+
// noinspection JSUnusedGlobalSymbols
6+
export default hardhatConfig

‎package-lock.json

Lines changed: 4296 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "@andreas-timm/solidity-preprocessor",
3+
"description": "Solidity preprocessor",
4+
"version": "0.1.5",
5+
"keywords": [
6+
"contract",
7+
"hardhat",
8+
"preprocessing",
9+
"solidity",
10+
"typescript",
11+
"viem"
12+
],
13+
"author": "Andreas Timm <info@andreas-timm.dev>",
14+
"license": "CC-BY-4.0",
15+
"homepage": "https://github.com/andreas-timm/solidity-preprocessor#readme",
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/andreas-timm/solidity-preprocessor.git"
19+
},
20+
"bugs": {
21+
"url": "https://github.com/andreas-timm/solidity-preprocessor/issues"
22+
},
23+
"type": "module",
24+
"devDependencies": {
25+
"@openzeppelin/contracts": "^5.0.2",
26+
"hardhat": "^2.22.6",
27+
"solc": "^0.8.26",
28+
"tsx": "^4.16.2",
29+
"typescript": "^5.5.3",
30+
"viem": "^2.17.3"
31+
},
32+
"scripts": {
33+
"build": "scripts/build.sh",
34+
"publish": "cd dist; npm publish --access public",
35+
"test": "node --import tsx --test tests/*"
36+
},
37+
"volta": {
38+
"node": "20.15.1"
39+
}
40+
}

‎scripts/build.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
rm -rf dist
6+
tsc -p tsconfig.json
7+
tsc -p tsconfig-cjs.json
8+
9+
cp README.md dist/
10+
mkdir dist/src
11+
cp src/preprocessor.ts dist/src/
12+
13+
COPY_FIELDS=(
14+
name
15+
description
16+
version
17+
keywords
18+
author
19+
license
20+
homepage
21+
repository
22+
bugs
23+
publishConfig
24+
)
25+
26+
cd dist
27+
npm init -y > /dev/null
28+
29+
for field in "${COPY_FIELDS[@]}"; do
30+
npm pkg set --json "${field}"="$(npm --prefix=.. pkg get --json "${field}")"
31+
done
32+
33+
npm pkg delete scripts
34+
npm pkg delete directories
35+
npm pkg set main=./lib/cjs/preprocessor.js
36+
npm pkg set module=./lib/esm/preprocessor.js
37+
38+
cat package.json

‎src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'solc'

‎src/preprocessor.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SPDX-License-Identifier: CC-BY-4.0
2+
// This work is licensed under the Creative Commons Attribution 4.0 International (CC BY 4.0) License.
3+
// To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/
4+
// Author: Andreas Timm
5+
// Repository: https://github.com/andreas-timm/solidity-preprocessor
6+
// Version: 0.1.0
7+
// @sha256sum 0x71568cb01d71b42d7e1ec017579f152776ddae32dfe46edcf4168e244e36998b
8+
// @eip191signature 0x7b4005a62ff7c445fafef58ccd0ebb20eca6d4bffe2ec7aa34aa8e285d643eb7682459f0b42d1dd759d6002362b3c2479b3c18917586934a01e74b6811627e981b
9+
import hre from 'hardhat'
10+
import { Abi } from 'viem'
11+
import solc from 'solc'
12+
import { basename } from 'path'
13+
14+
export type PreprocessArgs = {
15+
path: string
16+
callback?: (input: string) => string
17+
replaces?: [string | RegExp, string][]
18+
}
19+
20+
export type PreprocessResult = {
21+
output: Record<string, any>
22+
flattened: string
23+
abi: Abi
24+
bytecode: `0x${string}`
25+
size: number
26+
}
27+
28+
export class PreprocessError extends Error {
29+
errors: any[]
30+
31+
constructor(errors: any[]) {
32+
super(errors.map((i) => i.formattedMessage).join('\n'))
33+
this.name = this.constructor.name
34+
this.errors = errors
35+
}
36+
}
37+
38+
export const preprocess = async (args: PreprocessArgs): Promise<PreprocessResult> => {
39+
let flattened = await hre.run('flatten:get-flattened-sources', { files: [args.path] })
40+
41+
const name = basename(args.path)
42+
const stem = name.split('.')[0]
43+
44+
if (args.replaces) {
45+
flattened = args.replaces.reduce((acc, item) => acc.replace(item[0], item[1]), flattened)
46+
}
47+
48+
if (args.callback) {
49+
flattened = args.callback(flattened)
50+
}
51+
52+
const input = {
53+
language: 'Solidity',
54+
sources: { [name]: { content: flattened } },
55+
settings: {
56+
outputSelection: {
57+
'*': {
58+
'*': ['*'],
59+
},
60+
},
61+
},
62+
}
63+
64+
const output = JSON.parse(solc.compile(JSON.stringify(input)))
65+
66+
if (output.errors) {
67+
throw new PreprocessError(output.errors)
68+
}
69+
70+
return {
71+
output,
72+
flattened,
73+
abi: output.contracts[name][stem].abi as Abi,
74+
bytecode: `0x${output.contracts[name][stem].evm.bytecode.object}`,
75+
size: output.contracts[name][stem].evm.deployedBytecode.object.length / 2,
76+
}
77+
}

‎tests/test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { preprocess } from '../src/preprocessor'
2+
import test from 'node:test'
3+
import assert from 'node:assert'
4+
5+
test('TestNft1155', async () => {
6+
const addr = '0x630C6C3180d3b4B6912644D046f6769dA3e54843'
7+
const replace_1 = `_mint(${addr}, 1, 10000, "")`
8+
const replace_2 = `_mint(${addr}, 2, 11000, "")`
9+
10+
const res: any = await preprocess({
11+
path: 'contracts/TestNft1155.sol',
12+
replaces: [['_mint(address(0), 1, 100, "")', replace_1]],
13+
callback: (input: string) => input.replace('_mint(address(1), 1, 100, "")', replace_2)
14+
})
15+
16+
const assembly = res.output.contracts['TestNft1155.sol'].TestNft1155.evm.assembly
17+
assert.notEqual(assembly.split('\n').findIndex((line: string) => line.includes(replace_1)), -1, 'replace 1')
18+
assert.notEqual(assembly.split('\n').findIndex((line: string) => line.includes(replace_2)), -1, 'replace 2')
19+
})

‎tsconfig-cjs.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "CommonJS",
5+
"outDir": "./dist/lib/cjs"
6+
}
7+
}

‎tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "esnext",
5+
"strict": true,
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"moduleResolution": "node",
9+
"skipLibCheck": true,
10+
"outDir": "./dist/lib/esm",
11+
"declaration": true
12+
},
13+
"include": [
14+
"./src"
15+
]
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.