From eca652cb8581058ea5c88592522d6209d3e1f1e7 Mon Sep 17 00:00:00 2001 From: Kamil Molendys Date: Tue, 19 Mar 2024 12:44:23 +0100 Subject: [PATCH] Fixed CheckSignature and extend example --- README.md | 7 ++++--- _examples/swap/main.go | 42 +++++++++++++++++++++++++++++++++++++++--- solana/client.go | 2 +- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6a48f6b..e83ee29 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,8 @@ func main() { } if quoteResponse.JSON200 == nil { - // handle me - } + // handle me + } quote := quoteResponse.JSON200 @@ -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: diff --git a/_examples/swap/main.go b/_examples/swap/main.go index e46ebaa..7255bb1 100644 --- a/_examples/swap/main.go +++ b/_examples/swap/main.go @@ -3,8 +3,10 @@ package main import ( "context" "fmt" + "time" "github.com/ilkamo/jupiter-go/jupiter" + "github.com/ilkamo/jupiter-go/solana" ) func main() { @@ -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", @@ -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 { @@ -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) + } } diff --git a/solana/client.go b/solana/client.go index 6d7c0a2..cb8df82 100644 --- a/solana/client.go +++ b/solana/client.go @@ -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") }