Java/Kotlin WE contract SDK.

Related tags

SDK we-contract-sdk
Overview

we-contract-sdk

Java/Kotlin contract SDK used for building Docker smart contracts.

All transaction handling is done via methods of a single class marked with @ContractHandler annotation. The methods which implement handling logic are marked with @ContractInit (for CreateContractTx) and @ContractAction (for CallContractTx).

Module structure

  • we-contract-sdk-api. Contains mainly interfaces and annotations for marking contract handlers and their methods. Should be used directly by *-contract-api modules.
  • we-contract-sdk-core. Core processing contract transactions logic. Transport and mapping is not implemented in core module and should be provided externally.
  • we-contract-sdk-grpc. Implementation with GRPC transport and Jackson object mapping. Should be used directly by the module which starts and runs the contract.

Getting started

Dependencies

Maven

<dependency>
  <groupId>com.wavesenterprise</groupId>
  <artifactId>we-contract-sdk-grpc</artifactId>
  <version>1.0.0</version>
</dependency>

Gradle

dependencies {
    implementation("com.wavesenterprise:we-contract-sdk-grpc:1.0.0")
} 

Quick start

All examples are taken from the Samples.

1. Create contract handler

@ContractHandler
public class SampleContractHandler {

    private final ContractState contractState;
    private final ContractTransaction tx;

    private final Mapping<List<MySampleContractDto>> mapping;

    public SampleContractHandler(ContractState contractState, ContractTransaction tx) {
        this.contractState = contractState;
        mapping = contractState.getMapping(
                new TypeReference<List<MySampleContractDto>>() {
                }, "SOME_PREFIX");
        this.tx = tx;
    }
}

2. Add @ContractInit and @ContractAction method to handle contract transactions

public class SampleContractHandler {

    // ... 

    @ContractInit
    public void createContract(String initialParam) {
        contractState.put("INITIAL_PARAM", initialParam);
    }

    @ContractAction
    public void doSomeAction(String dtoId) {
        contractState.put("INITIAL_PARAM", Instant.ofEpochMilli(tx.getTimestamp().getUtcTimestampMillis()));

        if (mapping.has(dtoId)) {
            throw new IllegalArgumentException("Already has " + dtoId + " on state");
        }
        mapping.put(dtoId,
                Arrays.asList(
                        new MySampleContractDto("john", 18),
                        new MySampleContractDto("harry", 54)
                ));
    }
}

3. Dispatch contract with specified contract handler and settings

public class MainDispatch {
    public static void main(String[] args) {
        ContractDispatcher contractDispatcher = GrpcJacksonContractDispatcherBuilder.builder()
                .contractHandlerType(SampleContractHandler.class)
                .objectMapper(getObjectMapper())
                .build();

        contractDispatcher.dispatch();
    }

    private static ObjectMapper getObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        return objectMapper;
    }
}

4. Create Docker image

FROM openjdk:8-alpine
MAINTAINER Waves Enterprise <>

ENV JAVA_MEM="-Xmx256M"
ENV JAVA_OPTS=""

ADD build/libs/*-all.jar app.jar

RUN chmod +x app.jar
RUN eval $SET_ENV_CMD
CMD ["/bin/sh", "-c", "eval ${SET_ENV_CMD} ; java $JAVA_MEM $JAVA_OPTS -jar app.jar"]

5. Push to docker repository used by WE Node mining contract transactions

Publish to registry used by WE Blockchain Node. For convenience there is a bash script build_and_push_to_docker.sh which builds and pushes to specified registry.

 ./build_and_push_to_docker.sh my.registry.com/contracts/my-awesome-docker-contract:1.0.0

6. Sign and broadcast transactions for creating and invoking published contract

You will need image and imageHash of the published contract to create it.

CreateContractTx example

{
    "image": "my.registry.com/contracts/my-awesome-docker-contract:1.0.0",
    "fee": 0,
    "imageHash": "d17f6c1823176aa56e0e8184f9c45bc852ee9b076b06a586e40c23abde4d7dfa",
    "type": 103,
    "params": [
        {
            "type": "string",
            "value": "createContract",
            "key": "action"
        },
        {
            "type": "string",
            "value": "initialValue",
            "key": "createContract"
        }
    ],
    "version": 2,
    "sender": "3M3ybNZvLG7o7rnM4F7ViRPnDTfVggdfmRX",
    "feeAssetId": null,
    "contractName": "myAwesomeContract"
}

To call contract you will need contractId = CreateContractTx.id.

CallContractTx example

{
    "contractId": "7sVc6ybnqZr523xWK5Sg7xADsX597qga8iQNAS9f1D3c",
    "fee": 0,
    "type": 104,
    "params": [
      {
        "type": "string",
        "value": "doSomeAction",
        "key": "action"
      },
      {
        "type": "string",
        "value": "someValue",
        "key": "createContract"
      }
    ],
    "version": 2,
    "sender": "3M3ybNZvLG7o7rnM4F7ViRPnDTfVggdfmRX",
    "feeAssetId": null,
    "contractVersion": 1
}

Notes on usage

Usage with Java 11+

Library has been tested against java8 and java17. When using with java17 additional java options should be specified for the io.grpc to enable optimizations.

--add-opens java.base/jdk.internal.misc=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED -Dio.netty.tryReflectionSetAccessible=true

Full example can be found in Dockerfile for java17.

You might also like...
Implement a Casper Kotlin SDK to interact with the Casper network.
Implement a Casper Kotlin SDK to interact with the Casper network.

CSPR-Kotlin-SDK Kotlin SDK library for interacting with a CSPR node. What is CSPR-Kotlin-SDK? SDK to streamline the 3rd party Kotlin client integratio

Android Word/Letter Tracing SDK is a complet solution to build apps for kids learning in pure kotlin
Android Word/Letter Tracing SDK is a complet solution to build apps for kids learning in pure kotlin

Android Word/Letter Tracing SDK is a complet solution to build apps for kids learning in pure kotlin. It supports all kind of shapes and all language letters e.g. english, arabic, Urdu, hindi etc.

AWS SDK for Android. For more information, see our web site:

AWS SDK for Android For new projects, we recommend interacting with AWS using the Amplify Framework. The AWS SDK for Android is a collection of low-le

Countly Product Analytics Android SDK
Countly Product Analytics Android SDK

Countly Android SDK We're hiring: Countly is looking for Android SDK developers, full stack devs, devops and growth hackers (remote work). Click this

Android Real Time Chat & Messaging SDK
Android Real Time Chat & Messaging SDK

Android Chat SDK Overview Applozic brings real-time engagement with chat, video, and voice to your web, mobile, and conversational apps. We power emer

Evernote SDK for Android

Evernote SDK for Android version 2.0.0-RC4 Evernote API version 1.25 Overview This SDK wraps the Evernote Cloud API and provides OAuth authentication

Air Native Extension (iOS and Android) for the Facebook mobile SDK

Air Native Extension for Facebook (iOS + Android) This is an AIR Native Extension for the Facebook SDK on iOS and Android. It has been developed by Fr

Android Chat SDK built on Firebase
Android Chat SDK built on Firebase

Chat21 is the core of the open source live chat platform Tiledesk.com. Chat21 SDK Documentation Features With Chat21 Android SDK you can: Send a direc

Liquid SDK (Android)

Liquid Android SDK Quick Start to Liquid SDK for Android This document is just a quick start introduction to Liquid SDK for Android. We recommend you

Releases(1.2.0)
  • 1.2.0(Oct 10, 2022)

    • Added a client factory to interact with the contract from backend applications;
    • Added contract call preValidation;
    • Added Kotlin delegate methods for working with ContractState in a map-like fashion;
    • Fixed the converter of values from the contract state;
    • Added a README to create a client;
    • Added an example of a backend application using a client.
    Source code(tar.gz)
    Source code(zip)
Owner
Waves Enterprise
Waves Enterprise
Segmenkt - The SegmenKT Kotlin SDK is a Kotlin-first SDK for Segment

SegmenKT Kotlin SDK The SegmenKT Kotlin SDK is a Kotlin-first SDK for Segment. I

UNiDAYS 0 Nov 25, 2022
Sdk-android - SnapOdds Android SDK

Documentation For the full API documentation go to https://snapodds.github.io/sd

Snapodds 0 Jan 30, 2022
Frogo SDK - SDK Core for Easy Development

SDK for anything your problem to make easier developing android apps

Frogobox 10 Dec 15, 2022
Streem Server SDK for Java & Kotlin

Streem Server SDK for Java & Kotlin Server-side JVM library for interacting with the Streem API, and generation of Streem Tokens for use in client SDK

Streem, Inc. 1 Dec 15, 2022
Its measurement app made using kotlin with sceneform sdk by google

ARCORE MEASUREMENT This app is build using sceneform sdk for android using kotlin language It helps you measure the distance between multiple points i

Kashif Mehmood 39 Dec 9, 2022
Storyblok Kotlin Multiplatform SDK sample (Android, JVM, JS)

storyblok-mp-SDK-sample *WIP* ... a showcase of the Storyblok Kotlin Multiplatform Client SDK. (Android, JVM, JS, iOS, ...) What's included ?? • About

Mike Penz 6 Jan 8, 2022
Kotlin Multi Platform SDK

Xeon SDK (work-in-progress ?? ??️ ??‍♀️ ⛏ ) Development Version Release This Is Latest Release ~ In Development $version_release = ~ What's New?? * I

Frogobox 3 Oct 15, 2021
NextPay Kotlin SDK

Welcome to nextpay-kt ?? Connect to NextPay.ir payment gateway in easy way. ?? Homepage Setup Kotlin KTS 1- Add mavenCentral() to your repositories se

Farhad 5 Nov 6, 2021
WalletConnect Kotlin SDK v2

WalletConnect V2 - Kotlin Kotlin implementation of WalletConnect v2 protocol for Android applications. Requirements Android min SDK 21 Java 11 Install

WalletConnect Labs 11 Dec 22, 2021
Official Appwrite Kotlin SDK 💙🧡

Appwrite Kotlin SDK This SDK is compatible with Appwrite server version 0.11.x. For older versions, please check previous releases. This is the Kotlin

Appwrite 47 Nov 26, 2022