Skip to content

Commit

Permalink
sync glam
Browse files Browse the repository at this point in the history
  • Loading branch information
jpe7s committed Oct 5, 2024
1 parent 1eae968 commit c3a835e
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,109 @@
import software.sava.anchor.ProgramError;

public sealed interface GlamError extends ProgramError permits
GlamError.CloseNotEmptyError,
GlamError.NotAuthorizedError,
GlamError.InvalidFundName,
GlamError.InvalidFundSymbol,
GlamError.InvalidFundUri,
GlamError.InvalidAssetsLen,
GlamError.InvalidAssetsWeights,
GlamError.InvalidAssetForSwap,
GlamError.InvalidSwap {
GlamError.FundNotActive,
GlamError.InvalidShareClass,
GlamError.InvalidAssetSubscribe,
GlamError.InvalidPricingOracle,
GlamError.InvalidRemainingAccounts,
GlamError.InvalidTreasuryAccount,
GlamError.InvalidSignerAccount,
GlamError.InvalidAssetPrice,
GlamError.InvalidStableCoinPriceForSubscribe,
GlamError.SubscribeRedeemPaused,
GlamError.InvalidPolicyAccount {

static GlamError getInstance(final int errorCode) {
return switch (errorCode) {
case 6000 -> CloseNotEmptyError.INSTANCE;
case 6001 -> NotAuthorizedError.INSTANCE;
case 6002 -> InvalidFundName.INSTANCE;
case 6003 -> InvalidFundSymbol.INSTANCE;
case 6004 -> InvalidFundUri.INSTANCE;
case 6005 -> InvalidAssetsLen.INSTANCE;
case 6006 -> InvalidAssetsWeights.INSTANCE;
case 6007 -> InvalidAssetForSwap.INSTANCE;
case 6008 -> InvalidSwap.INSTANCE;
case 6000 -> FundNotActive.INSTANCE;
case 6001 -> InvalidShareClass.INSTANCE;
case 6002 -> InvalidAssetSubscribe.INSTANCE;
case 6003 -> InvalidPricingOracle.INSTANCE;
case 6004 -> InvalidRemainingAccounts.INSTANCE;
case 6005 -> InvalidTreasuryAccount.INSTANCE;
case 6006 -> InvalidSignerAccount.INSTANCE;
case 6007 -> InvalidAssetPrice.INSTANCE;
case 6008 -> InvalidStableCoinPriceForSubscribe.INSTANCE;
case 6009 -> SubscribeRedeemPaused.INSTANCE;
case 6010 -> InvalidPolicyAccount.INSTANCE;
default -> throw new IllegalStateException("Unexpected Glam error code: " + errorCode);
};
}

record CloseNotEmptyError(int code, String msg) implements GlamError {
record FundNotActive(int code, String msg) implements GlamError {

public static final CloseNotEmptyError INSTANCE = new CloseNotEmptyError(
6000, "Error closing account: not empty"
public static final FundNotActive INSTANCE = new FundNotActive(
6000, "Fund is not active"
);
}

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

public static final NotAuthorizedError INSTANCE = new NotAuthorizedError(
6001, "Error: not authorized"
public static final InvalidShareClass INSTANCE = new InvalidShareClass(
6001, "Share class not allowed to subscribe"
);
}

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

public static final InvalidFundName INSTANCE = new InvalidFundName(
6002, "Invalid fund name: max 30 chars"
public static final InvalidAssetSubscribe INSTANCE = new InvalidAssetSubscribe(
6002, "Asset not allowed to subscribe"
);
}

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

public static final InvalidFundSymbol INSTANCE = new InvalidFundSymbol(
6003, "Too many assets: max 50"
public static final InvalidPricingOracle INSTANCE = new InvalidPricingOracle(
6003, "Invalid oracle for asset price"
);
}

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

public static final InvalidFundUri INSTANCE = new InvalidFundUri(
6004, "Too many assets: max 20"
public static final InvalidRemainingAccounts INSTANCE = new InvalidRemainingAccounts(
6004, "Invalid accounts: the transaction is malformed"
);
}

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

public static final InvalidAssetsLen INSTANCE = new InvalidAssetsLen(
6005, "Too many assets: max 100"
public static final InvalidTreasuryAccount INSTANCE = new InvalidTreasuryAccount(
6005, "Invalid treasury ata"
);
}

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

public static final InvalidAssetsWeights INSTANCE = new InvalidAssetsWeights(
6006, "Number of weights should match number of assets"
public static final InvalidSignerAccount INSTANCE = new InvalidSignerAccount(
6006, "Invalid signer ata"
);
}

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

public static final InvalidAssetForSwap INSTANCE = new InvalidAssetForSwap(
6007, "Asset cannot be swapped"
public static final InvalidAssetPrice INSTANCE = new InvalidAssetPrice(
6007, "Invalid asset price"
);
}

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

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

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

public static final SubscribeRedeemPaused INSTANCE = new SubscribeRedeemPaused(
6009, "Fund is paused for subscription and redemption"
);
}

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

public static final InvalidPolicyAccount INSTANCE = new InvalidPolicyAccount(
6010, "Policy account is mandatory"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -681,10 +681,10 @@ public static Instruction driftWithdraw(final AccountMeta invokedGlamProgramMeta
final PublicKey userKey,
final PublicKey userStatsKey,
final PublicKey stateKey,
final PublicKey driftSignerKey,
final PublicKey treasuryKey,
final PublicKey treasuryAtaKey,
final PublicKey driftAtaKey,
final PublicKey driftSignerKey,
final PublicKey managerKey,
final PublicKey driftProgramKey,
final int marketIndex,
Expand All @@ -694,10 +694,10 @@ public static Instruction driftWithdraw(final AccountMeta invokedGlamProgramMeta
createWrite(userKey),
createWrite(userStatsKey),
createWrite(stateKey),
createRead(driftSignerKey),
createRead(treasuryKey),
createWrite(treasuryAtaKey),
createWrite(driftAtaKey),
createRead(driftSignerKey),
createWritableSigner(managerKey),
createRead(driftProgramKey),
createRead(solanaAccounts.tokenProgram())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public enum EngineFieldName implements Borsh.Enum {
DelegateAcls,
IntegrationAcls,
ExternalTreasuryAccounts,
LockUp;
LockUp,
DriftMarketIndexesPerp,
DriftMarketIndexesSpot;

public static EngineFieldName read(final byte[] _data, final int offset) {
return Borsh.read(EngineFieldName.values(), _data, offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public record FundModel(PublicKey id,
CreatedModel created,
DelegateAcl[] delegateAcls,
IntegrationAcl[] integrationAcls,
int[] driftMarketIndexesPerp,
int[] driftMarketIndexesSpot,
boolean isRawOpenfunds,
FundOpenfundsModel rawOpenfunds) implements Borsh {

Expand All @@ -38,6 +40,8 @@ public static FundModel createRecord(final PublicKey id,
final CreatedModel created,
final DelegateAcl[] delegateAcls,
final IntegrationAcl[] integrationAcls,
final int[] driftMarketIndexesPerp,
final int[] driftMarketIndexesSpot,
final boolean isRawOpenfunds,
final FundOpenfundsModel rawOpenfunds) {
return new FundModel(id,
Expand All @@ -53,6 +57,8 @@ public static FundModel createRecord(final PublicKey id,
created,
delegateAcls,
integrationAcls,
driftMarketIndexesPerp,
driftMarketIndexesSpot,
isRawOpenfunds,
rawOpenfunds);
}
Expand Down Expand Up @@ -104,6 +110,10 @@ public static FundModel read(final byte[] _data, final int offset) {
i += Borsh.lenVector(delegateAcls);
final var integrationAcls = Borsh.readVector(IntegrationAcl.class, IntegrationAcl::read, _data, i);
i += Borsh.lenVector(integrationAcls);
final var driftMarketIndexesPerp = Borsh.readintVector(_data, i);
i += Borsh.lenVector(driftMarketIndexesPerp);
final var driftMarketIndexesSpot = Borsh.readintVector(_data, i);
i += Borsh.lenVector(driftMarketIndexesSpot);
final var isRawOpenfunds = _data[i] == 1;
++i;
final var rawOpenfunds = _data[i++] == 0 ? null : FundOpenfundsModel.read(_data, i);
Expand All @@ -120,6 +130,8 @@ public static FundModel read(final byte[] _data, final int offset) {
created,
delegateAcls,
integrationAcls,
driftMarketIndexesPerp,
driftMarketIndexesSpot,
isRawOpenfunds,
rawOpenfunds);
}
Expand All @@ -140,6 +152,8 @@ public int write(final byte[] _data, final int offset) {
i += Borsh.writeOptional(created, _data, i);
i += Borsh.writeVector(delegateAcls, _data, i);
i += Borsh.writeVector(integrationAcls, _data, i);
i += Borsh.writeVector(driftMarketIndexesPerp, _data, i);
i += Borsh.writeVector(driftMarketIndexesSpot, _data, i);
_data[i] = (byte) (isRawOpenfunds ? 1 : 0);
++i;
i += Borsh.writeOptional(rawOpenfunds, _data, i);
Expand All @@ -161,6 +175,8 @@ public int l() {
+ (created == null ? 1 : (1 + Borsh.len(created)))
+ Borsh.lenVector(delegateAcls)
+ Borsh.lenVector(integrationAcls)
+ Borsh.lenVector(driftMarketIndexesPerp)
+ Borsh.lenVector(driftMarketIndexesSpot)
+ 1
+ (rawOpenfunds == null ? 1 : (1 + Borsh.len(rawOpenfunds)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
public enum IntegrationName implements Borsh.Enum {

Drift,
StakePool,
SplStakePool,
SanctumStakePool,
NativeStaking,
Marinade,
Jupiter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
// * Delegate ACL
public enum Permission implements Borsh.Enum {

DriftInitialize,
DriftUpdateUser,
DriftDeleteUser,
DriftDeposit,
DriftWithdraw,
DriftPlaceOrders,
DriftCancelOrders,
DriftPerpMarket,
DriftSpotMarket,
Stake,
Unstake,
LiquidUnstake,
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencyResolutionManagement {
}
versionCatalogs {
libs {
from("software.sava:solana-version-catalog:0.2.9")
from("software.sava:solana-version-catalog:0.2.10")
}
}
}

0 comments on commit c3a835e

Please sign in to comment.