Installation and usage of the platform

WE Contract SDK (Java/Kotlin Contract SDK) Client

This section describes the WE Contract SDK Client.The client is used to interact with the contract from the backend code of Java/Kotlin applications.

Main abstractions

  • ContractBlockingClientFactory – Factory to create a contract client.

  • NodeBlockingServiceFactory – A factory that creates services for interacting with a node.

  • TxService – Interface for working with transactions on the node.

  • TxSigner – Interface for signing transactions on the node.

  • ConverterFactory – Factory for creating services for converting values when working with a state.

  • ContractToDataValueConverter – Interface for converting values to DataValue objects.

  • ContractFromDataEntryConverter – Interface for converting Data Entry values from a state.

  • ContractClientParams – Class intended for the settings of the client being created.

  • ContractSignRequestBuilder – The SignRequest(transaction) builder that creates a contract creation object (103rd transaction) or a contract invocation object (104th transaction).

Quickstart

Follow these steps to create WE contract SDK client.

Note

All the examples below are taken from the Waves Enterprise GitHub. In addition, the Waves Enterprise `GitHub repository <https://github.com/waves-enterprise/we-contract-sdk/tree/master/we-contract-sdk-client>_ provides more examples.

1. Create and configure services to work with the node:

val objectMapper = ObjectMapper()
 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
 .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
 .registerModule(JavaTimeModule())
 .registerModule(
     KotlinModule.Builder()
         .configure(KotlinFeature.NullIsSameAsDefault, true)
         .build()
)
val converterFactory = JacksonConverterFactory(objectMapper)
val feignNodeClientParams = FeignNodeClientParams(
    url = "{node.url}",
    decode404 = true,
    connectTimeout = 5000L,
    readTimeout = 3000L,
    loggerLevel = Logger.Level.FULL,
)
val feignTxService = FeignTxService(
    weTxApiFeign = FeignWeApiFactory.createClient(
        clientClass = WeTxApiFeign::class.java,
        feignProperties = feignNodeClientParams,
    )
)
val feignNodeServiceFactory = FeignNodeServiceFactory(
    params = feignNodeClientParams
)
val contractProperties = ContractProperties(
    senderAddress = "",
    fee = 0L,
    contractId = "contractId",
    contractVersion = 1,
    version = 1,
    image = "image",
    imageHash = "imageHash",
    contractName = "contractName",
)
val contractClientParams = ContractClientParams(localValidationEnabled = true)
val contractSignRequestBuilder = ContractSignRequestBuilder()
    .senderAddress(Address.fromBase58(contractProperties.senderAddress))
    .fee(Fee(0L))
    .contractId(ContractId.fromBase58(contractProperties.contractId))
    .contractVersion(ContractVersion(contractProperties.contractVersion))
    .version(TxVersion(contractProperties.version))
    .image(ContractImage(contractProperties.image))
    .imageHash(Hash.fromHexString(contractProperties.imageHash))
    .contractName(ContractName(contractProperties.contractName))
val contractClientParams = ContractClientParams(localValidationEnabled = true)

2. Form transaction data:

 val contractSignRequestBuilder = ContractSignRequestBuilder()
.senderAddress(Address.fromBase58(contractProperties.senderAddress))
.fee(Fee(0L))
.contractId(ContractId.fromBase58(contractProperties.contractId))
.contractVersion(ContractVersion(contractProperties.contractVersion))
.version(TxVersion(contractProperties.version))
.image(ContractImage(contractProperties.image))
.imageHash(Hash.fromHexString(contractProperties.imageHash))
.contractName(ContractName(contractProperties.contractName))

3. Create a client contract factory and configure it:

val contractFactory = ContractBlockingClientFactory(
   contractClass = TestContractImpl::class.java,
   contractInterface = TestContract::class.java,
   converterFactory = converterFactory,
   contractClientProperties = contractClientParams,
   contractSignRequestBuilder = contractSignRequestBuilder,
   nodeBlockingServiceFactory = nodeBlockingServiceFactory,
)

4. Create TxSigner:

val txServiceTxSigner = TxServiceTxSignerFactory(
  txService = feignTxService,
)

5. Create and invoke client methods:

val executionContext: ExecutionContext = contractFactory.executeContract(
txSigner = txSigner) { contract ->
    contract.create()
}
See also