Skip to content

Commit d6c16c7

Browse files
authored
Use a small default priority fee when Jupiter doesn't provide compute budget instructions (#119)
1 parent 090a0ca commit d6c16c7

File tree

1 file changed

+25
-16
lines changed
  • pkg/code/server/grpc/transaction/v2

1 file changed

+25
-16
lines changed

pkg/code/server/grpc/transaction/v2/swap.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,17 @@ func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer)
214214
// Section: Transaction construction
215215
//
216216

217-
computeUnitLimit, _ := compute_budget.DecompileSetComputeUnitLimitIxnData(jupiterSwapIxns.ComputeBudgetInstructions[0].Data)
217+
var computeUnitLimit uint32
218+
var computeUnitPrice uint64
219+
if len(jupiterSwapIxns.ComputeBudgetInstructions) == 2 {
220+
computeUnitLimit, _ = compute_budget.DecompileSetComputeUnitLimitIxnData(jupiterSwapIxns.ComputeBudgetInstructions[0].Data)
218221

219-
computeUnitPrice, _ := compute_budget.DecompileSetComputeUnitPriceIxnData(jupiterSwapIxns.ComputeBudgetInstructions[1].Data)
220-
computeUnitPrice = uint64(s.conf.swapPriorityFeeMultiple.Get(ctx) * float64(computeUnitPrice))
222+
computeUnitPrice, _ = compute_budget.DecompileSetComputeUnitPriceIxnData(jupiterSwapIxns.ComputeBudgetInstructions[1].Data)
223+
computeUnitPrice = uint64(s.conf.swapPriorityFeeMultiple.Get(ctx) * float64(computeUnitPrice))
224+
} else {
225+
computeUnitLimit = 1_400_000
226+
computeUnitPrice = 10_000
227+
}
221228

222229
swapNonce, err := common.NewRandomAccount()
223230
if err != nil {
@@ -439,8 +446,8 @@ func (s *transactionServer) validateSwap(
439446
// Part 1: Expected instructions sanity check
440447
//
441448

442-
if len(ixns.ComputeBudgetInstructions) != 2 {
443-
return newSwapValidationError("expected two compute budget instructions")
449+
if len(ixns.ComputeBudgetInstructions) != 0 && len(ixns.ComputeBudgetInstructions) != 2 {
450+
return newSwapValidationError("expected zero or two compute budget instructions")
444451
}
445452

446453
if ixns.TokenLedgerInstruction != nil || ixns.CleanupInstruction != nil {
@@ -451,20 +458,22 @@ func (s *transactionServer) validateSwap(
451458
// Part 2: Compute budget instructions
452459
//
453460

454-
if !bytes.Equal(ixns.ComputeBudgetInstructions[0].Program, compute_budget.ProgramKey) || !bytes.Equal(ixns.ComputeBudgetInstructions[1].Program, compute_budget.ProgramKey) {
455-
return newSwapValidationError("invalid ComputeBudget program key")
456-
}
461+
if len(ixns.ComputeBudgetInstructions) > 0 {
462+
if !bytes.Equal(ixns.ComputeBudgetInstructions[0].Program, compute_budget.ProgramKey) || !bytes.Equal(ixns.ComputeBudgetInstructions[1].Program, compute_budget.ProgramKey) {
463+
return newSwapValidationError("invalid ComputeBudget program key")
464+
}
457465

458-
if len(ixns.ComputeBudgetInstructions[0].Accounts) != 0 || len(ixns.ComputeBudgetInstructions[1].Accounts) != 0 {
459-
return newSwapValidationError("invalid ComputeBudget instruction accounts")
460-
}
466+
if len(ixns.ComputeBudgetInstructions[0].Accounts) != 0 || len(ixns.ComputeBudgetInstructions[1].Accounts) != 0 {
467+
return newSwapValidationError("invalid ComputeBudget instruction accounts")
468+
}
461469

462-
if _, err := compute_budget.DecompileSetComputeUnitLimitIxnData(ixns.ComputeBudgetInstructions[0].Data); err != nil {
463-
return newSwapValidationErrorf("invalid ComputeBudget::SetComputeUnitLimit instruction data: %s", err.Error())
464-
}
470+
if _, err := compute_budget.DecompileSetComputeUnitLimitIxnData(ixns.ComputeBudgetInstructions[0].Data); err != nil {
471+
return newSwapValidationErrorf("invalid ComputeBudget::SetComputeUnitLimit instruction data: %s", err.Error())
472+
}
465473

466-
if _, err := compute_budget.DecompileSetComputeUnitPriceIxnData(ixns.ComputeBudgetInstructions[1].Data); err != nil {
467-
return newSwapValidationErrorf("invalid ComputeBudget::SetComputeUnitPrice instruction data: %s", err.Error())
474+
if _, err := compute_budget.DecompileSetComputeUnitPriceIxnData(ixns.ComputeBudgetInstructions[1].Data); err != nil {
475+
return newSwapValidationErrorf("invalid ComputeBudget::SetComputeUnitPrice instruction data: %s", err.Error())
476+
}
468477
}
469478

470479
//

0 commit comments

Comments
 (0)