Skip to content

Commit

Permalink
Add current gas price resolver to API server.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiri Malek committed Apr 16, 2020
1 parent 2b5f7af commit a957028
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# --------------------------------------------------------------------------
# Makefile for the Fantom API GaphQL Server
#
# v0.1 (2020/03/09) - Initial version, base API server build.
# (c) Fantom Foundation, 2020
# --------------------------------------------------------------------------

# project related vars
PROJECT := $(shell basename "$(PWD)")

# go related vars
GOBASE := $(shell pwd)
GOBIN=$(CURDIR)/build

## server: Make the API server as bin/frd
server:
go build -o $(GOBIN)/apiserver ./cmd/apiserver

.PHONY: help
all: help
help: Makefile
@echo
@echo "Choose a make command in "$(PROJECT)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo
3 changes: 3 additions & 0 deletions internal/graphql/resolvers/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ type ApiResolver interface {
// Price resolves price details of the Opera blockchain token for the given target symbols.
Price(*struct{ To string }) (types.Price, error)

// GasPrice resolves the current amount of WEI for single Gas.
GasPrice() (hexutil.Uint64, error)

// Close terminates resolver broadcast management.
Close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"fantom-api-graphql/internal/types"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"io"
)

Expand All @@ -13,6 +14,11 @@ func (rs *rootResolver) Price(args *struct{ To string }) (types.Price, error) {
return rs.repo.Price(args.To)
}

// GasPrice resolves the current amount of WEI for single Gas.
func (rs *rootResolver) GasPrice() (hexutil.Uint64, error) {
return rs.repo.GasPrice()
}

// uuid generates new random subscription UUID
func uuid() (string, error) {
// prep container
Expand Down
5 changes: 4 additions & 1 deletion internal/graphql/schema/bundle.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gqlschema

// Auto generated GraphQL schema bundle; created 2020-03-20 23:21
// Auto generated GraphQL schema bundle; created 2020-04-16 18:29
const schema = `
# StakerInfo represents extended staker information from smart contract.
type StakerInfo {
Expand Down Expand Up @@ -474,6 +474,9 @@ type Query {
"Get the details of a delegator by it's address."
delegation(address:Address!): Delegator
"Returns the current price per gas in WEI units."
gasPrice: Long!
"Get price details of the Opera blockchain token for the given target symbols."
price(to:String!):Price!
}
Expand Down
3 changes: 3 additions & 0 deletions internal/graphql/schema/definition/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type Query {
"Get the details of a delegator by it's address."
delegation(address:Address!): Delegator

"Returns the current price per gas in WEI units."
gasPrice: Long!

"Get price details of the Opera blockchain token for the given target symbols."
price(to:String!):Price!
}
Expand Down
3 changes: 3 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ type Repository interface {
// Price returns a price information for the given target symbol.
Price(sym string) (types.Price, error)

// GasPrice resolves the current amount of WEI for single Gas.
GasPrice() (hexutil.Uint64, error)

// FtmConnection returns open connection to Opera/Lachesis full node.
FtmConnection() *ftm.Client

Expand Down
40 changes: 40 additions & 0 deletions internal/repository/rpc/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Package rpc implements bridge to Lachesis full node API interface.
We recommend using local IPC for fast and the most efficient inter-process communication between the API server
and an Opera/Lachesis node. Any remote RPC connection will work, but the performance may be significantly degraded
by extra networking overhead of remote RPC calls.
You should also consider security implications of opening Lachesis RPC interface for a remote access.
If you considering it as your deployment strategy, you should establish encrypted channel between the API server
and Lachesis RPC interface with connection limited to specified endpoints.
We strongly discourage opening Lachesis RPC interface for unrestricted Internet access.
*/
package rpc

import "github.com/ethereum/go-ethereum/common/hexutil"

// GasPrice resolves the current amount of WEI for single Gas.
func (ftm *FtmBridge) GasPrice() (hexutil.Uint64, error) {
// keep track of the operation
ftm.log.Debugf("checking current gas price")

// call for data
var price hexutil.Big
err := ftm.rpc.Call(&price, "ftm_gasPrice")
if err != nil {
ftm.log.Error("current gas price could not be obtained")
return hexutil.Uint64(0), err
}

// if the price safely within the range
if !price.ToInt().IsUint64() {
ftm.log.Error("current gas price is too high and can not be extracted")
return hexutil.Uint64(0), err
}

// inform and return
ftm.log.Debugf("current gas price is %d", uint64(price.ToInt().Uint64()))
return hexutil.Uint64(price.ToInt().Uint64()), nil
}
5 changes: 5 additions & 0 deletions internal/repository/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const (
priceApiTargetSymbolVar = "tsyms="
)

// GasPrice resolves the current amount of WEI for single Gas.
func (p *proxy) GasPrice() (hexutil.Uint64, error) {
return p.rpc.GasPrice()
}

// Price returns a price information for the given target symbol.
func (p *proxy) Price(sym string) (types.Price, error) {
// inform what we do
Expand Down

0 comments on commit a957028

Please sign in to comment.