Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 41 additions & 20 deletions cli/sleuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { parse } from '../parser/pkg/parser';

interface Opts {
network?: string,
version?: number
version?: number,
solcCompileFn?: (jsonInput:string) => string
};

const defaultOpts = {
Expand Down Expand Up @@ -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
Copy link
Contributor

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)

): 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 {
Expand All @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The 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}`;
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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',
Expand All @@ -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));
}
Expand All @@ -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> {
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -20,9 +23,8 @@
},
"dependencies": {
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.2"
"@ethersproject/providers": "^5.7.2",
"solc": "^0.8.21"
Copy link
Contributor

Choose a reason for hiding this comment

The 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": {}
}
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2379,9 +2379,9 @@ resolve@^1.20.0:
supports-preserve-symlinks-flag "^1.0.0"

semver@^5.5.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
version "5.7.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.2.tgz#48d55db737c3287cd4835e17fa13feace1c41ef8"
integrity sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==

semver@^6.0.0, semver@^6.3.0:
version "6.3.0"
Expand Down Expand Up @@ -2422,10 +2422,10 @@ slash@^3.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==

solc@^0.8.17:
version "0.8.17"
resolved "https://registry.yarnpkg.com/solc/-/solc-0.8.17.tgz#c748fec6a64bf029ec406aa9b37e75938d1115ae"
integrity sha512-Dtidk2XtTTmkB3IKdyeg6wLYopJnBVxdoykN8oP8VY3PQjN16BScYoUJTXFm2OP7P0hXNAqWiJNmmfuELtLf8g==
solc@^0.8.21:
version "0.8.21"
resolved "https://registry.yarnpkg.com/solc/-/solc-0.8.21.tgz#c3cd505c360ea2fa0eaa5ab574ef96bffb1a2766"
integrity sha512-N55ogy2dkTRwiONbj4e6wMZqUNaLZkiRcjGyeafjLYzo/tf/IvhHY5P5wpe+H3Fubh9idu071i8eOGO31s1ylg==
dependencies:
command-exists "^1.2.8"
commander "^8.1.0"
Expand Down