All Products
Search
Document Center

Blockchain as a Service:How to Use Cloud IDE

Last Updated:Sep 18, 2023

Enter the Cloud IDE environment

You can enter the Cloud IDE contract development environment by using the following methods:

  • On the Consortium Contract Projects page the BaaS platform, you can enter the Cloud IDE contract development environment by creating a contract or editing an existing contract.

  • Or, you can go to the Overview page and click Depoly Contract on Contract Test Chain to go to the contract development environment (Cloud IDE).

Mange contract projects in Cloud IDE

In Cloud IDE, you can create, delete, or modify the folders and files of a contract project , as shown in the following figure:

image.png

No.

Icon

Describe

image.png

Add file or folder

image.png

Delete file or folder

image.png

Rename file or folder

Note

Note: If you enter the Cloud IDE environment from the Project Management page, the contract files are saved in real time by default. End new contract files with .sol to ensure successful compilation.

How to develop contracts in Cloud IDE

You can develop a contract in Cloud IDE as shown in the following five steps:

  1. Select a target contract blockchain

  2. Compile the contract

  3. Deploy the contract

  4. Call the contract

  5. Debug the contract

Select a target contract blockchain

In Cloud IDE, click Environment Configuration to specify the environment for the target contract blockchain.envir

In Environment Configuration, you can select an existing environment. For example, if you have applied for a test blockchain, you can select an existing contract blockchain. This test blockchain provides a default test account for you to deploy and call contracts. If this test account cannot meet your requirements, you can use the account created by using the SDK. In this case, you need to enter the private key (a hexadecimal string) to sign the transactions when you deploy or call a contract in IDE. For more information about how to obtain the private key, see the sample code in JS SDK quick start.

Compile contracts

When you develop a contract, you can compile the completed code at any time.

In Cloud IDE, click Compile to compile the code.compile

  • BytecodeThe bytecode is the compiled contract code and the key data used to deploy a contract. You can use the SDK to deploy the contract bytecode to the target production chain.

  • Contract ABIContract ABI refers to the application binary interface (ABI) of a contract. When the contract is compiled, its corresponding ABI is also generated.

ABI is similar to the interface documentation in a program. ABI describes properties and method signatures, including field names, field types, method names, parameter names, parameter types, and the types of the returned values.

ABI parameters:

Parameter

Description

name

The name of the function.

type

The method type, including function, constructor, and fallback (the unnamed “default” function).

constant

The Boolean value. If this value is true, the method does not modify the state variable of the contract field.

payable

The Boolean value. This value indicates whether the method can receive system transfers.

stateMutability

A string with one of the following values: pure (specified to not read the blockchain state), view (specified to not modify the blockchain state), nonpayable (same as payable above), and payable (same as payable above).

inputs

An array of objects, each of which contains the name of the parameter and the canonical type of the parameter.

name

The name of the parameter.

type

The type of the parameter.

outputs

Similar to inputs, it is an array of objects. It can be omitted if the function does not return anything.

For more information about ABI, see Official Description of Solidity ABI (English)

Deploy contracts

After you obtain the bytecode and ABI, click Deploy to deploy the contract to a target test blockchain.deploy

To deploy a contract with a defined constructor, the parameters of the constructor function must be specified. Cloud IDE provides tips for the parameter types and the initial values of some basic data types. You can initialize the contract based on your requirements.

After you deploy the contract, you can see a list of all contract methods.list

Call contracts

After you deploy a contract, you can see all public methods and state variables of the public type in the contract. You can click Call Contract on the right side of the target method to call the method.

You can call the method to query the state variable and obtain the value of the state variable. The following fields will be returned when a contract method is called:

Parameter

Description

input

The input data of the contract method. Typically, it is the contract method parameter.

output

The return values of the contract method. The values are encoded based on the data type.

log

If an event is used in the contract and the event is triggered, it will be displayed in the log.

call

Error description

When you use Cloud IDE, you may encounter some error messages. When you call the contract, errors may occur for multiple reasons. In this case, Cloud IDE will return the error codes for you to analyze the reason. The error codes used by Cloud IDE are consistent with that used by the SDK. For more information about the error codes, see Contract blockchain error codes.

The following example describes how to analyze the cause of a contract error by using the error description. You can also debug the contract by adding an event.

Sample error code:error

In the preceding sample error code, the system calls the voteForCandidate method of the Cloud IDE sample contract. The given candidate is Demi and the returned error code is 10201. The meaning of the error code is as follows:

Error code

Error code value

Description

VM_REVERT

10201

The error is caused by triggering the revert command.

According to the error code message, the call contract failed because the revert statement is triggered, which means the value returned by the require() function is false. When you check the contract code, you can find the relevant condition check in the voteForCandidate method.

require(validCandidate(candidate));

It can be inferred that the input candidate Demi is invalid and is not in candidate list candidateList. This is true. When the contract is deployed, the constructor parameter only specifies two candidates to participate in the election: Nick and Rose.

Debug contracts

At present, you can debug the internal logic of the contract by adding Event to trigger the log and analyze the code logic.

Call the validCandidate method.

debug
// This function will help to check whether target candidate is in the candidateList.
    function validCandidate(bytes32 candidate) view public returns (bool) {
        for (uint i = 0; i < candidateList.length; i++) {
            if (candidateList[i] == candidate) {
            emit VALID(true);
            return true;
            }
        }
        emit VALID(false);
        return false;
    }

When the validCandidate method is implemented, different events will be triggered based on the execution logic. You can analyze the execution path of the contract by checking the log of call results. In the preceding sample, the execution logic is simple. In a complex execution logic, you can add Event to track the execution path of a contract and debug the contract.