25 Commits

Author SHA256 Message Date
minimons d9f773c1f9 X: Updated for some manual testing in Main 2026-06-26 22:27:57 +02:00
minimons 2d68b83cf4 17: Add mint address field in JupiterPerpsPosition 2026-06-26 22:27:57 +02:00
minimons 7e6e9978a2 18: Add a codec class for Base58 2026-06-26 20:55:37 +02:00
minimons 35052a045a X: Rename AnchorIdlJupiterPerpsPositionServiceImpl 2026-06-26 19:26:24 +02:00
minimons ce74550825 X: Rename JupiterPerpsPositionService 2026-06-26 19:26:24 +02:00
minimons a5c9c78fdd X: Move JupiterPerpsPositionService to API instead of implementation 2026-06-26 19:26:24 +02:00
minimons c4f67fa0fc 16: Add 'side' to AnchorIdlJupiterPerpsPositionDecoder 2026-06-26 19:26:24 +02:00
minimons c8ca946fcc X: Move JupiterPerpsPosition to API from implementation 2026-06-26 18:08:26 +02:00
minimons 760ba0e823 X: Add some manuel testing code 2026-06-26 17:02:41 +02:00
minimons 1cb78fa3db 15: Add getOpenPositions() to Jupiter Perps API 2026-06-26 17:02:40 +02:00
minimons 09bd70b348 14: Add getProgramAccounts(...) API method 2026-06-26 14:14:06 +02:00
minimons cd573b1ce0 X: Some dependency cleanup 2026-06-25 19:08:52 +02:00
minimons 96d1289730 13: Create initial Jupiter Perps position API 2026-06-25 15:30:30 +02:00
minimons 539068978f 12: Add grace period support for persistent alarms 2026-06-23 18:26:23 +02:00
minimons 48f087dc6e 11: Remove temporary SelfTest code 2026-06-23 12:57:03 +02:00
minimons be73fe9ee6 10: Support constants in alarm configuration 2026-06-23 12:19:48 +02:00
minimons 7dcfb9977c 7: Make JupiterPerpsAlarm send the note on the push notification 2026-06-23 08:15:25 +02:00
minimons 0a6a7b4a3b 9: Support percentage expressions in alarm targets 2026-06-21 18:48:54 +02:00
minimons b74e4ad286 8: Add 'example' to configuration files 2026-06-21 17:16:27 +02:00
minimons 74b0b1dc64 6: Extend the JupiterPerpsAlarm with an ID column 2026-06-21 13:47:17 +02:00
minimons ca03200847 5: JupiterPerpsAlarm should be able to alarm on price 0 2026-06-21 13:47:17 +02:00
minimons dee96cf704 4: Implement severity in JupiterPerpsAlarm 2026-06-21 13:47:17 +02:00
minimons 781d008923 3: Remove support for Docker 2026-06-21 13:47:17 +02:00
minimons f57c00b834 2: Enable support for autostart of JupiterPerpsAlarmImpl 2026-06-21 13:47:17 +02:00
minimons 804b640af5 X: Dependency update 2026-06-21 13:47:17 +02:00
32 changed files with 808 additions and 507 deletions
+2
View File
@@ -6,3 +6,5 @@ build
logs/*.log logs/*.log
logs/*.log.gz logs/*.log.gz
conf/*.conf
conf/*.xml
+2 -1
View File
@@ -39,11 +39,12 @@ val detag by configurations.creating {
} }
dependencies { dependencies {
detag("com.r35157.tools:detag-impl_ref:0.1-dev") detag("com.r35157.tools:detag-impl_ref:0.1.0")
compileOnly("org.jetbrains:annotations:26.1.0") compileOnly("org.jetbrains:annotations:26.1.0")
runtimeOnly("org.apache.logging.log4j:log4j-core:2.26.0") runtimeOnly("org.apache.logging.log4j:log4j-core:2.26.0")
runtimeOnly("org.apache.logging.log4j:log4j-slf4j2-impl:2.26.0") runtimeOnly("org.apache.logging.log4j:log4j-slf4j2-impl:2.26.0")
implementation("com.r35157.nenjim:hubd-api:0.1-dev")
implementation("com.fasterxml.jackson.core:jackson-databind:2.18.6") implementation("com.fasterxml.jackson.core:jackson-databind:2.18.6")
implementation("com.fazecast:jSerialComm:2.11.4") implementation("com.fazecast:jSerialComm:2.11.4")
implementation("com.google.code.gson:gson:2.14.0") implementation("com.google.code.gson:gson:2.14.0")
-4
View File
@@ -1,4 +0,0 @@
# Asset Direction Target TRIGGER SEVERITY NOTE
#############################################################
SOL BELOW 70.0 ONETIME 2 "ALARM: Risiko for Perps Solana long LIKVIDERING!"
SOL BELOW 71.4 ONETIME 2 "ALARM: Risiko for Solana Raydium LÅN LIKVIDERING!"
+8
View File
@@ -0,0 +1,8 @@
# Id Asset Direction Target Trigger Severity Note
######################################################################################################################
1 SOL ABOVE 97.03-1.25% ONETIME CRITICAL "CRITICAL: Risiko for Perps Solana short LIKVIDERING!"
2 SOL BELOW 48.72+1.25% ONETIME CRITICAL "CRITICAL: Risiko for Perps Solana long LIKVIDERING!"
3 BTC ABOVE 85032.87-1% ONETIME CRITICAL "CRITICAL: Risiko for Perps Bitcoin short LIKVIDERING!"
4 BTC BELOW 42779.40+1% ONETIME CRITICAL "CRITICAL: Risiko for Perps Bitcoin long LIKVIDERING!"
5 ETH ABOVE 2296.13-1% ONETIME CRITICAL "CRITICAL: Risiko for Perps Ethereum short LIKVIDERING!"
6 ETH BELOW 1155.19+1% ONETIME CRITICAL "CRITICAL: Risiko for Perps Ethereum long LIKVIDERING!"
-59
View File
@@ -1,59 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
JREVERSION=25.0.2-0
export VERSION=$(git describe --tags --exact-match 2>/dev/null \
|| git symbolic-ref --short -q HEAD \
|| git rev-parse --short HEAD)
GITHASH=$(git rev-parse --short=8 HEAD)
echo "Preparing Docker image for '${VERSION}'..."
mkdir -p build/docker/{conf,libs}
cp src/main/docker/run.sh build/docker/run.sh
cp libs/*.jar build/docker/libs/
cp conf/* build/docker/conf/
sed \
-e "s|_VERSION_|${VERSION}|g" \
-e "s|_JREVERSION_|${JREVERSION}|g" \
src/main/docker/Dockerfile_template \
> build/docker/Dockerfile
GROUP=r35157
NAME=nenjimhub
BASETAG=dockerreg.r35157.com/${GROUP}/${NAME}
HASHTAG=${BASETAG}:${GITHASH}_amd64
VERSIONTAG=${BASETAG}:${VERSION}_amd64
LATESTTAG=${BASETAG}:latest_amd64
echo "Building ${HASHTAG}..."
docker buildx build \
--load \
-t ${HASHTAG} \
build/docker
docker tag ${HASHTAG} ${VERSIONTAG}
docker tag ${HASHTAG} ${LATESTTAG}
sed \
-e "s|_HASHTAG_|${HASHTAG}|g" \
-e "s|_VERSIONTAG_|${VERSIONTAG}|g" \
-e "s|_LATESTTAG_|${LATESTTAG}|g" \
src/main/docker/publish_template.sh \
> build/docker/publish.sh
chmod 755 build/docker/publish.sh
echo ""
echo "Docker image ready:"
echo " ${HASHTAG}"
echo " ${VERSIONTAG}"
echo " ${LATESTTAG}"
-5
View File
@@ -5,10 +5,5 @@ export VERSION=$(git describe --tags --exact-match 2>/dev/null \
|| git symbolic-ref --short -q HEAD \ || git symbolic-ref --short -q HEAD \
|| git rev-parse --short HEAD) || git rev-parse --short HEAD)
./docker.sh
echo "Publishing artifact to local Maven repo ($VERSION)..." echo "Publishing artifact to local Maven repo ($VERSION)..."
./gradlew -Pversion=$VERSION publishToMavenLocal ./gradlew -Pversion=$VERSION publishToMavenLocal
echo "Publishing docker container to public 'dockerreg.r35157.com' ($VERSION)..."
./build/docker/publish.sh
-1
View File
@@ -1 +0,0 @@
src/main/docker/run.sh
Executable
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
jars=(libs/*.jar)
if (( ${#jars[@]} == 0 )); then
echo "ERROR: No JARs found in libs/" >&2
exit 1
fi
CLASSPATH=$(IFS=:; echo "${jars[*]}")
exec java \
--enable-preview \
-Dlog4j.configurationFile=conf/log4j2.xml \
-cp "$CLASSPATH" \
com.r35157.nenjim.hubd.impl.ref.Main
-43
View File
@@ -1,43 +0,0 @@
FROM dockerreg.r35157.com/r35157/jre:_JREVERSION__amd64
LABEL maintainer="Minimons <minimons@r35157.com>"
# Setup environment
ENV APP_HOME=/usr/local/software/nenjimhub
WORKDIR /usr/local/software
USER root
RUN mkdir nenjimhub-_VERSION_ \
&& ln -s nenjimhub-_VERSION_ nenjimhub
WORKDIR nenjimhub
RUN mkdir libs
# These dirs are expected to be overshadowed by host mounts
RUN mkdir conf logs && chown user:user logs
# Set timezone
ENV TZ=Europe/Copenhagen
RUN apt-get update
RUN apt-get install -y tzdata \
&& ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
&& echo $TZ > /etc/timezone \
&& dpkg-reconfigure -f noninteractive tzdata
# Clean-up
RUN apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install application and configuration (Do this as late as possible to be able to reuse layers between builds)
COPY run.sh .
COPY conf/* conf/
COPY libs/*.jar libs/
RUN chown root:root -R conf
RUN chmod 755 ./run.sh \
&& chmod -R a+rX /usr/local/software/nenjimhub-_VERSION_
USER user:user
CMD ["./run.sh"]
-9
View File
@@ -1,9 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
echo "Publishing '_HASHTAG_', '_VERSIONTAG_' and '_LATESTTAG_'..."
docker push _HASHTAG_
docker push _VERSIONTAG_
docker push _LATESTTAG_
echo "Publishing completed!"
-18
View File
@@ -1,18 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
jars=(libs/*.jar)
if (( ${#jars[@]} == 0 )); then
echo "ERROR: No JARs found in libs/" >&2
exit 1
fi
CLASSPATH=$(IFS=:; echo "${jars[*]}")
exec java \
--enable-preview \
-Dlog4j.configurationFile=conf/log4j2.xml \
-cp "$CLASSPATH" \
com.r35157.nenjim.hubd.impl.ref.Main
-19
View File
@@ -1,19 +0,0 @@
version: '3.9'
services:
nenjimhub:
hostname: nenjimhub
image: dockerreg.r35157.com/r35157/nenjimhub:latest
deploy:
replicas: 1
resources:
limits:
memory: 128m
restart_policy:
condition: any
delay: 5s
max_attempts: 3
window: 120s
volumes:
- /home/op/nenjimhub/conf:/usr/local/software/nenjimhub/conf
- /home/op/nenjimhub/logs:/usr/local/software/nenjimhub/logs
-3
View File
@@ -1,3 +0,0 @@
#!/bin/bash
docker stack deploy --compose-file /home/op/stack.yml stack --detach=true
@@ -0,0 +1,10 @@
package com.r35157.jupiterperpsalarm;
public enum AlarmSeverity {
EMERGENCY, // Repeated wake-up alarm - sound always
CRITICAL, // One-shot wake-up alarm - sound always
WARN, // Audible warning - respecting quiet hours though
INFO, // Normal notification with visual and audible feedback
SILENT, // Low-priority notification - visual feedback without sound/vibration
GHOST // No visual/audible notification - only visible inside the Pushover app
}
@@ -1,28 +1,35 @@
package com.r35157.jupiterperpsalarm.impl.ref; package com.r35157.jupiterperpsalarm.impl.ref;
import com.r35157.jupiterperpsalarm.AlarmSeverity;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Locale;
public final class AlarmConfigurationParser { public final class AlarmConfigurationParser {
public static List<PriceAlarmDefinition> parse(Path path) throws IOException { public static List<PriceAlarmDefinition> parse(Path path) throws IOException {
List<String> lines = Files.readAllLines(path); List<String> lines = Files.readAllLines(path);
List<PriceAlarmDefinition> alarms = new ArrayList<>(); List<PriceAlarmDefinition> alarms = new ArrayList<>();
Map<String, String> constants = new LinkedHashMap<>();
for (int lineNumber = 1; lineNumber <= lines.size(); lineNumber++) { for (int lineNumber = 1; lineNumber <= lines.size(); lineNumber++) {
String line = lines.get(lineNumber - 1); String line = lines.get(lineNumber - 1);
String trimmed = line.trim(); String trimmed = line.trim();
if (trimmed.isEmpty() || trimmed.startsWith("#")) {
if(trimmed.isEmpty() || trimmed.startsWith("#")) {
continue; continue;
} }
try { try {
alarms.add(parseLine(line)); if(isConstantDefinition(trimmed)) {
parseConstantDefinition(constants, line);
} else {
String resolvedLine = replaceConstants(line, constants);
alarms.add(parseLine(resolvedLine));
}
} catch (RuntimeException exception) { } catch (RuntimeException exception) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
path + ":" + lineNumber + ": " + exception.getMessage(), path + ":" + lineNumber + ": " + exception.getMessage(),
@@ -41,18 +48,26 @@ public final class AlarmConfigurationParser {
static PriceAlarmDefinition parseLine(String line) { static PriceAlarmDefinition parseLine(String line) {
Cursor cursor = new Cursor(line); Cursor cursor = new Cursor(line);
int id = Integer.parseInt(cursor.nextToken("id"));
JupiterPerpsAsset asset = JupiterPerpsAsset.valueOf( JupiterPerpsAsset asset = JupiterPerpsAsset.valueOf(
cursor.nextToken("asset").toUpperCase(Locale.ROOT) cursor.nextToken("asset").toUpperCase(Locale.ROOT)
); );
PriceDirection direction = PriceDirection.valueOf( PriceDirection direction = PriceDirection.valueOf(
cursor.nextToken("direction").toUpperCase(Locale.ROOT) cursor.nextToken("direction").toUpperCase(Locale.ROOT)
); );
BigDecimal target = new BigDecimal(cursor.nextToken("target")); BigDecimal target = parseTarget(cursor.nextToken("target"));
AlarmTrigger trigger = AlarmTrigger.valueOf(
cursor.nextToken("trigger").toUpperCase(Locale.ROOT) TriggerConfiguration triggerConfiguration = parseTrigger(
cursor.nextToken("trigger")
); );
int severity = Integer.parseInt(cursor.nextToken("severity"));
AlarmSeverity severity = AlarmSeverity.valueOf(
cursor.nextToken("severity").toUpperCase()
);
String note = cursor.nextQuotedString("note"); String note = cursor.nextQuotedString("note");
cursor.skipWhitespace(); cursor.skipWhitespace();
@@ -63,15 +78,112 @@ public final class AlarmConfigurationParser {
} }
return new PriceAlarmDefinition( return new PriceAlarmDefinition(
id,
asset, asset,
direction, direction,
target, target,
trigger, triggerConfiguration.trigger(),
triggerConfiguration.gracePeriod(),
severity, severity,
note note
); );
} }
private static BigDecimal parseTargetPercentage(
String targetStr,
int operatorIndex,
boolean add
) {
BigDecimal base = new BigDecimal(targetStr.substring(0, operatorIndex));
String percentStr = targetStr.substring(operatorIndex + 1, targetStr.length() - 1);
BigDecimal percent = new BigDecimal(percentStr);
BigDecimal delta = base
.multiply(percent)
.divide(BigDecimal.valueOf(100));
BigDecimal target = add ? base.add(delta) : base.subtract(delta);
validateTarget(base, targetStr);
validateTarget(percent, targetStr);
validateTarget(target, targetStr);
return target;
}
private static TriggerConfiguration parseTrigger(String triggerText) {
String normalized = triggerText.toUpperCase(Locale.ROOT);
if (normalized.equals("ONETIME")) {
return new TriggerConfiguration(AlarmTrigger.ONETIME, 0);
}
if (normalized.equals("PERSISTENT")) {
return new TriggerConfiguration(AlarmTrigger.PERSISTENT, 0);
}
if (normalized.startsWith("PERSISTENT:")) {
String graceText = normalized.substring("PERSISTENT:".length());
if (graceText.isEmpty()) {
throw new IllegalArgumentException("Missing persistent grace period: " + triggerText);
}
ΩsecondsΩ gracePeriodSeconds;
try {
gracePeriodSeconds = Integer.parseInt(graceText);
} catch (NumberFormatException exception) {
throw new IllegalArgumentException(
"Invalid persistent grace period: " + triggerText,
exception
);
}
if (gracePeriodSeconds < 0) {
throw new IllegalArgumentException(
"Persistent grace period cannot be negative: " + triggerText
);
}
return new TriggerConfiguration(AlarmTrigger.PERSISTENT, gracePeriodSeconds);
}
if (normalized.startsWith("ONETIME:")) {
throw new IllegalArgumentException("ONETIME cannot have a grace period: " + triggerText);
}
throw new IllegalArgumentException("Unknown trigger: " + triggerText);
}
private static void validateTarget(BigDecimal target, String originalTargetStr) {
if (target.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException(
"Target must be zero or positive: " + originalTargetStr
);
}
}
private static BigDecimal parseTarget(String targetStr) {
String trimmedTargetStr = targetStr.trim();
if (trimmedTargetStr.endsWith("%")) {
int plusIndex = trimmedTargetStr.indexOf('+');
int minusIndex = trimmedTargetStr.indexOf('-', 1);
if (plusIndex >= 0) {
return parseTargetPercentage(trimmedTargetStr, plusIndex, true);
}
if (minusIndex >= 0) {
return parseTargetPercentage(trimmedTargetStr, minusIndex, false);
}
}
BigDecimal target = new BigDecimal(trimmedTargetStr);
validateTarget(target, targetStr);
return target;
}
private static final class Cursor { private static final class Cursor {
private Cursor(String line) { private Cursor(String line) {
this.line = line; this.line = line;
@@ -156,6 +268,64 @@ public final class AlarmConfigurationParser {
private int position; private int position;
} }
private static boolean isConstantDefinition(String trimmedLine) {
return trimmedLine.startsWith("{{");
}
private static void parseConstantDefinition(
Map<String, String> constants,
String line
) {
Cursor cursor = new Cursor(line);
String name = cursor.nextToken("constant name");
String value = cursor.nextToken("constant value");
cursor.skipWhitespace();
if (!cursor.atEnd() && cursor.current() != '#') {
throw new IllegalArgumentException(
"Unexpected text after constant value: " + cursor.remaining()
);
}
validateConstantName(name);
if (constants.putIfAbsent(name, value) != null) {
throw new IllegalArgumentException("Duplicate constant: " + name);
}
}
private static String replaceConstants(
String line,
Map<String, String> constants
) {
String result = line;
for (Map.Entry<String, String> entry : constants.entrySet()) {
result = result.replace(entry.getKey(), entry.getValue());
}
if (result.contains("{{") || result.contains("}}")) {
throw new IllegalArgumentException(
"Unknown or malformed constant in line: " + line
);
}
return result;
}
private static void validateConstantName(String name) {
if (!name.matches("\\{\\{[A-Z0-9_]+}}")) {
throw new IllegalArgumentException("Invalid constant name: " + name);
}
}
private record TriggerConfiguration(
AlarmTrigger trigger,
ΩsecondsΩ gracePeriod
) {
}
private AlarmConfigurationParser() { private AlarmConfigurationParser() {
} }
} }
@@ -48,25 +48,6 @@ public final class DovesAgPriceFeedDecoder {
); );
} }
public static void selfTest() {
byte[] data = new byte[ACCOUNT_SIZE];
System.arraycopy(AG_PRICE_FEED_DISCRIMINATOR, 0, data, 0, 8);
putUnsignedLongLittleEndian(data, PRICE_OFFSET, new BigInteger("123456789"));
data[EXPONENT_OFFSET] = (byte) -6;
ByteBuffer.wrap(data, TIMESTAMP_OFFSET, Long.BYTES)
.order(ByteOrder.LITTLE_ENDIAN)
.putLong(1_700_000_000L);
OraclePrice decoded = decode(JupiterPerpsAsset.SOL, data, 42L, "self-test");
if (decoded.priceUsd().compareTo(new BigDecimal("123.456789")) != 0) {
throw new IllegalStateException("Decoder self-test failed: " + decoded.priceUsd());
}
if (!decoded.oracleTime().equals(Instant.ofEpochSecond(1_700_000_000L))) {
throw new IllegalStateException("Timestamp self-test failed");
}
}
private static BigInteger readUnsignedLongLittleEndian(byte[] data, int offset) { private static BigInteger readUnsignedLongLittleEndian(byte[] data, int offset) {
byte[] positiveBigEndian = new byte[Long.BYTES + 1]; byte[] positiveBigEndian = new byte[Long.BYTES + 1];
for (int index = 0; index < Long.BYTES; index++) { for (int index = 0; index < Long.BYTES; index++) {
@@ -16,25 +16,17 @@ public final class JupiterPerpsAlarmImpl {
try { try {
config = Config.parse(args, System.getenv()); config = Config.parse(args, System.getenv());
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
System.err.println(exception.getMessage());
printUsage(); printUsage();
System.exit(2); String errMsg = "Could not parse configuration for JupiterPerpsAlarm: " + exception.getMessage() + "!";
return; throw new IllegalStateException(errMsg, exception);
}
if (config.selfTest()) {
SelfTest.run();
System.out.println("All self-tests passed.");
return;
} }
List<PriceAlarmDefinition> definitions; List<PriceAlarmDefinition> definitions;
try { try {
definitions = AlarmConfigurationParser.parse(config.alarmConfiguration()); definitions = AlarmConfigurationParser.parse(config.alarmConfiguration());
} catch (Exception exception) { } catch (Exception exception) {
System.err.println("Could not load alarm configuration: " + exception.getMessage()); String errMsg = "Could not load alarm configuration: " + exception.getMessage() + "!";
System.exit(2); throw new IllegalStateException(errMsg, exception);
return;
} }
List<AlarmAction> actions = new ArrayList<>(); List<AlarmAction> actions = new ArrayList<>();
@@ -91,7 +83,7 @@ public final class JupiterPerpsAlarmImpl {
asset.oracleAccount() asset.oracleAccount()
); );
assetDefinitions.forEach(definition -> System.out.printf( assetDefinitions.forEach(definition -> System.out.printf(
" %s %s USD, %s, severity=%d%n", " %s %s USD, %s, severity=%s%n",
definition.direction(), definition.direction(),
definition.target().toPlainString(), definition.target().toPlainString(),
definition.trigger(), definition.trigger(),
@@ -128,7 +120,6 @@ public final class JupiterPerpsAlarmImpl {
Options: Options:
--config=<path> Default: price-alarms.conf --config=<path> Default: price-alarms.conf
--ws=<url1,url2,...> Default: wss://api.mainnet-beta.solana.com --ws=<url1,url2,...> Default: wss://api.mainnet-beta.solana.com
--self-test Test parser, alarm semantics and oracle decoder
Environment: Environment:
PRICE_ALARMS_CONFIG Alternative default configuration path PRICE_ALARMS_CONFIG Alternative default configuration path
@@ -142,11 +133,9 @@ public final class JupiterPerpsAlarmImpl {
Path alarmConfiguration, Path alarmConfiguration,
List<URI> webSocketEndpoints, List<URI> webSocketEndpoints,
String pushoverToken, String pushoverToken,
String pushoverUserKey, String pushoverUserKey
boolean selfTest
) { ) {
private static Config parse(String[] args, Map<String, String> environment) { private static Config parse(String[] args, Map<String, String> environment) {
boolean selfTest = Arrays.asList(args).contains("--self-test");
Map<String, String> options = Arrays.stream(args) Map<String, String> options = Arrays.stream(args)
.filter(argument -> argument.startsWith("--") && argument.contains("=")) .filter(argument -> argument.startsWith("--") && argument.contains("="))
.map(argument -> argument.substring(2).split("=", 2)) .map(argument -> argument.substring(2).split("=", 2))
@@ -186,8 +175,7 @@ public final class JupiterPerpsAlarmImpl {
Path.of(configurationText), Path.of(configurationText),
endpoints, endpoints,
blankToNull(environment.get("PUSHOVER_APP_TOKEN")), blankToNull(environment.get("PUSHOVER_APP_TOKEN")),
blankToNull(environment.get("PUSHOVER_USER_KEY")), blankToNull(environment.get("PUSHOVER_USER_KEY"))
selfTest
); );
} }
@@ -1,5 +1,7 @@
package com.r35157.jupiterperpsalarm.impl.ref; package com.r35157.jupiterperpsalarm.impl.ref;
import java.time.Instant;
public final class PriceAlarm { public final class PriceAlarm {
public PriceAlarm(PriceAlarmDefinition definition, AlarmAction action) { public PriceAlarm(PriceAlarmDefinition definition, AlarmAction action) {
@@ -25,16 +27,28 @@ public final class PriceAlarm {
previousReached = reached; previousReached = reached;
if (!enteredTriggeredSide) { if (!reached) {
previousReached = false;
return; return;
} }
if (definition.trigger() == AlarmTrigger.ONETIME && triggerCount > 0) { if (definition.trigger() == AlarmTrigger.ONETIME) {
if (!enteredTriggeredSide || triggerCount > 0) {
return; return;
} }
triggerCount++; trigger(price);
action.trigger(price, definition); return;
}
if (definition.trigger() == AlarmTrigger.PERSISTENT) {
if (lastTriggeredAt == null || persistentGracePeriodHasPassed()) {
trigger(price);
}
return;
}
throw new IllegalStateException("Unsupported alarm trigger: " + definition.trigger());
} }
public PriceAlarmDefinition definition() { public PriceAlarmDefinition definition() {
@@ -45,9 +59,32 @@ public final class PriceAlarm {
return triggerCount; return triggerCount;
} }
private boolean persistentGracePeriodHasPassed() {
if (lastTriggeredAt == null) {
return true;
}
ΩsecondsΩ gracePeriod = definition.triggerGracePeriod();
if (gracePeriod == 0) {
return true;
}
return !Instant.now().isBefore(
lastTriggeredAt.plusSeconds(gracePeriod)
);
}
private void trigger(OraclePrice price) {
triggerCount++;
lastTriggeredAt = Instant.now();
action.trigger(price, definition);
}
private final PriceAlarmDefinition definition; private final PriceAlarmDefinition definition;
private final AlarmAction action; private final AlarmAction action;
private Instant lastTriggeredAt;
private Boolean previousReached; private Boolean previousReached;
private long triggerCount; private long triggerCount;
} }
@@ -1,14 +1,18 @@
package com.r35157.jupiterperpsalarm.impl.ref; package com.r35157.jupiterperpsalarm.impl.ref;
import com.r35157.jupiterperpsalarm.AlarmSeverity;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Objects; import java.util.Objects;
public record PriceAlarmDefinition( public record PriceAlarmDefinition(
int id,
JupiterPerpsAsset asset, JupiterPerpsAsset asset,
PriceDirection direction, PriceDirection direction,
BigDecimal target, BigDecimal target,
AlarmTrigger trigger, AlarmTrigger trigger,
int severity, ΩsecondsΩ triggerGracePeriod,
AlarmSeverity severity,
String note String note
) { ) {
public PriceAlarmDefinition { public PriceAlarmDefinition {
@@ -18,11 +22,14 @@ public record PriceAlarmDefinition(
Objects.requireNonNull(trigger, "trigger"); Objects.requireNonNull(trigger, "trigger");
Objects.requireNonNull(note, "note"); Objects.requireNonNull(note, "note");
if (target.signum() <= 0) { if (target.signum() < 0) {
throw new IllegalArgumentException("Target price must be positive"); throw new IllegalArgumentException("Target price cannot be negative (was: " + target.signum() + ")!");
} }
if (severity < 0) {
throw new IllegalArgumentException("Severity must be zero or positive"); if (triggerGracePeriod < 0) {
throw new IllegalArgumentException(
"Trigger grace period cannot be negative: " + triggerGracePeriod
);
} }
} }
} }
@@ -1,5 +1,7 @@
package com.r35157.jupiterperpsalarm.impl.ref; package com.r35157.jupiterperpsalarm.impl.ref;
import com.r35157.jupiterperpsalarm.AlarmSeverity;
import java.net.URI; import java.net.URI;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.net.http.HttpClient; import java.net.http.HttpClient;
@@ -18,23 +20,13 @@ public final class PushoverAlarmAction implements AlarmAction {
@Override @Override
public void trigger(OraclePrice price, PriceAlarmDefinition alarm) { public void trigger(OraclePrice price, PriceAlarmDefinition alarm) {
String title = "Jupiter Perps " + price.asset() + " alarm"; String title = "Jupiter Perps " + price.asset() + " alarm";
String message = String.format( String message = createMessage(price, alarm);
"%s is %s USD. Target: %s %s USD. Oracle time: %s. Slot: %d.",
price.asset(),
price.priceUsd().toPlainString(),
alarm.direction(),
alarm.target().toPlainString(),
price.oracleTime(),
price.slot()
);
// Severity and note are intentionally parsed and retained in the model,
// but are not used by Pushover yet.
String body = form("token", applicationToken) + "&" + String body = form("token", applicationToken) + "&" +
form("user", userKey) + "&" + form("user", userKey) + "&" +
form("title", title) + "&" + form("title", title) + "&" +
form("message", message) + "&" + form("message", message) + "&" +
"priority=2&retry=30&expire=10800&sound=persistent"; createPushoverSeverityParameters(alarm.severity());;
HttpRequest request = HttpRequest.newBuilder(PUSHOVER_URI) HttpRequest request = HttpRequest.newBuilder(PUSHOVER_URI)
.timeout(Duration.ofSeconds(15)) .timeout(Duration.ofSeconds(15))
@@ -56,7 +48,7 @@ public final class PushoverAlarmAction implements AlarmAction {
response.body() response.body()
); );
} else { } else {
System.out.println("Pushover emergency alarm sent."); System.out.println("Pushover alarm sent: " + alarm.severity());
} }
}); });
} }
@@ -66,6 +58,32 @@ public final class PushoverAlarmAction implements AlarmAction {
URLEncoder.encode(value, StandardCharsets.UTF_8); URLEncoder.encode(value, StandardCharsets.UTF_8);
} }
private static String createPushoverSeverityParameters(AlarmSeverity severity) {
return switch (severity) {
case EMERGENCY -> "priority=2&retry=30&expire=10800&sound=persistent";
case CRITICAL -> "priority=1&sound=spacealarm";
case WARN -> "priority=0&sound=siren";
case INFO -> "priority=0";
case SILENT -> "priority=-1";
case GHOST -> "priority=-2";
};
}
private static String createMessage(OraclePrice price, PriceAlarmDefinition alarm) {
return String.format(
"%d - %s: %s%n%n%s is %s USD.%nTarget: %s %s USD.%nOracle time: %s.%nSlot: %d.",
alarm.id(),
alarm.severity(),
alarm.note(),
price.asset(),
price.priceUsd().toPlainString(),
alarm.direction(),
alarm.target().toPlainString(),
price.oracleTime(),
price.slot()
);
}
private final HttpClient httpClient = HttpClient.newHttpClient(); private final HttpClient httpClient = HttpClient.newHttpClient();
private final String applicationToken; private final String applicationToken;
private final String userKey; private final String userKey;
@@ -1,118 +0,0 @@
package com.r35157.jupiterperpsalarm.impl.ref;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
public final class SelfTest {
public static void run() {
DovesAgPriceFeedDecoder.selfTest();
configurationParserTest();
oneTimeAlarmTest();
persistentAlarmTest();
initialTriggerTest();
}
private static void configurationParserTest() {
PriceAlarmDefinition alarm = AlarmConfigurationParser.parseLine(
"SOL ABOVE 75.7 ONETIME 2 \"ALARM: Risiko for likvidering!\""
);
require(alarm.asset() == JupiterPerpsAsset.SOL, "Asset parsing failed");
require(alarm.direction() == PriceDirection.ABOVE, "Direction parsing failed");
require(alarm.target().compareTo(new BigDecimal("75.7")) == 0, "Target parsing failed");
require(alarm.trigger() == AlarmTrigger.ONETIME, "Trigger parsing failed");
require(alarm.severity() == 2, "Severity parsing failed");
require(alarm.note().equals("ALARM: Risiko for likvidering!"), "Note parsing failed");
}
private static void oneTimeAlarmTest() {
List<BigDecimal> triggeredPrices = new ArrayList<>();
PriceAlarm alarm = new PriceAlarm(
definition(PriceDirection.ABOVE, "100", AlarmTrigger.ONETIME),
(price, definition) -> triggeredPrices.add(price.priceUsd())
);
accept(alarm, "90");
accept(alarm, "100");
accept(alarm, "110");
accept(alarm, "90");
accept(alarm, "101");
require(triggeredPrices.equals(List.of(new BigDecimal("100"))), "ONETIME semantics failed");
}
private static void persistentAlarmTest() {
List<BigDecimal> triggeredPrices = new ArrayList<>();
PriceAlarm alarm = new PriceAlarm(
definition(PriceDirection.BELOW, "100", AlarmTrigger.PERSISTENT),
(price, definition) -> triggeredPrices.add(price.priceUsd())
);
accept(alarm, "110");
accept(alarm, "99");
accept(alarm, "98");
accept(alarm, "101");
accept(alarm, "100");
require(
triggeredPrices.equals(List.of(new BigDecimal("99"), new BigDecimal("100"))),
"PERSISTENT crossing semantics failed"
);
}
private static void initialTriggerTest() {
List<BigDecimal> triggeredPrices = new ArrayList<>();
PriceAlarm alarm = new PriceAlarm(
definition(PriceDirection.ABOVE, "100", AlarmTrigger.PERSISTENT),
(price, definition) -> triggeredPrices.add(price.priceUsd())
);
accept(alarm, "105");
accept(alarm, "106");
require(
triggeredPrices.equals(List.of(new BigDecimal("105"))),
"Initial satisfied alarm did not trigger exactly once"
);
}
private static PriceAlarmDefinition definition(
PriceDirection direction,
String target,
AlarmTrigger trigger
) {
return new PriceAlarmDefinition(
JupiterPerpsAsset.SOL,
direction,
new BigDecimal(target),
trigger,
2,
"ignored for now"
);
}
private static void accept(PriceAlarm alarm, String price) {
BigDecimal decimal = new BigDecimal(price);
alarm.accept(new OraclePrice(
JupiterPerpsAsset.SOL,
decimal.unscaledValue().abs(),
-decimal.scale(),
decimal,
Instant.ofEpochSecond(1_700_000_000L),
1L,
"self-test"
));
}
private static void require(boolean condition, String message) {
if (!condition) {
throw new IllegalStateException(message);
}
}
private SelfTest() {
}
}
@@ -0,0 +1,2 @@
package com.r35157.jupiterperpsalarm.impl.ref;
@@ -0,0 +1,70 @@
package com.r35157.libs.codec.impl.ref;
import com.r35157.libs.codec.Base58Codec;
public class Base58CodecImpl implements Base58Codec {
public String encode(byte[] input) {
if (input.length == 0) {
return "";
}
byte[] copy = input.clone();
int zeros = 0;
while (zeros < copy.length && copy[zeros] == 0) {
zeros++;
}
char[] encoded = new char[copy.length * 2];
int outputStart = encoded.length;
int inputStart = zeros;
while (inputStart < copy.length) {
int remainder = divmod58(
copy,
inputStart
);
if (copy[inputStart] == 0) {
inputStart++;
}
encoded[--outputStart] = ALPHABET[remainder];
}
while (outputStart < encoded.length && encoded[outputStart] == ENCODED_ZERO) {
outputStart++;
}
while (zeros-- > 0) {
encoded[--outputStart] = ENCODED_ZERO;
}
return new String(
encoded,
outputStart,
encoded.length - outputStart
);
}
private static int divmod58(byte[] number, int startAt) {
int remainder = 0;
for (int i = startAt; i < number.length; i++) {
int digit = number[i] & 0xff;
int temp = remainder * 256 + digit;
number[i] = (byte) (temp / 58);
remainder = temp % 58;
}
return remainder;
}
private static final char[] ALPHABET =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
private static final char ENCODED_ZERO =
ALPHABET[0];
}
@@ -0,0 +1,50 @@
package com.r35157.libs.jupiter.perps.impl.anchoridl;
import com.r35157.libs.codec.Base58Codec;
import com.r35157.libs.codec.impl.ref.Base58CodecImpl;
import com.r35157.libs.solana.SolanaAccountInfo;
import java.util.Base64;
class AnchorIdlJupiterPerpsCustodyDecoder {
ΩSPLMintAddressΩ decodeMint(
SolanaAccountInfo custodyAccountInfo
) {
byte[] data = Base64.getDecoder().decode(custodyAccountInfo.dataBase64());
if (data.length < MINT_OFFSET + PUBLIC_KEY_LENGTH) {
throw new IllegalArgumentException(
"Jupiter Perps custody account data is too short: " + data.length
);
}
return readPublicKey(data, MINT_OFFSET);
}
private ΩSPLMintAddressΩ readPublicKey(
byte[] data,
int offset
) {
byte[] publicKeyBytes = new byte[PUBLIC_KEY_LENGTH];
System.arraycopy(
data,
offset,
publicKeyBytes,
0,
PUBLIC_KEY_LENGTH
);
return base58.encode(publicKeyBytes);
}
private static final int ANCHOR_DISCRIMINATOR_LENGTH = 8;
private static final int PUBLIC_KEY_LENGTH = 32;
private static final int MINT_OFFSET =
ANCHOR_DISCRIMINATOR_LENGTH
+ PUBLIC_KEY_LENGTH; // pool
private static final Base58Codec base58 = new Base58CodecImpl();
}
@@ -0,0 +1,114 @@
package com.r35157.libs.jupiter.perps.impl.anchoridl;
import com.r35157.libs.codec.Base58Codec;
import com.r35157.libs.codec.impl.ref.Base58CodecImpl;
import com.r35157.libs.jupiter.perps.JupiterPerpsPosition;
import com.r35157.libs.jupiter.perps.JupiterPerpsPositionDirection;
import com.r35157.libs.solana.SolanaAccountInfo;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Base64;
class AnchorIdlJupiterPerpsPositionDecoder {
JupiterPerpsPosition decode(
ΩJupiterPerpsPositionAccountΩ positionAccount,
SolanaAccountInfo accountInfo,
ΩSPLMintAddressΩ tradedTokenMint
) {
byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64());
if (data.length < PRICE_OFFSET + U64_LENGTH) {
throw new IllegalArgumentException(
"Jupiter Perps position account data is too short: " + data.length
);
}
JupiterPerpsPositionDirection direction =
decodeDirection(data[SIDE_OFFSET]);
long rawEntryPrice = ByteBuffer
.wrap(data, PRICE_OFFSET, U64_LENGTH)
.order(ByteOrder.LITTLE_ENDIAN)
.getLong();
ΩUSDCPriceΩ entryPrice = BigDecimal
.valueOf(rawEntryPrice)
.movePointLeft(6);
JupiterPerpsPosition pos = new JupiterPerpsPosition(
positionAccount,
entryPrice,
direction,
tradedTokenMint
);
return pos;
}
ΩSolanaAddressΩ decodeCustodyAccount(SolanaAccountInfo accountInfo) {
byte[] data = Base64.getDecoder().decode(accountInfo.dataBase64());
if (data.length < CUSTODY_OFFSET + PUBLIC_KEY_LENGTH) {
throw new IllegalArgumentException(
"Jupiter Perps position account data is too short: " + data.length
);
}
return readPublicKey(
data,
CUSTODY_OFFSET
);
}
private JupiterPerpsPositionDirection decodeDirection(
byte rawSide
) {
// Jupiter Perps position side values are encoded as 1 = LONG, 2 = SHORT.
JupiterPerpsPositionDirection direction = switch (rawSide) {
case 1 -> JupiterPerpsPositionDirection.LONG;
case 2 -> JupiterPerpsPositionDirection.SHORT;
default -> throw new IllegalArgumentException(
"Unknown Jupiter Perps position side: " + rawSide
);
};
return direction;
}
private ΩSolanaAddressΩ readPublicKey(
byte[] data,
int offset
) {
byte[] publicKeyBytes = new byte[PUBLIC_KEY_LENGTH];
System.arraycopy(
data,
offset,
publicKeyBytes,
0,
PUBLIC_KEY_LENGTH
);
return base58.encode(publicKeyBytes);
}
private static final int ANCHOR_DISCRIMINATOR_LENGTH = 8;
private static final int PUBLIC_KEY_LENGTH = 32;
private static final int I64_LENGTH = 8;
private static final int SIDE_ENUM_LENGTH = 1;
private static final int U64_LENGTH = 8;
private static final int OWNER_OFFSET = ANCHOR_DISCRIMINATOR_LENGTH;
private static final int POOL_OFFSET = OWNER_OFFSET + PUBLIC_KEY_LENGTH;
private static final int CUSTODY_OFFSET = POOL_OFFSET + PUBLIC_KEY_LENGTH;
private static final int COLLATERAL_CUSTODY_OFFSET = CUSTODY_OFFSET + PUBLIC_KEY_LENGTH; // custody
private static final int OPEN_TIME_OFFSET = COLLATERAL_CUSTODY_OFFSET + PUBLIC_KEY_LENGTH;
private static final int UPDATE_TIME_OFFSET = OPEN_TIME_OFFSET + I64_LENGTH; // openTime
private static final int SIDE_OFFSET = UPDATE_TIME_OFFSET + I64_LENGTH;
private static final int PRICE_OFFSET = SIDE_OFFSET + SIDE_ENUM_LENGTH;
private static final Base58Codec base58 = new Base58CodecImpl();
}
@@ -0,0 +1,99 @@
package com.r35157.libs.jupiter.perps.impl.anchoridl;
import com.r35157.libs.jupiter.perps.JupiterPerpsPosition;
import com.r35157.libs.jupiter.perps.JupiterPerpsService;
import com.r35157.libs.solana.SolanaAccountInfo;
import com.r35157.libs.solana.SolanaBlockChain;
import com.r35157.libs.solana.SolanaProgramAccountMemcmpFilter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class AnchorIdlJupiterPerpsServiceImpl implements JupiterPerpsService {
public AnchorIdlJupiterPerpsServiceImpl(
SolanaBlockChain solanaBlockChain
) {
this.solanaBlockChain = solanaBlockChain;
this.positionDecoder = new AnchorIdlJupiterPerpsPositionDecoder();
this.custodyDecoder = new AnchorIdlJupiterPerpsCustodyDecoder();
}
@Override
public JupiterPerpsPosition getPosition(ΩJupiterPerpsPositionAccountΩ positionAccount)
throws IOException, InterruptedException {
SolanaAccountInfo accountInfo = solanaBlockChain.getAccountInfo(positionAccount);
if (accountInfo == null) {
throw new IllegalArgumentException("Jupiter Perps position account does not exist: " + positionAccount);
}
if (!JUPITER_PERPS_PROGRAM_ID.equals(accountInfo.owner())) {
throw new IllegalArgumentException(
"Account is not owned by Jupiter Perps program: " + positionAccount
);
}
ΩSPLMintAddressΩ tradedTokenMint = getTradedTokenMint(accountInfo);
JupiterPerpsPosition pos = positionDecoder.decode(positionAccount, accountInfo, tradedTokenMint);
return pos;
}
@Override
public Set<JupiterPerpsPosition> getOpenPositions(ΩSolanaWalletIdΩ owner)
throws IOException, InterruptedException {
Set<SolanaAccountInfo> accountInfos = solanaBlockChain.getProgramAccounts(
JUPITER_PERPS_PROGRAM_ID,
Set.of(new SolanaProgramAccountMemcmpFilter(
POSITION_OWNER_OFFSET,
owner
))
);
Set<JupiterPerpsPosition> positions = new HashSet<>();
for (SolanaAccountInfo accountInfo : accountInfos) {
ΩSolanaAddressΩ address = accountInfo.address();
ΩSolanaProgramIdΩ programId = accountInfo.owner();
if (!JUPITER_PERPS_PROGRAM_ID.equals(programId)) {
String errorMsg = "Account '" + address + "' is not owned by Jupiter Perps program '" +
programId + "'";
throw new IllegalArgumentException(errorMsg);
}
ΩSPLMintAddressΩ tradedTokenMint = getTradedTokenMint(accountInfo);
JupiterPerpsPosition position = positionDecoder.decode(address, accountInfo, tradedTokenMint);
positions.add(position);
}
return Set.copyOf(positions);
}
private ΩSPLMintAddressΩ getTradedTokenMint(SolanaAccountInfo positionAccountInfo)
throws IOException, InterruptedException
{
ΩSolanaAddressΩ custodyAccount = positionDecoder.decodeCustodyAccount(positionAccountInfo);
SolanaAccountInfo custodyAccountInfo = solanaBlockChain.getAccountInfo(custodyAccount);
if (custodyAccountInfo == null) {
throw new IllegalArgumentException(
"Jupiter Perps custody account does not exist: " + custodyAccount
);
}
ΩSPLMintAddressΩ mintAddress = custodyDecoder.decodeMint(custodyAccountInfo);
return mintAddress;
}
private static final ΩJupiterPerpsProgramIdΩ JUPITER_PERPS_PROGRAM_ID =
"PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu";
private static final int POSITION_OWNER_OFFSET = 8;
private final SolanaBlockChain solanaBlockChain;
private final AnchorIdlJupiterPerpsPositionDecoder positionDecoder;
private final AnchorIdlJupiterPerpsCustodyDecoder custodyDecoder;
}
@@ -1,146 +0,0 @@
package com.r35157.libs.solana;
import com.r35157.libs.solana.valuetypes.SolanaProgramDerivedAddress;
import com.r35157.libs.solana.valuetypes.economic.SolanaSPLTokenProgram;
import com.r35157.libs.valuetypes.basic.MoneyAmount;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Provides read-oriented access to the Solana blockchain.
*
* <p>This interface exposes the Solana operations needed by higher-level
* integrations. It can fetch native SOL balances, SPL token holdings, NFT-like
* token holding candidates, account information and program derived addresses.</p>
*
* <p>The interface is intentionally generic and does not contain Raydium-specific
* logic. Higher-level integrations are expected to interpret Solana accounts,
* token holdings and derived addresses according to their own domain rules.</p>
*/
public interface SolanaBlockChain {
/**
* Fetches the native SOL balance for a Solana address.
*
* @param address the Solana address to inspect
* @return the native SOL balance for the address
* @throws IOException if the balance could not be fetched or parsed
* @throws InterruptedException if the calling thread is interrupted while fetching the balance
*/
ΩSolanaAmountΩ getBalanceInSolana(ΩSolanaAddressΩ address) throws IOException, InterruptedException;
/**
* Fetches the native SOL balance for a Solana address in lamports.
*
* <p>Lamports are the smallest unit of native SOL.</p>
*
* @param address the Solana address to inspect
* @return the native SOL balance for the address in lamports
* @throws IOException if the balance could not be fetched or parsed
* @throws InterruptedException if the calling thread is interrupted while fetching the balance
*/
ΩlamportsΩ getBalanceInLamport(ΩSolanaAddressΩ address) throws IOException, InterruptedException;
/**
* Fetches SPL token holdings owned by a Solana address for a specific token program.
*
* <p>The supplied token program decides which token accounts are inspected. For example,
* callers may query the original SPL Token Program or the Token-2022 Program depending
* on which token accounts they need to discover.</p>
*
* @param ownerAddress the Solana owner address whose token holdings should be inspected
* @param splProgramId the SPL token program to query
* @return a map of SPL mint addresses to token holding information
* @throws IOException if the token holdings could not be fetched or parsed
* @throws InterruptedException if the calling thread is interrupted while fetching token holdings
*/
Map<ΩSPLMintAddressΩ, SPLTokenHolding> getSPLTokenHoldings(
ΩSolanaAddressΩ ownerAddress,
SolanaSPLTokenProgram splProgramId
) throws IOException, InterruptedException;
/**
* Fetches NFT-like token mint address candidates owned by a Solana address for a specific token program.
*
* <p>This method identifies token holdings that look like NFTs within the supplied token
* program. A returned address is only a candidate. Higher-level integrations are responsible
* for deciding whether the returned address has domain-specific meaning.</p>
*
* // TODO This method currently identifies candidates from the owner's token holdings only.
* // A token with zero decimals and an owner balance of one is not guaranteed to be a real NFT,
* // because the mint's total supply may still be greater than one. A future implementation
* // should verify the mint supply, for example by using Solana getTokenSupply, before treating
* // the result as a confirmed NFT.
*
* @param ownerAddress the Solana owner address whose NFT-like holdings should be inspected
* @param splProgram the SPL token program to query
* @return the NFT-like Solana mint address candidates owned by the address
* @throws IOException if the NFT candidate addresses could not be fetched or parsed
* @throws InterruptedException if the calling thread is interrupted while fetching NFT candidate addresses
*/
Set<ΩSolanaNFTAddressΩ> getSolanaNFTCandidateAddresses(
ΩSolanaAddressΩ ownerAddress,
SolanaSPLTokenProgram splProgram
) throws IOException, InterruptedException;
/**
* Finds a Solana program derived address for a program id and a set of seeds.
*
* <p>The seeds describe the logical inputs used to derive the address. The implementation
* is responsible for converting each seed into the byte representation required by Solana.</p>
*
* @param programId the Solana program id used to derive the address
* @param seeds the seeds used when deriving the program address
* @return the derived Solana address together with its bump value
*/
SolanaProgramDerivedAddress findProgramAddress(
ΩSolanaProgramIdΩ programId,
List<SolanaProgramAddressSeed> seeds
);
/**
* Fetches account information for a Solana account address.
*
* <p>If the account does not exist, this method returns {@code null}. If the account exists,
* the returned value contains the account address, the owning Solana program id and the
* account data encoded as Base64.</p>
*
* @param accountAddress the Solana account address to inspect
* @return account information, or {@code null} if the account does not exist
* @throws IOException if the account information could not be fetched or parsed
* @throws InterruptedException if the calling thread is interrupted while fetching account information
*/
SolanaAccountInfo getAccountInfo(ΩSolanaAddressΩ accountAddress)
throws IOException, InterruptedException;
/**
* Encodes a raw 32-byte Solana address into its textual Solana address representation.
*
* <p>This method is intended for callers that have obtained raw Solana address bytes from
* account data and need the normal string representation used elsewhere in the API.</p>
*
* @param addressBytes the raw 32-byte Solana address
* @return the encoded Solana address
* @throws IllegalArgumentException if the supplied byte array is not a valid Solana address length
*/
ΩSolanaAddressΩ encodeSolanaAddress(byte[] addressBytes);
/**
* Fetches the total supply of an SPL token mint for a specific token program.
*
* <p>The supplied token program identifies which SPL token program owns the mint,
* for example the original SPL Token Program or the Token-2022 Program.</p>
*
* @param mintAddress the SPL mint address whose supply should be fetched
* @param splProgram the SPL token program to query
* @return the SPL token supply for the mint
* @throws IOException if the token supply could not be fetched or parsed
* @throws InterruptedException if the calling thread is interrupted while fetching token supply
*/
SPLTokenSupply getSPLTokenSupply(
ΩSPLMintAddressΩ mintAddress,
SolanaSPLTokenProgram splProgram
) throws IOException, InterruptedException;
}
@@ -331,6 +331,113 @@ public class SolanaBlockChainImpl implements SolanaBlockChain {
); );
} }
@Override
public Set<SolanaAccountInfo> getProgramAccounts(
ΩSolanaProgramIdΩ programId,
Set<SolanaProgramAccountMemcmpFilter> filters
) throws IOException, InterruptedException {
String jsonBody = createGetProgramAccountsBody(programId, filters);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(RPC_URL))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
HttpResponse<String> response = sendThrottled(request);
if (response.statusCode() != 200) {
throw new IOException("RPC call failed: HTTP " + response.statusCode() + "\n" + response.body());
}
JsonNode root = objectMapper.readTree(response.body());
if (root.has("error")) {
throw new IOException("Solana RPC error: " + root.get("error").toPrettyString());
}
JsonNode result = root.path("result");
if (!result.isArray()) {
throw new IOException("getProgramAccounts response did not contain result array!");
}
Set<SolanaAccountInfo> accountInfos = new HashSet<>();
for (JsonNode accountNode : result) {
JsonNode pubkeyNode = accountNode.path("pubkey");
JsonNode account = accountNode.path("account");
if (!pubkeyNode.isTextual()) {
throw new IOException("getProgramAccounts response contained account without textual pubkey!");
}
JsonNode ownerNode = account.path("owner");
if (!ownerNode.isTextual()) {
throw new IOException("getProgramAccounts response contained account without textual owner!");
}
JsonNode dataNode = account.path("data");
if (!dataNode.isArray() || dataNode.isEmpty() || !dataNode.get(0).isTextual()) {
throw new IOException("getProgramAccounts response contained account without base64 data!");
}
accountInfos.add(new SolanaAccountInfo(
pubkeyNode.asText(),
ownerNode.asText(),
dataNode.get(0).asText()
));
}
return Set.copyOf(accountInfos);
}
private String createGetProgramAccountsBody(
ΩSolanaProgramIdΩ programId,
Set<SolanaProgramAccountMemcmpFilter> filters
) throws IOException {
StringBuilder filtersJson = new StringBuilder();
boolean first = true;
for (SolanaProgramAccountMemcmpFilter filter : filters) {
if (!first) {
filtersJson.append(",");
}
filtersJson.append("""
{
"memcmp": {
"offset": %d,
"bytes": "%s"
}
}
""".formatted(
filter.offset(),
filter.bytes()
));
first = false;
}
return """
{
"jsonrpc": "2.0",
"id": 1,
"method": "getProgramAccounts",
"params": [
"%s",
{
"commitment": "finalized",
"encoding": "base64",
"filters": [
%s
]
}
]
}
""".formatted(programId, filtersJson);
}
private synchronized HttpResponse<String> sendThrottled(HttpRequest request) throws IOException, InterruptedException { private synchronized HttpResponse<String> sendThrottled(HttpRequest request) throws IOException, InterruptedException {
waitBeforeRemoteCall(); waitBeforeRemoteCall();
@@ -1,5 +1,10 @@
package com.r35157.nenjim.hubd.impl.ref; package com.r35157.nenjim.hubd.impl.ref;
import com.r35157.libs.jupiter.perps.JupiterPerpsPosition;
import com.r35157.libs.jupiter.perps.JupiterPerpsService;
import com.r35157.libs.jupiter.perps.impl.anchoridl.AnchorIdlJupiterPerpsServiceImpl;
import com.r35157.libs.solana.SolanaBlockChain;
import com.r35157.libs.solana.impl.ref.SolanaBlockChainImpl;
import com.r35157.nenjim.hubd.ctx.Context; import com.r35157.nenjim.hubd.ctx.Context;
import com.r35157.nenjim.hubd.NenjimHub; import com.r35157.nenjim.hubd.NenjimHub;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -12,12 +17,24 @@ import com.r35157.nenjim.hubd.journal.JournalManager;
import com.r35157.nenjim.hubd.impl.ref.JournalManagerImpl; import com.r35157.nenjim.hubd.impl.ref.JournalManagerImpl;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Set;
public class Main { public class Main {
static void main(String[] args) throws Exception {
NenjimHub nenjimHub = new NenjimHubImpl();
/* try { // TODO: Consider if we really need a Main class or we just need to move the main method to NenjimHubImpl?
static void main(String[] args) throws Exception {
NenjimHubImpl nenjimHub = new NenjimHubImpl();
/*
SolanaBlockChain sbc = new SolanaBlockChainImpl();
JupiterPerpsService jupiter = new AnchorIdlJupiterPerpsServiceImpl(sbc);
ΩSolanaWalletIdΩ walletId = "vj98roDZ7744EBfxyuDFkKpEGCsKQLr7K8UFRumJNHf";
Set<JupiterPerpsPosition> positions = jupiter.getOpenPositions(walletId);
int a=0;
*/
nenjimHub.awaitShutdown();
/* try {
log.info("Auto-starting 2 Nenjim application(s)..."); log.info("Auto-starting 2 Nenjim application(s)...");
log.info(" Starting AssetAZCore..."); log.info(" Starting AssetAZCore...");
//com.r35157.; //com.r35157.;
@@ -1,5 +1,6 @@
package com.r35157.nenjim.hubd.impl.ref; package com.r35157.nenjim.hubd.impl.ref;
import com.r35157.jupiterperpsalarm.impl.ref.JupiterPerpsAlarmImpl;
import com.r35157.nenjim.hubd.NenjimHub; import com.r35157.nenjim.hubd.NenjimHub;
import com.r35157.nenjim.hubd.journal.Journal; import com.r35157.nenjim.hubd.journal.Journal;
import crypto.r35157.nenjim.NenjimProcess; import crypto.r35157.nenjim.NenjimProcess;
@@ -8,6 +9,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
public class NenjimHubImpl implements NenjimHub { public class NenjimHubImpl implements NenjimHub {
public NenjimHubImpl() throws Exception { public NenjimHubImpl() throws Exception {
@@ -38,19 +40,24 @@ public class NenjimHubImpl implements NenjimHub {
} }
private void startAutoRunProcesses() throws Exception { private void startAutoRunProcesses() throws Exception {
String[] processesToAutoStart = { startJupiterPerpsAlarm(); // TODO: Hardcoded/hacky way to auto-start but good enough for now.
"crypto.r35157.nenjim.NenjimHubSocketAdminAdapter", // TODO: Old but more correct way to auto start plugins - but it is currently broken.
"crypto.r35157.nenjim.NenjimHubRestAdminAdapter", /*
"crypto.r35157.nenjim.NenjimHubRPCAdminAdapter", String[] processesToAutoStart = {
"crypto.r35157.nenjim.SuwimoHub", //"com.r35157.jupiterperpsalarm.impl.ref.JupiterPerpsAlarmImpl"
"crypto.r35157.nenjim.SodaTaskManager", //"crypto.r35157.nenjim.NenjimHubSocketAdminAdapter",
"crypto.r35157.assetaz.hub.AssetAZHub" //"crypto.r35157.nenjim.NenjimHubRestAdminAdapter",
//"crypto.r35157.nenjim.NenjimHubRPCAdminAdapter",
//"crypto.r35157.nenjim.SuwimoHub",
//"crypto.r35157.nenjim.SodaTaskManager",
//"crypto.r35157.assetaz.hub.AssetAZHub"
}; };
for (String processInterfaceName : processesToAutoStart) { for (String processInterfaceName : processesToAutoStart) {
startProcess(processInterfaceName); startProcess(processInterfaceName);
} }
*/
} }
@Override @Override
@@ -90,7 +97,27 @@ public class NenjimHubImpl implements NenjimHub {
System.out.println("NenjimHub command: 'noop'"); System.out.println("NenjimHub command: 'noop'");
} }
public void awaitShutdown() throws InterruptedException {
shutdownLatch.await();
}
private void startJupiterPerpsAlarm() {
Thread thread = new Thread(() -> {
try {
JupiterPerpsAlarmImpl.main(new String[] {
"--config=conf/alarms.conf"
});
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}, "Nenjim Plugin - JupiterPerpsAlarm");
thread.setDaemon(false);
thread.start();
}
private static final Logger log = LoggerFactory.getLogger(NenjimHubImpl.class); private static final Logger log = LoggerFactory.getLogger(NenjimHubImpl.class);
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
private HashMap<Integer, NenjimProcess> processes; private HashMap<Integer, NenjimProcess> processes;
//private StructuredTaskScope.ShutdownOnFailure processesScope; //private StructuredTaskScope.ShutdownOnFailure processesScope;