Skip to content
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

feat: fixed CheckSignature and extended example #9

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func main() {
}

if quoteResponse.JSON200 == nil {
// handle me
}
// handle me
}

quote := quoteResponse.JSON200

Expand Down Expand Up @@ -125,9 +125,10 @@ func main() {
panic(err)
}
}

```

A full swap example is available in the [examples](_examples) folder.

## Jupiter client

The Jupiter client is generated from the [official Jupiter openapi definition](https://github.com/jup-ag/jupiter-quote-api-node/blob/main/swagger.yaml) and provides the following methods to interact with the Jupiter API:
Expand Down
42 changes: 39 additions & 3 deletions _examples/swap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"context"
"fmt"
"time"

"github.com/ilkamo/jupiter-go/jupiter"
"github.com/ilkamo/jupiter-go/solana"
)

func main() {
Expand All @@ -17,7 +19,9 @@ func main() {

slippageBps := 250

// Get the current quote for a swap
// Get the current quote for a swap.
// Ensure that the input and output mints are valid.
// The amount is the smallest unit of the input token.
quoteResponse, err := jupClient.GetQuoteWithResponse(ctx, &jupiter.GetQuoteParams{
InputMint: "So11111111111111111111111111111111111111112",
OutputMint: "WENWENvqqNya429ubCdR81ZmD69brwQaaBYY6p3LCpk",
Expand All @@ -41,11 +45,12 @@ func main() {
}

dynamicComputeUnitLimit := true
// Get instructions for a swap
// Get instructions for a swap.
// Ensure your public key is valid.
swapResponse, err := jupClient.PostSwapWithResponse(ctx, jupiter.PostSwapJSONRequestBody{
PrioritizationFeeLamports: &prioritizationFeeLamports,
QuoteResponse: *quote,
UserPublicKey: "your public key here",
UserPublicKey: "{YOUR_PUBLIC_KEY}",
DynamicComputeUnitLimit: &dynamicComputeUnitLimit,
})
if err != nil {
Expand All @@ -58,4 +63,35 @@ func main() {

swap := swapResponse.JSON200
fmt.Printf("%+v", swap)

// Create a wallet from private key.
walletPrivateKey := "{YOUR_PRIVATE_KEY}"
wallet, err := solana.NewWalletFromPrivateKeyBase58(walletPrivateKey)
if err != nil {
panic(err)
}

// Create a Solana client. Change the URL to the desired Solana node.
solanaClient, err := solana.NewClient(wallet, "https://api.mainnet-beta.solana.com")
if err != nil {
panic(err)
}

// Sign and send the transaction
signedTx, err := solanaClient.SendTransactionOnChain(ctx, swap.SwapTransaction)
if err != nil {
panic(err)
}

// Wait a bit to let the transaction propagate to the network.
// This is just an example and not a best practice.
// You could use a ticker or wait until we implement the WebSocket monitoring ;)
time.Sleep(20 * time.Second)

// Get the status of the transaction (pull the status from the blockchain at intervals
// until the transaction is confirmed)
_, err = solanaClient.CheckSignature(ctx, signedTx)
if err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion solana/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (e Client) CheckSignature(ctx context.Context, tx TxID) (bool, error) {
return false, fmt.Errorf("could not confirm transaction: no valid status")
}

if status.Value[0].ConfirmationStatus != rpc.ConfirmationStatusFinalized {
if status.Value[0] == nil || status.Value[0].ConfirmationStatus != rpc.ConfirmationStatusFinalized {
return false, fmt.Errorf("transaction not finalized yet")
}

Expand Down
Loading