All Products
Search
Document Center

Solidity Development Tool

Last Updated: Sep 05, 2019

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:

  1. Download and install Node.js.

  2. Install solc-js globally. Run the following command in the directory where you downloaded alipay-solc-0.1.10.tgz:

    1. 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:

  1. 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:

  1. pragma solidity ^0.4.20;
  2. contract Hello {
  3. string name;
  4. identity id; //The identity data type is similar to the address data type in the native Solidity language.
  5. constructor() public {
  6. name = 'Hello world!' ;
  7. }
  8. function hello() view public returns (identity, string) {
  9. return (msg.sender, name);
  10. }
  11. }

Compile contracts in command lines

  • Run the following command line to compile the hello.sol contract and obtain the compiled bytecode:

    1. solcjs --bin hello.sol

    If 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):

    1. solcjs --abi hello.sol

    If the contract is compiled, the result file (hello_sol_Hello.abi) containing the ABI is displayed in the directory.

Compile contracts in JavaScript code

  1. Create the solcjs-test directory, run npm init, and move the alipay-solc-0.1.10.tgz file to the solcjs-test directory.

  2. Run the following command line to install solc-js in the solcjs-test directory:

    1. npm i alipay-solc-0.1.10.tgz --save
  3. Compile the contract in JavaScript code and create the index.js file as follows:

    1. var solc = require('@alipay/solc')
    2. var input = 'contract test { function g(identity a) {} }'
    3. // Setting 1 as second paramateractivates the optimiser
    4. var output = solc.compile(input, 1)
    5. for (var contractName in output.contracts) {
    6. // code and ABI
    7. console.log(contractName + ': ' + output.contracts[contractName].bytecode)
    8. console.log(contractName + ': ' + output.contracts[contractName].interface)
    9. }
  4. Run index.js:

    1. node index.js
  5. The output contains the ABI and the compiled bytecode:

    1. :test: 6080604052348015600f57600080fd5b5060898061001e6000396000f300608060405260043610603e5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166338a1231d81146043575b600080fd5b348015604e57600080fd5b506058600435605a565b005b505600a165627a7a7230582008d3450904d4f09535ba76326aae5ecd2f61113b791d633dbb3c0799ff75b3ad0029
    2. :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 instructions.

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 instructions.

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 --bin-runtime parameter. solc-mac.zip
0.1.10 CentOS 7.2 version - Compatible with Solidity syntax prior to version 0.4.24. - Supports the --bin-runtime parameter. solc-cenos7.2.zip

More