Skip to content

Commit

Permalink
support linking sub libraries in e2e test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed Jun 23, 2022
1 parent ce2240e commit d015305
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 85 deletions.
103 changes: 41 additions & 62 deletions test/cases/wand.only/0.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
"frame": { "title": "FLOURISHING MISTY WORLD", "level3": true },
"stars": { "starsSeed": 132413 },
"planets": [
{ "x": -335, "y": 0, "visible": true },
{ "x": -377, "y": -50, "visible": true },
{ "x": -564, "y": -330, "visible": false },
{ "x": -614, "y": -517, "visible": false },
{ "x": -208, "y": -1465, "visible": false },
{ "x": -428, "y": -123, "visible": true },
{ "x": -629, "y": -712, "visible": false },
{ "x": 1025, "y": -1320, "visible": false }
{ "x": 10, "y": 154, "visible": true },
{ "x": -39, "y": 157, "visible": true },
{ "x": -247, "y": 163, "visible": true },
{ "x": -341, "y": 148, "visible": true },
{ "x": -694, "y": 20, "visible": false },
{ "x": -105, "y": 156, "visible": true },
{ "x": -421, "y": 131, "visible": true },
{ "x": -1060, "y": -286, "visible": false }
],
"aspects": [
{ "x1": 223, "y1": -133, "x2": -218, "y2": 142 },
{ "x1": 260, "y1": 0, "x2": 120, "y2": 231 },
{ "x1": 258, "y1": -34, "x2": -106, "y2": 238 },
{ "x1": 224, "y1": -131, "x2": 234, "y2": 113 },
{ "x1": 172, "y1": -195, "x2": 257, "y2": 40 }
{ "x1": 178, "y1": -189, "x2": 249, "y2": 74 },
{ "x1": 202, "y1": 163, "x2": -57, "y2": 254 },
{ "x1": 202, "y1": 163, "x2": -166, "y2": 200 },
{ "x1": 17, "y1": -259, "x2": 240, "y2": -101 },
{ "x1": 63, "y1": 252, "x2": -247, "y2": -83 },
{ "x1": 217, "y1": 143, "x2": 0, "y2": 260 },
{ "x1": 248, "y1": 77, "x2": 68, "y2": 251 },
{ "x1": 0, "y1": 0, "x2": 0, "y2": 0 }
],
"background": {
"hue": 0,
Expand Down Expand Up @@ -59,12 +62,12 @@
"layers": [
{
"turbType": "turbulence",
"turbFreqX": 0.001,
"turbFreqY": 0.001,
"turbFreqX": 1,
"turbFreqY": 1,
"turbOct": 1,
"turbBlur": 2,
"dispScale": 200,
"specConstant": 0.75,
"specConstant": 75,
"specExponent": 30,
"surfaceScale": 5,
"pointX": -50,
Expand All @@ -78,57 +81,33 @@
]
},
"stone": {
"turbType": "fractalNoise",
"turbFreqX": 0.004,
"turbFreqY": 0.007,
"fractalNoise": true,
"turbFreqX": 4,
"turbFreqY": 7,
"turbOct": 2,
"redAmp": 0.69,
"redExp": -0.43,
"redOff": 0.16,
"greenAmp": 0.61,
"greenExp": -0.66,
"greenOff": -0.63,
"blueAmp": 0.58,
"blueExp": 0.01,
"blueOff": -0.15,
"rotation": 26,
"redAmp": 69,
"redExp": -43,
"redOff": 16,
"greenAmp": 61,
"greenExp": -66,
"greenOff": -63,
"blueAmp": 58,
"blueExp": 1,
"blueOff": -15,
"rotation": 306,
"seed": 1004123123,
"seasonsAmplitude": 76,
"secondInDay": 59604.55,
"secondInYear": 13969053
"secondInDay": 41269,
"secondInYear": 14037118
},
"sparkle": {
"sparkles": [
{
"ty": 391.6136462835958,
"tx": 187.4746810737406,
"scale": 0.8962530326055658
},
{
"ty": 589.0896098447873,
"tx": 1633.1620912485369,
"scale": 0.409025855746877
},
{
"ty": 382.8053562020036,
"tx": 425.8586546432447,
"scale": 0.5538792607669576
},
{
"ty": 1435.297180663201,
"tx": 273.8071962589959,
"scale": 0.9541531025098982
},
{
"ty": 380.6652321184142,
"tx": 1230.476996666956,
"scale": 0.3493180213573832
},
{
"ty": 689.6458358601778,
"tx": 1723.309567315027,
"scale": 0.4153771077466417
}
{ "scale": 1, "tx": 1352, "ty": 349 },
{ "scale": 0.35, "tx": 249, "ty": 1000 },
{ "scale": 0.55, "tx": 1169, "ty": 1155 },
{ "scale": 0.75, "tx": 1448, "ty": 1355 },
{ "scale": 0.55, "tx": 1148, "ty": 1410 }
]
},
"xp": { "cap": 10000, "amount": 3221, "crown": false }
"xp": { "cap": 10000, "amount": 2705, "crown": false }
}
88 changes: 65 additions & 23 deletions test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import path, { dirname } from "path"
import { expect } from "chai"
import { cosmiconfigSync } from "cosmiconfig"
import { Contract } from "ethers"
import { Bytes, keccak256, toUtf8Bytes } from "ethers/lib/utils"
import { ethers } from "hardhat"
import solc from "solc"
import linker from "solc/linker"

import "@nomiclabs/hardhat-ethers"

import prettierConfig from "../.prettierrc.json"
import { compile } from "../src/compile"
import { createOptimizingParse } from "../src/optimizingParse"

// eslint-disable-next-line @typescript-eslint/no-var-requires
const solc = require("solc")

function solCompile(contractSource: string) {
return JSON.parse(
solc.compile(
Expand Down Expand Up @@ -108,47 +108,89 @@ describe.only("end-to-end test suite", () => {
let contract: Contract

before(async () => {
const contractSource = compile(template, {
let contractSource = compile(template, {
contract: true,
partials,
parse: createOptimizingParse(configFile?.config),
...prettierConfig,
})

writeFileSync(
path.join(__dirname, "cases", name, "Template.sol"),
contractSource
)

// contractSource = readFileSync(
// writeFileSync(
// path.join(__dirname, "cases", name, "Template.sol"),
// { encoding: "utf-8" }
// contractSource
// )

contractSource = readFileSync(
path.join(__dirname, "cases", name, "Template.sol"),
{ encoding: "utf-8" }
)
const solcOutput = solCompile(contractSource)

if (!solcOutput.contracts || solcOutput.errors) {
console.error("Solc failed")
console.error(solcOutput)
return
}
console.log("Successfully compiled Template.sol to bytecode")

const { abi } = solcOutput.contracts["Template.sol"].Template
const bytecode =
solcOutput.contracts["Template.sol"].Template.evm.bytecode.object

const size = computeBytecodeSizeInKiB(bytecode)
const [signer] = await ethers.getSigners()
const MAX_CONTRACT_SIZE = 24

const { Template, ...compiledPartials } =
solcOutput.contracts["Template.sol"]
// console.log(Template)
const { abi } = Template
let templateBytecode = Template.evm.bytecode.object

const partialEntries = Object.entries(compiledPartials)
const links = {} as Record<string, string>
for (let i = 0; i < partialEntries.length; i++) {
const [name, Partial] = partialEntries[i] as [string, any]
const { abi, evm } = Partial
const factory = new ethers.ContractFactory(
abi,
evm.bytecode.object,
signer
)

const size = computeBytecodeSizeInKiB(evm.bytecode.object)
console.log(
`${name} bytecode size: ${Math.round(size)} KiB, ${Math.round(
(100 * size) / MAX_CONTRACT_SIZE
)}% of limit`
)

const gas = await ethers.provider.estimateGas(
factory.getDeployTransaction()
)
contract = await factory.deploy()
console.log(
`Successfully deployed ${name} to ${contract.address} (gas: ${gas})`
)
const key = keccak256(toUtf8Bytes(`Template.sol:${name}`)).substring(
2,
36
)
links[`$${key}$`] = contract.address
}

if (partialEntries.length > 0) {
templateBytecode = linker.linkBytecode(templateBytecode, links)
console.log(`Successfully linked ${partialEntries.length} libraries`)
}

const size = computeBytecodeSizeInKiB(templateBytecode)
console.log(
`Successfully compiled Template.sol to bytecode (size: ${Math.round(
size
)} KiB, ${Math.round((100 * size) / MAX_CONTRACT_SIZE)}% of limit)`
`Template bytecode size: ${Math.round(size)} KiB, ${Math.round(
(100 * size) / MAX_CONTRACT_SIZE
)}% of limit`
)

const factory = new ethers.ContractFactory(
abi,
templateBytecode,
signer
)

const [signer] = await ethers.getSigners()
const factory = new ethers.ContractFactory(abi, bytecode, signer)
// const deploymentData = factory.interface.encodeDeploy()
const gas = await ethers.provider.estimateGas(
factory.getDeployTransaction()
)
Expand Down
2 changes: 2 additions & 0 deletions typings/prettier-plugin-solidity/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
declare module "prettier-plugin-solidity"
declare module "solc"
declare module "solc/linker"

0 comments on commit d015305

Please sign in to comment.