Skip to content

Commit

Permalink
feat: add support for getRecentPrioritizationFees RPC method (#140)
Browse files Browse the repository at this point in the history
* feat: add wrapper for getRecentPrioritizationFees rpc call

* test: getRecentPrioritizationFees rpc call

* docs: README entry for getRecentPrioritizationFees rpc method

* docs: update errors in README

* refactor: add comments to GetRecentPrioritizationFees
  • Loading branch information
GCrispino committed Mar 28, 2023
1 parent 47267c3 commit 290a21a
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
Expand Up @@ -872,6 +872,7 @@ func main() {
- To be used with **solana v1.8**
- For solana v1.9 or newer: **DEPRECATED: Please use [GetLatestBlockhash](#index--rpc--getlatestblockhash) instead** (This method is expected to be removed in **solana-core v2.0**)
- [GetRecentPerformanceSamples](#index--rpc--getrecentperformancesamples)
- [GetRecentPrioritizationFees](#index--rpc--getrecentprioritizationfees)
- [GetSignatureStatuses](#index--rpc--getsignaturestatuses)
- [GetSignaturesForAddress](#index--rpc--getsignaturesforaddress)
- [GetSlot](#index--rpc--getslot)
Expand Down Expand Up @@ -2218,7 +2219,6 @@ func main() {
spew.Dump(recent)
}
```

#### [index](#contents) > [RPC](#rpc-methods) > GetRecentPerformanceSamples

```go
Expand Down Expand Up @@ -2247,6 +2247,36 @@ func main() {
}
```

#### [index](#contents) > [RPC](#rpc-methods) > GetRecentPrioritizationFees

```go
package main

import (
"context"

"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)

out, err := client.GetRecentPrioritizationFees(
context.TODO(),
[]solana.PublicKey{
solana.MustPublicKeyFromBase58("q5BgreVhTyBH1QCeriVb7kQYEPneanFXPLjvyjdf8M3"),
},
)
if err != nil {
panic(err)
}
spew.Dump(out)
}
```

#### [index](#contents) > [RPC](#rpc-methods) > GetSignatureStatuses

```go
Expand Down
40 changes: 40 additions & 0 deletions rpc/client_test.go
Expand Up @@ -2766,3 +2766,43 @@ func TestClient_GetLatestBlockhash(t *testing.T) {

assert.Equal(t, expected, got, "both deserialized values must be equal")
}

func TestClient_GetRecentPrioritizationFees(t *testing.T) {
responseBody := `[ { "slot": 348125, "prioritizationFee": 0 }, { "slot": 348126, "prioritizationFee": 1000 }, { "slot": 348127, "prioritizationFee": 500 } ]`
server, closer := mockJSONRPC(t, stdjson.RawMessage(wrapIntoRPC(responseBody)))
defer closer()

client := New(server.URL)

accounts := []solana.PublicKey{
solana.MustPublicKeyFromBase58("41twqNJmPHv8a5AW32if2CcGRcPzaetwErXaNggGWu1q"),
solana.MustPublicKeyFromBase58("5U3bH5b6XtG99aVWLqwVzYPVpQiFHytBD68Rz2eFPZd7"),
}

out, err := client.GetRecentPrioritizationFees(
context.Background(),
accounts,
)
require.NoError(t, err)

assert.Equal(t,
map[string]interface{}{
"id": float64(0),
"jsonrpc": "2.0",
"method": "getRecentPrioritizationFees",
"params": []interface{}{
[]interface{}{
accounts[0].String(),
accounts[1].String(),
},
},
},
server.RequestBody(t),
)

expected := mustJSONToInterface([]byte(responseBody))

got := mustJSONToInterface(mustAnyToJSON(out))

assert.Equal(t, expected, got, "both deserialized values must be equal")
}
41 changes: 41 additions & 0 deletions rpc/getRecentPrioritizationFees.go
@@ -0,0 +1,41 @@
// Copyright 2021 github.com/gagliardetto
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rpc

import (
"context"

"github.com/gagliardetto/solana-go"
)

// GetRecentPrioritizationFees returns a list of prioritization fees from recent blocks.
// Currently, a node's prioritization-fee cache stores data from up to 150 blocks.
func (cl *Client) GetRecentPrioritizationFees(
ctx context.Context,
accounts solana.PublicKeySlice, // optional
) (out []PriorizationFeeResult, err error) {
params := []interface{}{accounts}
err = cl.rpcClient.CallForInto(ctx, &out, "getRecentPrioritizationFees", params)

return
}

type PriorizationFeeResult struct {
// Slot in which the fee was observed
Slot uint64 `json:"slot"`

// The per-compute-unit fee paid by at least one successfully landed transaction, specified in increments of 0.000001 lamports
PrioritizationFee uint64 `json:"prioritizationFee"`
}

0 comments on commit 290a21a

Please sign in to comment.