【Solidity】使用编译器

简介: 使用编译器使用命令行编译器Solidity库的构建目标之一是solc,即solidity命令行编译器。 使用solc --help可以为您提供所有选项的解释。 编译器可以生成各种输出,从简单的二进制文件和通过抽象语法树(解析树)的汇编到气体使用的估计。

使用编译器

使用命令行编译器

Solidity库的构建目标之一是solc,即solidity命令行编译器。 使用solc --help可以为您提供所有选项的解释。 编译器可以生成各种输出,从简单的二进制文件和通过抽象语法树(解析树)的汇编到气体使用的估计。 如果你只想编译一个文件,你可以运行它作为solc -bin sourceFile.sol,它将打印二进制文件。 在部署合同之前,请先使用solc --optimize --bin sourceFile.sol进行编译时激活优化器。 如果要获取一些更高级的solc的输出变量,最好使用solc -o outputDirectory --bin --ast --asm sourceFile.sol将其输出到单独的文件中。

命令行编译器将自动从文件系统读取导入的文件,但也可以通过以下方式使用prefix = path提供路径重定向:

solc github.com/ethereum/dapp-bin/=/usr/local/lib/dapp-bin/ =/usr/local/lib/fallback file.sol

这本质上指示编译器搜索任何从/usr/local/lib/dapp-bin下的github.com/ethereum/dapp-bin/开始的任何东西,如果没有找到该文件,它将会看/usr/local/lib/fallback(空前缀总是匹配)。 solc将不会从位于重映射目标之外的文件系统读取文件,而是在显式指定的源文件所在目录之外,因此import "/etc/passwd"; 只有在添加=/作为重映射时才可以工作。

如果重新映射有多个匹配项,则选择具有最长公用前缀的匹配项。

为了安全起见,编译器可以限制可以访问哪些目录。 允许在命令行上指定的源文件的路径(及其子目录)和重新映射定义的路径用于导入语句,但其他所有内容都被拒绝。 额外的路径(及其子目录)可以通--allow-paths /sample/path,/another/sample/path来实现。

如果您的合约使用库,您会注意到字节码中包含__LibraryName ___形式的子字符串。 您可以使用solc作为链接器,这意味着它会在这些点插入库地址:

添加--libraries "Math:0x12345678901234567890 Heap:0xabcdef0123456"命令为每个库提供一个地址或将该字符串存储在一个文件(每行一个库)中,并使用--libraries fileName运行solc。

如果使用选项--link调用solc,所有输入文件将被解释为上面给出的__LibraryName ____格式中的未链接二进制文件(十六进制编码),并且是原位置的(如果输入是从stdin读取的,则写入 到stdout)。 在这种情况下,除了--libraries之外的所有选项都将被忽略(包括-o)。

如果使用--standard-json选项调用solc,则会在标准输入上输入JSON输入(如下所述),并在标准输出上返回一个JSON输出。

编译器输入和输出JSON描述

这些JSON格式由编译器API使用,也可以通过solc获得。 这些都可以更改,一些字段是可选的(如所指出),但其目的仅在于向后兼容的更改。

编译器API期望使用JSON格式的输入,并以JSON格式输出输出编译结果。

输入说明

{
  // 必须: 源代码用的语言, such as "Solidity", "serpent", "lll", "assembly", etc.
  language: "Solidity",
  // 必须
  sources:
  {
    // The keys here are the "global" names of the source files,
    // imports can use other files via remappings (see below).
    "myFile.sol":
    {
      // Optional: keccak256 hash of the source file
      // It is used to verify the retrieved content if imported via URLs.
      "keccak256": "0x123...",
      // Required (unless "content" is used, see below): URL(s) to the source file.
      // URL(s) should be imported in this order and the result checked against the
      // keccak256 hash (if available). If the hash doesn't match or none of the
      // URL(s) result in success, an error should be raised.
      "urls":
      [
        "bzzr://56ab...",
        "ipfs://Qma...",
        "file:///tmp/path/to/file.sol"
      ]
    },
    "mortal":
    {
      // Optional: keccak256 hash of the source file
      "keccak256": "0x234...",
      // Required (unless "urls" is used): literal contents of the source file
      "content": "contract mortal is owned { function kill() { if (msg.sender == owner) selfdestruct(owner); } }"
    }
  },
  // Optional
  settings:
  {
    // Optional: Sorted list of remappings
    remappings: [ ":g/dir" ],
    // Optional: Optimizer settings (enabled defaults to false)
    optimizer: {
      enabled: true,
      runs: 500
    },
    // Metadata settings (optional)
    metadata: {
      // Use only literal content and not URLs (false by default)
      useLiteralContent: true
    },
    // Addresses of the libraries. If not all libraries are given here, it can result in unlinked objects whose output data is different.
    libraries: {
      // The top level key is the the name of the source file where the library is used.
      // If remappings are used, this source file should match the global path after remappings were applied.
      // If this key is an empty string, that refers to a global level.
      "myFile.sol": {
        "MyLib": "0x123123..."
      }
    }
    // The following can be used to select desired outputs.
    // If this field is omitted, then the compiler loads and does type checking, but will not generate any outputs apart from errors.
    // The first level key is the file name and the second is the contract name, where empty contract name refers to the file itself,
    // while the star refers to all of the contracts.
    //
    // The available output types are as follows:
    //   abi - ABI
    //   ast - AST of all source files
    //   legacyAST - legacy AST of all source files
    //   devdoc - Developer documentation (natspec)
    //   userdoc - User documentation (natspec)
    //   metadata - Metadata
    //   ir - New assembly format before desugaring
    //   evm.assembly - New assembly format after desugaring
    //   evm.legacyAssembly - Old-style assembly format in JSON
    //   evm.bytecode.object - Bytecode object
    //   evm.bytecode.opcodes - Opcodes list
    //   evm.bytecode.sourceMap - Source mapping (useful for debugging)
    //   evm.bytecode.linkReferences - Link references (if unlinked object)
    //   evm.deployedBytecode* - Deployed bytecode (has the same options as evm.bytecode)
    //   evm.methodIdentifiers - The list of function hashes
    //   evm.gasEstimates - Function gas estimates
    //   ewasm.wast - eWASM S-expressions format (not supported atm)
    //   ewasm.wasm - eWASM binary format (not supported atm)
    //
    // Note that using a using `evm`, `evm.bytecode`, `ewasm`, etc. will select every
    // target part of that output.
    //
    outputSelection: {
      // Enable the metadata and bytecode outputs of every single contract.
      "*": {
        "*": [ "metadata", "evm.bytecode" ]
      },
      // Enable the abi and opcodes output of MyContract defined in file def.
      "def": {
        "MyContract": [ "abi", "evm.opcodes" ]
      },
      // Enable the source map output of every single contract.
      "*": {
        "*": [ "evm.sourceMap" ]
      },
      // Enable the legacy AST output of every single file.
      "*": {
        "": [ "legacyAST" ]
      }
    }
  }
}

输出描述

{
  // Optional: not present if no errors/warnings were encountered
  errors: [
    {
      // Optional: Location within the source file.
      sourceLocation: {
        file: "sourceFile.sol",
        start: 0,
        end: 100
      ],
      // Mandatory: Error type, such as "TypeError", "InternalCompilerError", "Exception", etc
      type: "TypeError",
      // Mandatory: Component where the error originated, such as "general", "ewasm", etc.
      component: "general",
      // Mandatory ("error" or "warning")
      severity: "error",
      // Mandatory
      message: "Invalid keyword"
      // Optional: the message formatted with source location
      formattedMessage: "sourceFile.sol:100: Invalid keyword"
    }
  ],
  // This contains the file-level outputs. In can be limited/filtered by the outputSelection settings.
  sources: {
    "sourceFile.sol": {
      // Identifier (used in source maps)
      id: 1,
      // The AST object
      ast: {},
      // The legacy AST object
      legacyAST: {}
    }
  },
  // This contains the contract-level outputs. It can be limited/filtered by the outputSelection settings.
  contracts: {
    "sourceFile.sol": {
      // If the language used has no contract names, this field should equal to an empty string.
      "ContractName": {
        // The Ethereum Contract ABI. If empty, it is represented as an empty array.
        // See https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
        abi: [],
        // See the Metadata Output documentation (serialised JSON string)
        metadata: "{...}",
        // User documentation (natspec)
        userdoc: {},
        // Developer documentation (natspec)
        devdoc: {},
        // Intermediate representation (string)
        ir: "",
        // EVM-related outputs
        evm: {
          // Assembly (string)
          assembly: "",
          // Old-style assembly (object)
          legacyAssembly: {},
          // Bytecode and related details.
          bytecode: {
            // The bytecode as a hex string.
            object: "00fe",
            // Opcodes list (string)
            opcodes: "",
            // The source mapping as a string. See the source mapping definition.
            sourceMap: "",
            // If given, this is an unlinked object.
            linkReferences: {
              "libraryFile.sol": {
                // Byte offsets into the bytecode. Linking replaces the 20 bytes located there.
                "Library1": [
                  { start: 0, length: 20 },
                  { start: 200, length: 20 }
                ]
              }
            }
          },
          // The same layout as above.
          deployedBytecode: { },
          // The list of function hashes
          methodIdentifiers: {
            "delegate(address)": "5c19a95c"
          },
          // Function gas estimates
          gasEstimates: {
            creation: {
              codeDepositCost: "420000",
              executionCost: "infinite",
              totalCost: "infinite"
            },
            external: {
              "delegate(address)": "25000"
            },
            internal: {
              "heavyLifting()": "infinite"
            }
          }
        },
        // eWASM related outputs
        ewasm: {
          // S-expressions format
          wast: "",
          // Binary format (hex string)
          wasm: ""
        }
      }
    }
  }
}
目录
相关文章
|
机器学习/深度学习 存储 人工智能
第3章:知识表示:概述、符号知识表示、向量知识表示
第3章:知识表示:概述、符号知识表示、向量知识表示
第3章:知识表示:概述、符号知识表示、向量知识表示
|
人工智能 安全 机器人
无代码革命:10分钟打造企业专属数据库查询AI机器人
随着数字化转型加速,企业对高效智能交互解决方案的需求日益增长。阿里云AppFlow推出的AI助手产品,借助创新网页集成技术,助力企业打造专业数据库查询助手。本文详细介绍通过三步流程将AI助手转化为数据库交互工具的核心优势与操作指南,包括全场景适配、智能渲染引擎及零代码配置等三大技术突破。同时提供Web集成与企业微信集成方案,帮助企业实现便捷部署与安全管理,提升内外部用户体验。
1378 12
无代码革命:10分钟打造企业专属数据库查询AI机器人
|
4月前
|
机器学习/深度学习 数据采集 人工智能
企业知识库搭建实战:RAG 从文档导入到检索调优全流程拆解
本文详解企业知识库搭建实战:以RAG为核心,覆盖文档导入、智能解析分段、语义/增强检索调优全流程。结合硅基边界平台案例,直击解析策略、分段长度、相似度阈值等关键参数调优要点,助技术/产品/运营团队两周内快速验证AI问答效果,让私域资料真正变成“会回答的AI”。
676 1
API
522 0
|
4月前
|
数据采集 人工智能 监控
电梯内车辆入梯目标检测数据集:28,000张图像 | 目标检测
本数据集含28,000张真实电梯监控图像,精准标注“自行车”与“电动车”两类目标,覆盖多场景、多尺度、强遮挡及复杂光照条件,专为AI电梯安全防控设计,支持YOLO等主流模型训练与边缘部署,助力构建“检测-告警-阻停”闭环安防系统。(239字)
255 0
|
5月前
|
人工智能 监控 Linux
阿里云部署Hermes Agent集成Token Plan:保姆级配置全流程
Hermes Agent是阿里云推出的可自主进化的AI智能体框架,支持对接各类大模型服务,实现任务自动化、代码生成、多轮对话与流程编排。百炼Token Plan作为团队版包月订阅模型服务,提供固定Credits额度、多模型共享、高峰不限速与企业级管理能力,是Hermes Agent的优选模型底座。本文从Token Plan订阅、密钥获取、Hermes Agent多方式配置、模型验证、用量监控到常见问题,提供完整集成教程,覆盖个人与企业用户全场景需求。
456 0
|
6月前
|
弹性计算 人工智能
使用阿里云官方扩展程序安装Hermes到ECS(不写代码,分钟级一键安装)
Hermes Agent是Nous Research开源的自我进化AI智能体。本文详解如何通过阿里云OOS扩展程序,3~5分钟一键安装至ECS,全程免手动配置,并指导大模型凭证配置与快速验证。
|
12月前
|
自然语言处理 算法
分词器详解
分词器将文本转为模型可处理的数字序列,主流算法包括BPE、WordPiece和SentencePiece。BPE高效但中文支持弱;WordPiece适合英文,用于BERT;SentencePiece语言无关,尤擅中文。实战中需结合语种与需求选择,并合理配置参数与特殊标记。
|
人工智能 自然语言处理 算法
微软AutoGen:多智能体协作的工业级解决方案
作为一名长期关注AI技术发展的开发者,我深深被微软AutoGen框架所展现的多智能体协作能力所震撼。在当今企业数字化转型的浪潮中,单一AI模型已难以满足复杂业务场景的需求,而AutoGen框架的出现为我们提供了一个革命性的解决方案。它不仅突破了传统单体AI的局限性,更通过其独特的多智能体协作机制,实现了真正意义上的"AI团队协作"。经过深入研究和实践,我发现AutoGen在智能体角色定义、通信协议设计、任务协调机制等方面都展现出了工业级的成熟度。特别是其对话驱动的编程范式和灵活的工作流编排能力,为企业级AI应用开发带来了前所未有的便利性和可扩展性。本文将从技术架构、实现原理到企业应用等多个维度
774 1
微软AutoGen:多智能体协作的工业级解决方案
|
XML API 开发工具
如何接入电竞体育直播API或者SDK
接入电竞体育直播API或SDK的步骤包括:1. 选择合适的API/SDK提供商,如ESL、Riot Games、Twitch等;2. 注册并获取API密钥;3. 阅读官方文档,了解接口调用和数据处理;4. 实现实时更新,确保数据同步;5. 测试与优化,确保功能稳定;6. 遵守使用规范。通过这些步骤,可成功集成电竞直播功能。