All Products
Search
Document Center

Blockchain as a Service:Contract Interface

Last Updated:Nov 06, 2024

Deploy smart contracts

deployContract

You can call this synchronous operation to deploy a smart contract.

  • Function

public MychainBaseResult<ReplyTransactionReceipt> deployContract(DeployContractRequest request)
  • Request parameters

Name

Required

Type

Description

request

true

DeployContractRequest

The request to deploy a smart contract.

  • Response fields

Response field

Field type

Description

result

MychainBaseResult

MychainBaseResult contains a response type.

  • Examples

// build  params
MychainParams params = new MychainParams.Builder()
  .gas(BigInteger.valueOf(40000))
  .privateKeyList(adminPrivateKeys)
  .build();

ContractParameters contractParameters = new ContractParameters();

//deploy contract
MychainBaseResult<ReplyTransactionReceipt> result = sdk.getContractService().deployContract(
    DeployContractRequest.build(
        adminAccount.getIdentity(),
        Utils.getIdentityByName(testContractId),
        contractCode,
        contractParameters,
        new BigInteger("10000"),
        params
    )
);

asyncDeployContract

You can call this asynchronous operation to deploy a smart contract.

  • Function

public MychainBaseResult<Response> asyncDeployContract(DeployContractRequest request, ICallback callback)
  • Request parameters

Name

Required

Type

Description

request

true

DeployContractRequest

The request to deploy a smart contract.

callback

true

ICallback

The callback function.

  • Response fields

Response field

Field type

Description

result

MychainBaseResult

MychainBaseResult contains a response type.

  • Examples

// build  params
MychainParams params = new MychainParams.Builder()
  .gas(BigInteger.valueOf(40000))
  .privateKeyList(adminPrivateKeys)
  .build();

ContractParameters contractParameters = new ContractParameters();

//deploy contract
MychainBaseResult<Response> result = sdk.getContractService().asyncDeployContract(
    DeployContractRequest.build(
        adminAccount.getIdentity(),
        Utils.getIdentityByName(testContractId),
        contractCode,
        contractParameters,
        new BigInteger("10000"),
        params
    ),
    (txHash, response) -> {
        System.out.println("async deploy contract, txHash:"+txHash+", result: "+ response.getErrorCode());
    }
);

DeployContractRequest

Parameters required to deploy a smart contract:

Name

Type

Description

acctId

Identity

The account ID used to deploy a smart contract.

contractId

Identity

The ID of the smart contract.

code

byte[]

The code of the smart contract.

vmTypeEnum

VMTypeEnum

The type of the virtual machine. Ethereum Virtual Machine (EVM) is used by default. Other types of virtual machines will be available later.

parameters

Parameters

The contract parameter.

value

BigInteger

The amount of the smart contract.

mychainParams

MychainParams

The extension parameter.

Call smart contracts

callContract

You can call this synchronous operation to call a smart contract.

  • Function

public MychainBaseResult<ReplyTransactionReceipt> callContract(CallContractRequest request)
  • Request parameters

Name

Required

Type

Description

request

true

CallContractRequest

The request to call a smart contract.

  • Response fields

Response field

Field type

Description

result

MychainBaseResult

MychainBaseResult contains a response type.

  • Examples

// build  params
MychainParams params = new MychainParams.Builder()
  .gas(BigInteger.valueOf(40000))
  .privateKeyList(adminPrivateKeys)
  .build();

BigInteger value = new BigInteger("0");
String method = "set(uint256)";
List<String> parameters = new ArrayList<>();
parameters.add("100");

//call contract
MychainBaseResult<ReplyTransactionReceipt> replyTransactionReceipt = sdk.getContractService().callContract(
    CallContractRequest.build(
        adminAccount.getIdentity(),
        Utils.getIdentityByName(testContractId),
        parameters,
        value,
        method,
        params
    )
);

asyncCallContract

You can call this asynchronous operation to call a smart contract.

  • Function

public MychainBaseResult<Response> asyncCallContract(CallContractRequest request, ICallback callback)
  • Request parameters

Name

Required

Type

Description

request

true

CallContractRequest

The request to call a smart contract.

callback

true

ICallback

The callback function.

  • Response fields

Response field

Field type

Description

result

MychainBaseResult

MychainBaseResult contains a response type.

  • Examples

// build  params
MychainParams params = new MychainParams.Builder()
  .gas(BigInteger.valueOf(40000))
  .privateKeyList(adminPrivateKeys)
  .build();

ContractParameters parameters = new ContractParameters("set(uint256)");
parameters.addUint(new BigInteger("100"));

// call contract
MychainBaseResult<Response> replyTransactionReceipt = sdk.getContractService().asyncCallContract(
    CallContractRequest.build(
        adminAccount.getIdentity(),
        Utils.getIdentityByName(testContractId),
        parameters,
        value,
        method,
        params
    ),
    (txHash, response) -> {
        System.out.println("async call contract, txHash:"+txHash+", result: "+ response.getErrorCode());
    }
);

CallContractRequest

Parameters required to call a smart contract:

Name

Type

Description

acctId

Identity

The account ID used to deploy the smart contract.

contractId

Identity

The ID of the smart contract.

parameters

Parameters

The contract parameter.

value

BigInteger

The amount of the smart contract.

mychainParams

MychainParams

The extension parameter.

Update smart contracts

updateContract

You can call this synchronous operation to update a smart contract.

  • Function

public MychainBaseResult<ReplyTransactionReceipt> updateContract(UpdateContractRequest request)
  • Request parameters

Name

Required

Type

Description

request

true

UpdateContractRequest

The request to update a smart contract.

  • Response fields

Response field

Field type

Description

result

MychainBaseResult

MychainBaseResult contains a response type.

  • Examples

// build  params
MychainParams params = new MychainParams.Builder()
  .gas(BigInteger.valueOf(40000))
  .privateKeyList(adminPrivateKeys)
  .build();

ContractParameters contractParameters = new ContractParameters();

//update contract
MychainBaseResult<ReplyTransactionReceipt> result = sdk.getContractService()
    .updateContract(
        UpdateContractRequest.build(Utils.getIdentityByName(testContractId), newContractCode, VMTypeEnum.EVM, params));

asyncUpdateContract

You can call this asynchronous operation to update a contract.

  • Function

public MychainBaseResult<Response> asyncUpdateContract(UpdateContractRequest request, ICallback callback)
  • Request parameters

Name

Required

Type

Description

request

true

UpdateContractRequest

The request to update the smart contract.

callback

true

ICallback

The callback function.

  • Response fields

Response field

Field type

Description

result

MychainBaseResult

MychainBaseResult contains a response type.

  • Examples

// build  params
// build  params
MychainParams params = new MychainParams.Builder()
  .gas(BigInteger.valueOf(40000))
  .privateKeyList(adminPrivateKeys)
  .build();

ContractParameters contractParameters = new ContractParameters();

//update contract
MychainBaseResult<Response> result = sdk.getContractService().asyncUpdateContract(
    UpdateContractRequest.build(contractId, newContractCode, VMTypeEnum.EVM, params),
    new ICallback() {
        @Override
        public void onResponse(String txHash, Response response) {
            System.out.println("async update contract, txHash:" + txHash + ", result: "
                               + response.getErrorCode());
            assertTrue(response.isSuccess());
            assertEquals("0", response.getErrorCode().getErrorCode());
        }
    });

UpdateContractRequest

Parameters required to update a smart contract.

Name

Type

Description

contractId

Identity

The ID of the smart contract.

code

byte[]

The bytecode of the target smart contract. The bytecode is in the hexadecimal format without the 0x prefix.

vmTypeEnum

VMTypeEnum

The type of the virtual machine.

mychainParams

MychainParams

The extension parameter.

Notes: To update the contract, the compiled runtime bytecode is required. This bytecode can be obtained directly by using the --bin-runtime parameter of solc compiler, the Solidity compiler. It is the subset of the bytecode of the compiled --bin parameter. The runtime bytecode can also be obtained by deploying local contracts.

The contract parameter object.

The contract parameter class used to compile smart contract methods and parameters in these methods.

The EVM contract parameter object.

  • ContractParameters

Function

Parameter type

Parameter description

setMethodSignature()

String

Uses the method name and the parameter type list to select a function.

getEncodedData()

String

The EVM parameter bytecode.

addInt()

BigInteger

Adds static int data.

addUInt()

BigInteger

Adds static uint data.

addBool()

Boolean

Adds static boolean data.

addIdentity()

Identity

Adds static identity data.

addString ()

String

Adds dynamic string data.

addBytes()

byte[]

Adds dynamic byte array data.

addBytesN()

byte[]

Adds static byte array data.

addIntArray()

List<BigInteger>

Adds dynamic int array data.

addUIntArray()

List<BigInteger>

Adds dynamic uint array data.

addBytesNArray()

byte[] value

Adds dynamic byte array data.

addBooleanArray()

List<Boolean>

Adds dynamic boolean array data.

Resolve EVM contract return values

  • ContractReturnValues

Function

Parameter type

Parameter description

ContractReturnValues()

String

The string returned by the virtual machine.

getInt()

BigInteger

Obtains static int data.

getUInt()

BigInteger

Obtains static uint data.

getBool()

Boolean

Obtains the boolean data.

getIdentity()

Identity

Obtains the identity data.

getString ()

String

Obtains the string data.

getBytes()

byte[]

Obtains the byte array data.

getBytesN()

byte[]

Obtains the byte array data.

getIntArray()

List<BigInteger>

Obtains the int array data.

getUIntArray()

List<BigInteger>

Obtains the uint array data.

getBytesNArray()

byte[] value

Obtains the byte array data.

getBooleanArray()

List<Boolean>

Obtains the boolean array data.

WASM contract parameter objects

Note: The C++ smart contract development feature has not been officially released.

  • WasmContractParameters

Function

Parameter type

Parameter description

setMethodSignature()

String

Uses the method name and the parameter type list to select a function.

getEncodedData()

String

The EVM parameter bytecode.

addInt8()

BigInteger

Adds static int_8 data.

addInt16()

BigInteger

Adds static int_16 data.

addInt32()

BigInteger

Adds static int_32 data.

addInt64()

BigInteger

Adds static int_64 data.

addUInt8()

BigInteger

Adds static uint_8 data.

addUInt16()

BigInteger

Add static uint_16 data.

addUInt32()

BigInteger

Adds static uint_32 data.

addUInt64()

BigInteger

Adds static uint_64 data.

addBool()

boolean

Adds static boolean data.

addIdentity()

Identity

Adds static identity data.

addString()

String

Adds dynamic string data.

addBytes()

byte[]

Adds dynamic byte array data.

addInt8Array()

List

Adds dynamic int_8 array data.

addInt16Array()

List

Adds dynamic int_16 array data.

addInt32Array()

List

Adds dynamic int_32 array data.

addInt64Array()

List

Adds dynamic int_64 array data.

addUInt8Array()

List`

Adds dynamic uint_8 array data.

addUInt16Array()

List

Adds dynamic uint_16 array data.

addUInt32Array()

List

Adds dynamic uint_32 array data.

addUInt64Array()

List

Adds dynamic uint_64 array data.

addBooleanArray()

List

Adds dynamic boolean array data.

addUtfStringArray()

List

Adds dynamic string data.