# redspot 多合约编译 ink 的官方编译工具, cargo-contract 是不支持编译多合约的,需要编写额外脚本完成(https://github.com/paritytech/ink/blob/master/examples/delegator/build-all.sh) 。但在 redspot v0.4 中,我们很好的实现了多合约编译。它支持合约的文件的自动检测,多语言的合约,同时编译。 对于 ink 合约,redspot 会通过查找已配置的目录下的所有 cargo.toml 文件,然后解析 cargo.toml 文件,如果包含 ink_lang 依赖,则会认为这是一个 ink 合约。然后即可找到该项目下所有的合约。 对于 solang 合约,redspot 是通过匹配 .sol 文件,从而实现查找所有合约的功能。 redspot 默认的模版的合约的编译配置如下: ```javascript= ... export default { ... contract: { ink: { toolchain: "nightly", sources: ["contracts/**/*"], }, }, ... }; ``` 其中,toolchain 表示编译合约时需要使用的 toolchain, sources 表示需要找到的目录,支持 glob 语法。同时 sources 选项,也可以通过命令行指定,这会覆盖 config 文件中的配置选项: ```bash= npx redspot compile "contracts/**/*" ``` 也可以指定单个目录,这个就可以编译单个合约了: ```bash= npx redspot compile "contracts/erc20" ``` solang 也是类似的配置: ```javascript= ... export default { ... contract: { ink: { toolchain: "nightly", sources: ["contracts/**/*"], }, solang: { sources: ['contracts/**/*.sol'] } }, ... }; ``` ink 和 solang 可以同时存在,redspot 对 ink 合约使用 cargo-contract,对 solang 合约使用 solang 编译器编译。将来我们也会对其他编译器提供支持。 所有的编译产物能够在 artifacts (或者用户自己配置的目录)下找到: ``` -- artifacts -- erc20.contract -- erc20.json ``` redspot 会对同一个合约生成一个 `.contract` 文件和一个 `.json` 文件。`.contract` 包含了所有 `.json` 所有信息,并且还额外包含 wasm 的源码。所以 `.json` 仅仅只是提供给前端,在前端不需要上传 wasm 的情况下使用。 注意,项目中不应该出现多个合约的名字一样的。redspot 会检测到这种情况然后报错。 可以在 https://github.com/patractlabs/redspot/tree/master/examples/multi-contract/README.md 中找到合约编译的例子。 克隆 redspot 仓库,然后进入 `redspot/examples/multi-contract` 目录。 然后安装依赖: ``` $ yarn ``` 然后你需要确保你已安装 [cargo-contract](https://github.com/paritytech/cargo-contract) 和 [solang](https://github.com/hyperledger-labs/solang)。redspot 不会自动安装他们。 然后运行编译命令: ```bash= $ npx redspot compile ``` 这将会一次性编译所有合约,包括 solang 合约,solang 合约可以在 contracts/filpper 下找到。编译产物可以在 `artifacts` 目录下找到,然后我们删除 `artifacts` 文件夹。运行命令: ```bash= $ npx redspot compile contracts/erc20 ``` 可以编译 erc20 合约。检测 `artifacts` 文件夹,只有 erc20 合约的编译产物。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up