Skip to content

Commit

Permalink
Added first code example (#7)
Browse files Browse the repository at this point in the history
* Added first code example
  • Loading branch information
ilkamo committed Mar 19, 2024
1 parent 332f85b commit 21c450c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func main() {
slippageBps := 250

// Get the current quote for a swap
quoteResponse, err := jupClient.GetQuoteWithResponse(ctx, jupiter.GetQuoteParams{
quoteResponse, err := jupClient.GetQuoteWithResponse(ctx, &jupiter.GetQuoteParams{
InputMint: "So11111111111111111111111111111111111111112",
OutputMint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
Amount: 10000000,
OutputMint: "WENWENvqqNya429ubCdR81ZmD69brwQaaBYY6p3LCpk",
Amount: 100000,
SlippageBps: &slippageBps,
})
if err != nil {
Expand All @@ -52,12 +52,16 @@ func main() {
quote := quoteResponse.JSON200

// More info: https://station.jup.ag/docs/apis/troubleshooting
prioritizationFeeLamports := "auto"
prioritizationFeeLamports := jupiter.SwapRequest_PrioritizationFeeLamports{}
if err = prioritizationFeeLamports.UnmarshalJSON([]byte(`"auto"`)); err != nil {
// handle me
}

dynamicComputeUnitLimit := true
// Get instructions for a swap
swapResponse, err := jupClient.PostSwapWithResponse(ctx, jupiter.PostSwapJSONRequestBody{
PrioritizationFeeLamports: &prioritizationFeeLamports,
QuoteResponse: quote,
QuoteResponse: *quote,
UserPublicKey: "the public key of your wallet",
DynamicComputeUnitLimit: &dynamicComputeUnitLimit,
})
Expand Down
10 changes: 10 additions & 0 deletions _examples/swap/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module main.go

go 1.21.3

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/ilkamo/jupiter-go v0.0.4 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect
)
16 changes: 16 additions & 0 deletions _examples/swap/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/ilkamo/jupiter-go v0.0.4 h1:lPcqBsYJeY4wuGtTb8em6PllVPEzJy/KYojKDTDYOzI=
github.com/ilkamo/jupiter-go v0.0.4/go.mod h1:ocIddd14hMfjdlcJqO3eawEWGwOaLVpWdexHGjfFCVM=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
61 changes: 61 additions & 0 deletions _examples/swap/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"context"
"fmt"

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

func main() {
jupClient, err := jupiter.NewClientWithResponses(jupiter.DefaultAPIURL)
if err != nil {
panic(err)
}

ctx := context.TODO()

slippageBps := 250

// Get the current quote for a swap
quoteResponse, err := jupClient.GetQuoteWithResponse(ctx, &jupiter.GetQuoteParams{
InputMint: "So11111111111111111111111111111111111111112",
OutputMint: "WENWENvqqNya429ubCdR81ZmD69brwQaaBYY6p3LCpk",
Amount: 100000,
SlippageBps: &slippageBps,
})
if err != nil {
panic(err)
}

if quoteResponse.JSON200 == nil {
panic("invalid GetQuoteWithResponse response")
}

quote := quoteResponse.JSON200

// More info: https://station.jup.ag/docs/apis/troubleshooting
prioritizationFeeLamports := jupiter.SwapRequest_PrioritizationFeeLamports{}
if err = prioritizationFeeLamports.UnmarshalJSON([]byte(`"auto"`)); err != nil {
panic(err)
}

dynamicComputeUnitLimit := true
// Get instructions for a swap
swapResponse, err := jupClient.PostSwapWithResponse(ctx, jupiter.PostSwapJSONRequestBody{
PrioritizationFeeLamports: &prioritizationFeeLamports,
QuoteResponse: *quote,
UserPublicKey: "your public key here",
DynamicComputeUnitLimit: &dynamicComputeUnitLimit,
})
if err != nil {
panic(err)
}

if swapResponse.JSON200 == nil {
panic("invalid PostSwapWithResponse response")
}

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

0 comments on commit 21c450c

Please sign in to comment.