The contract platform of Ant Blockchain supports Solidity smart contracts. In Cloud IDE, the development environment of Ant Blockchain, you can compile, deploy, test, and debug a smart contract.
This topic describes how to download, install, and use the Solidity compiler solc-js provided by Ant Blockchain. This topic also introduces the solc compiler.
Download solc-js
Click here to obtain the installation package: alipay-solc-0.1.10.tgz. The size of the package is about 1.76 MB.
Notes
:
Ant Blockchain does not support the native Solidity language. You can only use the compiler solc-js provided by BaaS.
The compiler solc-js provided by BaaS is alipay-solc-0.1.10.tgz. It is compatible with Solidity syntax prior to version 0.4.24 (excluding version 0.4.24).
Install solc-js
Node.js is required when you install solc-js. Make sure you have installed Node.js before you install solc-js. The process is as follows:
Download and install Node.js.
Install solc-js globally. Run the following command in the directory where you downloaded alipay-solc-0.1.10.tgz:
npm i -g alipay-solc-0.1.10.tgz
Use solc-js
After you install solc-js globally, you can use this tool directly in the command line. You can use solcjs --help to view the parameters supported by this tool.
Command line syntax:
Usage: /usr/local/bin/solcjs [options] <FILE_NAME>Options:
--versionDescription: Display the version number.Type: bool--optimizeDescription: Enable the bytecode optimizer.Type: bool--binDescription: Convert the hexadecimal contract to a binary contract.Type: bool--abiDescription: Display the application binary interface (ABI).Type: bool--standard-jsonDescription: Open the standard JSON input or output mode.Type: bool--output-dir,-oDescription: Display the output directory of the contract.Type: string--helpDescription: Display the help information.Type: bool
Examples:
Create the Solidity contract hello.sol as follows:
pragma solidity ^0.4.20;
contract Hello {
string name;
identity id; //The identity data type is similar to the address data type in the native Solidity language.
constructor() public {
name = 'Hello world!' ;
}
function hello() view public returns (identity, string) {
return (msg.sender, name);
}
}Compile contracts in command lines
Run the following command line to compile the hello.sol contract and obtain the compiled bytecode:
solcjs --bin hello.solIf the contract is compiled, the result file (hello_sol_Hello.bin) containing the compiled bytecode is displayed in the directory. If the compilation fails, an error message is displayed.
Run the following command line to compile the hello.sol contract and obtain the application binary interface (ABI):
solcjs --abi hello.solIf the contract is compiled, the result file (hello_sol_Hello.abi) containing the ABI is displayed in the directory.
Compile contracts in JavaScript code
Create the solcjs-test directory, run
npm init, and move the alipay-solc-0.1.10.tgz file to the solcjs-test directory.Run the following command line to install solc-js in the solcjs-test directory:
npm i alipay-solc-0.1.10.tgz --saveCompile the contract in JavaScript code and create the index.js file as follows:
var solc = require('@alipay/solc') var input = 'contract test { function g(identity a) {} }' // Setting 1 as second paramateractivates the optimiser var output = solc.compile(input, 1) for (var contractName in output.contracts) { // code and ABI console.log(contractName + ': ' + output.contracts[contractName].bytecode) console.log(contractName + ': ' + output.contracts[contractName].interface) }Run index.js:
node index.jsThe output contains the ABI and the compiled bytecode:
:test: 6080604052348015600f57600080fd5b5060898061001e6000396000f300608060405260043610603e5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166338a1231d81146043575b600080fd5b348015604e57600080fd5b506058600435605a565b005b505600a165627a7a7230582008d3450904d4f09535ba76326aae5ecd2f61113b791d633dbb3c0799ff75b3ad0029 :test: [{"constant":false,"inputs":[{"name":"a","type":"identity"}],"name":"g","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]
Use solc-js with JS SDK
After you compile a Solidity contract in JavaScript code, you can obtain the ABI and the compiled bytecode. You can then directly use the JS SDK to deploy and call a contract. For more information, see JS SDK Introduction.
Solidity compiler (solc)
solc-js is cross-platform and can be used with JS SDK to automatically deploy and call contracts. solc-js supports the --bin parameter on the command line. However, it cannot use the--bin-runtime parameter to compile the bytecode that is required to update the contract operation.
In JavaScript code, solc-js uses the --bin parameter by default to compile a contract to obtain the compiled bytecode. This bytecode cannot be directly used to update a contract. However, after you deploy a contract locally, you can obtain the runtime bytecode to update the contract. For more information, see JS SDK Introduction.
The solc compiler supports both the --bin-runtime parameter and the --bin parameter. Targeting the same Solidity contract, the compiling results of these two parameters are different. When you use the --bin-runtime parameter, the compiled bytecode is a part of the bytecode compiled using the --bin parameter. The bytecode compiled using the --bin parameter includes not only the bytecode compiled using the --bin-runtime parameter, but also the bytecode of the constructor method.
To use the update contract operation in the SDK, we recommend that you use the solc compiler and use the --bin-runtime parameter to obtain the compiled bytecode.
Version | Description | Download link |
0.1.10 | macOS version - Compatible with Solidity syntax prior to version 0.4.24. - Supports the | |
0.1.10 | CentOS 7.2 version - Compatible with Solidity syntax prior to version 0.4.24. - Supports the |