-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathobserver_gas.go
63 lines (50 loc) · 2.05 KB
/
observer_gas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package observer
import (
"context"
"github.com/gagliardetto/solana-go/rpc"
"github.com/pkg/errors"
zetamath "github.com/zeta-chain/node/pkg/math"
)
const (
// SolanaTransactionFee is the static fee per transaction, 5k lamports.
SolanaTransactionFee = 5000
// MicroLamportsPerLamport is the number of micro lamports in a lamport.
MicroLamportsPerLamport = 1_000_000
// SolanaDefaultComputeBudget is the default compute budget for a transaction.
SolanaDefaultComputeBudget = 200_000
// Solana uses micro lamports (0.000001 lamports) as the smallest unit of gas price.
// The gas fee formula 'gasFee = gasPrice * gasLimit' won't fit Solana in the ZRC20 SOL contract.
// We could use lamports as the unit of gas price and 10K CU as the smallest unit of compute units.
// SolanaDefaultGasPrice10KCUs is the default gas price (in lamports) per 10K compute units.
SolanaDefaultGasPrice10KCUs = 100
// SolanaDefaultGasLimit is the default compute units (in 10K CU) for a transaction.
SolanaDefaultGasLimit10KCU = 50
)
// PostGasPrice posts gas price to zetacore
func (ob *Observer) PostGasPrice(ctx context.Context) error {
// get current slot
slot, err := ob.solClient.GetSlot(ctx, rpc.CommitmentConfirmed)
if err != nil {
return errors.Wrap(err, "GetSlot error")
}
// query recent priority fees
recentFees, err := ob.solClient.GetRecentPrioritizationFees(ctx, nil)
if err != nil {
return errors.Wrap(err, "GetRecentPrioritizationFees error")
}
// locate median priority fee
priorityFees := make([]uint64, len(recentFees))
for i, fee := range recentFees {
if fee.PrioritizationFee > 0 {
priorityFees[i] = fee.PrioritizationFee
}
}
// the priority fee is in increments of 0.000001 lamports (micro lamports)
medianFee := zetamath.SliceMedianValue(priorityFees, true)
// there is no Ethereum-like gas price in Solana, we only post priority fee for now
_, err = ob.ZetacoreClient().PostVoteGasPrice(ctx, ob.Chain(), 1, medianFee, slot)
if err != nil {
return errors.Wrapf(err, "PostVoteGasPrice error for chain %d", ob.Chain().ChainId)
}
return nil
}