-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move solc from optional to dev dependency and update Slueth interface #3
Open
torreyatcitty
wants to merge
1
commit into
main
Choose a base branch
from
torrey/move-solc-as-optional-interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ import { parse } from '../parser/pkg/parser'; | |
|
||
interface Opts { | ||
network?: string, | ||
version?: number | ||
version?: number, | ||
solcCompileFn?: (jsonInput:string) => string | ||
}; | ||
|
||
const defaultOpts = { | ||
|
@@ -64,14 +65,19 @@ interface SolcOutput { | |
errors?: string[], | ||
} | ||
|
||
function solcCompile(input: SolcInput): SolcOutput { | ||
let solc; | ||
try { | ||
solc = require('solc'); | ||
} catch (e) { | ||
throw new Error(`solc.js yarn dependency not found. Please build with optional dependencies included`); | ||
function solcCompile( | ||
input: SolcInput, | ||
compileFn: ((input: string) => string) | undefined | ||
): SolcOutput { | ||
if (compileFn === undefined) { | ||
throw new Error(`This option requires solc.compile() to be set in Opts or passed into as an arg to querySol(). Please ensure you are assigning solc.compile() and try again.`); | ||
} | ||
return JSON.parse(solc.compile(JSON.stringify(input))); | ||
|
||
const inputJson = JSON.stringify(input); | ||
if (inputJson == undefined) { | ||
throw new Error(`Input not able to be compiled: ${input}`); | ||
} | ||
return JSON.parse(compileFn(inputJson)); | ||
} | ||
|
||
function hexify(v: string): string { | ||
|
@@ -85,19 +91,24 @@ export class Sleuth { | |
sleuthAddr: string; | ||
sources: Source[]; | ||
coder: AbiCoder; | ||
solcCompileFn: ((jsonInput: string) => string) | undefined; | ||
|
||
constructor(provider: Provider, opts: Opts = {}) { | ||
this.provider = provider; | ||
this.network = opts.network ?? defaultOpts.network; | ||
this.version = opts.version ?? defaultOpts.version; | ||
this.sleuthAddr = getContractAddress({ from: sleuthDeployer, nonce: this.version - 1 }); | ||
this.sleuthAddr = getContractAddress({ | ||
from: sleuthDeployer, | ||
nonce: this.version - 1, | ||
}); | ||
this.sources = []; | ||
this.coder = new AbiCoder(); | ||
this.solcCompileFn = opts.solcCompileFn; | ||
} | ||
|
||
query<T>(q: string): Query<T, []> { | ||
let registrations = this.sources.map((source) => { | ||
let iface = JSON.stringify(source.iface.format(FormatTypes.full)); | ||
let iface = JSON.stringify(source.iface.format(FormatTypes.full)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this indent be here? |
||
return `REGISTER CONTRACT ${source.name} AT ${source.address} WITH INTERFACE ${iface};` | ||
}).join("\n"); | ||
let fullQuery = `${registrations}${q}`; | ||
|
@@ -120,12 +131,12 @@ export class Sleuth { | |
} | ||
}; | ||
|
||
let result = solcCompile(input); | ||
let result = solcCompile(input, this.solcCompileFn); | ||
console.log(result.contracts['query.yul']); | ||
if (result.errors && result.errors.length > 0) { | ||
throw new Error("Compilation Error: " + JSON.stringify(result.errors)); | ||
} | ||
|
||
let bytecode = result?.contracts['query.yul']?.Query?.evm?.bytecode?.object; | ||
|
||
if (!bytecode) { | ||
|
@@ -144,8 +155,12 @@ export class Sleuth { | |
}; | ||
} | ||
|
||
static querySol<T, A extends any[] = []>(q: string | object, opts: SolidityQueryOpts = {}): Query<T, A> { | ||
if (typeof(q) === 'string') { | ||
static querySol<T, A extends any[] = []>( | ||
q: string | object, | ||
opts: SolidityQueryOpts = {}, | ||
solcCompileFn: ((jsonInput: string) => string) | undefined | ||
): Query<T, A> { | ||
if (typeof q === 'string') { | ||
let r; | ||
try { | ||
// Try to parse as JSON, if that fails, then consider a query | ||
|
@@ -158,16 +173,18 @@ export class Sleuth { | |
return this.querySolOutput(r, opts); | ||
} else { | ||
// This must be a source file, try to compile | ||
return this.querySolSource(q, opts); | ||
return this.querySolSource(q, opts, solcCompileFn); | ||
} | ||
|
||
} else { | ||
// This was passed in as a pre-parsed contract. Or at least, it should have been. | ||
return this.querySolOutput(q as SolcContract, opts); | ||
} | ||
} | ||
|
||
static querySolOutput<T, A extends any[] = []>(c: SolcContract, opts: SolidityQueryOpts = {}): Query<T, A> { | ||
static querySolOutput<T, A extends any[] = []>( | ||
c: SolcContract, | ||
opts: SolidityQueryOpts = {} | ||
): Query<T, A> { | ||
let queryFunctionName = opts.queryFunctionName ?? 'query'; | ||
let b = c.evm?.bytecode?.object ?? c.bytecode?.object; | ||
if (!b) { | ||
|
@@ -185,7 +202,11 @@ export class Sleuth { | |
}; | ||
} | ||
|
||
static querySolSource<T, A extends any[] = []>(q: string, opts: SolidityQueryOpts = {}): Query<T, A> { | ||
static querySolSource<T, A extends any[] = []>( | ||
q: string, | ||
opts: SolidityQueryOpts = {}, | ||
solcCompileFn: ((jsonInput: string) => string) | undefined | ||
): Query<T, A> { | ||
let fnName = opts.queryFunctionName ?? 'query'; | ||
let input = { | ||
language: 'Solidity', | ||
|
@@ -203,7 +224,7 @@ export class Sleuth { | |
} | ||
}; | ||
|
||
let result = solcCompile(input); | ||
let result = solcCompile(input, solcCompileFn); | ||
if (result.errors && result.errors.length > 0) { | ||
throw new Error("Compilation Error: " + JSON.stringify(result.errors)); | ||
} | ||
|
@@ -224,7 +245,7 @@ export class Sleuth { | |
if (Array.isArray(iface)) { | ||
iface = new Interface(iface); | ||
} | ||
this.sources.push({name, address, iface}); | ||
this.sources.push({ name, address, iface }); | ||
} | ||
|
||
async fetch<T, A extends any[] = []>(q: Query<T, A>, args?: A): Promise<T> { | ||
|
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 |
---|---|---|
|
@@ -3,7 +3,10 @@ | |
"version": "1.0.1-alpha4", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": ["dist/**/*", "parser/pkg/**/*"], | ||
"files": [ | ||
"dist/**/*", | ||
"parser/pkg/**/*" | ||
], | ||
"repository": "https://github.com/compound-finance/sleuth", | ||
"author": "Geoffrey Hayes <[email protected]>", | ||
"license": "MIT", | ||
|
@@ -20,9 +23,8 @@ | |
}, | ||
"dependencies": { | ||
"@ethersproject/contracts": "^5.7.0", | ||
"@ethersproject/providers": "^5.7.2" | ||
"@ethersproject/providers": "^5.7.2", | ||
"solc": "^0.8.21" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like it should be even less here? |
||
}, | ||
"optionalDependencies": { | ||
"solc": "^0.8.17" | ||
} | ||
"optionalDependencies": {} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really think this should be async, which would be a way more common way to use it (e.g. via a WebWorker)