diff --git a/_backend-services/cf-lob-tx-submitter/.env.template b/_backend-services/cf-lob-tx-submitter/.env.template new file mode 100644 index 0000000..1c9b48e --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/.env.template @@ -0,0 +1,3 @@ +ORGANISER_MNEMONIC_PHRASE="test test test test test test test test test test test test test test test test test test test test test test test sauce" +BLOCKFOST_URL=https://cardano-mainnet.blockfrost.io/api/v1/ +BLOCKFROST_PASSWORD= diff --git a/_backend-services/cf-lob-tx-submitter/.gitignore b/_backend-services/cf-lob-tx-submitter/.gitignore new file mode 100644 index 0000000..ff2a3ef --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/.gitignore @@ -0,0 +1 @@ +spring-shell.log \ No newline at end of file diff --git a/_backend-services/cf-lob-tx-submitter/.run/CFLOBTxSubmitter (PRE-PROD).run.xml b/_backend-services/cf-lob-tx-submitter/.run/CFLOBTxSubmitter (PRE-PROD).run.xml new file mode 100644 index 0000000..a0d23c9 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/.run/CFLOBTxSubmitter (PRE-PROD).run.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file diff --git a/_backend-services/cf-lob-tx-submitter/.run/CFLOBTxSubmitter (PROD).run.xml b/_backend-services/cf-lob-tx-submitter/.run/CFLOBTxSubmitter (PROD).run.xml new file mode 100644 index 0000000..a07ffe9 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/.run/CFLOBTxSubmitter (PROD).run.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/_backend-services/cf-lob-tx-submitter/.run/VotingAppAdminService (YACI_DEV_NET).run.xml b/_backend-services/cf-lob-tx-submitter/.run/VotingAppAdminService (YACI_DEV_NET).run.xml new file mode 100644 index 0000000..bbb0309 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/.run/VotingAppAdminService (YACI_DEV_NET).run.xml @@ -0,0 +1,26 @@ + + + + \ No newline at end of file diff --git a/_backend-services/cf-lob-tx-submitter/Dockerfile b/_backend-services/cf-lob-tx-submitter/Dockerfile new file mode 100644 index 0000000..b765827 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/Dockerfile @@ -0,0 +1,9 @@ +FROM openjdk:20-jdk-slim AS build +WORKDIR /app +COPY . /app +RUN ./gradlew clean build + +FROM openjdk:20-jdk-slim AS runtime +WORKDIR /app +COPY --from=build /app/build/libs/*SNAPSHOT.jar /app/app.jar +ENTRYPOINT ["java", "-jar", "app.jar"] diff --git a/_backend-services/cf-lob-tx-submitter/README.md b/_backend-services/cf-lob-tx-submitter/README.md new file mode 100644 index 0000000..e69de29 diff --git a/_backend-services/cf-lob-tx-submitter/build.gradle.kts b/_backend-services/cf-lob-tx-submitter/build.gradle.kts new file mode 100644 index 0000000..4b76dd8 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/build.gradle.kts @@ -0,0 +1,55 @@ +plugins { + java + id("io.spring.dependency-management") version "1.1.3" + id("org.graalvm.buildtools.native") version "0.9.27" + id("com.github.ben-manes.versions") version "0.48.0" + id("org.springframework.boot") version "3.1.2" +} + +group = "org.cardano.foundation" +version = "1.0.0-SNAPSHOT" +java.sourceCompatibility = JavaVersion.VERSION_21 + +configurations { + compileOnly { + extendsFrom(configurations.annotationProcessor.get()) + } +} + +repositories { + mavenCentral() + maven { url = uri("https://repo.spring.io/milestone") } +} + +extra["springShellVersion"] = "3.1.2" + +dependencies { + implementation("org.springframework.shell:spring-shell-starter") + testImplementation("org.springframework.boot:spring-boot-starter-test") + + compileOnly("org.projectlombok:lombok:1.18.34") + annotationProcessor("org.projectlombok:lombok:1.18.34") + + testCompileOnly("org.projectlombok:lombok:1.18.34") + testAnnotationProcessor("org.projectlombok:lombok:1.18.34") + + implementation("com.bloxbean.cardano:cardano-client-crypto:0.6.2") // 0.6.2 + implementation("com.bloxbean.cardano:cardano-client-address:0.6.2") + implementation("com.bloxbean.cardano:cardano-client-metadata:0.6.2") + implementation("com.bloxbean.cardano:cardano-client-quicktx:0.6.2") + implementation("com.bloxbean.cardano:cardano-client-backend-blockfrost:0.6.2") + + implementation("io.blockfrost:blockfrost-java:0.1.3") + + implementation("me.paulschwarz:spring-dotenv:4.0.0") +} + +dependencyManagement { + imports { + mavenBom("org.springframework.shell:spring-shell-dependencies:${property("springShellVersion")}") + } +} + +tasks.withType { + useJUnitPlatform() +} diff --git a/_backend-services/cf-lob-tx-submitter/gradle/wrapper/gradle-wrapper.jar b/_backend-services/cf-lob-tx-submitter/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..d64cd49 Binary files /dev/null and b/_backend-services/cf-lob-tx-submitter/gradle/wrapper/gradle-wrapper.jar differ diff --git a/_backend-services/cf-lob-tx-submitter/gradle/wrapper/gradle-wrapper.properties b/_backend-services/cf-lob-tx-submitter/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..1af9e09 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/_backend-services/cf-lob-tx-submitter/gradlew b/_backend-services/cf-lob-tx-submitter/gradlew new file mode 100755 index 0000000..1aa94a4 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/gradlew @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/_backend-services/cf-lob-tx-submitter/gradlew.bat b/_backend-services/cf-lob-tx-submitter/gradlew.bat new file mode 100644 index 0000000..6689b85 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/_backend-services/cf-lob-tx-submitter/script/preprod--start.sh b/_backend-services/cf-lob-tx-submitter/script/preprod--start.sh new file mode 100755 index 0000000..c7224ef --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/script/preprod--start.sh @@ -0,0 +1,3 @@ +export SPRING_CONFIG_LOCATION=classpath:/application.properties +export SPRING_PROFILES_ACTIVE=dev--preprod +./gradlew clean build && java -jar build/libs/voting-admin-app-1.0.0-SNAPSHOT.jar diff --git a/_backend-services/cf-lob-tx-submitter/settings.gradle.kts b/_backend-services/cf-lob-tx-submitter/settings.gradle.kts new file mode 100644 index 0000000..1e8212d --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/settings.gradle.kts @@ -0,0 +1,8 @@ +pluginManagement { + repositories { + gradlePluginPortal() + mavenCentral() + } +} + +rootProject.name = "cf-lob-tx-submitter" diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/CFLOBTxSubmitter.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/CFLOBTxSubmitter.java new file mode 100644 index 0000000..a064012 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/CFLOBTxSubmitter.java @@ -0,0 +1,23 @@ +package org.cardano.foundation.lob; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.shell.command.annotation.EnableCommand; + +@SpringBootApplication +@ComponentScan(basePackages = { + "org.cardano.foundation.lob.service", + "org.cardano.foundation.lob.config", + "org.cardano.foundation.lob.shell" +}) +@Slf4j +@EnableCommand +public class CFLOBTxSubmitter { + + public static void main(String[] args) { + SpringApplication.run(CFLOBTxSubmitter.class, args); + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/BlockchainDataConfig.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/BlockchainDataConfig.java new file mode 100644 index 0000000..1eb5e4e --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/BlockchainDataConfig.java @@ -0,0 +1,29 @@ +package org.cardano.foundation.lob.config; + +import com.bloxbean.cardano.client.backend.api.BackendService; +import lombok.extern.slf4j.Slf4j; +import org.cardano.foundation.lob.service.blockchain_state.*; +import org.cardano.foundation.lob.service.transaction_submit.BackendServiceBlockchainTransactionSubmissionService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@Slf4j +public class BlockchainDataConfig { + + @Bean + public BlockchainTransactionSubmissionService backendServiceTransactionSubmissionService(BackendService backendService) { + return new BackendServiceBlockchainTransactionSubmissionService(backendService); + } + + @Bean + public BlockchainDataChainTipService blockchainDataChainTipService(BackendService backendService) { + return new BackendServiceBlockchainDataChainTipService(backendService); + } + + @Bean + public BlockchainDataTransactionDetailsService blockchainDataTransactionDetailsService(BackendService backendService) { + return new BackendServiceBlockchainDataTransactionDetailsService(backendService); + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/CardanoClientLibConfig.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/CardanoClientLibConfig.java new file mode 100644 index 0000000..507fe69 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/CardanoClientLibConfig.java @@ -0,0 +1,44 @@ +package org.cardano.foundation.lob.config; + +import com.bloxbean.cardano.client.account.Account; +import com.bloxbean.cardano.client.backend.api.BackendService; +import com.bloxbean.cardano.client.backend.blockfrost.service.BFBackendService; +import com.bloxbean.cardano.client.common.model.Network; +import com.bloxbean.cardano.client.common.model.Networks; +import lombok.extern.slf4j.Slf4j; +import org.cardano.foundation.lob.domain.CardanoNetwork; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@Slf4j +public class CardanoClientLibConfig { + + @Bean + public BackendService backendService(@Value("${blockfrost.url}") String blockfrostUrl, + @Value("${blockfrost.api.key}") String blockfrostApiKey) { + return new BFBackendService(blockfrostUrl, blockfrostApiKey); + } + + @Bean + public Network network(CardanoNetwork cardanoNetwork) { + return switch(cardanoNetwork) { + case MAIN -> Networks.mainnet(); + case PREPROD -> Networks.preprod(); + case PREVIEW -> Networks.preview(); + case DEV -> Networks.testnet(); + }; + } + + @Bean + public Account organiserAccount(Network network, + @Value("${organiser.account.mnemonic}" ) String organiserMnemonic) { + var organiserAccount = new Account(network, organiserMnemonic); + + log.info("Organiser's address:{}, stakeAddress:{}, network:{}", organiserAccount.baseAddress(), organiserAccount.stakeAddress(), network); + + return organiserAccount; + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/CardanoConfig.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/CardanoConfig.java new file mode 100644 index 0000000..ce3c8ec --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/CardanoConfig.java @@ -0,0 +1,20 @@ +package org.cardano.foundation.lob.config; + +import lombok.extern.slf4j.Slf4j; +import org.cardano.foundation.lob.domain.CardanoNetwork; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@Slf4j +public class CardanoConfig { + + @Bean + public CardanoNetwork cardanoNetwork(@Value("${cardano.network:main}") CardanoNetwork network) { + log.info("Configured backend network:{}", network); + + return network; + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/HttpConfig.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/HttpConfig.java new file mode 100644 index 0000000..94bba07 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/HttpConfig.java @@ -0,0 +1,21 @@ +package org.cardano.foundation.lob.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.net.http.HttpClient; +import java.time.Duration; + +@Configuration +public class HttpConfig { + + @Bean + public HttpClient httpClient() { + return HttpClient.newBuilder() + .version(HttpClient.Version.HTTP_1_1) + .followRedirects(HttpClient.Redirect.NORMAL) + .connectTimeout(Duration.ofSeconds(30)) + .build(); + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/JsonConfig.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/JsonConfig.java new file mode 100644 index 0000000..c98ea0c --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/JsonConfig.java @@ -0,0 +1,28 @@ +package org.cardano.foundation.lob.config; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@Slf4j +public class JsonConfig { + + @Bean + public ObjectMapper objectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + + objectMapper.findAndRegisterModules(); + + log.info("Registered jackson modules:"); + objectMapper.getRegisteredModuleIds().forEach(moduleId -> { + log.info("Module: {}", moduleId); + }); + + return objectMapper; + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/TimeConfig.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/TimeConfig.java new file mode 100644 index 0000000..2a718e5 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/config/TimeConfig.java @@ -0,0 +1,16 @@ +package org.cardano.foundation.lob.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.time.Clock; + +@Configuration +public class TimeConfig { + + @Bean + public Clock clock() { + return Clock.systemDefaultZone(); + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/CardanoNetwork.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/CardanoNetwork.java new file mode 100644 index 0000000..d2b8d34 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/CardanoNetwork.java @@ -0,0 +1,17 @@ +package org.cardano.foundation.lob.domain; + +import java.util.Arrays; +import java.util.List; + +public enum CardanoNetwork { + + MAIN, // main-net + PREPROD, // preprod-net + PREVIEW, // preview-net + DEV; // e.g. locally hosted cardano-node + + public static List supportedNetworks() { + return Arrays.stream(CardanoNetwork.values()).map(network -> network.name().toLowerCase()).toList(); + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/ChainTip.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/ChainTip.java new file mode 100644 index 0000000..de7b214 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/ChainTip.java @@ -0,0 +1,11 @@ +package org.cardano.foundation.lob.domain; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ChainTip { + private long absoluteSlot; + private String hash; +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/L1SubmissionData.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/L1SubmissionData.java new file mode 100644 index 0000000..81208ef --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/L1SubmissionData.java @@ -0,0 +1,13 @@ +package org.cardano.foundation.lob.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@AllArgsConstructor +@Builder +public class L1SubmissionData { + String txHash; + long absoluteSlot; +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/TransactionDetails.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/TransactionDetails.java new file mode 100644 index 0000000..bf2d45b --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/domain/TransactionDetails.java @@ -0,0 +1,14 @@ +package org.cardano.foundation.lob.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@AllArgsConstructor +@Builder +public class TransactionDetails { + private String transactionHash; + private long absoluteSlot; + private String blockHash; +} \ No newline at end of file diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BackendServiceBlockchainDataChainTipService.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BackendServiceBlockchainDataChainTipService.java new file mode 100644 index 0000000..e1cd72e --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BackendServiceBlockchainDataChainTipService.java @@ -0,0 +1,36 @@ +package org.cardano.foundation.lob.service.blockchain_state; + +import com.bloxbean.cardano.client.backend.api.BackendService; +import io.vavr.control.Either; +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.cardano.foundation.lob.domain.ChainTip; + +@Slf4j +@RequiredArgsConstructor +public class BackendServiceBlockchainDataChainTipService implements BlockchainDataChainTipService { + + private final BackendService backendService; + + @Override + public Either getChainTip() { + try { + val latestBlock = backendService.getBlockService().getLatestBlock(); + + if (latestBlock.isSuccessful()) { + val block = latestBlock.getValue(); + + return Either.right(new ChainTip(block.getSlot(), block.getHash())); + } + + return Either.left(new RuntimeException("Unable to get chain tip via backend service")); + } catch (Exception e) { + log.error("Error getting chain tip", e); + + return Either.left(e); + } + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BackendServiceBlockchainDataTransactionDetailsService.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BackendServiceBlockchainDataTransactionDetailsService.java new file mode 100644 index 0000000..6a12bef --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BackendServiceBlockchainDataTransactionDetailsService.java @@ -0,0 +1,43 @@ +package org.cardano.foundation.lob.service.blockchain_state; + +import com.bloxbean.cardano.client.api.exception.ApiException; +import com.bloxbean.cardano.client.backend.api.BackendService; +import io.vavr.control.Either; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.cardano.foundation.lob.domain.TransactionDetails; + +import java.util.Optional; + +@Slf4j +@RequiredArgsConstructor +public class BackendServiceBlockchainDataTransactionDetailsService implements BlockchainDataTransactionDetailsService { + + private final BackendService backendService; + + @Override + public Either> getTransactionDetails(String transactionHash) { + try { + val result = backendService.getTransactionService().getTransaction(transactionHash); + + if (result.isSuccessful()) { + val trx = result.getValue(); + + return Either.right(Optional.of(TransactionDetails.builder() + .transactionHash(trx.getHash()) + .absoluteSlot(trx.getSlot()) + .blockHash(trx.getBlock()) + .build()) + ); + } + } catch (ApiException e) { + log.error("Error getting transaction details for hash: {}", transactionHash, e); + + return Either.left(e); + } + + return Either.left(new RuntimeException("Unable to get transaction details via backend service")); + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BlockchainDataChainTipService.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BlockchainDataChainTipService.java new file mode 100644 index 0000000..3f2adad --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BlockchainDataChainTipService.java @@ -0,0 +1,11 @@ +package org.cardano.foundation.lob.service.blockchain_state; + + +import io.vavr.control.Either; +import org.cardano.foundation.lob.domain.ChainTip; + +public interface BlockchainDataChainTipService { + + Either getChainTip(); + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BlockchainDataTransactionDetailsService.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BlockchainDataTransactionDetailsService.java new file mode 100644 index 0000000..c4722b5 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BlockchainDataTransactionDetailsService.java @@ -0,0 +1,14 @@ +package org.cardano.foundation.lob.service.blockchain_state; + +import io.vavr.control.Either; +import org.cardano.foundation.lob.domain.TransactionDetails; + +import java.util.Optional; + +public interface BlockchainDataTransactionDetailsService { + + default Either> getTransactionDetails(String transactionHash) { + return Either.right(Optional.empty()); + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BlockchainTransactionSubmissionService.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BlockchainTransactionSubmissionService.java new file mode 100644 index 0000000..c8f669b --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/blockchain_state/BlockchainTransactionSubmissionService.java @@ -0,0 +1,15 @@ +package org.cardano.foundation.lob.service.blockchain_state; + +import io.vavr.control.Either; + +public interface BlockchainTransactionSubmissionService { + + /** + * Submit transaction and return transaction hash. + * + * @param txData + * @return transaction hash + */ + Either submitTransaction(byte[] txData); + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/transaction_submit/BackendServiceBlockchainTransactionSubmissionService.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/transaction_submit/BackendServiceBlockchainTransactionSubmissionService.java new file mode 100644 index 0000000..8578b99 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/transaction_submit/BackendServiceBlockchainTransactionSubmissionService.java @@ -0,0 +1,33 @@ +package org.cardano.foundation.lob.service.transaction_submit; + +import com.bloxbean.cardano.client.backend.api.BackendService; +import io.vavr.control.Either; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.cardano.foundation.lob.service.blockchain_state.BlockchainTransactionSubmissionService; + +@RequiredArgsConstructor +@Slf4j +public class BackendServiceBlockchainTransactionSubmissionService implements BlockchainTransactionSubmissionService { + + private final BackendService backendService; + + @Override + public Either submitTransaction(byte[] txData) { + try { + val result = backendService.getTransactionService().submitTransaction(txData); + + if (result.isSuccessful()) { + return Either.right(result.getValue()); + } + + return Either.left(new RuntimeException("Unable to submit transaction via backend service, error: " + result.getResponse())); + } catch (Exception e) { + log.error("Error submitting transaction", e); + + return Either.left(e); + } + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/transaction_submit/DefaultTransactionSubmissionService.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/transaction_submit/DefaultTransactionSubmissionService.java new file mode 100644 index 0000000..e76fd14 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/transaction_submit/DefaultTransactionSubmissionService.java @@ -0,0 +1,75 @@ +package org.cardano.foundation.lob.service.transaction_submit; + +import io.vavr.control.Either; +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.cardano.foundation.lob.domain.L1SubmissionData; +import org.cardano.foundation.lob.service.blockchain_state.BlockchainDataTransactionDetailsService; +import org.cardano.foundation.lob.service.blockchain_state.BlockchainTransactionSubmissionService; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.time.Clock; +import java.time.LocalDateTime; +import java.util.concurrent.TimeoutException; + +@Service +@Slf4j +@RequiredArgsConstructor +public class DefaultTransactionSubmissionService implements TransactionSubmissionService { + + private final BlockchainTransactionSubmissionService transactionSubmissionService; + private final BlockchainDataTransactionDetailsService blockchainDataTransactionDetailsService; + private final Clock clock; + + @Value("${transaction.submission.timeout.minutes:15}") + private int timeoutInMinutes; + + @Value("${transaction.submission.sleep.seconds:5}") + private int sleepTimeInSeconds; + + @Override + public Either submitTransaction(byte[] txData) { + return transactionSubmissionService.submitTransaction(txData); + } + + @Override + @SneakyThrows + public Either submitTransactionWithConfirmation(byte[] txData) throws InterruptedException { + val txHashE = submitTransaction(txData); + if (txHashE.isLeft()) { + return Either.left(txHashE.getLeft()); + } + + val txHash = txHashE.get(); + + val start = LocalDateTime.now(clock); + + val future = start.plusMinutes(timeoutInMinutes); + + while (LocalDateTime.now(clock).isBefore(future)) { + val transactionDetailsE = blockchainDataTransactionDetailsService.getTransactionDetails(txHash); + + if (transactionDetailsE.isRight()) { + var transactionDetails = transactionDetailsE.get(); + if (transactionDetails.isPresent()) { + + val trxDetails = transactionDetails.get(); + return Either.right(L1SubmissionData.builder() + .txHash(trxDetails.getTransactionHash()) + .absoluteSlot(trxDetails.getAbsoluteSlot()) + .build()); + } + } + + log.info("Transaction not confirmed yet. Sleeping for {} seconds... until deadline:{}", sleepTimeInSeconds, future); + + Thread.sleep(sleepTimeInSeconds * 1000L); + } + + return Either.left(new TimeoutException("Transaction not confirmed within timeout period!")); + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/transaction_submit/TransactionSubmissionService.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/transaction_submit/TransactionSubmissionService.java new file mode 100644 index 0000000..17940b2 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/service/transaction_submit/TransactionSubmissionService.java @@ -0,0 +1,25 @@ +package org.cardano.foundation.lob.service.transaction_submit; + +import io.vavr.control.Either; +import org.cardano.foundation.lob.domain.L1SubmissionData; + +import java.util.concurrent.TimeoutException; + +public interface TransactionSubmissionService { + + /** + * Submit transaction and return transaction hash. + * + * @param txData + * @return transaction hash + */ + Either submitTransaction(byte[] txData); + + /** + * Submits transaction and gets L1 confirmation data + * @param txData + * @return + */ + Either submitTransactionWithConfirmation(byte[] txData) throws TimeoutException, InterruptedException; + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/shell/API3SubmissionCommands2024.java b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/shell/API3SubmissionCommands2024.java new file mode 100644 index 0000000..481669c --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/java/org/cardano/foundation/lob/shell/API3SubmissionCommands2024.java @@ -0,0 +1,319 @@ +package org.cardano.foundation.lob.shell; + +import com.bloxbean.cardano.client.account.Account; +import com.bloxbean.cardano.client.api.model.Amount; +import com.bloxbean.cardano.client.backend.api.BackendService; +import com.bloxbean.cardano.client.common.model.Network; +import com.bloxbean.cardano.client.function.helper.SignerProviders; +import com.bloxbean.cardano.client.metadata.Metadata; +import com.bloxbean.cardano.client.metadata.MetadataBuilder; +import com.bloxbean.cardano.client.metadata.MetadataMap; +import com.bloxbean.cardano.client.quicktx.QuickTxBuilder; +import com.bloxbean.cardano.client.quicktx.Tx; +import com.bloxbean.cardano.client.transaction.util.TransactionUtil; +import io.vavr.control.Either; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.cardano.foundation.lob.service.blockchain_state.BlockchainDataChainTipService; +import org.cardano.foundation.lob.service.transaction_submit.TransactionSubmissionService; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.annotation.Order; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; + +import java.math.BigInteger; + +@ShellComponent +@Slf4j +@RequiredArgsConstructor +public class API3SubmissionCommands2024 { // reports + + private final BackendService backendService; + private final TransactionSubmissionService transactionSubmissionService; + private final BlockchainDataChainTipService blockchainDataChainTipService; + private final Account organiserAccount; + private final Network network; + + @Value("${l1.transaction.metadata.label:1447}") + private int metadataLabel; + + @ShellMethod(key = "01_create-income-statement") + @Order(1) + public String submitReportIncomeStatement() throws Exception { + log.info("Creating API3 Income Statement event on a {}} network...", network); + + val chainTipE = blockchainDataChainTipService.getChainTip(); + if (chainTipE.isLeft()) { + throw chainTipE.getLeft(); + } + + val chainTip = chainTipE.get(); + val creationSlot = chainTip.getAbsoluteSlot(); + + val reportMap = createIncomeStatementMetadataMap(creationSlot); + + log.info("Serialising metadata and submitting transaction..."); + + log.info("INCOME STATEMENT: {}", reportMap.toJson()); + + val txBytesE = serialiseTransaction(serialiseMetadata(reportMap)); + if (txBytesE.isLeft()) { + throw txBytesE.getLeft(); + } + val txBytes = txBytesE.get(); + val txHash = TransactionUtil.getTxHash(txBytes); + + log.info("Transaction hash: {}", txHash); + + val l1SubmissionDataE = transactionSubmissionService.submitTransactionWithConfirmation(txBytes); + if (l1SubmissionDataE.isLeft()) { + throw l1SubmissionDataE.getLeft(); + } + val l1SubmissionData = l1SubmissionDataE.get(); + log.info("Transaction submitted with confirmation. l1Data: {}", l1SubmissionData); + + return "Created API3 Income Statement report."; + } + + @ShellMethod(key = "02_create-report-balance-sheet") + @Order(2) + public String submitBalanceSheet() throws Exception { + log.info("Creating API3 Balance Sheet report on a {} network...", network); + + val chainTipE = blockchainDataChainTipService.getChainTip(); + if (chainTipE.isLeft()) { + log.error("Error getting chain tip. Reason: {}", chainTipE.getLeft().getMessage()); + + throw chainTipE.getLeft(); + } + val chainTip = chainTipE.get(); + val creationSlot = chainTip.getAbsoluteSlot(); + + val reportMap = createReportBalanceSheetMetadata(creationSlot); + + log.info("Serialising metadata and submitting transaction..."); + + log.info("BALANCE SHEET REPORT: {}", reportMap.toJson()); + + val txBytesE = serialiseTransaction(serialiseMetadata(reportMap)); + if (txBytesE.isLeft()) { + throw txBytesE.getLeft(); + } + val txBytes = txBytesE.get(); + val txHash = TransactionUtil.getTxHash(txBytes); + + log.info("Transaction hash: {}", txHash); + + val l1SubmissionDataE = transactionSubmissionService.submitTransactionWithConfirmation(txBytes); + if (l1SubmissionDataE.isLeft()) { + log.error("Error submitting transaction. Reason: {}", l1SubmissionDataE.getLeft().getMessage()); + + throw l1SubmissionDataE.getLeft(); + } + + val l1SubmissionData = l1SubmissionDataE.get(); + log.info("Transaction submitted with confirmation. l1Data: {}", l1SubmissionData); + + log.info("Creating API3 Balance Sheet report on a network: {}", network); + + return "Created API3 Balance Sheet report."; + } + + private MetadataMap createIncomeStatementMetadataMap(long creationSlot) { + val topLevel = MetadataBuilder.createMap(); + + topLevel.put("type", "REPORT"); + topLevel.put("subType", "INCOME_STATEMENT"); + topLevel.put("interval", "YEAR"); + topLevel.put("year", "2023"); + topLevel.put("mode", "USER"); + + // Organization section + val org = createOrg(); + topLevel.put("org", org); + + // Metadata section + val metadataMap = createMetadataMap(creationSlot); + topLevel.put("metadata", metadataMap); + + // Data section + val data = buildIncomeStatementData(); + + // Add data section to the top-level map + topLevel.put("data", data); + + return topLevel; + } + + private MetadataMap createReportBalanceSheetMetadata(long creationSlot) { + val topLevel = MetadataBuilder.createMap(); + + topLevel.put("type", "REPORT"); + topLevel.put("subType", "BALANCE_SHEET"); + topLevel.put("interval", "YEAR"); + topLevel.put("year", "2023"); + topLevel.put("mode", "USER"); + + // Organization section + val org = createOrg(); + topLevel.put("org", org); + + // Metadata section + val metadataMap = createMetadataMap(creationSlot); + topLevel.put("metadata", metadataMap); + + // Data section + val data = createBalanceSheetData(); + + // Add data section to the top-level map + topLevel.put("data", data); + + return topLevel; + } + + private static MetadataMap createMetadataMap(long creationSlot) { + val metadata = MetadataBuilder.createMap(); + metadata.put("format", "1.0"); + metadata.put("creation_slot", BigInteger.valueOf(creationSlot)); + + return metadata; + } + + private static MetadataMap createOrg() { + val org = MetadataBuilder.createMap(); + org.put("name", "Cardano Foundation"); + org.put("country_code", "CH"); + org.put("tax_id_number", "CHE-184477354"); + org.put("currency_id", "ISO_4217:CHF"); + org.put("id", "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94"); + + return org; + } + + private MetadataMap createBalanceSheetData() { + val data = MetadataBuilder.createMap(); + + // Assets section + val assets = MetadataBuilder.createMap(); + + // Non-current assets + val nonCurrentAssets = MetadataBuilder.createMap(); + nonCurrentAssets.put("property_plant_equipment", "500000.00"); + nonCurrentAssets.put("intangible_assets", "200000.00"); + nonCurrentAssets.put("investments", "150000.00"); + nonCurrentAssets.put("financial_assets", "100000.00"); + assets.put("non_current_assets", nonCurrentAssets); + + // Current assets + val currentAssets = MetadataBuilder.createMap(); + currentAssets.put("prepayments_and_other_short_term_assets", "50000.00"); + currentAssets.put("other_receivables", "75000.00"); + currentAssets.put("crypto_assets", "25000.00"); + currentAssets.put("cash_and_cash_equivalents", "300000.00"); + assets.put("current_assets", currentAssets); + + data.put("assets", assets); + + // Liabilities section + val liabilities = MetadataBuilder.createMap(); + + // Non-current liabilities + val nonCurrentLiabilities = MetadataBuilder.createMap(); + nonCurrentLiabilities.put("provisions", "250000.00"); + liabilities.put("non_current_liabilities", nonCurrentLiabilities); + + // Current liabilities + val currentLiabilities = MetadataBuilder.createMap(); + currentLiabilities.put("trade_accounts_payables", "50000.00"); + currentLiabilities.put("other_current_liabilities", "75000.00"); + currentLiabilities.put("accruals_and_short_term_provisions", "25000.00"); + liabilities.put("current_liabilities", currentLiabilities); + + data.put("liabilities", liabilities); + + // Capital section + val capital = MetadataBuilder.createMap(); + capital.put("capital", "300000.00"); + capital.put("retained_earnings", "200000.00"); + + data.put("capital", capital); + + return data; + } + + public static MetadataMap buildIncomeStatementData() { + val data = MetadataBuilder.createMap(); + + // Revenues section + val revenues = MetadataBuilder.createMap(); + revenues.put("other_income", "5000000.00"); + revenues.put("build_of_long_term_provision", "1000000.00"); + data.put("revenues", revenues); + + // COGS (Cost of Goods Sold) section + val cogs = MetadataBuilder.createMap(); + cogs.put("cost_of_providing_services", "15000000.00"); + data.put("cogs", cogs); + + // Operating Expenses section + val operatingExpenses = MetadataBuilder.createMap(); + operatingExpenses.put("personnel_expenses", "20000000.00"); + operatingExpenses.put("general_and_administrative_expenses", "5000000.00"); + operatingExpenses.put("depreciation_and_impairment_losses_on_tangible_assets", "3000000.00"); + operatingExpenses.put("amortization_on_intangible_assets", "2000000.00"); + operatingExpenses.put("rentExpenses", "1111.00"); + data.put("operating_expenses", operatingExpenses); + + // Financial Income section + val financialIncome = MetadataBuilder.createMap(); + financialIncome.put("finance_income", "3000000.00"); + financialIncome.put("finance_expenses", "1500000.00"); + financialIncome.put("realised_gains_on_sale_of_cryptocurrencies", "1000000.00"); + financialIncome.put("staking_rewards_income", "2000000.00"); + financialIncome.put("net_income_options_sale", "500000.00"); + data.put("financial_income", financialIncome); + + // Extraordinary Income section + val extraordinaryIncome = MetadataBuilder.createMap(); + extraordinaryIncome.put("extraordinary_expenses", "1000000.00"); + data.put("extraordinary_income", extraordinaryIncome); + + // Tax Expenses section + val taxExpenses = MetadataBuilder.createMap(); + taxExpenses.put("income_tax_expense", "2500000.00"); + data.put("tax_expenses", taxExpenses); + + return data; + } + + protected Metadata serialiseMetadata(MetadataMap childMetadata) { + val metadata = MetadataBuilder.createMetadata(); + metadata.put(metadataLabel, childMetadata); + + return metadata; + } + + protected Either serialiseTransaction(Metadata metadata) { + val quickTxBuilder = new QuickTxBuilder(backendService); + + val tx = new Tx() + .payToAddress(organiserAccount.baseAddress(), Amount.ada(1)) + .attachMetadata(metadata) + .from(organiserAccount.baseAddress()); + + try { + val txBytes = quickTxBuilder.compose(tx) + .withSigner(SignerProviders.signerFrom(organiserAccount)) + .buildAndSign() + .serialize(); + + return Either.right(txBytes); + } catch (Exception e) { + log.error("Error serialising transaction", e); + + return Either.left(e); + } + } + +} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/resources/application-dev--yaci-dev-kit.properties b/_backend-services/cf-lob-tx-submitter/src/main/resources/application-dev--yaci-dev-kit.properties new file mode 100644 index 0000000..f4195d1 --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/resources/application-dev--yaci-dev-kit.properties @@ -0,0 +1,12 @@ +# here we specify only properties to override from the default values specified in the application-pre-prod.properties file + +# local YaciDevKit network, see: https://github.com/bloxbean/yaci-devkit +cardano.network=DEV + +blockfrost.url=${BLOCKFROST_URL:http://localhost:8080/api/v1/} +# locally YaciDevKit has an empty password +blockfrost.api.key=${BLOCKFROST_PASSWORD:} + +organiser.account.mnemonic=${ORGANISER_MNEMONIC_PHRASE} + +#spring.profiles.active=dev--yaci-dev-kit diff --git a/_backend-services/cf-lob-tx-submitter/src/main/resources/application-pre-prod.properties b/_backend-services/cf-lob-tx-submitter/src/main/resources/application-pre-prod.properties new file mode 100644 index 0000000..a807cae --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/resources/application-pre-prod.properties @@ -0,0 +1,13 @@ +spring.banner.location=classpath:/banner.txt + +cardano.network=${CARDANO_NETWORK:PREPROD} + +l1.transaction.metadata.label=${VOTING_METADATA_LABEL:1447} + +blockfrost.url=${BLOCKFOST_URL:https://cardano-preprod.blockfrost.io/api/v0} +blockfrost.api.key=${BLOCKFROST_PASSWORD:} + +organiser.account.mnemonic=${ORGANISER_MNEMONIC_PHRASE} + +# default spring profile is a development profile with an external preprod environment +spring.profiles.active=${SPRING_PROFILES_ACTIVE:dev--preprod} diff --git a/_backend-services/cf-lob-tx-submitter/src/main/resources/application-prod.properties b/_backend-services/cf-lob-tx-submitter/src/main/resources/application-prod.properties new file mode 100644 index 0000000..90e0d8d --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/resources/application-prod.properties @@ -0,0 +1,9 @@ +cardano.network=${CARDANO_NETWORK:MAIN} + +l1.transaction.metadata.label=${VOTING_METADATA_LABEL:1447} + +blockfrost.url=${BLOCKFOST_URL:https://blockfrost-api.pro.dandelion-mainnet.eu-west-1.metadata.dev.cf-deployments.org} +blockfrost.api.key=${BLOCKFROST_PASSWORD:password} + +organiser.account.mnemonic=${ORGANISER_MNEMONIC_PHRASE} + diff --git a/_backend-services/cf-lob-tx-submitter/src/main/resources/application.properties b/_backend-services/cf-lob-tx-submitter/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/_backend-services/cf-lob-tx-submitter/src/main/resources/banner.txt b/_backend-services/cf-lob-tx-submitter/src/main/resources/banner.txt new file mode 100644 index 0000000..f8fd44e --- /dev/null +++ b/_backend-services/cf-lob-tx-submitter/src/main/resources/banner.txt @@ -0,0 +1,12 @@ +_________ ___________ ____ ____ __ .__ +\_ ___ \\_ _____/ \ \ / /____/ |_|__| ____ ____ +/ \ \/ | __) \ Y / _ \ __\ |/ \ / ___\ +\ \____| \ \ ( <_> ) | | | | \/ /_/ > + \______ /\___ / \___/ \____/|__| |__|___| /\___ / + \/ \/ \//_____/ + _____ .___ .__ _____ + / _ \ __| _/_____ |__| ____ / _ \ ______ ______ + / /_\ \ / __ |/ \| |/ \ / /_\ \\____ \\____ \ +/ | \/ /_/ | Y Y \ | | \ / | \ |_> > |_> > +\____|__ /\____ |__|_| /__|___| / \____|__ / __/| __/ + \/ \/ \/ \/ \/|__| |__|