Skip to content

Commit

Permalink
sync drift, glam and jupiter programs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpe7s committed Dec 29, 2024
1 parent 0ec7205 commit 2c41ca4
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2645,7 +2645,7 @@ record MismatchedSwiftOrderParamsMarketIndex(int code, String msg) implements Dr
record InvalidSwiftOrderParam(int code, String msg) implements DriftError {

public static final InvalidSwiftOrderParam INSTANCE = new InvalidSwiftOrderParam(
6288, "Swift only available for market/oracle perp orders"
6288, "Invalid swift order param"
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,6 @@ public static Instruction placeSwiftTakerOrder(final AccountMeta invokedDriftPro
// The Instruction Sysvar has not been implemented
// in the Anchor framework yet, so this is the safe approach.
final PublicKey ixSysvarKey,
final byte[] swiftMessageBytes,
final byte[] swiftOrderParamsMessageBytes) {
final var keys = List.of(
createRead(stateKey),
Expand All @@ -1144,15 +1143,14 @@ public static Instruction placeSwiftTakerOrder(final AccountMeta invokedDriftPro
createRead(ixSysvarKey)
);

final byte[] _data = new byte[16 + Borsh.lenVector(swiftMessageBytes) + Borsh.lenVector(swiftOrderParamsMessageBytes)];
final byte[] _data = new byte[12 + Borsh.lenVector(swiftOrderParamsMessageBytes)];
int i = writeDiscriminator(PLACE_SWIFT_TAKER_ORDER_DISCRIMINATOR, _data, 0);
i += Borsh.writeVector(swiftMessageBytes, _data, i);
Borsh.writeVector(swiftOrderParamsMessageBytes, _data, i);

return Instruction.createInstruction(invokedDriftProgramMeta, keys, _data);
}

public record PlaceSwiftTakerOrderIxData(Discriminator discriminator, byte[] swiftMessageBytes, byte[] swiftOrderParamsMessageBytes) implements Borsh {
public record PlaceSwiftTakerOrderIxData(Discriminator discriminator, byte[] swiftOrderParamsMessageBytes) implements Borsh {

public static PlaceSwiftTakerOrderIxData read(final Instruction instruction) {
return read(instruction.data(), instruction.offset());
Expand All @@ -1164,23 +1162,20 @@ public static PlaceSwiftTakerOrderIxData read(final byte[] _data, final int offs
}
final var discriminator = parseDiscriminator(_data, offset);
int i = offset + discriminator.length();
final byte[] swiftMessageBytes = Borsh.readbyteVector(_data, i);
i += Borsh.lenVector(swiftMessageBytes);
final byte[] swiftOrderParamsMessageBytes = Borsh.readbyteVector(_data, i);
return new PlaceSwiftTakerOrderIxData(discriminator, swiftMessageBytes, swiftOrderParamsMessageBytes);
return new PlaceSwiftTakerOrderIxData(discriminator, swiftOrderParamsMessageBytes);
}

@Override
public int write(final byte[] _data, final int offset) {
int i = offset + discriminator.write(_data, offset);
i += Borsh.writeVector(swiftMessageBytes, _data, i);
i += Borsh.writeVector(swiftOrderParamsMessageBytes, _data, i);
return i - offset;
}

@Override
public int l() {
return 8 + Borsh.lenVector(swiftMessageBytes) + Borsh.lenVector(swiftOrderParamsMessageBytes);
return 8 + Borsh.lenVector(swiftOrderParamsMessageBytes);
}
}

Expand Down Expand Up @@ -2312,7 +2307,7 @@ public static Instruction deleteUser(final AccountMeta invokedDriftProgramMeta,
createWrite(userKey),
createWrite(userStatsKey),
createWrite(stateKey),
createReadOnlySigner(authorityKey)
createWritableSigner(authorityKey)
);

return Instruction.createInstruction(invokedDriftProgramMeta, keys, DELETE_USER_DISCRIMINATOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import software.sava.core.borsh.Borsh;

import static software.sava.core.encoding.ByteUtil.getInt16LE;
import static software.sava.core.encoding.ByteUtil.getInt64LE;
import static software.sava.core.encoding.ByteUtil.putInt16LE;
import static software.sava.core.encoding.ByteUtil.putInt64LE;

public record SwiftOrderParamsMessage(OrderParams swiftOrderParams,
int subAccountId,
long slot,
byte[] uuid,
SwiftTriggerOrderParams takeProfitOrderParams,
SwiftTriggerOrderParams stopLossOrderParams) implements Borsh {

Expand All @@ -19,13 +23,19 @@ public static SwiftOrderParamsMessage read(final byte[] _data, final int offset)
i += Borsh.len(swiftOrderParams);
final var subAccountId = getInt16LE(_data, i);
i += 2;
final var slot = getInt64LE(_data, i);
i += 8;
final var uuid = new byte[8];
i += Borsh.readArray(uuid, _data, i);
final var takeProfitOrderParams = _data[i++] == 0 ? null : SwiftTriggerOrderParams.read(_data, i);
if (takeProfitOrderParams != null) {
i += Borsh.len(takeProfitOrderParams);
}
final var stopLossOrderParams = _data[i++] == 0 ? null : SwiftTriggerOrderParams.read(_data, i);
return new SwiftOrderParamsMessage(swiftOrderParams,
subAccountId,
slot,
uuid,
takeProfitOrderParams,
stopLossOrderParams);
}
Expand All @@ -36,13 +46,21 @@ public int write(final byte[] _data, final int offset) {
i += Borsh.write(swiftOrderParams, _data, i);
putInt16LE(_data, i, subAccountId);
i += 2;
putInt64LE(_data, i, slot);
i += 8;
i += Borsh.writeArray(uuid, _data, i);
i += Borsh.writeOptional(takeProfitOrderParams, _data, i);
i += Borsh.writeOptional(stopLossOrderParams, _data, i);
return i - offset;
}

@Override
public int l() {
return Borsh.len(swiftOrderParams) + 2 + (takeProfitOrderParams == null ? 1 : (1 + Borsh.len(takeProfitOrderParams))) + (stopLossOrderParams == null ? 1 : (1 + Borsh.len(stopLossOrderParams)));
return Borsh.len(swiftOrderParams)
+ 2
+ 8
+ Borsh.lenArray(uuid)
+ (takeProfitOrderParams == null ? 1 : (1 + Borsh.len(takeProfitOrderParams)))
+ (stopLossOrderParams == null ? 1 : (1 + Borsh.len(stopLossOrderParams)));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ public Instruction subscribe(final PublicKey assetKey,
assetATAKey,
policyPDA.publicKey(),
manager.publicKey(),
shareClassId,
amount,
true
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,109 +3,28 @@
import software.sava.anchor.ProgramError;

public sealed interface GlamError extends ProgramError permits
GlamError.InvalidShareClass,
GlamError.InvalidAssetSubscribe,
GlamError.InvalidPricingOracle,
GlamError.InvalidRemainingAccounts,
GlamError.InvalidTreasuryAccount,
GlamError.InvalidSignerAccount,
GlamError.InvalidAssetPrice,
GlamError.InvalidStableCoinPriceForSubscribe,
GlamError.SubscribeRedeemDisable,
GlamError.InvalidPolicyAccount,
GlamError.PriceTooOld {
GlamError.ShareClassNotEmpty,
GlamError.InvalidTokenAccount {

static GlamError getInstance(final int errorCode) {
return switch (errorCode) {
case 6000 -> InvalidShareClass.INSTANCE;
case 6001 -> InvalidAssetSubscribe.INSTANCE;
case 6002 -> InvalidPricingOracle.INSTANCE;
case 6003 -> InvalidRemainingAccounts.INSTANCE;
case 6004 -> InvalidTreasuryAccount.INSTANCE;
case 6005 -> InvalidSignerAccount.INSTANCE;
case 6006 -> InvalidAssetPrice.INSTANCE;
case 6007 -> InvalidStableCoinPriceForSubscribe.INSTANCE;
case 6008 -> SubscribeRedeemDisable.INSTANCE;
case 6009 -> InvalidPolicyAccount.INSTANCE;
case 6010 -> PriceTooOld.INSTANCE;
case 6000 -> ShareClassNotEmpty.INSTANCE;
case 6001 -> InvalidTokenAccount.INSTANCE;
default -> throw new IllegalStateException("Unexpected Glam error code: " + errorCode);
};
}

record InvalidShareClass(int code, String msg) implements GlamError {
record ShareClassNotEmpty(int code, String msg) implements GlamError {

public static final InvalidShareClass INSTANCE = new InvalidShareClass(
6000, "Share class not allowed to subscribe"
public static final ShareClassNotEmpty INSTANCE = new ShareClassNotEmpty(
6000, "Share class mint supply not zero"
);
}

record InvalidAssetSubscribe(int code, String msg) implements GlamError {
record InvalidTokenAccount(int code, String msg) implements GlamError {

public static final InvalidAssetSubscribe INSTANCE = new InvalidAssetSubscribe(
6001, "Asset not allowed to subscribe"
);
}

record InvalidPricingOracle(int code, String msg) implements GlamError {

public static final InvalidPricingOracle INSTANCE = new InvalidPricingOracle(
6002, "Invalid oracle for asset price"
);
}

record InvalidRemainingAccounts(int code, String msg) implements GlamError {

public static final InvalidRemainingAccounts INSTANCE = new InvalidRemainingAccounts(
6003, "Invalid accounts: the transaction is malformed"
);
}

record InvalidTreasuryAccount(int code, String msg) implements GlamError {

public static final InvalidTreasuryAccount INSTANCE = new InvalidTreasuryAccount(
6004, "Invalid treasury ata"
);
}

record InvalidSignerAccount(int code, String msg) implements GlamError {

public static final InvalidSignerAccount INSTANCE = new InvalidSignerAccount(
6005, "Invalid signer ata"
);
}

record InvalidAssetPrice(int code, String msg) implements GlamError {

public static final InvalidAssetPrice INSTANCE = new InvalidAssetPrice(
6006, "Invalid asset price"
);
}

record InvalidStableCoinPriceForSubscribe(int code, String msg) implements GlamError {

public static final InvalidStableCoinPriceForSubscribe INSTANCE = new InvalidStableCoinPriceForSubscribe(
6007, "Subscription not allowed: invalid stable coin price"
);
}

record SubscribeRedeemDisable(int code, String msg) implements GlamError {

public static final SubscribeRedeemDisable INSTANCE = new SubscribeRedeemDisable(
6008, "Fund is disabled for subscription and redemption"
);
}

record InvalidPolicyAccount(int code, String msg) implements GlamError {

public static final InvalidPolicyAccount INSTANCE = new InvalidPolicyAccount(
6009, "Policy account is mandatory"
);
}

record PriceTooOld(int code, String msg) implements GlamError {

public static final PriceTooOld INSTANCE = new PriceTooOld(
6010, "Price is too old"
public static final InvalidTokenAccount INSTANCE = new InvalidTokenAccount(
6001, "Invalid token account"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public static ProgramDerivedAddress fromAtaPDA(final PublicKey program,
}

public static ProgramDerivedAddress fundPDA(final PublicKey program,
final PublicKey managerAccount,
final PublicKey signerAccount,
final byte[] fundModelCreated) {
return PublicKey.findProgramAddress(List.of(
"fund".getBytes(US_ASCII),
managerAccount.toByteArray(),
signerAccount.toByteArray(),
fundModelCreated
), program);
}
Expand All @@ -67,11 +67,11 @@ public static ProgramDerivedAddress inputSignerAtaPDA(final PublicKey program,
}

public static ProgramDerivedAddress managerAtaPDA(final PublicKey program,
final PublicKey managerAccount,
final PublicKey signerAccount,
final PublicKey tokenProgramAccount,
final PublicKey assetAccount) {
return PublicKey.findProgramAddress(List.of(
managerAccount.toByteArray(),
signerAccount.toByteArray(),
tokenProgramAccount.toByteArray(),
assetAccount.toByteArray()
), program);
Expand Down
Loading

0 comments on commit 2c41ca4

Please sign in to comment.