All Products
Search
Document Center

Blockchain as a Service:Contract Interface

Last Updated:Dec 24, 2019

Deploy smart contracts

deployContract

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

  • Function
  1. 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
  1. // build params
  2. MychainParams params = new MychainParams.Builder()
  3. .gas(BigInteger.valueOf(40000))
  4. .privateKeyList(adminPrivateKeys)
  5. .build();
  6. ContractParameters contractParameters = new ContractParameters();
  7. //deploy contract
  8. MychainBaseResult<ReplyTransactionReceipt> result = sdk.getContractService().deployContract(
  9. DeployContractRequest.build(
  10. adminAccount.getIdentity(),
  11. Utils.getIdentityByName(testContractId),
  12. contractCode,
  13. contractParameters,
  14. new BigInteger("10000"),
  15. params
  16. )
  17. );

asyncDeployContract

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

  • Function
  1. 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
  1. // build params
  2. MychainParams params = new MychainParams.Builder()
  3. .gas(BigInteger.valueOf(40000))
  4. .privateKeyList(adminPrivateKeys)
  5. .build();
  6. ContractParameters contractParameters = new ContractParameters();
  7. //deploy contract
  8. MychainBaseResult<Response> result = sdk.getContractService().asyncDeployContract(
  9. DeployContractRequest.build(
  10. adminAccount.getIdentity(),
  11. Utils.getIdentityByName(testContractId),
  12. contractCode,
  13. contractParameters,
  14. new BigInteger("10000"),
  15. params
  16. ),
  17. (txHash, response) -> {
  18. System.out.println("async deploy contract, txHash:"+txHash+", result: "+ response.getErrorCode());
  19. }
  20. );

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
  1. 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
  1. // build params
  2. MychainParams params = new MychainParams.Builder()
  3. .gas(BigInteger.valueOf(40000))
  4. .privateKeyList(adminPrivateKeys)
  5. .build();
  6. BigInteger value = new BigInteger("0");
  7. String method = "set(uint256)";
  8. List<String> parameters = new ArrayList<>();
  9. parameters.add("100");
  10. //call contract
  11. MychainBaseResult<ReplyTransactionReceipt> replyTransactionReceipt = sdk.getContractService().callContract(
  12. CallContractRequest.build(
  13. adminAccount.getIdentity(),
  14. Utils.getIdentityByName(testContractId),
  15. parameters,
  16. value,
  17. method,
  18. params
  19. )
  20. );

asyncCallContract

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

  • Function
  1. 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
  1. // build params
  2. MychainParams params = new MychainParams.Builder()
  3. .gas(BigInteger.valueOf(40000))
  4. .privateKeyList(adminPrivateKeys)
  5. .build();
  6. ContractParameters parameters = new ContractParameters("set(uint256)");
  7. parameters.addUint(new BigInteger("100"));
  8. // call contract
  9. MychainBaseResult<Response> replyTransactionReceipt = sdk.getContractService().asyncCallContract(
  10. CallContractRequest.build(
  11. adminAccount.getIdentity(),
  12. Utils.getIdentityByName(testContractId),
  13. parameters,
  14. value,
  15. method,
  16. params
  17. ),
  18. (txHash, response) -> {
  19. System.out.println("async call contract, txHash:"+txHash+", result: "+ response.getErrorCode());
  20. }
  21. );

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
  1. 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
  1. // build params
  2. MychainParams params = new MychainParams.Builder()
  3. .gas(BigInteger.valueOf(40000))
  4. .privateKeyList(adminPrivateKeys)
  5. .build();
  6. ContractParameters contractParameters = new ContractParameters();
  7. //update contract
  8. MychainBaseResult<ReplyTransactionReceipt> result = sdk.getContractService()
  9. .updateContract(
  10. UpdateContractRequest.build(Utils.getIdentityByName(testContractId), newContractCode, VMTypeEnum.EVM, params));

asyncUpdateContract

You can call this asynchronous operation to update a contract.

  • Function
  1. 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
  1. // build params
  2. // build params
  3. MychainParams params = new MychainParams.Builder()
  4. .gas(BigInteger.valueOf(40000))
  5. .privateKeyList(adminPrivateKeys)
  6. .build();
  7. ContractParameters contractParameters = new ContractParameters();
  8. //update contract
  9. MychainBaseResult<Response> result = sdk.getContractService().asyncUpdateContract(
  10. UpdateContractRequest.build(contractId, newContractCode, VMTypeEnum.EVM, params),
  11. new ICallback() {
  12. @Override
  13. public void onResponse(String txHash, Response response) {
  14. System.out.println("async update contract, txHash:" + txHash + ", result: "
  15. + response.getErrorCode());
  16. assertTrue(response.isSuccess());
  17. assertEquals("0", response.getErrorCode().getErrorCode());
  18. }
  19. });

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.