Skip to content

Commit

Permalink
Write abi, opcodes, gas estimates to Solidity project output dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
allisterb committed Dec 3, 2024
1 parent 9b73203 commit 2ca7093
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 137 deletions.
52 changes: 38 additions & 14 deletions src/Stratis.VS.SolidityProjectBuildTasks/CompileContracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;

using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand All @@ -23,20 +23,20 @@ public class CompileContracts : Task
public ITaskItem[] Contracts { get; set; }

[Required]
public string OutputDir { get; set; }
public string OutputPath { get; set; }


public Dictionary<string, Source> Sources => Contracts.ToDictionary(k => k.GetMetadata("Filename"), v => new Source() { Urls = new[] { Path.Combine(v.GetMetadata("RelativeDir"), v.ItemSpec) } });

public override bool Execute()
{

var solcpath = Directory.Exists(Path.Combine(ProjectDir, "node_modules", "solc", "solc.js")) ? Path.Combine(ProjectDir, "node_modules", "solc", "solc.js") :
var solcpath = File.Exists(Path.Combine(ProjectDir, "node_modules", "solc", "solc.js")) ? Path.Combine(ProjectDir, "node_modules", "solc", "solc.js") :
Path.Combine(ExtDir, "node_modules", "solc", "solc.js");
var cmdline = "node \"" + solcpath + "\" --standard-json --base-path=\"" + ProjectDir + "\"";
var cmdline = "node \"" + solcpath + "\" --standard-json --base-path=\"" + ProjectDir + "\"" + " --include-path=\"" + Path.Combine(ProjectDir, "node_modules") + "\"";
var sources = Contracts.ToDictionary(k => k.GetMetadata("Filename"), v => new Source() { Urls = new[] { Path.Combine(v.GetMetadata("RelativeDir"), v.ItemSpec) } });

if (!(Directory.Exists(Path.Combine(ExtDir, "node_modules")) && File.Exists(Path.Combine(ExtDir, "node_modules", "solidity", "dist", "cli", "server.js"))))
if (!File.Exists(Path.Combine(ProjectDir, "node_modules", "solc", "solc.js")) && !(Directory.Exists(Path.Combine(ExtDir, "node_modules")) && File.Exists(Path.Combine(ExtDir, "node_modules", "solidity", "dist", "cli", "server.js"))))
{
if (!InstallSolidityLanguageServer())
{
Expand Down Expand Up @@ -71,7 +71,7 @@ public override bool Execute()
{
{"*", new Dictionary<string, string[]>()
{
{"*", new [] {"evm.bytecode" } }
{"*", new [] {"abi", "evm.bytecode", "evm.gasEstimates" } }
}
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public override bool Execute()
}
var on = o.Split('\n');
var jo = on.First().TrimStart().StartsWith(">") ? on.Skip(1).Aggregate((s, n) => s + "\n" + n) : o;
Log.LogMessage(MessageImportance.High, jo);
Log.LogMessage(MessageImportance.Low, jo);
SolidityCompilerOutput output;
try
{
Expand Down Expand Up @@ -155,24 +155,40 @@ public override bool Execute()
}
}
}

}

if (haserror)
{
return false;
}

if (Directory.Exists(OutputDir))
var outputdir = Path.Combine(ProjectDir, OutputPath);
if (!Directory.Exists(outputdir))
{
Directory.CreateDirectory(OutputDir);
Directory.CreateDirectory(outputdir);
}

foreach(var c in output.contracts)
{
if (c.Value.Values.First().evm.bytecode._object != null)
var cs = c.Value.Values.First();
if (Sources.ContainsKey(c.Key))
{
File.WriteAllBytes(Path.Combine(OutputDir, c.Key + ".bin"), FromHexString(c.Value.Values.First().evm.bytecode._object));
if (cs.evm.bytecode._object != null)
{
File.WriteAllBytes(Path.Combine(outputdir, c.Key + ".bin"), FromHexString(c.Value.Values.First().evm.bytecode._object));
}
if (!string.IsNullOrEmpty(cs.evm.bytecode.opcodes))
{
File.WriteAllText(Path.Combine(outputdir, c.Key + ".opcodes.txt"), cs.evm.bytecode.opcodes);
}
if (cs.evm.gasEstimates != null)
{
File.WriteAllText(Path.Combine(outputdir, c.Key + ".gas.txt"), Deserialize(cs.evm.gasEstimates));
}
if (cs.abi != null)
{
File.WriteAllText(Path.Combine(outputdir, c.Key + ".abi"), Deserialize(cs.abi));
}
}
}
return true;
Expand Down Expand Up @@ -240,7 +256,7 @@ public static Dictionary<string, object> RunCmd(string filename, string argument
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = filename;
info.Arguments = arguments;
info.WorkingDirectory = "\"" + workingdirectory + "\"";
info.WorkingDirectory = workingdirectory;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
Expand Down Expand Up @@ -322,9 +338,10 @@ public bool CheckRunCmdOutput(Dictionary<string, object> output, string checktex
}
}

private static int PerformModularArithmeticCalculation(char value) => (value % 32 + 9) % 25;
public static byte[] FromHexString(string input)
{
int PerformModularArithmeticCalculation(char value) => (value % 32 + 9) % 25;

if (input.Length % 2 != 0)
throw new ArgumentException("Input has invalid length", nameof(input));

Expand All @@ -341,5 +358,12 @@ public static byte[] FromHexString(string input)

return dest;
}

public string Deserialize<T>(T obj)
{
var sb = new StringBuilder();
CompactJson.Serializer.Write(obj, new StringWriter(sb), true);
return sb.ToString();
}
}
}
41 changes: 39 additions & 2 deletions src/Stratis.VS.SolidityProjectBuildTasks/SolidityCompilerIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,21 @@ public class Contract

public class ContractStorage
{
public Abi[] abi { get; set; }
public Evm evm { get; set; }
}

public class Evm
{
public Bytecode bytecode { get; set; }
public GasEstimates gasEstimates { get; set; }
}

public class Bytecode
{
public Functiondebugdata functionDebugData { get; set; }
public object[] generatedSources { get; set; }
public Linkreferences linkReferences { get; set; }

[JsonProperty("object")]
public string _object { get; set; }
public string opcodes { get; set; }
Expand Down Expand Up @@ -134,4 +135,40 @@ public class SourceLocation
public int start { get; set; }
}

}
}

public class GasEstimates
{
public Creation creation { get; set; }
public External external { get; set; }
}

public class Creation
{
public string codeDepositCost { get; set; }
public string executionCost { get; set; }
public string totalCost { get; set; }
}

public class External
{
public string basicFunctionuint256 { get; set; }
}

public class Abi
{
public bool anonymous { get; set; }
public Input[] inputs { get; set; }
public string name { get; set; }
public string type { get; set; }
public object[] outputs { get; set; }
public string stateMutability { get; set; }
}

public class Input
{
public bool indexed { get; set; }
public string internalType { get; set; }
public string name { get; set; }
public string type { get; set; }
}
2 changes: 1 addition & 1 deletion src/Stratis.VS.SolidityProjectBuildTasks/Test.testproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<Compile Include="SmartContract1.sol" />
</ItemGroup>
<Target Name="Build">
<CompileContracts Contracts="@(Compile)" ExtDir="C:\Users\Allister\AppData\Local\Microsoft\VisualStudio\17.0_13a12150Exp\Extensions\Stratis DevEx\StratisEVM\0.1.4" ProjectDir="$(MSBuildProjectDirectory)" OutputDir="$(MSBuildProjectDirectory)\bin\Debug"/>
<CompileContracts Contracts="@(Compile)" ExtDir="C:\Users\Allister\AppData\Local\Microsoft\VisualStudio\17.0_c69ae1a2Exp\Extensions\Stratis DevEx\StratisEVM\0.1.4" ProjectDir="$(MSBuildProjectDirectory)" OutputPath="bin\solidity"/>
</Target>
</Project>
1 change: 1 addition & 0 deletions src/Stratis.VS.StratisEVM/BuildSystem/Solidity.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputPath>bin\solidity</OutputPath>

<DebuggerFlavor>ScriptDebugger</DebuggerFlavor>
<RunCommand>$(WinDir)\System32\cscript.exe</RunCommand>
Expand Down
2 changes: 1 addition & 1 deletion src/Stratis.VS.StratisEVM/BuildSystem/Solidity.targets
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

<UsingTask TaskName="CompileContracts" AssemblyFile="$(LocalAppData)\CustomProjectSystems\Solidity\Tools\Stratis.VS.SolidityProjectBuildTasks.dll"/>
<Target Name="Build">
<CompileContracts ExtDir="$(ExtDir)" ProjectDir="$(MSBuildProjectDirectory)" OutputDir="$(OutputPath)" Contracts="@(Contract)" />
<CompileContracts ExtDir="$(ExtDir)" ProjectDir="$(MSBuildProjectDirectory)" OutputPath="$(OutputPath)" Contracts="@(Contract)" />
</Target>

<Target Name="Rebuild">
Expand Down
117 changes: 0 additions & 117 deletions src/Stratis.VS.StratisEVM/SolidityCompilerIO.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/Stratis.VS.StratisEVM/Stratis.VS.StratisEVM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<Compile Include="SolidityProjectProperties.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SolidityCompiler.cs" />
<Compile Include="SolidityCompilerIO.cs" />
<Compile Include="SolidityContentTypeDefinition.cs" />
<Compile Include="SolidityFileContextActionsProviderFactory.cs" />
<Compile Include="SolidityFileContextProviderFactory.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"MSBuild": {
"commandName": "Executable",
"executablePath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe",
"executablePath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\MSBuild\\Current\\Bin\\MSBuild.exe",
"commandLineArgs": "C:\\Projects\\Stratis.DevEx\\src\\Stratis.VS.SolidityProjectBuildTasks\\Test.testproj"
}
}
Expand Down

0 comments on commit 2ca7093

Please sign in to comment.