Ant Blockchain uses a customized Solidity compiler — solc-js — that is compatible with Solidity syntax prior to version 0.4.24 (excluding version 0.4.24). This page covers how to install and use solc-js, and when to switch to the solc binary compiler instead.
Ant Blockchain does not support the standard Solidity compiler. Use only the BaaS-provided solc-js (alipay-solc-0.1.10.tgz). Contracts using Solidity 0.4.24 or later will not compile.
You can also compile, deploy, test, and debug contracts directly in Cloud IDE, the browser-based development environment for Ant Blockchain.
solc-js
solc-js is a cross-platform JavaScript compiler that you can use from the command line or within JavaScript code. It integrates with the JS SDK to automate contract deployment and invocation.
Limitation: solc-js supports the --bin flag but not --bin-runtime. If you need --bin-runtime output (required for contract updates), use the solc binary compiler instead.
Prerequisites
Before you begin, make sure you have:
Node.js installed
Download and install solc-js
Download the installation package: alipay-solc-0.1.10.tgz (~1.76 MB).
Run the following command in the directory where you downloaded the package to install solc-js globally:
npm i -g alipay-solc-0.1.10.tgz
After installation, solcjs is available in your terminal. Run solcjs --help to see all supported options.
CLI reference
Usage: /usr/local/bin/solcjs [options] <FILE_NAME>| Option | Type | Description |
|---|---|---|
--version | bool | Print the version number. |
--bin | bool | Output the compiled bytecode (hex to binary). Use this for initial contract deployment. |
--abi | bool | Output the Application Binary Interface (ABI). |
--optimize | bool | Enable the bytecode optimizer. |
--standard-json | bool | Enable standard JSON input/output mode. |
--output-dir, -o | string | Write output files to the specified directory. |
--help | bool | Print help information. |
solc-js does not support --bin-runtime. To compile runtime bytecode for contract updates, use the solc binary compiler.Compile a contract
The examples below use the following sample contract (hello.sol):
pragma solidity ^0.4.20;
contract Hello {
string name;
identity id; // identity is an Ant Blockchain type similar to address in standard Solidity
constructor() public {
name = 'Hello world!';
}
function hello() view public returns (identity, string) {
return (msg.sender, name);
}
}Get the compiled bytecode
Run the following command to compile hello.sol and output the bytecode:
solcjs --bin hello.solOn success, the output file hello_sol_Hello.bin appears in the current directory. If compilation fails, an error message is displayed.
Get the ABI
Run the following command to compile hello.sol and output the ABI:
solcjs --abi hello.solOn success, the output file hello_sol_Hello.abi appears in the current directory.
Compile in JavaScript code
Use the following steps to compile a contract programmatically and get both the ABI and bytecode.
Create a
solcjs-testdirectory, runnpm initinside it, and movealipay-solc-0.1.10.tgzinto the directory.Install solc-js as a local dependency:
npm i alipay-solc-0.1.10.tgz --saveCreate
index.jswith the following code. The second argument tosolc.compile()activates the optimizer when set to1.var solc = require('@alipay/solc') var input = 'contract test { function g(identity a) {} }' // Setting 1 as the second parameter activates the optimizer var output = solc.compile(input, 1) for (var contractName in output.contracts) { // Print the bytecode and ABI for each contract console.log(contractName + ': ' + output.contracts[contractName].bytecode) console.log(contractName + ': ' + output.contracts[contractName].interface) }Run the script:
node index.jsThe output contains the compiled bytecode and ABI:
:test: 6080604052348015600f57600080fd5b5060898061001e6000396000f300608060405260043610603e5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166338a1231d81146043575b600080fd5b348015604e57600080fd5b506058600435605a565b005b505600a165627a7a7230582008d3450904d4f09535ba76326aae5ecd2f61113b791d633dbb3c0799ff75b3ad0029 :test: [{"constant":false,"inputs":[{"name":"a","type":"identity"}],"name":"g","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]
Deploy and call a contract with the JS SDK
After compiling, pass the ABI and bytecode directly to the JS SDK to deploy and invoke the contract. For details, see JS SDK Introduction.
solc binary compiler
Use solc when you need to update an existing contract. Unlike solc-js, solc supports --bin-runtime, which produces the runtime bytecode required by the SDK's contract update operation.
--bin vs --bin-runtime
| Flag | Output | Use for |
|---|---|---|
--bin | Full deployment bytecode = runtime bytecode + constructor bytecode | Initial contract deployment |
--bin-runtime | Runtime bytecode only | Updating an existing contract |
For contract updates, compile with --bin-runtime:
solc --bin-runtime <your-contract.sol>Pass the resulting bytecode to the JS SDK's update contract operation. For details, see JS SDK Introduction.
Download solc
Both versions are compatible with Solidity syntax prior to version 0.4.24 and support --bin-runtime.
| Version | Platform | Download |
|---|---|---|
| 0.1.10 | macOS | solc-mac.zip |
| 0.1.10 | CentOS 7.2 | solc-cenos7.2.zip |
What's next
JS SDK Introduction — Deploy and call contracts using the JS SDK
Solidity documentation — Reference for Solidity 0.4.x syntax