Skip to content

Swap RPC implementation #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 22, 2024
Prev Previous commit
Next Next commit
Support umlimited swaps and add balance checks
  • Loading branch information
jeffyanta committed Jan 19, 2024
commit 867804aff01175d69e67fcd8532202df871a1589
26 changes: 19 additions & 7 deletions pkg/code/server/grpc/transaction/v2/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer)
return handleSwapError(streamer, status.Error(codes.InvalidArgument, "SwapRequest.Initiate is nil"))
}

if initiateReq.Limit == 0 {
// todo: support this
return handleSwapError(streamer, status.Error(codes.Unimplemented, "unlimited swap is not supported yet"))
}

amountToSwap := initiateReq.Limit

owner, err := common.NewAccountFromProto(initiateReq.Owner)
if err != nil {
log.WithError(err).Warn("invalid owner account")
Expand Down Expand Up @@ -125,6 +118,25 @@ func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer)
}
log = log.WithField("swap_destination", swapDestination.PublicKey().ToBase58())

swapSourceBalance, err := s.data.GetBlockchainBalance(ctx, swapSource.PublicKey().ToBase58())
if err != nil {
log.WithError(err).Warn("failure getting swap source account balance")
return handleSwapError(streamer, err)
}

var amountToSwap uint64
if initiateReq.Limit == 0 {
// todo: Should we bound this to max send or daily limit?
amountToSwap = swapSourceBalance
} else {
amountToSwap = initiateReq.Limit
}
if amountToSwap == 0 {
return handleSwapError(streamer, newSwapValidationError("usdc account balance is 0"))
} else if swapSourceBalance < amountToSwap {
return handleSwapError(streamer, newSwapValidationError("insufficient usdc balance"))
}

quote, err := s.jupiterClient.GetQuote(
ctx,
usdc.Mint,
Expand Down